gpt4 book ai didi

javascript - 如何创建多输出文件?

转载 作者:行者123 更新时间:2023-11-28 16:45:41 25 4
gpt4 key购买 nike

我想将我的 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")
}
};

和文件夹结构:
enter image description here

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

enter image description here

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

谢谢

最佳答案

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

文件结构

src
├── actions
│ ├── index.html
│ └── index.js
└── options
├── index.html
└── index.js

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']
})
]
};

还有更正确的版本;)

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
};

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

关于javascript - 如何创建多输出文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60589413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com