tweb icon indicating copy to clipboard operation
tweb copied to clipboard

[BUG] pnpm build failed

Open ezrawic4j opened this issue 2 years ago • 9 comments

✓ 781 modules transformed. rendering chunks (28)...The emitted file "lang-5a385cdb.js" overwrites a previously emitted file of the same name. The emitted file "langSign-66e8939d.js" overwrites a previously emitted file of the same name. The emitted file "pluralPolyfill-61f068d6.js" overwrites a previously emitted file of the same name. The emitted file "countries-5301fc59.js" overwrites a previously emitted file of the same name. The emitted file "lang-5a385cdb.js.map" overwrites a previously emitted file of the same name. The emitted file "langSign-66e8939d.js.map" overwrites a previously emitted file of the same name. The emitted file "pluralPolyfill-61f068d6.js.map" overwrites a previously emitted file of the same name. The emitted file "countries-5301fc59.js.map" overwrites a previously emitted file of the same name. [vite:build-import-analysis] Cannot read properties of undefined (reading 'forEach') ✓ built in 39.95s error during build: TypeError: Cannot read properties of undefined (reading 'forEach') at addDeps (file:///home/tweb/node_modules/.pnpm/[email protected]_@[email protected][email protected]/node_modules/vite/dist/node/chunks/dep-24daf00c.js:45215:55) at Object.generateBundle (file:///home/tweb/node_modules/.pnpm/[email protected]_@[email protected][email protected]/node_modules/vite/dist/node/chunks/dep-24daf00c.js:45236:33) at file:///home/tweb/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:24412:40  ELIFECYCLE  Command failed with exit code 1.

image

ezrawic4j avatar Oct 13 '23 04:10 ezrawic4j

Same issue

Leev1s avatar Nov 12 '23 09:11 Leev1s

someone have solution for this error?

slavabarkal avatar Nov 19 '23 14:11 slavabarkal

@morethanwords @ezrawic4j We have faced the same issue. Is there any solution?

I've found that some of the chunks have no imports field array as lang.ts and that's why the chunk.imports.forEach method in the mentioned vite file doesn't exist and throwing an error due to the field is undefined

Screenshot 2023-11-24 at 10 39 21

And when I fixed the file by adding conditions - the build has been successful

So maybe the problem with the chunk generation or something else

KarafiziArtur avatar Nov 24 '23 02:11 KarafiziArtur

So maybe the problem with the chunk generation or something else

It is. The bug is inside the vite. In order to build it, conditionals should be added in vite/dist/node/chunks/dep-24daf00c.js on lines 45215 and 45218 this way:

image

morethanwords avatar Nov 24 '23 18:11 morethanwords

So maybe the problem with the chunk generation or something else

It is. The bug is inside the vite. In order to build it, conditionals should be added in vite/dist/node/chunks/dep-24daf00c.js on lines 45215 and 45218 this way

I see, thanks. As I understand it's your official workaround for this bug. Can you write an issue to the specific package (rollup/vite) to fix the problem from the root?

KarafiziArtur avatar Nov 26 '23 02:11 KarafiziArtur

Compilation error reported:

PS \tweb> vite build
has built solid true
vite v4.3.1 building for production...
✓ 1111 modules transformed.
rendering chunks (30)...The emitted file "lang-49055ff2.js" overwrites a previously emitted file of the same name.
The emitted file "pluralPolyfill-61f068d6.js" overwrites a previously emitted file of the same name.
The emitted file "countries-5301fc59.js" overwrites a previously emitted file of the same name.
The emitted file "langSign-66e8939d.js" overwrites a previously emitted file of the same name.
The emitted file "lang-49055ff2.js.map" overwrites a previously emitted file of the same name.
The emitted file "pluralPolyfill-61f068d6.js.map" overwrites a previously emitted file of the same name.
The emitted file "langSign-66e8939d.js.map" overwrites a previously emitted file of the same name.
[vite:build-import-analysis] Cannot read properties of undefined (reading 'forEach')
✓ built in 48.32s
error during build:
TypeError: Cannot read properties of undefined (reading 'forEach')
    at addDeps (/tweb/node_modules/.pnpm/[email protected]_@[email protected][email protected]/node_modules/vite/dist/node/chunks/dep-24daf00c.js:45215:55)
    at Object.generateBundle (/tweb/node_modules/.pnpm/[email protected]_@[email protected][email protected]/node_modules/vite/dist/node/chunks/dep-24daf00c.js:45239:33)
    at /tweb/node_modules/.pnpm/[email protected]/node_modules/rollup/dist/es/shared/node-entry.js:24412:40

Solution: Find the error line in the dep-24daf00c.js file and modify it:

Aboriginal:

chunk.imports.forEach(addDeps);

// Ensure that the css imported by current chunk is loaded after the dependencies.
// So the style of current chunk won't be overwritten unexpectedly.
chunk.viteMetadata.importedCss.forEach((file) => {
    deps.add(file);
});

Modified:

if (chunk.imports){
    chunk.imports.forEach(addDeps);
}

// Ensure that the css imported by current chunk is loaded after the dependencies.
// So the style of current chunk won't be overwritten unexpectedly.
if (chunk.viteMetadata && chunk.viteMetadata.importedCss){
    chunk.viteMetadata.importedCss.forEach((file) => {
        deps.add(file);
    });
}

Finally, run the 'vite build' again and compile it successfully.

https://github.com/morethanwords/tweb/issues/269#issuecomment-1825082899

bufanyun avatar Jan 29 '24 02:01 bufanyun

Hello @morethanwords, I am having this issue when trying to migrate quite a huge frontend project from CRA to Vite and this is the only things stopping me currently

Do you know if they will approve and let your fix into production?

SimonsUnikaNamn avatar Feb 12 '24 14:02 SimonsUnikaNamn

Do you know if they will approve and let your fix into production?

Not sure yet, you could also ask them here https://github.com/vitejs/vite/pull/15469, maybe they will change the priority from minor.

morethanwords avatar Feb 12 '24 14:02 morethanwords

Thanks for answering, I have found a solution on my end

I used "build": "vite build --minify false", in order to not minimize the code in node_modules I applied your fix and console logged the files that did not have imports By looking into the files in the dist I was able to deduce where they belonged, in my case it came from dependencies in jszip

In my vite.config.ts I added manual chunking to make sure they were added into one chunk

build: {
    outDir: "build",
    target: "es2020",
    rollupOptions: {
      output: {
        manualChunks(id) {
          if (id.includes("jszip") || id.includes("pako")) {
            return "jszip";
          }
          if (id.includes("node_modules")) {
            return "vendor";
          }

          return "index";
        },
      },
    },
  },```

SimonsUnikaNamn avatar Feb 12 '24 15:02 SimonsUnikaNamn