Pete
Pete

Reputation: 3451

Why Does Excluding node_modules from webpack build not cause problems?

It's clear that the right thing to do when building a react app with webpack is to exclude node modules in a way that the file below shows. My question is "why is this OK?". It seems to me that the code in those node modules is necessary to make the minimized JavaScript work. I see comments like "Exclude those files because they are not necessary". How can that not be necessary?

module: {
    rules: [
        {
            test: /\.js?$/,
            loader: 'babel-loader',
            exclude: /node_modules/,
            options: {
                presets: [
                    'react',
                    'stage-2',
                    ['env', { targets: { browsers: ['last 2 versions'] } }]
                ]
            }
        }
    ]
}

Upvotes: 0

Views: 555

Answers (1)

Anthony N
Anthony N

Reputation: 972

The exclude that you see here excludes it from running through the babel-loader. They are not excluded from your final output file though.

If you run webpack and look at the final output file, you will notice that all of the node_modules that you included will be in there.

The thinking behind this is that it is the responsibility of the library authors to do all the transpilation/compilation for their code.

Upvotes: 3

Related Questions