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

[Bug]: import "binaryen" throws error "'import.meta' cannot be used outside of module code" when minimize is on #6513

Closed
xc2 opened this issue May 11, 2024 · 2 comments · Fixed by #6521
Labels
bug Something isn't working

Comments

@xc2
Copy link
Collaborator

xc2 commented May 11, 2024

System Info

  System:
    OS: macOS 14.5
    CPU: (12) arm64 Apple M2 Max
    Memory: 97.03 MB / 32.00 GB
    Shell: 3.7.1 - /opt/homebrew/bin/fish
  Binaries:
    Node: 20.12.2 - ~/.managed/n/bin/node
    Yarn: 1.22.21 - ~/.managed/n/bin/yarn
    npm: 10.5.0 - ~/.managed/n/bin/npm
    pnpm: 8.15.4 - ~/.managed/n/bin/pnpm
    bun: 1.0.34 - /opt/homebrew/bin/bun
    Watchman: 2024.03.18.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome: 124.0.6367.158
    Safari: 17.5
  npmPackages:
    @rspack/cli: ^0.6.3 => 0.6.5 
    @rspack/core: ^0.6.3 => 0.6.5 

Details

Code

import a from 'binaryen';

console.log(a);

Configuration

module.exports = {
  mode: "none",
  optimization: {
    // when minimize is on, importing binaryen will produce error "'import.meta' cannot be used outside of module code"
    minimize: true,
  },
}

Got


> rspack-repro@1.0.0 build:rspack /Users/kfll/Developer/rspack-repro-binaryen
> rspack -c rspack.config.js

ERROR in × JavaScript parsing error: 'import.meta' cannot be used outside of module code.
   ╭─[4:1]
 4 │  * SPDX-License-Identifier: Apache-2.0
 5 │  */
 6 │ var JC=(()=>{var vA=import.meta.url;return async fun...

Expected

There's type:"module" in binaryen/package.json so it should be considered as module code

More

This only happens when minimize is on

Reproduce link

https://github.com/xc2/rspack-repro-binaryen

Reproduce Steps

pnpm i
pnpm build:rspack | less
@xc2 xc2 added bug Something isn't working pending triage The issue/PR is currently untouched. labels May 11, 2024
@h-a-n-a
Copy link
Collaborator

h-a-n-a commented May 11, 2024

Try to enable module in SWC js minifier plugin.

@xc2
Copy link
Collaborator Author

xc2 commented May 13, 2024

I've simplified the reproduction.

This problem occurs when importing a "module" package whose main file contains new URL("./", import.meta.url).

Both rspack and webpack will create an asset file xxx.js for the main file itself because of the statement new URL("./", import.meta.url) as documented: https://webpack.js.org/guides/asset-modules/#url-assets

Consider the foo example, the package foo has these two traits:

  1. there's new URL("./", import.meta.url) in foo/index.js
  2. is a "module" package

When importing foo, both rspack and webpack will

  1. bundle content of foo/index.js into the main asset file
  2. and also create an asset file xxxx.js whose content is same as foo/index.js

When minimizing the xxxx.js file, depending on the value of package "type"

package type of foo minimize rspack webpack
"module" ON [1]
"commonjs" ON [2] [2]
"module" OFF
"commonjs" OFF [2] [2]

After dig into it, I just found

  • failure [2] is dropped before minimizing which is as expected and has nothing to do with this issue.
  • while [1] is dropped on minimizing which causes this issue.
  • webpack minifies the file with module: undefined while rspack minifies it with module: false

@xc2 xc2 self-assigned this May 13, 2024
@xc2 xc2 removed the pending triage The issue/PR is currently untouched. label May 13, 2024
@xc2 xc2 removed their assignment May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants