georgwittberger / vite-plugin-entrypoints

Vite plugin to generate additional entry points for files matching glob patterns
MIT License
2 stars 1 forks source link

ESM module resolution not working #5

Closed AllySummers closed 1 month ago

AllySummers commented 1 month ago

Hi,

I've tried to use this package in a simple library and I've come across an issue with module resolution due to the package.json file with ESM module resolution, despite typescript being satisfied with the types.

Node version: v18.20.1 Using Windows 10

I've made a repro here: https://github.com/AllySummers/vite-plugin-entrypoints-repro

Even just running node on the file doesn't work because the export cannot be resolved, however using CJS works (despite vite's CJS API being deprecated, at least in v5).

Log of error ``` PS C:\node\vite-plugin-entrypoints-repro> npm run test1 > vite-plugin-entrypoints-repro@1.0.0 test1 > node vite.config.cjs The CJS build of Vite's Node API is deprecated. See https://vitejs.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated for more details. C:\node\vite-plugin-entrypoints-repro\node_modules\vite-plugin-entrypoints\dist\index.js:2 import { glob } from "glob"; ^^^^^^ SyntaxError: Cannot use import statement outside a module at internalCompileFunction (node:internal/vm:76:18) at wrapSafe (node:internal/modules/cjs/loader:1283:20) at Module._compile (node:internal/modules/cjs/loader:1328:27) at Module._extensions..js (node:internal/modules/cjs/loader:1422:10) at Module.load (node:internal/modules/cjs/loader:1203:32) at Module._load (node:internal/modules/cjs/loader:1019:12) at Module.require (node:internal/modules/cjs/loader:1231:19) at require (node:internal/modules/helpers:177:18) at Object. (C:\node\vite-plugin-entrypoints-repro\vite.config.cjs:4:34) at Module._compile (node:internal/modules/cjs/loader:1364:14) Node.js v18.20.1 PS C:\node\vite-plugin-entrypoints-repro> npm run test2 > vite-plugin-entrypoints-repro@1.0.0 test2 > node vite.config.mjs (node:14020) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension. (Use `node --trace-warnings ...` to show where the warning was created) C:\node\vite-plugin-entrypoints-repro\node_modules\vite-plugin-entrypoints\dist\index.js:2 import { glob } from "glob"; ^^^^^^ SyntaxError: Cannot use import statement outside a module at internalCompileFunction (node:internal/vm:76:18) at wrapSafe (node:internal/modules/cjs/loader:1283:20) at Module._compile (node:internal/modules/cjs/loader:1328:27) at Module._extensions..js (node:internal/modules/cjs/loader:1422:10) at Module.load (node:internal/modules/cjs/loader:1203:32) at Module._load (node:internal/modules/cjs/loader:1019:12) at ModuleWrap. (node:internal/modules/esm/translators:203:29) at ModuleJob.run (node:internal/modules/esm/module_job:195:25) at async ModuleLoader.import (node:internal/modules/esm/loader:337:24) at async loadESM (node:internal/process/esm_loader:34:7) Node.js v18.20.1 PS C:\node\vite-plugin-entrypoints-repro> npm run test3 > vite-plugin-entrypoints-repro@1.0.0 test3 > node vite2.config.mjs (node:1112) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension. (Use `node --trace-warnings ...` to show where the warning was created) C:\node\vite-plugin-entrypoints-repro\node_modules\vite-plugin-entrypoints\dist\index.js:2 import { glob } from "glob"; ^^^^^^ SyntaxError: Cannot use import statement outside a module at internalCompileFunction (node:internal/vm:76:18) at wrapSafe (node:internal/modules/cjs/loader:1283:20) at Module._compile (node:internal/modules/cjs/loader:1328:27) at Module._extensions..js (node:internal/modules/cjs/loader:1422:10) at Module.load (node:internal/modules/cjs/loader:1203:32) at Module._load (node:internal/modules/cjs/loader:1019:12) at ModuleWrap. (node:internal/modules/esm/translators:203:29) at ModuleJob.run (node:internal/modules/esm/module_job:195:25) at async ModuleLoader.import (node:internal/modules/esm/loader:337:24) at async loadESM (node:internal/process/esm_loader:34:7) Node.js v18.20.1 PS C:\node\vite-plugin-entrypoints-repro> npm run test4 > vite-plugin-entrypoints-repro@1.0.0 test4 > node vite3.config.mjs (node:36236) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension. (Use `node --trace-warnings ...` to show where the warning was created) C:\node\vite-plugin-entrypoints-repro\node_modules\vite-plugin-entrypoints\dist\index.js:2 import { glob } from "glob"; ^^^^^^ SyntaxError: Cannot use import statement outside a module at internalCompileFunction (node:internal/vm:76:18) at wrapSafe (node:internal/modules/cjs/loader:1283:20) at Module._compile (node:internal/modules/cjs/loader:1328:27) at Module._extensions..js (node:internal/modules/cjs/loader:1422:10) at Module.load (node:internal/modules/cjs/loader:1203:32) at Module._load (node:internal/modules/cjs/loader:1019:12) at ModuleWrap. (node:internal/modules/esm/translators:203:29) at ModuleJob.run (node:internal/modules/esm/module_job:195:25) at async ModuleLoader.import (node:internal/modules/esm/loader:337:24) at async file:///C:/node//vite-plugin-entrypoints-repro/vite3.config.mjs:8:5 Node.js v18.20.1PS C:\node\vite-plugin-entrypoints-repro> npm run test1 > vite-plugin-entrypoints-repro@1.0.0 test1 > node vite.config.cjs The CJS build of Vite's Node API is deprecated. See https://vitejs.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated for more details. C:\node\vite-plugin-entrypoints-repro\node_modules\vite-plugin-entrypoints\dist\index.js:2 import { glob } from "glob"; ^^^^^^ SyntaxError: Cannot use import statement outside a module at internalCompileFunction (node:internal/vm:76:18) at wrapSafe (node:internal/modules/cjs/loader:1283:20) at Module._compile (node:internal/modules/cjs/loader:1328:27) at Module._extensions..js (node:internal/modules/cjs/loader:1422:10) at Module.load (node:internal/modules/cjs/loader:1203:32) at Module._load (node:internal/modules/cjs/loader:1019:12) at Module.require (node:internal/modules/cjs/loader:1231:19) at require (node:internal/modules/helpers:177:18) at Object. (C:\node\vite-plugin-entrypoints-repro\vite.config.cjs:4:34) at Module._compile (node:internal/modules/cjs/loader:1364:14) Node.js v18.20.1 PS C:\node\vite-plugin-entrypoints-repro> npm run test2 > vite-plugin-entrypoints-repro@1.0.0 test2 > node vite.config.mjs (node:14020) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension. (Use `node --trace-warnings ...` to show where the warning was created) C:\node\vite-plugin-entrypoints-repro\node_modules\vite-plugin-entrypoints\dist\index.js:2 import { glob } from "glob"; ^^^^^^ SyntaxError: Cannot use import statement outside a module at internalCompileFunction (node:internal/vm:76:18) at wrapSafe (node:internal/modules/cjs/loader:1283:20) at Module._compile (node:internal/modules/cjs/loader:1328:27) at Module._extensions..js (node:internal/modules/cjs/loader:1422:10) at Module.load (node:internal/modules/cjs/loader:1203:32) at Module._load (node:internal/modules/cjs/loader:1019:12) at ModuleWrap. (node:internal/modules/esm/translators:203:29) at ModuleJob.run (node:internal/modules/esm/module_job:195:25) at async ModuleLoader.import (node:internal/modules/esm/loader:337:24) at async loadESM (node:internal/process/esm_loader:34:7) Node.js v18.20.1 PS C:\node\vite-plugin-entrypoints-repro> npm run test3 > vite-plugin-entrypoints-repro@1.0.0 test3 > node vite2.config.mjs (node:1112) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension. (Use `node --trace-warnings ...` to show where the warning was created) C:\node\vite-plugin-entrypoints-repro\node_modules\vite-plugin-entrypoints\dist\index.js:2 import { glob } from "glob"; ^^^^^^ SyntaxError: Cannot use import statement outside a module at internalCompileFunction (node:internal/vm:76:18) at wrapSafe (node:internal/modules/cjs/loader:1283:20) at Module._compile (node:internal/modules/cjs/loader:1328:27) at Module._extensions..js (node:internal/modules/cjs/loader:1422:10) at Module.load (node:internal/modules/cjs/loader:1203:32) at Module._load (node:internal/modules/cjs/loader:1019:12) at ModuleWrap. (node:internal/modules/esm/translators:203:29) at ModuleJob.run (node:internal/modules/esm/module_job:195:25) at async ModuleLoader.import (node:internal/modules/esm/loader:337:24) at async loadESM (node:internal/process/esm_loader:34:7) Node.js v18.20.1 PS C:\node\vite-plugin-entrypoints-repro> npm run test4 > vite-plugin-entrypoints-repro@1.0.0 test4 > node vite3.config.mjs (node:36236) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension. (Use `node --trace-warnings ...` to show where the warning was created) C:\node\vite-plugin-entrypoints-repro\node_modules\vite-plugin-entrypoints\dist\index.js:2 import { glob } from "glob"; ^^^^^^ SyntaxError: Cannot use import statement outside a module at internalCompileFunction (node:internal/vm:76:18) at wrapSafe (node:internal/modules/cjs/loader:1283:20) at Module._compile (node:internal/modules/cjs/loader:1328:27) at Module._extensions..js (node:internal/modules/cjs/loader:1422:10) at Module.load (node:internal/modules/cjs/loader:1203:32) at Module._load (node:internal/modules/cjs/loader:1019:12) at ModuleWrap. (node:internal/modules/esm/translators:203:29) at ModuleJob.run (node:internal/modules/esm/module_job:195:25) at async ModuleLoader.import (node:internal/modules/esm/loader:337:24) at async file:///C:/node//vite-plugin-entrypoints-repro/vite3.config.mjs:8:5 Node.js v18.20.1 ```

I noticed adding this to the package.json fixed the error, however I'm not sure if this would be considered a breaking change:

  "exports": {
    ".": {
      "types": "./dist/index.d.ts",
      "require": "./dist/index.cjs",
      "import": "./dist/index.js",
      "default": "./dist/index.js"
    }
  },
georgwittberger commented 1 month ago

Thanks for reporting this issue. It's the same problem that has been raised here: https://github.com/vitejs/vite/issues/5694

I suppose it has something to do with TSUP bundling the default export from my source file:

export default entrypoints;

as

var src_default = entrypoints;
export {
  src_default as default
};

in the build output. This seems to cause trouble when using an ESM default import in vite.config.mjs. The function is then only available as default property on the imported identifier which is not how it's supposed to be.

I've followed your suggestion and added conditional exports to package.json. Taking a look at the Vite plugin checker from the issue linked at the top they've also included such exports directives.

I've released the new version 2.0.0 and as far as I've tested it, should work with your project setup.

AllySummers commented 1 month ago

Awesome, thanks for that quick fix!