包含/排除不工作

时间:2017-02-01 11:12:50

标签: webpack

Webpack 2.2.0

我在配置中包含/排除文件/文件夹,但webpack保留了被排除的捆绑:

文件夹结构

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

./服务器/ test.js

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

我在浏览器控制台中看到了该消息。

1 个答案:

答案 0 :(得分:1)

答案是,如果您在webpack配置中include/exclude某些东西,它将不会被加载器转换,但它将被导入到捆绑包中。要从捆绑中完全排除某些内容,您需要使用module.noParse选项:https://webpack.js.org/configuration/module/#module-noparse

相关问题