我正在为我的项目整合一个新的webpack构建,它实际上工作得很好。我有一个问题,我想得到一些帮助,如果有人有一个想法。我必须从我的工作电脑重新输入所有这些,所以请原谅任何错别字。
Package.json
{
"name": "test_webpack",
"version": "1.0.0",
"description": "",
"main": "index.js",
scripts: {
"build": "webpack --config webpack.production.js"
"dev": "webpack serve --config webpack.development.js"
},
"devDependencies": {
"@babel/core": "^7.22.9",
"@babel/preset-env": "^7.22.9",
"babel-loader": "^9.1.3",
"css-loader": "^6.8.1",
"html-webpack-plugin": "^5.5.3",
"mini-css-extract-plugin": "^2.7.6",
"sass": "^1.64.1",
"sass-loader": "^13.3.2",
"style-loader": "^3.3.3",
"webpack": "^5.88.2",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^4.15.1"
}
}
字符串
webpack.production.js
const path = reqire('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
module.exports = {
mode: "production",
entry: {
"script.min": path.resolve(_dirname, 'src/js/index.js'),
"search": path.resolve(_dirname, 'src/js/functional/modules.js'),
"modules/carousel": path.resolve(_dirname, 'src/js/modules/carousel.js'),
"modules/handleEmoji": path.resolve(_dirname, 'src/js/modules/handleEmoji.js'),
"modules/modalAccordion": path.resolve(_dirname, 'src/js/modules/modalAccordion.js'),
"modules/sliders": path.resolve(_dirname, 'src/js/modules/sliders.js'),
"modules/tourVideo": path.resolve(_dirname, 'src/js/modules/tourVideo.js'),
"modules/vectorSwap": path.resolve(_dirname, 'src/js/modules/vectorSwap.js')
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js',
clean: true,
assetModuleFilename: '[name].[ext]'
},
module: {
rules: [
{
est:/\.scss$/i,
use: [
MiniCssExtractPlugin.loader,
{
loader:'css-loader',
options: {url: false}
},
{loader: 'sass-loader'}
]
},
{
test:/\.js$/,
exclude:/node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
},
{
test:/\.(png|svg|jpg|jpeg|gif)$/i,
type: 'asset/resource'
}
]
},
plugins: [
new MiniCssExtractPlugin({
filename: "shared.min.css"
})
]
}
型
问题是否有一种方法,我可以取代这个......与动态的东西?:
"modules/carousel": path.resolve(_dirname, 'src/js/modules/carousel.js'),
"modules/handleEmoji": path.resolve(_dirname, 'src/js/modules/handleEmoji.js'),
"modules/modalAccordion": path.resolve(_dirname, 'src/js/modules/modalAccordion.js'),
"modules/sliders": path.resolve(_dirname, 'src/js/modules/sliders.js'),
"modules/tourVideo": path.resolve(_dirname, 'src/js/modules/tourVideo.js'),
"modules/vectorSwap": path.resolve(_dirname, 'src/js/modules/vectorSwap.js')
型
这样,我的modules文件夹中的每个文件都将被单独编译和输出。我想出了如何把它们都捆绑在一起,但我需要把它们分开。现在,每次我们添加一个模块,我们都必须将其添加到webpack配置文件中。现在它输出到“dist”文件夹中的“modules”文件夹。
2条答案
按热度按时间hgncfbus1#
似乎
entry
选项也可以接受一个返回promise的函数:个字符
在该函数中,您可以读取
modules
目录中的文件,并相应地生成entry对象。dba5bblo2#
您可以创建一个模块名称数组,然后使用Array.reduce()创建一个具有入口点的对象。
字符串
生成的entryPoints对象将如下所示:
型
这样,您就不必手动重复相同的入口点,并且构建配置变得更具可伸缩性。