javascript 如何从 Webpack 捆绑中排除目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52059366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 09:48:27  来源:igfitidea点击:

How to exclude directory from getting bundled by Webpack?

javascriptnpmwebpack

提问by NewScientists

Expected:

预期的:

When I build with webpack, all my JS files get bundled except for the files in the ./src/Portfolio directory as per my Webpack.config.js settings.

当我使用 webpack 构建时,除 ./src/Portfolio 目录中的文件外,我的所有 JS 文件都根据我的 Webpack.config.js 设置进行了捆绑。

Actual:

实际的:

Webpack bundles all the files including the ones in the directory despite the settings and other variations i have provided within webpack.config.js.

尽管我在 webpack.config.js 中提供了设置和其他变体,但 Webpack 捆绑了所有文件,包括目录中的文件。

Code:

代码:

Webpack.config.js

webpack.config.js

const path = require('path');

module.exports = {
  entry: './src/index.js',
  devtool: 'source-map',
  mode: 'development',
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: [
          path.resolve(__dirname, './src/Portfolio/')
        ]
      }
    ]
  },
  output: {
    filename: 'main.js',
    path: path.resolve(__dirname, 'dist')
  }
};

Output:

输出:

enter image description here

在此处输入图片说明

How can i successfully exclude the ./src/Portfolio directory and its contents?

如何成功排除 ./src/Portfolio 目录及其内容?

采纳答案by Chris Hawkes

Depending on what your folder structure looks like it appears you aren't providing it the right directory location to exclude. I would think something like this should work, but if not please share your folder structure.

根据您的文件夹结构,看起来您没有为其提供正确的目录位置以排除。我认为这样的事情应该可行,但如果不行,请分享您的文件夹结构。

const path = require('path');

module.exports = {
  entry: './src/index.js',
  devtool: 'source-map',
  mode: 'development',
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: [
          './src/Portfolio/'
        ]
      }
    ]
  },
  output: {
    filename: 'main.js',
    path: path.resolve(__dirname, 'dist')
  }
};