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

Using build.lib.entry yields error The symbol "__cjs_url__" has already been declared and similar with ES module #393

Closed
4 tasks done
vincesp opened this issue Jan 24, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@vincesp
Copy link

vincesp commented Jan 24, 2024

Describe the bug

Defining one or several build.lib.entry entries results in an error during the Vite build if "type" is set to "module" in package.json:

export default defineConfig({
// ...
  preload: {
    plugins: [externalizeDepsPlugin()],
    build: {
      lib: {
        entry: [resolve('src/preload/myindex.js')]
      }
    }
  },
// ...
})

Error:

The symbol "__cjs_url__" has already been declared
8  |  
9  |  // -- CommonJS Shims --
10 |  import __cjs_url__ from 'node:url';
   |         ^
11 |  import __cjs_path__ from 'node:path';
12 |  import __cjs_mod__ from 'node:module';

The symbol "__cjs_path__" has already been declared
9  |  // -- CommonJS Shims --
10 |  import __cjs_url__ from 'node:url';
11 |  import __cjs_path__ from 'node:path';
   |         ^
12 |  import __cjs_mod__ from 'node:module';
13 |  const __filename = __cjs_url__.fileURLToPath(import.meta.url);

The symbol "__cjs_mod__" has already been declared
10 |  import __cjs_url__ from 'node:url';
11 |  import __cjs_path__ from 'node:path';
12 |  import __cjs_mod__ from 'node:module';
   |         ^
13 |  const __filename = __cjs_url__.fileURLToPath(import.meta.url);
14 |  const __dirname = __cjs_path__.dirname(__filename);

The symbol "__filename" has already been declared
14 |  const __dirname = __cjs_path__.dirname(__filename);
15 |  const require = __cjs_mod__.createRequire(import.meta.url);
16 |  const __filename = __cjs_url__.fileURLToPath(import.meta.url);
   |        ^
17 |  const __dirname = __cjs_path__.dirname(__filename);
18 |  __cjs_mod__.createRequire(import.meta.url);

The symbol "__dirname" has already been declared
15 |  const require = __cjs_mod__.createRequire(import.meta.url);
16 |  const __filename = __cjs_url__.fileURLToPath(import.meta.url);
17 |  const __dirname = __cjs_path__.dirname(__filename);
   |        ^
18 |  __cjs_mod__.createRequire(import.meta.url);
19 |  const icon = join(__dirname, "../../resources/icon.png");

✓ built in 55ms
 ERROR  Transform failed with 5 errors:
index.mjs:10:7: ERROR: The symbol "__cjs_url__" has already been declared
index.mjs:11:7: ERROR: The symbol "__cjs_path__" has already been declared
index.mjs:12:7: ERROR: The symbol "__cjs_mod__" has already been declared
index.mjs:16:6: ERROR: The symbol "__filename" has already been declared
index.mjs:17:6: ERROR: The symbol "__dirname" has already been declared

Here is a Codesandbox to reproduce the error.

Electron-Vite Version

2.0.0

Electron Version

28.1.4

Vite Version

5.0.12

Validations

@vincesp
Copy link
Author

vincesp commented Jan 24, 2024

There is a workaround: we can use build.rollupOptions.input instead:

build: {
  rollupOptions: {
    input: [resolve('src/preload/myindex.js')]
  }
}

This also requires to update the BrowserWindow options in main/index.js and change the file name extension to .mjs:

webPreferences: {
  preload: join(__dirname, '../preload/myindex.mjs'),
  sandbox: false
}

See branch fix/double-symbol

@alex8088 alex8088 added the bug Something isn't working label Feb 23, 2024
@alex8088
Copy link
Owner

alex8088 commented Mar 3, 2024

new ver 2.1.0 is out 🎉.

@alex8088 alex8088 closed this as completed Mar 3, 2024
@vincesp
Copy link
Author

vincesp commented Apr 11, 2024

I tested it, see branch test/v2.1.0

I noticed the odd behavior that the files under out/preload/ are generated with the .mjs extension, while the files under out/main/ are correctly generated with the .js extension. As the whole package is of `"type": "module"``, the .mjs extension should not be required.

This means that in main.js, I cannot simply reference the preload file with the name they have under src/:

webPreferences: {
  preload: join(__dirname, '../preload/myindex.js'),
  sandbox: false
}

But I have to use a different file name extension, which is not intuitive:

webPreferences: {
  preload: join(__dirname, '../preload/myindex.mjs'),
  sandbox: false
}

Is there a reason for this deviation between src/ and out/?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants