ice-lab/swc-webpack-plugin

error: 'delete' cannot be called on an identifier in strict mode

andersk opened this issue · 4 comments

Hi! I tried out swc-webpack-plugin on my project and ran into a problem where swc interprets a non-strict third-party module as if it were in strict mode, leading to incorrect errors. This might be an swc bug but I figured I’d ask you first.

Minimal reproduction:

package.json

{
  "dependencies": {
    "@swc/core": "^1.2.57",
    "sorttable": "^1.0.2",
    "swc-webpack-plugin": "^1.0.0",
    "webpack": "^5.37.1",
    "webpack-cli": "^4.7.0"
  }
}

webpack.config.js

const { SWCMinifyPlugin } = require("swc-webpack-plugin");

module.exports = {
  optimization: {
    minimize: true,
    minimizer: [new SWCMinifyPlugin()],
  },
};

src.js

require("sorttable");

Output

$ npx webpack
error: 'delete' cannot be called on an identifier in strict mode
  --> <anon>:80:14
   |
80 |       delete sortbottomrows;
   |              ^^^^^^^^^^^^^^

error: 'delete' cannot be called on an identifier in strict mode
   --> <anon>:166:17
    |
166 |             delete row_array;
    |                    ^^^^^^^^^

error: 'delete' cannot be called on an identifier in strict mode
   --> <anon>:261:12
    |
261 |     delete newrows;
    |            ^^^^^^^

[webpack-cli] HookWebpackError: failed to process js file

Caused by:
    failed to parse module: error was recoverable, but proceeding would result in wrong codegen
    at makeWebpackError (/home/anders/zulip/test/swc-test/node_modules/webpack/lib/HookWebpackError.js:49:9)
    at /home/anders/zulip/test/swc-test/node_modules/webpack/lib/Compilation.js:2495:12
    at eval (eval at create (/home/anders/zulip/test/swc-test/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:33:1)
-- inner error --
Error: failed to process js file

Caused by:
    failed to parse module: error was recoverable, but proceeding would result in wrong codegen
caused by plugins in Compilation.hooks.processAssets
Error: failed to process js file

Caused by:
    failed to parse module: error was recoverable, but proceeding would result in wrong codegen

Hi! I tried out swc-webpack-plugin on my project and ran into a problem where swc interprets a non-strict third-party module as if it were in strict mode, leading to incorrect errors. This might be an swc bug but I figured I’d ask you first.

Minimal reproduction:

package.json

{
  "dependencies": {
    "@swc/core": "^1.2.57",
    "sorttable": "^1.0.2",
    "swc-webpack-plugin": "^1.0.0",
    "webpack": "^5.37.1",
    "webpack-cli": "^4.7.0"
  }
}

webpack.config.js

const { SWCMinifyPlugin } = require("swc-webpack-plugin");

module.exports = {
  optimization: {
    minimize: true,
    minimizer: [new SWCMinifyPlugin()],
  },
};

src.js

require("sorttable");

Output

$ npx webpack
error: 'delete' cannot be called on an identifier in strict mode
  --> <anon>:80:14
   |
80 |       delete sortbottomrows;
   |              ^^^^^^^^^^^^^^

error: 'delete' cannot be called on an identifier in strict mode
   --> <anon>:166:17
    |
166 |             delete row_array;
    |                    ^^^^^^^^^

error: 'delete' cannot be called on an identifier in strict mode
   --> <anon>:261:12
    |
261 |     delete newrows;
    |            ^^^^^^^

[webpack-cli] HookWebpackError: failed to process js file

Caused by:
    failed to parse module: error was recoverable, but proceeding would result in wrong codegen
    at makeWebpackError (/home/anders/zulip/test/swc-test/node_modules/webpack/lib/HookWebpackError.js:49:9)
    at /home/anders/zulip/test/swc-test/node_modules/webpack/lib/Compilation.js:2495:12
    at eval (eval at create (/home/anders/zulip/test/swc-test/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:33:1)
-- inner error --
Error: failed to process js file

Caused by:
    failed to parse module: error was recoverable, but proceeding would result in wrong codegen
caused by plugins in Compilation.hooks.processAssets
Error: failed to process js file

Caused by:
    failed to parse module: error was recoverable, but proceeding would result in wrong codegen

Can add module. strictMode to false to resolve this problem?

See: https://swc.rs/docs/configuring-swc/#strictmode

new SWCMinifyPlugin({ module: { type: "commonjs", strictMode: false } }) didn’t resolve this.

It is a swc bug indeed. I tried to bundle code with swc-loader, the problem exists too.