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

Rollup code-split error during lib build #9310

Closed
7 tasks done
lukeed opened this issue Jul 23, 2022 · 4 comments · Fixed by rollup/rollup#4589
Closed
7 tasks done

Rollup code-split error during lib build #9310

lukeed opened this issue Jul 23, 2022 · 4 comments · Fixed by rollup/rollup#4589
Labels
bug: upstream Bug in a dependency of Vite p2-edge-case Bug, but has workaround or limited in scope (priority)

Comments

@lukeed
Copy link

lukeed commented Jul 23, 2022

Describe the bug

Building a library with iife and es formats selected. As soon as a dynamic import is introduced, the ESM build fails to compile:

$ vite build
vite v3.0.2 building for production...
✓ 3 modules transformed.
dist/howdy.iife.js   0.19 KiB / gzip: 0.17 KiB
dist/style.css       1.01 KiB / gzip: 0.54 KiB
Cannot split a chunk that has already been edited (1:7 – "import("./foobar")")
error during build:
Error: Cannot split a chunk that has already been edited (1:7 – "import("./foobar")")
    at MagicString._splitChunk (file:///Users/.../node_modules/rollup/dist/es/shared/rollup.js:1033:10)
    at MagicString._split (file:///Users/.../node_modules/rollup/dist/es/shared/rollup.js:1023:43)
    at MagicString.overwrite (file:///Users/.../node_modules/rollup/dist/es/shared/rollup.js:806:8)
    at ImportExpression.renderFinalResolution (file:///Users/.../node_modules/rollup/dist/es/shared/rollup.js:10250:14)
    at Chunk.finaliseDynamicImports (file:///Users/.../node_modules/rollup/dist/es/shared/rollup.js:15009:22)
    at Chunk.render (file:///Users/.../node_modules/rollup/dist/es/shared/rollup.js:14808:14)
    at file:///Users/.../node_modules/rollup/dist/es/shared/rollup.js:15988:52
    at Array.map (<anonymous>)
    at Bundle.addFinalizedChunksToBundle (file:///Users/.../node_modules/rollup/dist/es/shared/rollup.js:15986:34)
    at Bundle.generate (file:///Users/.../node_modules/rollup/dist/es/shared/rollup.js:15966:24)

Found similar issue(s) in Rollup tracker, starting with rollup/rollup#2660

In that issue, @lukastaegert mentions "three levels of dynamic imports", so it makes me think that it may have something to do with how Vite is pitching/preparing the entry module for rollup?

Either way, even though this is a Rollup error, I cannot reproduce it when using the same source & Rollup directly.

Reproduction

https://github.com/lukeed/repro-vite-lib-split

System Info

System:
    OS: macOS 12.2.1
  Binaries:
    Node: 18.3.0 - ~/.volta/tools/image/node/18.3.0/bin/node
    Yarn: 1.22.10 - ~/.volta/tools/image/yarn/1.22.10/bin/yarn
    npm: 8.11.0 - ~/.volta/tools/image/node/18.3.0/bin/npm
  Browsers:
    Brave Browser: 86.1.15.76
    Chrome: 103.0.5060.134
    Firefox: 99.0.1
    Safari: 15.3
  npmPackages:
    vite: 3.0.2 => 3.0.2

Used Package Manager

pnpm

Logs

Click to expand!
$ vite build --debug
  vite:config bundled config file loaded in 26.67ms +0ms
  vite:config using resolved config: {
  vite:config   build: {
  vite:config     target: [ 'es2020', 'edge88', 'firefox78', 'chrome87', 'safari13' ],
  vite:config     polyfillModulePreload: true,
  vite:config     outDir: 'dist',
  vite:config     assetsDir: 'assets',
  vite:config     assetsInlineLimit: 4096,
  vite:config     cssCodeSplit: false,
  vite:config     cssTarget: [ 'es2020', 'edge88', 'firefox78', 'chrome87', 'safari13' ],
  vite:config     sourcemap: false,
  vite:config     rollupOptions: {},
  vite:config     minify: 'esbuild',
  vite:config     terserOptions: {},
  vite:config     write: true,
  vite:config     emptyOutDir: null,
  vite:config     manifest: false,
  vite:config     lib: { name: 'howdy', entry: 'src/main.ts', formats: [Array] },
  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   },
  vite:config   optimizeDeps: {
  vite:config     disabled: 'build',
  vite:config     force: undefined,
  vite:config     esbuildOptions: { preserveSymlinks: undefined }
  vite:config   },
  vite:config   configFile: '/Users/.../repro/vite.config.ts',
  vite:config   configFileDependencies: [ '/Users/.../repro/vite.config.ts' ],
  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/.../repro',
  vite:config   base: '/',
  vite:config   resolve: { alias: [ [Object], [Object] ] },
  vite:config   publicDir: '/Users/.../repro/public',
  vite:config   cacheDir: '/Users/.../repro/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:watch-package-data',
  vite:config     'commonjs',
  vite:config     'vite:data-uri',
  vite:config     'vite:asset-import-meta-url',
  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       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object], [Object], [Object],
  vite:config       [Object]
  vite:config     ],
  vite:config     rollupOptions: {}
  vite:config   },
  vite:config   appType: 'spa',
  vite:config   experimental: { importGlobRestoreExtension: false, hmrPartialAccept: false }
  vite:config } +6ms
vite v3.0.2 building for production...
✓ 3 modules transformed.
dist/howdy.iife.js   0.19 KiB / gzip: 0.17 KiB
dist/style.css       1.01 KiB / gzip: 0.54 KiB
Cannot split a chunk that has already been edited (1:7 – "import("./foobar")")
error during build:
Error: Cannot split a chunk that has already been edited (1:7 – "import("./foobar")")
    at MagicString._splitChunk (file:///Users/.../repro/node_modules/rollup/dist/es/shared/rollup.js:1033:10)
    at MagicString._split (file:///Users/.../repro/node_modules/rollup/dist/es/shared/rollup.js:1023:43)
    at MagicString.overwrite (file:///Users/.../repro/node_modules/rollup/dist/es/shared/rollup.js:806:8)
    at ImportExpression.renderFinalResolution (file:///Users/.../repro/node_modules/rollup/dist/es/shared/rollup.js:10250:14)
    at Chunk.finaliseDynamicImports (file:///Users/.../repro/node_modules/rollup/dist/es/shared/rollup.js:15009:22)
    at Chunk.render (file:///Users/.../repro/node_modules/rollup/dist/es/shared/rollup.js:14808:14)
    at file:///Users/.../repro/node_modules/rollup/dist/es/shared/rollup.js:15988:52
    at Array.map (<anonymous>)
    at Bundle.addFinalizedChunksToBundle (file:///Users/.../repro/node_modules/rollup/dist/es/shared/rollup.js:15986:34)
    at Bundle.generate (file:///Users/.../repro/node_modules/rollup/dist/es/shared/rollup.js:15966:24)

Validations

@lukastaegert
Copy link

Without looking into it yet, I can tell you that the error message is misleading—this has nothing to do with code-splitting but rather with file transformation. The error is caused by using magic-string wrongly in some way. Still, it looks like it might be a bug in Rollup considering it happens during dynamic import rendering, where only Rollup is doing anything with magic-string.

@sapphi-red
Copy link
Member

It seems this does not happen when format is ['es', 'iife'] instead of ['iife', 'es'].
I was able to reproduce only with rollup and I've created a issue there (rollup/rollup#4586).

@sapphi-red sapphi-red added the p2-edge-case Bug, but has workaround or limited in scope (priority) label Jul 23, 2022
@lukastaegert
Copy link

Thanks to your observation, I managed to find a fix: rollup/rollup#4589

@sapphi-red
Copy link
Member

Thanks!

Closing as rollup 2.77.2 that includes the fix was released.

@github-actions github-actions bot locked and limited conversation to collaborators Aug 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug: upstream Bug in a dependency of Vite p2-edge-case Bug, but has workaround or limited in scope (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants