webpack: `"sideEffects": false` not working

Bug report

What is the current behavior?

If the current behavior is a bug, please provide the steps to reproduce.

// package.json
{
  "sideEffects": false,
  "dependencies": {
    "webpack": "^4.39.2",
    "webpack-cli": "^3.3.7"
  }
}
// index.js (entry)
import { identity } from "./test";
console.log(identity);
// test.js
export const identity = x => x;

const myFunction = I => {
  const r = {};
  if (typeof I.map === "function") {
    r.map = () => {};
  }
  return r;
};

const result = myFunction({});
// webpack.config.js
const config = {
  mode: "production",
  entry: './src/index.js',
  output: {
    path: './target',
    filename: 'index.js'
  }
};

module.exports = config;

Now run webpack.

What is the expected behavior?

When I run webpack, I expect the myFunction call and body to removed (because I have specified "sideEffects": false), however they are not. Excerpt below:

})([
  function(e, t, n) {
    "use strict";
    n.r(t);
    (e => {
      const t = {};
      "function" == typeof e.map && (t.map = () => {});
    })({});
    console.log(e => e);
  }
]);

For context, I originally discovered this whilst trying to import from an npm module: import { max } from 'fp-ts/es6/Ord';.

Other relevant information: webpack version: 4.39.2 Node.js version: 12.8.1 Operating System: N/A Additional tools:

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 18 (10 by maintainers)

Most upvoted comments

Note that even this code:

r.map = () => {};

can potentially have side-effects when a setter is defined on Object.prototype.