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]: bad case of unused pure expression #7016

Closed
xc2 opened this issue Jul 2, 2024 · 1 comment · Fixed by #7043
Closed

[Bug]: bad case of unused pure expression #7016

xc2 opened this issue Jul 2, 2024 · 1 comment · Fixed by #7043
Labels
bug Something isn't working

Comments

@xc2
Copy link
Collaborator

xc2 commented Jul 2, 2024

System Info

  System:
    OS: macOS 15.0
    CPU: (12) arm64 Apple M2 Max
    Memory: 1.34 GB / 32.00 GB
    Shell: 3.7.1 - /opt/homebrew/bin/fish
  Browsers:
    Chrome: 126.0.6478.127
  npmPackages:
    @rspack/cli: 0.7.0 => 0.7.0 
    @rspack/core: 0.7.0 => 0.7.0 

Details

Another problem related to #7012

This problem is included in rspack 0.6.0-1.0.0.alpha.0 and newTreeshaking-enabled prior-0.6.0 as well.

Case

js

if (true) {
  const hasConsole = typeof console !== 'undefined';
  const warn = (msg) => {
    if (hasConsole) {
      console.warn(msg)
    }
  }
  warn()
}

rspack.config.js

module.exports = {
  mode: "none",
  devtool: false,
  entry: './src/index.js'.
  optimization: {
    innerGraph: true,
  }
};

Expected

Webpack output as well.

var __webpack_exports__ = {};
if (true) {
  const hasConsole = typeof console !== 'undefined';
  const warn = (msg) => {
    if (hasConsole) {
      console.warn(msg)
    }
  }
  warn()
}

Got

var __webpack_exports__ = {};
if (true) {
  const hasConsole = (/* unused pure expression or super */ null && (typeof console !== 'undefined'));
  const warn = (msg) => {
    if (hasConsole) {
      console.warn(msg)
    }
  }
  warn()
}

Reproduce link

No response

Reproduce Steps

@xc2 xc2 added bug Something isn't working pending triage The issue/PR is currently untouched. labels Jul 2, 2024
@h-a-n-a
Copy link
Collaborator

h-a-n-a commented Jul 3, 2024

This is related to us not aligning InnerGraphPlugin with webpack. typeof console is regarded as of side effect in webpack, whereas rspack is not and regarded this case as side effect free. PureExpression is generated based on that.

@h-a-n-a h-a-n-a removed the pending triage The issue/PR is currently untouched. label Jul 3, 2024
JSerFeng added a commit that referenced this issue Jul 5, 2024
JSerFeng added a commit that referenced this issue Jul 8, 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