Skip to content

The webpack.ProvidePlugin does not tree shake code as documented #12441

@zerustech

Description

@zerustech

Steps to reproduce this issue

  1. Initialize the project structure:
$ cd <base>
$ mkdir test
$ cd test
$ yarn init
$ yarn add --dev webpack webpack-cli
$ mkdir src
  1. Create src/index.js:
function component() {
    const element = document.createElement('pre');
    element.innerHTML = ['Hello', 'webpack', '5 cube is equal to ' + cube(5)].join('\n\n');
    return element;
}
document.body.appendChild(component());
  1. Create src/math.js:
export function square(x) {
    return x * x;
};

export function cube(x) {
    return x * x * x;
};
  1. Create webpack.config.js:
const path = require('path');
const webpack = require('webpack');

module.exports = {
    entry: './src/index.js',
    output: {
        filename: '[name].bundle.js',
        path: path.resolve(__dirname, 'dist'),
    },
    plugins: [
        new webpack.ProvidePlugin({
            cube: ['./math.js', 'cube'],
        }),
    ],
    mode: 'production',
    optimization: {
        runtimeChunk: true,
    },
};
  1. Build the bundle:
$ yarn webpack
  1. Expected Result:
    ONLY the cube() function is included in dist/main.bundle.js

  2. Actual Result:
    BOTH the cube() and the square() functions are included.

Environment

  • webpack - 5.14.10
  • node - 15.5.0
  • OS - macOS Big Sur

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions