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]: JavaScript parsing error: 'import', and 'export' cannot be used outside of module code #7887

Closed
yinhunfeixue opened this issue Sep 13, 2024 · 4 comments
Assignees
Labels
bug Something isn't working pending triage The issue/PR is currently untouched.

Comments

@yinhunfeixue
Copy link

System Info

system: macos 15.0
node: 20.16.0

Details

rspack.zip

操作步骤:

  1. 解压附件文件
  2. npm i
  3. node pack.js

JavaScript parsing error: 'import', and 'export' cannot be used outside of module code

问题:
mode: "development" 正常
mode: "production", 报错
image

Reproduce link

No response

Reproduce Steps

see details

@yinhunfeixue yinhunfeixue added bug Something isn't working pending triage The issue/PR is currently untouched. labels Sep 13, 2024
@GiveMe-A-Name GiveMe-A-Name self-assigned this Sep 13, 2024
@inottn
Copy link
Contributor

inottn commented Sep 15, 2024

Rspack uses the SwcJsMinimizerRspackPlugin by default to compress code, and you may need to configure the minimizerOptions to set module to true.

@hardfist
Copy link
Contributor

hardfist commented Sep 19, 2024

Rspack uses the SwcJsMinimizerRspackPlugin by default to compress code, and you may need to configure the minimizerOptions to set module to true.

this is not the best solution since it will cause all js assets are treated module, you can control specific assets by set assetInfo

 compilation.updateAsset(entry, new ConcatSource(
          `export default function(global, globalThis, window, $app_exports$, $app_evaluate$){`,
          source,
          `}`
        ), {
          javascriptModule:true // set this asset as module
  });

but rspack has some bugs about dealing with assetInfo, I'm gonna fix it

@hardfist
Copy link
Contributor

should be fixed in #7924

@hardfist
Copy link
Contributor

Rspack uses the SwcJsMinimizerRspackPlugin by default to compress code, and you may need to configure the minimizerOptions to set module to true.

this is not the best solution since it will cause all js assets are treated module, you can control specific assets by set assetInfo

 compilation.updateAsset(entry, new ConcatSource(
          `export default function(global, globalThis, window, $app_exports$, $app_evaluate$){`,
          source,
          `}`
        ), {
          javascriptModule:true // set this asset as module
  });

but rspack has some bugs about dealing with assetInfo, I'm gonna fix it

should be fixed in should be fixed in #7924

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pending triage The issue/PR is currently untouched.
Projects
None yet
Development

No branches or pull requests

4 participants