Skip to content

Rollup config breaks some third-party libraries #3071

Closed

Description

@adorack

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

Activity

  1. added
    pending triage
    on Feb 6, 2025
  2. pi0 commented on Feb 6, 2025

    @pi0
    Member

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

  3. adorack commented on Feb 6, 2025

    @adorack
    Author

    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/
    
  4. adorack commented on Feb 6, 2025

    @adorack
    Author

    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.

  5. pi0 commented on Feb 6, 2025

    @pi0
    Member

    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)

  6. adorack commented on Feb 6, 2025

    @adorack
    Author

    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
    
  7. adorack commented on Feb 6, 2025

    @adorack
    Author

    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.)

  8. adorack commented on Feb 6, 2025

    @adorack
    Author

    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.

  9. pi0 commented on Feb 6, 2025

    @pi0
    Member

    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 🙏🏼

  10. added
    bug
    documentation
    and removed
    pending triage
    on Feb 6, 2025
  11. adorack commented on Feb 6, 2025

    @adorack
    Author

    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?

  12. pi0 commented on Feb 6, 2025

    @pi0
    Member

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

  13. fubhy commented on Feb 16, 2025

    @fubhy

    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.

  14. pi0x commented on Sep 7, 2026

    @pi0x
    Contributor

    Fixed in v3 — Nitro no longer replaces typeof window in the bundle, so string literals containing it are left alone; the only default replacements are the import.meta.* flags in https://github.com/nitrojs/nitro/blob/main/src/build/config.ts.

    Triaged with AI assistance.

  15. added theissue type on Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugdocumentation

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions