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 config breaks some third-party libraries #3071

Open
adorack opened this issue Feb 6, 2025 · 11 comments · May be fixed by #3078
Open

Rollup config breaks some third-party libraries #3071

adorack opened this issue Feb 6, 2025 · 11 comments · May be fixed by #3078
Labels
bug Something isn't working documentation Improvements or additions to documentation v2 wontfix This will not be worked on workaround available

Comments

@adorack
Copy link

adorack commented Feb 6, 2025

Environment

  • Operating System: Linux
  • Node Version: v20.18.1
  • Nitro Version: 2.10.4
  • Package Manager: [email protected]

Reproduction

adorack/nitro-error@fe1f99f

Steps to reproduce:

  • Clone the above repository
  • Install dependencies
  • Run npm run build

Describe the bug

I first encountered this in Nuxt, and I'm able to work around this particular issue by applying this setting in the Nuxt config:

  nitro: {
    preset: 'cloudflare-module',
    replace: {
      'typeof window': '`undefined`',
    },
  },

I'm not clear on why Nitro tries to apply this optimization.

Additional context

No response

Logs

@pi0
Copy link
Member

pi0 commented Feb 6, 2025

I'm worndering if it is double transform with wranger deploy. Can you spot same issue in .output / dist/_worker also?

@adorack
Copy link
Author

adorack commented Feb 6, 2025

Hello -- thanks for the quick response! I'm not sure exactly what you mean... .output doesn't contain any files in my Nitro build:

fd . .output
.output/public/
.output/server/

@adorack
Copy link
Author

adorack commented Feb 6, 2025

I don't think it's a double transform issue, though -- this would arise from a single transform. The package contains a line of code that's something like

const blah = "if (typeof window === 'undefined') ...";

When Rollup applies its search-and-replace, the resulting source code is

const blah = "if ("undefined" === 'undefined') ...";`

This causes a syntax error when the build process actually tries to parse the module.

@pi0
Copy link
Member

pi0 commented Feb 6, 2025

Nitro output for cloudflare-module presets goes into .output/server/* dir.

the resulting source code is

Do you see this issue in a file inside .output/server/* files when using build --preset cloudflare-module

(consider before deploy, wrangler always does another transform step)

@adorack
Copy link
Author

adorack commented Feb 6, 2025

I don't see the issue in .output/server; that directory is empty. (I think the build fails before emitting anything there?)

I see the issue in the full log output; sorry, here that is:

npm run build

> build
> nitro build

✔ Generated public .output/public                                                                                                          nitro 11:04:56 AM
ℹ Building Nitro Server (preset: cloudflare-module, compatibility date: 2025-01-31)                                                        nitro 11:04:56 AM

 ERROR  RollupError: Expected ',', got 'undefined' in [...]/nitro/node_modules/papaparse/papaparse.js                          nitro 11:04:56 AM


50:     var URL = global.URL || global.webkitURL || null;
51:     var code = moduleFactory.toString();
52:     return Papa.BLOB_URL || (Papa.BLOB_URL = URL.createObjectURL(new Blob(["var global = (function() { if (typeof self !== 'undefined') { return self; } if ("undefined"...
                                                                                                                                                                  ^
53:   }


 ERROR  Expected ',', got 'undefined' in [...]/nitro/node_modules/papaparse/papaparse.js                                             11:04:56 AM

    at getRollupError (node_modules/rollup/dist/es/shared/parseAst.js:397:41)
    at convertProgram (node_modules/rollup/dist/es/shared/parseAst.js:1085:26)
    at parseAst (node_modules/rollup/dist/es/shared/parseAst.js:2070:87)
    at tryParse (node_modules/@rollup/plugin-commonjs/dist/es/index.js:17:12)
    at analyzeTopLevelStatements (node_modules/@rollup/plugin-commonjs/dist/es/index.js:37:15)
    at Object.transformAndCheckExports (node_modules/@rollup/plugin-commonjs/dist/es/index.js:2113:68)
    at Object.transform (node_modules/@rollup/plugin-commonjs/dist/es/index.js:2317:41)
    at node_modules/rollup/dist/es/shared/node-entry.js:21838:40



 ERROR  Expected ',', got 'undefined' in [...]/nitro/node_modules/papaparse/papaparse.js

@adorack
Copy link
Author

adorack commented Feb 6, 2025

I tried npm run build --preset cloudflare-module and the build completed successfully, but still nothing was generated in the server directory. (I have preset set in the config; I linked adorack/nitro-error@fe1f99f in the ticket description, which is the diff from the starter project.)

@adorack
Copy link
Author

adorack commented Feb 6, 2025

Oh, weird -- I do see output in cloudflare-module/.output/server -- but it doesn't look like this is pulling in PapaParse; I don't see the offending code fragment at all.

@pi0
Copy link
Member

pi0 commented Feb 6, 2025

cloudflare-module/.output/server it seems wrong build/rootDir.

Your issue stands correct and I checked locally, it happens for any builds without having external (output/server/node_modules) support, and cloudflare is currently one of them.

Your solution is also nice. Sadly we cannot either change or drop this change as it breaks other places that depend on this tree-shaking method. If you like to contribute to docs perhaps additing a section to end of cloudflare.md it is more than welcome 🙏🏼

@pi0 pi0 added bug Something isn't working documentation Improvements or additions to documentation wontfix This will not be worked on workaround available v2 and removed preset:cloudflare pending triage labels Feb 6, 2025
@adorack
Copy link
Author

adorack commented Feb 6, 2025

OK -- thank you for looking into it.

Sadly we cannot either change or drop this change as it breaks other places that depend on this tree-shaking method.

I'm happy to make a small docs PR (probably tomorrow at this point). However, is there any further explanation for what this tree-shaking accomplishes that I can link to or describe? Is it that it's somehow allowing a compiler to exclude branches that are client-only or server-only, and that compiler refuses to make assumptions about the window object?

@pi0
Copy link
Member

pi0 commented Feb 6, 2025

Yes exactly it is to guide compiler to remove such branches. Both to eliminate code and remove incompatible code paths.

@pi0 pi0 mentioned this issue Feb 6, 2025
2 tasks
@adorack adorack linked a pull request Feb 7, 2025 that will close this issue
8 tasks
@fubhy
Copy link

fubhy commented Feb 16, 2025

This is indeed a bug. Replacing arbitrary code like this globally and without considering the AST will inevitably lead to incredibly difficult to debug problems. This bug is also triggered in combination with effect: Effect-TS/effect#3864

In our case, this configuration causes typeof window to be replaced in a string which it should absolutely not do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation v2 wontfix This will not be worked on workaround available
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants