alex8088 / electron-vite

Next generation Electron build tooling based on Vite 新一代 Electron 开发构建工具,支持源代码保护
https://electron-vite.org
MIT License
3.57k stars 153 forks source link

SyntaxError: Cannot use import statement outside a module #528

Closed NWYLZW closed 5 months ago

NWYLZW commented 5 months ago

Describe the bug

failed to load config from /Users/yijie/codes/projects/spirit/electron.vite.config.ts
error during start dev server and electron app:
/Users/yijie/codes/projects/spirit/node_modules/.pnpm/vite-plugin-glob-accept@0.1.0_@types+node@18.19.33_sass@1.77.2/node_modules/vite-plugin-glob-accept/dist/index.esm.js:1
import { posix, isAbsolute } from 'node:path';
^^^^^^

SyntaxError: Cannot use import statement outside a module

https://github.com/alex8088/electron-vite/blob/1838bdbf3ee91e5a0cd39840190ad2506edfd583/src/utils.ts#L78

 if (/\.m[jt]s$/.test(filePath)) {

Remove ts extension check, it can't be load esm when use ts extension.

Electron-Vite Version

2.2.0

Electron Version

28.2.0

Vite Version

5.0.12

Validations

alex8088 commented 5 months ago

vite-plugin-glob-accept plugin problem, "type": "module" is not specified in package.json or name index.esm.js into index.esm.mjs.

NWYLZW commented 5 months ago

vite-plugin-glob-accept plugin problem, "type": "module" is not specified in package.json or name index.esm.js into index.esm.mjs.

I feel there's a bit of an issue. Why is it that even after setting up exports and module, it still continues to load as CommonJS (CJS), resulting in the outside module problem?

I'll take another look at the specific standards and behavior. I feel that directly assuming a file with a .ts extension to be ESM seems a bit strange.

alex8088 commented 5 months ago

Whether the module is resolved as ESM or CJS can be determined by the type field of package.json or the file suffix.

However, since Electron does not support ESM before version 28, using the type field of package.json to support configuration file parsing comes at a cost. Of course, electron-vite is supported, but we cannot force developers to choose this option, and vite has such a requirement. On the other hand, most plugin distributions for Vite or Rollup are released as ESM, so it is intentional to parse ts configuration files into ESM.

In fact, there is an issue with the release of this plugin vite-plugin-glob-accept. If the configuration file suffix is changed to mts or mjs, this error will also occur. The reason has been mentioned earlier. Of course, if you change the configuration file to cts, there is no problem.

NWYLZW commented 5 months ago

Directly using vite.config.ts seems to be fine.

https://github.com/NWYLZW/recurrent-vite-config-load-ts/blob/239ab1c7ca7e80fc3b690250a70e0c78f571ecfc/vite.config.ts#L1-L8

image
alex8088 commented 5 months ago

Directly using vite.config.ts seems to be fine.

https://github.com/NWYLZW/recurrent-vite-config-load-ts/blob/239ab1c7ca7e80fc3b690250a70e0c78f571ecfc/vite.config.ts#L1-L8

image

this way will use commonjs module. Try to add "type": "module" to your package.json will get the error.

NWYLZW commented 5 months ago

You are right.