Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vite.js failed to transpile @juggle/resize-observer #2349

Open
ben-ikt opened this issue Mar 14, 2023 · 2 comments
Open

Vite.js failed to transpile @juggle/resize-observer #2349

ben-ikt opened this issue Mar 14, 2023 · 2 comments
Assignees

Comments

@ben-ikt
Copy link

ben-ikt commented Mar 14, 2023

Hi, I would like to switch my project to vite.js instead of react-scripts but I can't because of a dependency of ketcher-react.

Steps to Reproduce

  1. Replace react-scripts by vite.js
  2. Configure vite
export default defineConfig(({ mode }) => {
  return {
    plugins: [react(), viteTsconfigPaths(), svgrPlugin()],
    optimizeDeps: {
      esbuildOptions: {
        // Node.js global to browser globalThis
        define: {
          global: 'globalThis',
        },
        // Enable esbuild polyfill plugins
        plugins: [
          NodeGlobalsPolyfillPlugin({
            buffer: true,
            process: true // -> needed for some of my external packages
          }),
          NodeModulesPolyfillPlugin(),
        ],
      },
    },
    build: {
      sourcemap: false,
      rollupOptions: {
        cache: false,
        plugins: [
          // Enable rollup polyfills plugin
          // used during production bundling
          rollupNodePolyFill(),
        ],
      },
    },
    server: {
      port: 3000,
    },
  };
});
  1. Start vite and open http://localhost:3000
  2. Open the error console

Actual behavior

Uncaught SyntaxError: Identifier 'process' has already been declared (at ketcher-react.js?v=c1ad07e1:7821:5)
// node_modules/@juggle/resize-observer/lib/utils/process.js
var process; // <- process is already defined. process seems to be declare as a global var.
var init_process2 = __esm({
  "node_modules/@juggle/resize-observer/lib/utils/process.js"() {
    init_process();
    init_buffer();
    init_hasActiveObservations();
    init_hasSkippedObservations();
    init_deliverResizeLoopError();
    init_broadcastActiveObservations();
    init_gatherActiveObservationsAtDepth();
    process = function() {
      var depth = 0;
      gatherActiveObservationsAtDepth(depth);
      while (hasActiveObservations()) {
        depth = broadcastActiveObservations();
        gatherActiveObservationsAtDepth(depth);
      }
      if (hasSkippedObservations()) {
        deliverResizeLoopError();
      }
      return depth > 0;
    };
  }
});

Desktop (please complete the following information):

  • OS: macOS 13.2.1
  • Browser: chrome
  • Version: Version 111.0.5563.64

Ketcher version
2.7.2

@KonstantinEpam23
Copy link
Collaborator

Hi @ben-ikt thanks for reaching out! We are currently investigating using vite.js as the build tool for the project. You can follow this issue to check progress with that: #2411

@ben-ikt
Copy link
Author

ben-ikt commented Mar 30, 2023

Hi @KonstantinEpam23, here is how I patch @juggle/resize-observer via an esbuild plugin.

/**
 * HACK @juggle/resize-observer/lib/utils/process.js used by ketcher-react
 * 'var process' create a conflict with NodeGlobalsPolyfillPlugin({ process: true })
 */
const ketcherReactTranspile = () => {
  return {
    name: 'ketcher-react-transpile',
    setup(build) {
      build.onLoad({ filter: /@juggle\/resize-observer\/lib\/utils\/process.js/ }, async (args) => {
        const source = await readFile(args.path, 'utf8');
        const hackedSource = source
          .replace('var process =', 'var processTask =')
          .replace('export { process }', 'export { processTask as process }');
        return { contents: hackedSource };
      });
    },
  };
};

And in my vite.config.js

optimizeDeps: {
      esbuildOptions: {
        // Node.js global to browser globalThis
        define: {
          global: 'globalThis',
        },
        // Enable esbuild polyfill plugins
        plugins: [
          ketcherReactTranspile(),
          NodeGlobalsPolyfillPlugin({
            process: true,
          }),
          NodeModulesPolyfillPlugin(),
        ],
      },
    },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants