Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include/exclude don't work

Tags:

webpack

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.

like image 322
Sergey Avatar asked Oct 17 '22 17:10

Sergey


1 Answers

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

like image 150
Sergey Avatar answered Oct 21 '22 07:10

Sergey