7

Webpack 2.2.0

I include/exclude file/folder in my config, but webpack keeps bundling that was excluded:

the folder structure

src/
  index.js // entry point
server/
  test.js // file for testing
build/

webpack.config.js

const path = require('path')
const SRC = path.resolve(process.cwd(), './src')
const BUILD = path.resolve(process.cwd(), './build')

module.exports = {
  context: SRC,
  entry: {
    main: './'
  },
  output: {
    path: BUILD,
    filename: '[name].bundle.js',
    publicPath: '/assets/'
  },
  module: {
    rules: [{
      test: /\.jsx?/,
      include: SRC,
      exclude: path.resolve(process.cwd(), './server’), // even explicit excluding changes nothing
      loader: 'babel-loader'
    }]
  }
}

./src/index.js

import func from ‘../server/test.js’ // this must not be imported
func() // working

./server/test.js

export default function () { console.log(`I’m in the bundle`) } // this is being executed

I see the message in the browser console.

Sergey
  • 1,000
  • 8
  • 19

1 Answers1

5

And the answer is if you include/exclude something in the webpack config it won't be transformed by the loader but it will be imported into the bundle. To completely exclude something from bundling you need to use module.noParse option: https://webpack.js.org/configuration/module/#module-noparse

Sergey
  • 1,000
  • 8
  • 19
  • 5
    This answer does not seem to be entirely correct -- `module.noParse` does not "exclude [...] from bundling". Per the docs linked, it is mainly a performance optimization that skips _parsing_ of modules. It is very much still bundled, just not parsed. To "exclude" modules from a bundle, you'd want to use [`IgnorePlugin`](https://webpack.js.org/plugins/ignore-plugin/). Per the docs, `IgnorePlugin` will "prevent generation of modules for" the given regex. – agilgur5 Oct 26 '19 at 04:07