有人知道如何在webpack.config.js文件中创建多个输出路径吗?我正在使用bootstrap-sass,它附带了一些不同的字体文件等。为了webpack处理这些,我已经包括了正常工作的文件加载器,但是它输出的文件被保存到我为其他文件指定的输出路径:

    output: {
      path: __dirname + "/js",
      filename: "scripts.min.js"
    }

我想实现的东西,我可以看看扩展类型的任何webpack输出和东西结束。woff .eot等,有他们转移到不同的输出路径。这可能吗?

我做了一点谷歌搜索,在github上遇到了这个*问题,那里提供了一些解决方案,编辑:

但是,似乎您需要知道入口点才能使用散列方法指定输出 例如:

var entryPointsPathPrefix = './src/javascripts/pages';
var WebpackConfig = {
  entry : {
    a: entryPointsPathPrefix + '/a.jsx',
    b: entryPointsPathPrefix + '/b.jsx',
    c: entryPointsPathPrefix + '/c.jsx',
    d: entryPointsPathPrefix + '/d.jsx'
  },

  // send to distribution
  output: {
    path: './dist/js',
    filename: '[name].js'
  }
}

* https://github.com/webpack/webpack/issues/1189

然而在我的例子中,就字体文件而言,输入过程有点抽象,我所知道的只是输出。在我的其他文件进行转换的情况下,有一个已知的点,我要求他们在然后由我的加载器处理。如果有办法找到这一步发生在哪里,那么我就可以使用散列方法自定义输出路径,但我不知道这些文件需要放在哪里。


当前回答

现在(从Webpack v5.0.0开始)可以使用新的“描述符”语法(https://webpack.js.org/configuration/entry-context/#entry-descriptor) -为每个条目指定唯一的输出路径

module.exports = {
  entry: {
    home: { import: './home.js', filename: 'unique/path/1/[name][ext]' },
    about: { import: './about.js', filename: 'unique/path/2/[name][ext]' }
  }
};

其他回答

我的情况是这样的

const config = {
    entry: {
    moduleA: './modules/moduleA/index.js',
    moduleB: './modules/moduleB/index.js',
    moduleC: './modules/moduleB/v1/index.js',
    moduleC: './modules/moduleB/v2/index.js',
  },
}

我这样解(webpack4)

const config = {
entry: {
        moduleA: './modules/moduleA/index.js',
        moduleB: './modules/moduleB/index.js',
        'moduleC/v1/moduleC': './modules/moduleB/v1/index.js',
        'moduleC/v2/MoculeC': './modules/moduleB/v2/index.js',
      },
    }

如果你可以使用多个具有相同深度和文件夹结构的输出路径,在webpack 2中有一种方法可以做到这一点(尚未使用webpack 1.x进行测试)

基本上你不需要遵循doc规则,你需要为文件名提供一个路径。

module.exports = {
    entry: {
      foo: 'foo.js',
      bar: 'bar.js'
    },

    output: {
      path: path.join(__dirname, 'components'),
      filename: '[name]/dist/[name].bundle.js', // Hacky way to force webpack   to have multiple output folders vs multiple files per one path
    }
};

这将采用这个文件夹结构

/-
  foo.js
  bar.js

然后把它变成

/-
  foo.js
  bar.js
  components/foo/dist/foo.js
  components/bar/dist/bar.js

你当然可以从webpack中返回配置数组。配置文件。但如果你只是想在项目文档的文件夹中保存一份工件副本,这并不是一个最佳的解决方案,因为它会使webpack构建代码的时间增加两倍。

在这种情况下,我建议使用FileManagerWebpackPlugin插件:

const FileManagerPlugin = require('filemanager-webpack-plugin');
// ...
plugins: [
    // ...
    new FileManagerPlugin({
      onEnd: {
        copy: [{
          source: './dist/*.*',
          destination: './public/',
        }],
      },
    }),
],

问题已经在语言中了:

条目(是一个对象(键/值),用于定义输入*) 输出(是一个对象(键/值),用于定义输出*)

根据有限的占位符(如'[name]')来区分输出的想法定义了限制。

我喜欢webpack的核心功能,但是它的使用需要用基于逻辑和简单性的抽象定义重写…软件开发中最困难的事情……逻辑性和简单性。

所有这些问题都可以通过提供输入/输出定义列表来解决。输入/输出定义列表。

Vinod Kumar的好办法是:

module.exports = {
   plugins: [
    new FileManagerPlugin({
      events: {
        onEnd: {
          copy: [
            {source: 'www', destination: './vinod test 1/'},
            {source: 'www', destination: './vinod testing 2/'},
            {source: 'www', destination: './vinod testing 3/'},
          ],
        },
      }
    }),
  ],
};

请不要使用任何变通方法,因为这会影响构建性能。

Webpack文件管理器插件

将这个标签复制到webpack.config.js的顶部很容易安装

const FileManagerPlugin = require('filemanager-webpack-plugin');

安装

NPM安装文件管理器-webpack-plugin

添加插件

module.exports = {
    plugins: [
        new FileManagerPlugin({
            onEnd: {
                copy: [
                    {source: 'www', destination: './vinod test 1/'},
                    {source: 'www', destination: './vinod testing 2/'},
                    {source: 'www', destination: './vinod testing 3/'},
                ],
            },
        }),
    ],
};

截图