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

[commonjs-resolver] can not parse umd file properly (possibly)due to BOM marker #5258

Closed
GuichiZhao opened this issue Nov 20, 2023 · 8 comments · Fixed by #5268
Closed

[commonjs-resolver] can not parse umd file properly (possibly)due to BOM marker #5258

GuichiZhao opened this issue Nov 20, 2023 · 8 comments · Fixed by #5268
Assignees

Comments

@GuichiZhao
Copy link

GuichiZhao commented Nov 20, 2023

Rollup Version

4.5.0

Operating System (or Browser)

Ubuntu 22.04.3 LTS

Node Version (if applicable)

v20.6.1

Link To Reproduction

https://vitejs.dev/

Expected Behaviour

Report
RollupError: Expected '(', got 'string literal (object, 'object')' after upgrade vite 5, the error disappears after bringing back to vite 4, obviously there is something wrong with the rollup parser, it can not parse a legal syntax somehow
Screenshot from 2023-11-20 14-56-48
else if (typeof module !== "undefined" && module.exports) should be a valid javascript syntax

Actual Behaviour

Report error

@lukastaegert
Copy link
Member

Do you have an actual reproduction we can have a look at? E.g. something in the Rollup REPL?

@GuichiZhao
Copy link
Author

GuichiZhao commented Nov 20, 2023

@lukastaegert It is a super complicate project, I am trying to strip it down

@GuichiZhao
Copy link
Author

Sorry, I can not reproduce the bug, just some more hint hopefully it will be helpful

Screenshot from 2023-11-20 14-56-48
The [commonjs-resolve] can not parse the umd file which come from

    "node_modules/lerc": {
      "version": "3.0.0",
      "resolved": "https://registry.npmjs.org/lerc/-/lerc-3.0.0.tgz",
      "integrity": "sha512-Rm4J/WaHhRa93nCN2mwWDZFoRVF18G1f47C+kvQWyHGEZxFpTUi73p7lMVSAndyxGt6lJ2/CFbOcf9ra5p8aww=="
    },

which in turn required by

    "node_modules/geotiff": {
      "version": "2.1.0",
      "resolved": "https://registry.npmjs.org/geotiff/-/geotiff-2.1.0.tgz",
      "integrity": "sha512-B/iFJuFfRpmPHXf8aIRPRgUWwfaNb6dlsynkM8SWeHAPu7CpyvfqEa43KlBt7xxq5OTVysQacFHxhCn3SZhRKQ==",
      "dependencies": {
        "@petamoriken/float16": "^3.4.7",
        "lerc": "^3.0.0",
        "pako": "^2.0.4",
        "parse-headers": "^2.0.2",
        "quick-lru": "^6.1.1",
        "web-worker": "^1.2.0",
        "xml-utils": "^1.0.2",
        "zstddec": "^0.1.0"
      },
      "engines": {
        "node": ">=10.19"
      }
    },

and finally required by

    "node_modules/geotiff": {
      "version": "2.1.0",
      "resolved": "https://registry.npmjs.org/geotiff/-/geotiff-2.1.0.tgz",
      "integrity": "sha512-B/iFJuFfRpmPHXf8aIRPRgUWwfaNb6dlsynkM8SWeHAPu7CpyvfqEa43KlBt7xxq5OTVysQacFHxhCn3SZhRKQ==",
      "dependencies": {
        "@petamoriken/float16": "^3.4.7",
        "lerc": "^3.0.0",
        "pako": "^2.0.4",
        "parse-headers": "^2.0.2",
        "quick-lru": "^6.1.1",
        "web-worker": "^1.2.0",
        "xml-utils": "^1.0.2",
        "zstddec": "^0.1.0"
      },
      "engines": {
        "node": ">=10.19"
      }
    },

All I can observe is that the dependency path is quite deep, will it trick the [commonjs-resolve] somehow?
Another observation I can make is that when I comment out some real code not import statement, the build can pass but it is not reliable , I guess there is some kind of cache I do not know

For now, switching back to the old version solve my problem

@sapphi-red
Copy link
Contributor

sapphi-red commented Nov 20, 2023

Rollup doesn't throw an error if I pass in the content of lerc@3.0.0/LercDecode.js.
I guess one of the plugin is generating invalid code.

@ThenTech
Copy link

ThenTech commented Nov 20, 2023

I just encountered the exact same issue, also in the lerc package (imported by geotiff which is used by OpenLayers).

On Windows, with:

"rollup": "4.5.0",
"vite": "5.0.0",

Original error:

[vite-plugin-pwa:build] Expected '(', got 'string literal (object, 'object')'
file: /node_modules/.pnpm/lerc@3.0.0/node_modules/lerc/LercDecode.js:2314:10
2312:     define([], function() { return Lerc; });/* jshint ignore:line */
2313:   }
2314:   else if (typeof module !== "undefined" && module.exports) {/* jshint ignore:line */
                ^
2315:     //commonJS module 1.0/1.1/1.1.1 systems, such as nodeJS
2316:     //http://wiki.commonjs.org/wiki/Modules
error during build:
RollupError: Expected '(', got 'string literal (object, 'object')'
    at error (/node_modules/.pnpm/rollup@4.5.0/node_modules/rollup/dist/es/shared/parseAst.js:337:30)
    at nodeConverters (/node_modules/.pnpm/rollup@4.5.0/node_modules/rollup/dist/es/shared/parseAst.js:2090:9)
    at convertNode (/node_modules/.pnpm/rollup@4.5.0/node_modules/rollup/dist/es/shared/parseAst.js:975:12)
    at convertProgram (/node_modules/.pnpm/rollup@4.5.0/node_modules/rollup/dist/es/shared/parseAst.js:966:48)
    at parseAstAsync (/node_modules/.pnpm/rollup@4.5.0/node_modules/rollup/dist/es/shared/parseAst.js:2156:20)
    at async Module.tryParseAsync (/node_modules/.pnpm/rollup@4.5.0/node_modules/rollup/dist/es/shared/node-entry.js:13490:21)
    at async Module.setSource (/node_modules/.pnpm/rollup@4.5.0/node_modules/rollup/dist/es/shared/node-entry.js:13071:35)
    at async ModuleLoader.addModuleSource (/node_modules/.pnpm/rollup@4.5.0/node_modules/rollup/dist/es/shared/node-entry.js:17713:13)

Manually putting typeof module in that file:

[vite-plugin-pwa:build] Expected ',', got 'e'
file: /node_modules/.pnpm/lerc@3.0.0/node_modules/lerc/LercDecode.js:2310:39
2310:     console.log(">>><<< ", typeof module)
                                             ^
error during build:
RollupError: Expected ',', got 'e'
    at error (/node_modules/.pnpm/rollup@4.5.0/node_modules/rollup/dist/es/shared/parseAst.js:337:30)
    at nodeConverters (/node_modules/.pnpm/rollup@4.5.0/node_modules/rollup/dist/es/shared/parseAst.js:2090:9)
    at convertNode (/node_modules/.pnpm/rollup@4.5.0/node_modules/rollup/dist/es/shared/parseAst.js:975:12)
    at convertProgram (/node_modules/.pnpm/rollup@4.5.0/node_modules/rollup/dist/es/shared/parseAst.js:966:48)
    at parseAstAsync (/node_modules/.pnpm/rollup@4.5.0/node_modules/rollup/dist/es/shared/parseAst.js:2156:20)
    at async Module.tryParseAsync (/node_modules/.pnpm/rollup@4.5.0/node_modules/rollup/dist/es/shared/node-entry.js:13490:21)
    at async Module.setSource (/node_modules/.pnpm/rollup@4.5.0/node_modules/rollup/dist/es/shared/node-entry.js:13071:35)
    at async ModuleLoader.addModuleSource (/node_modules/.pnpm/rollup@4.5.0/node_modules/rollup/dist/es/shared/node-entry.js:17713:13)

As @GuichiZhao says, it doesn't happen in Vite v4.5, only in v5

Edit: If I remove vite-plugin-pwa the same error shows with commonjs--resolver instead, just as @GuichiZhao also showed.

@simon04
Copy link

simon04 commented Nov 21, 2023

Here's a minimal example to reproduce (involving only rollup 4.5.0, without vite): https://github.com/simon04/ol-rollup/tree/rollup-5258

@sapphi-red
Copy link
Contributor

It didn't reproduce in stackblitz but reproduced on my local Windows machine. The code after transformed by commonjs plugin was

if (typeofundefinede === "function" &&undefinede.amd) {/* jshint ignore:line */
	    //amd loaders such as dojo and requireJS
	    //http://wiki.commonjs.org/wiki/Modules/AsynchronousDefinition
	   undefinede([], function() { return Lerc; });/* jshint ignore:line */
	  }
	  else if 'object'e !== "undefined" && module.exports) {/* jshint ignore:line */
	    //commonJS module 1.0/1.1/1.1.1 systems, such as nodeJS
	    //http://wiki.commonjs.org/wiki/Modules
	    module.exports = Lerc;/* jshint ignore:line */
	  }
	  else {
	    //assign to this, most likely window
	    this.Lerc = Lerc;
	  }

I found that node_modules/lerc/LercDecode.js has a BOM. If I saved it without BOM, the build was successful. I guess it's related to the BOM handling.

refs #5162, #5164

@GuichiZhao GuichiZhao changed the title Can not parse umd file properly [commonjs-resolver] can not parse und file properly (possibly)due to BOM marker Nov 22, 2023
@GuichiZhao GuichiZhao changed the title [commonjs-resolver] can not parse und file properly (possibly)due to BOM marker [commonjs-resolver] can not parse umd file properly (possibly)due to BOM marker Nov 22, 2023
@TrickyPi TrickyPi self-assigned this Nov 23, 2023
Copy link

This issue has been resolved via #5268 as part of rollup@4.5.2. You can test it via npm install rollup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants