如何创建多输出文件?

zer*_*ing 0 javascript webpack webpack-4

我想将我的 chrome 扩展与 Webpack 捆绑在一起。源包含多个条目,webpack.config.js外观内容如下:

const path = require("path");

module.exports = {
  entry: {
    actions: './src/actions/index.js',
    options: './src/options/index.js'
  },
  output: {
    filename: '[name].js',
    path: path.resolve(__dirname, "dist")
  }
};
Run Code Online (Sandbox Code Playgroud)

和文件夹结构:
在此处输入图片说明

actions/index.jsoptions/index.js文件条目。我的目标是,当src捆绑后dist文件夹应如下所示:

在此处输入图片说明

如何配置 webpack config 以获得上面所需的文件夹结构?

谢谢

Grz*_* T. 5

这应该可以解决您的问题;)

文件结构

src
??? actions
?   ??? index.html
?   ??? index.js
??? options
    ??? index.html
    ??? index.js
Run Code Online (Sandbox Code Playgroud)

webpack.config.js

const path = require('path');
const HtmlWebPackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: {
    actions: './src/actions/index.js',
    options: './src/options/index.js'
  },
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: '[name]/index.js'
  },
  plugins: [
    new HtmlWebPackPlugin({
      template: './src/actions/index.html',
      filename: 'actions/index.html',
      chunks: ['actions']
    }),
    new HtmlWebPackPlugin({
      template: './src/options/index.html',
      filename: 'options/index.html',
      chunks: ['options']
    })
  ]
};
Run Code Online (Sandbox Code Playgroud)

还有一个更正确的版本;)

const path = require('path');
const HtmlWebPackPlugin = require('html-webpack-plugin');

const ENTRY = {
  actions: './src/actions/index.js',
  options: './src/options/index.js'
}

const entryHtmlPlugins = Object.keys(ENTRY).map(entryName => {
  return new HtmlWebPackPlugin({
    template: `./src/${entryName}/index.html`,
    filename: `${entryName}/index.html`,
    chunks: [entryName]
  });
});

module.exports = {
  entry: ENTRY,
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: '[name]/index.js'
  },
  plugins: entryHtmlPlugins
};
Run Code Online (Sandbox Code Playgroud)

我在 github many-outputs上创建了一个分支