【问题标题】:Include/exclude don't work包含/排除不起作用
【发布时间】:2017-02-01 11:12:50
【问题描述】:

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

./server/test.js

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

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

【问题讨论】:

  • Webpack 不能排除您在 index.js 中导入和使用的内容
  • excludeinclude 选项的用途是什么?
  • 你可以在这里阅读一个很好的解释:stackoverflow.com/questions/37823764/…顺便说一句,你正在测试.jsx,但你的文件是js
  • 我正在测试两者,正则表达式中有一个问号。
  • 对不起。没看到。

标签: webpack


【解决方案1】:

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

【讨论】:

  • 这个答案似乎并不完全正确——module.noParse 没有“从捆绑中排除 [...]”。根据链接的文档,它主要是跳过解析模块的性能优化。它仍然捆绑在一起,只是没有解析。要从包中“排除”模块,您需要使用IgnorePlugin。根据文档,IgnorePlugin 将“阻止为给定正则表达式生成模块。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-17
  • 2013-06-02
  • 2015-06-09
  • 2016-02-17
  • 2016-11-06
  • 2015-12-25
  • 1970-01-01
相关资源
最近更新 更多