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 prefers the file specified in module to exports under the certain situations #11676

Closed
7 tasks done
tasshi-me opened this issue Jan 12, 2023 · 1 comment
Closed
7 tasks done

Comments

@tasshi-me
Copy link

tasshi-me commented Jan 12, 2023

Describe the bug

Vite prefers the file specified in module to exports under the following situations.

  • The package is ESM
  • There are exports and module fields on package.json
  • The extension of the file specified in exports.import is .mjs (with js, the problem does not happen)
{
  "exports": {
    "import": "./lib/index.mjs",
    "require": "./lib/index.cjs"
  },
  "type": "module",
  "module": "./lib/old.mjs",
  "main": "./lib/index.cjs"
}

Because of this behavior, we cannot build some libraries like y18n; a dependency of yargs.

https://github.com/yargs/y18n/blob/master/package.json#L8

Reproduction

https://github.com/tasshi-playground/repro-vite-prefers-module-to-exports

Steps to reproduce

Run npm install and npm run build

System Info

System:
    OS: macOS 13.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 3.07 GB / 64.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.8.0 - ~/.nodenv/versions/18.8.0/bin/node
    Yarn: 1.22.19 - /opt/homebrew/bin/yarn
    npm: 8.18.0 - ~/.nodenv/versions/18.8.0/bin/npm
  Browsers:
    Chrome: 109.0.5414.87
    Firefox Developer Edition: 108.0
    Safari: 16.2
  npmPackages:
    vite: ^4.0.4 => 4.0.4

Used Package Manager

npm

Logs

Vite failed to build output.
That's because the file specified in module field is loaded. (old.mjs).
(To make it easier to understand the impact, the ./lib/old.mjs is a deprecated file, and the interface is different from ./lib/index.mjs.)

"default" is not exported by "src/submodule/lib/old.mjs", imported by "src/index.mjs".
vite build
$ vite build        
vite v4.0.4 building for production...
✓ 2 modules transformed.
"default" is not exported by "src/submodule/lib/old.mjs", imported by "src/index.mjs".
file: /Users/tasshi/git/mshrtsr/repro-vite-prefers-module-to-exports/src/index.mjs:2:7
1: // eslint-disable-next-line node/no-extraneous-import
2: import submodule from "submodule-repro-vite-prefers-module-to-exports";
          ^
3: 
4: console.log("Loaded file is:", submodule);
error during build:
RollupError: "default" is not exported by "src/submodule/lib/old.mjs", imported by "src/index.mjs".
    at error (file:///Users/tasshi/git/mshrtsr/repro-vite-prefers-module-to-exports/node_modules/rollup/dist/es/shared/rollup.js:2041:30)
    at Module.error (file:///Users/tasshi/git/mshrtsr/repro-vite-prefers-module-to-exports/node_modules/rollup/dist/es/shared/rollup.js:13062:16)
    at Module.traceVariable (file:///Users/tasshi/git/mshrtsr/repro-vite-prefers-module-to-exports/node_modules/rollup/dist/es/shared/rollup.js:13445:29)
    at ModuleScope.findVariable (file:///Users/tasshi/git/mshrtsr/repro-vite-prefers-module-to-exports/node_modules/rollup/dist/es/shared/rollup.js:11926:39)
    at Identifier.bind (file:///Users/tasshi/git/mshrtsr/repro-vite-prefers-module-to-exports/node_modules/rollup/dist/es/shared/rollup.js:7855:40)
    at CallExpression.bind (file:///Users/tasshi/git/mshrtsr/repro-vite-prefers-module-to-exports/node_modules/rollup/dist/es/shared/rollup.js:5649:28)
    at CallExpression.bind (file:///Users/tasshi/git/mshrtsr/repro-vite-prefers-module-to-exports/node_modules/rollup/dist/es/shared/rollup.js:9372:15)
    at ExpressionStatement.bind (file:///Users/tasshi/git/mshrtsr/repro-vite-prefers-module-to-exports/node_modules/rollup/dist/es/shared/rollup.js:5653:23)
    at Program.bind (file:///Users/tasshi/git/mshrtsr/repro-vite-prefers-module-to-exports/node_modules/rollup/dist/es/shared/rollup.js:5649:28)
    at Module.bindReferences (file:///Users/tasshi/git/mshrtsr/repro-vite-prefers-module-to-exports/node_modules/rollup/dist/es/shared/rollup.js:13058:18)
vite build --debug
$ vite build --debug
  vite:config bundled config file loaded in 36.43ms +0ms
  vite:esbuild init tsconfck (root: /Users/tasshi/git/mshrtsr/repro-vite-prefers-module-to-exports) +0ms
  vite:esbuild init tsconfck (root: /Users/tasshi/git/mshrtsr/repro-vite-prefers-module-to-exports) +0ms
  vite:esbuild init tsconfck (root: /Users/tasshi/git/mshrtsr/repro-vite-prefers-module-to-exports) +0ms
  vite:esbuild init tsconfck (root: /Users/tasshi/git/mshrtsr/repro-vite-prefers-module-to-exports) +0ms
  vite:esbuild init tsconfck end +2ms
  vite:esbuild init tsconfck end +0ms
  vite:esbuild init tsconfck end +0ms
  vite:esbuild init tsconfck end +0ms
  vite:config using resolved config: {
  vite:config   build: {
  vite:config     target: [ 'es2020', 'edge88', 'firefox78', 'chrome87', 'safari14' ],
  vite:config     cssTarget: [ 'es2020', 'edge88', 'firefox78', 'chrome87', 'safari14' ],
  vite:config     outDir: 'dist',
  vite:config     assetsDir: 'assets',
  vite:config     assetsInlineLimit: 4096,
  vite:config     cssCodeSplit: true,
  vite:config     sourcemap: false,
  vite:config     rollupOptions: { input: 'src/index.mjs', output: [Object] },
  vite:config     minify: 'esbuild',
  vite:config     terserOptions: {},
  vite:config     write: true,
  vite:config     emptyOutDir: null,
  vite:config     copyPublicDir: true,
  vite:config     manifest: false,
  vite:config     lib: false,
  vite:config     ssr: false,
  vite:config     ssrManifest: false,
  vite:config     reportCompressedSize: true,
  vite:config     chunkSizeWarningLimit: 500,
  vite:config     watch: null,
  vite:config     commonjsOptions: { include: [Array], extensions: [Array] },
  vite:config     dynamicImportVarsOptions: { warnOnError: true, exclude: [Array] },
  vite:config     modulePreload: { polyfill: true }
  vite:config   },
  vite:config   optimizeDeps: {
  vite:config     disabled: 'build',
  vite:config     force: undefined,
  vite:config     esbuildOptions: { preserveSymlinks: false }
  vite:config   },
  vite:config   configFile: '/Users/tasshi/git/mshrtsr/repro-vite-prefers-module-to-exports/vite.config.mjs',
  vite:config   configFileDependencies: [
  vite:config     '/Users/tasshi/git/mshrtsr/repro-vite-prefers-module-to-exports/vite.config.mjs'
  vite:config   ],
  vite:config   inlineConfig: {
  vite:config     root: undefined,
  vite:config     base: undefined,
  vite:config     mode: undefined,
  vite:config     configFile: undefined,
  vite:config     logLevel: undefined,
  vite:config     clearScreen: undefined,
  vite:config     optimizeDeps: { force: undefined },
  vite:config     build: {}
  vite:config   },
  vite:config   root: '/Users/tasshi/git/mshrtsr/repro-vite-prefers-module-to-exports',
  vite:config   base: '/',
  vite:config   rawBase: '/',
  vite:config   resolve: {
  vite:config     mainFields: [ 'module', 'jsnext:main', 'jsnext' ],
  vite:config     browserField: true,
  vite:config     conditions: [],
  vite:config     extensions: [
  vite:config       '.mjs',  '.js',
  vite:config       '.mts',  '.ts',
  vite:config       '.jsx',  '.tsx',
  vite:config       '.json'
  vite:config     ],
  vite:config     dedupe: [],
  vite:config     preserveSymlinks: false,
  vite:config     alias: [ [Object], [Object] ]
  vite:config   },
  vite:config   publicDir: '/Users/tasshi/git/mshrtsr/repro-vite-prefers-module-to-exports/public',
  vite:config   cacheDir: '/Users/tasshi/git/mshrtsr/repro-vite-prefers-module-to-exports/node_modules/.vite',
  vite:config   command: 'build',
  vite:config   mode: 'production',
  vite:config   ssr: {
  vite:config     format: 'esm',
  vite:config     target: 'node',
  vite:config     optimizeDeps: { disabled: true, esbuildOptions: [Object] }
  vite:config   },
  vite:config   isWorker: false,
  vite:config   mainConfig: null,
  vite:config   isProduction: true,
  vite:config   plugins: [
  vite:config     'vite:build-metadata',
  vite:config     'vite:pre-alias',
  vite:config     'alias',
  vite:config     'vite:modulepreload-polyfill',
  vite:config     'vite:resolve',
  vite:config     'vite:html-inline-proxy',
  vite:config     'vite:css',
  vite:config     'vite:esbuild',
  vite:config     'vite:json',
  vite:config     'vite:wasm-helper',
  vite:config     'vite:worker',
  vite:config     'vite:asset',
  vite:config     'vite:wasm-fallback',
  vite:config     'vite:define',
  vite:config     'vite:css-post',
  vite:config     'vite:build-html',
  vite:config     'vite:worker-import-meta-url',
  vite:config     'vite:asset-import-meta-url',
  vite:config     'vite:force-systemjs-wrap-complete',
  vite:config     'vite:watch-package-data',
  vite:config     'commonjs',
  vite:config     'vite:data-uri',
  vite:config     'vite:dynamic-import-vars',
  vite:config     'vite:import-glob',
  vite:config     'vite:build-import-analysis',
  vite:config     'vite:esbuild-transpile',
  vite:config     'vite:terser',
  vite:config     'vite:reporter',
  vite:config     'vite:load-fallback'
  vite:config   ],
  vite:config   server: {
  vite:config     preTransformRequests: true,
  vite:config     middlewareMode: false,
  vite:config     fs: { strict: true, allow: [Array], deny: [Array] }
  vite:config   },
  vite:config   preview: {
  vite:config     port: undefined,
  vite:config     strictPort: undefined,
  vite:config     host: undefined,
  vite:config     https: undefined,
  vite:config     open: undefined,
  vite:config     proxy: undefined,
  vite:config     cors: undefined,
  vite:config     headers: undefined
  vite:config   },
  vite:config   env: { BASE_URL: '/', MODE: 'production', DEV: false, PROD: true },
  vite:config   assetsInclude: [Function: assetsInclude],
  vite:config   logger: {
  vite:config     hasWarned: false,
  vite:config     info: [Function: info],
  vite:config     warn: [Function: warn],
  vite:config     warnOnce: [Function: warnOnce],
  vite:config     error: [Function: error],
  vite:config     clearScreen: [Function: clearScreen],
  vite:config     hasErrorLogged: [Function: hasErrorLogged]
  vite:config   },
  vite:config   packageCache: Map(0) { set: [Function (anonymous)] },
  vite:config   createResolver: [Function: createResolver],
  vite:config   worker: {
  vite:config     format: 'iife',
  vite:config     plugins: [
  vite:config       'vite:build-metadata',
  vite:config       'vite:pre-alias',
  vite:config       'alias',
  vite:config       'vite:modulepreload-polyfill',
  vite:config       'vite:resolve',
  vite:config       'vite:html-inline-proxy',
  vite:config       'vite:css',
  vite:config       'vite:esbuild',
  vite:config       'vite:json',
  vite:config       'vite:wasm-helper',
  vite:config       'vite:worker',
  vite:config       'vite:asset',
  vite:config       'vite:wasm-fallback',
  vite:config       'vite:define',
  vite:config       'vite:css-post',
  vite:config       'vite:build-html',
  vite:config       'vite:worker-import-meta-url',
  vite:config       'vite:asset-import-meta-url',
  vite:config       'vite:force-systemjs-wrap-complete',
  vite:config       'vite:watch-package-data',
  vite:config       'commonjs',
  vite:config       'vite:data-uri',
  vite:config       'vite:dynamic-import-vars',
  vite:config       'vite:import-glob',
  vite:config       'vite:build-import-analysis',
  vite:config       'vite:esbuild-transpile',
  vite:config       'vite:terser',
  vite:config       'vite:load-fallback'
  vite:config     ],
  vite:config     rollupOptions: {},
  vite:config     getSortedPlugins: [Function: getSortedPlugins],
  vite:config     getSortedPluginHooks: [Function: getSortedPluginHooks]
  vite:config   },
  vite:config   appType: 'spa',
  vite:config   experimental: { importGlobRestoreExtension: false, hmrPartialAccept: false },
  vite:config   getSortedPlugins: [Function: getSortedPlugins],
  vite:config   getSortedPluginHooks: [Function: getSortedPluginHooks]
  vite:config } +6ms
vite v4.0.4 building for production...
✓ 2 modules transformed.
"default" is not exported by "src/submodule/lib/old.mjs", imported by "src/index.mjs".
file: /Users/tasshi/git/mshrtsr/repro-vite-prefers-module-to-exports/src/index.mjs:2:7
1: // eslint-disable-next-line node/no-extraneous-import
2: import submodule from "submodule-repro-vite-prefers-module-to-exports";
          ^
3: 
4: console.log("Loaded file is:", submodule);
error during build:
RollupError: "default" is not exported by "src/submodule/lib/old.mjs", imported by "src/index.mjs".
    at error (file:///Users/tasshi/git/mshrtsr/repro-vite-prefers-module-to-exports/node_modules/rollup/dist/es/shared/rollup.js:2041:30)
    at Module.error (file:///Users/tasshi/git/mshrtsr/repro-vite-prefers-module-to-exports/node_modules/rollup/dist/es/shared/rollup.js:13062:16)
    at Module.traceVariable (file:///Users/tasshi/git/mshrtsr/repro-vite-prefers-module-to-exports/node_modules/rollup/dist/es/shared/rollup.js:13445:29)
    at ModuleScope.findVariable (file:///Users/tasshi/git/mshrtsr/repro-vite-prefers-module-to-exports/node_modules/rollup/dist/es/shared/rollup.js:11926:39)
    at Identifier.bind (file:///Users/tasshi/git/mshrtsr/repro-vite-prefers-module-to-exports/node_modules/rollup/dist/es/shared/rollup.js:7855:40)
    at CallExpression.bind (file:///Users/tasshi/git/mshrtsr/repro-vite-prefers-module-to-exports/node_modules/rollup/dist/es/shared/rollup.js:5649:28)
    at CallExpression.bind (file:///Users/tasshi/git/mshrtsr/repro-vite-prefers-module-to-exports/node_modules/rollup/dist/es/shared/rollup.js:9372:15)
    at ExpressionStatement.bind (file:///Users/tasshi/git/mshrtsr/repro-vite-prefers-module-to-exports/node_modules/rollup/dist/es/shared/rollup.js:5653:23)
    at Program.bind (file:///Users/tasshi/git/mshrtsr/repro-vite-prefers-module-to-exports/node_modules/rollup/dist/es/shared/rollup.js:5649:28)
    at Module.bindReferences (file:///Users/tasshi/git/mshrtsr/repro-vite-prefers-module-to-exports/node_modules/rollup/dist/es/shared/rollup.js:13058:18)

Validations

@bluwy
Copy link
Member

bluwy commented Jan 17, 2023

Duplicate of #11114

@bluwy bluwy marked this as a duplicate of #11114 Jan 17, 2023
@bluwy bluwy closed this as not planned Won't fix, can't repro, duplicate, stale Jan 17, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Feb 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants