通过与节点的一些争论,您可以创建一个entry
对象来传递配置。在我的例子中,我使用二级文件夹的名称来创建不同的包,但您可以轻松地适应您的需求。
const path = require('path');
var glob = require('glob')
const exportPath = path.resolve(__dirname,`./../public/javascripts/plugins`);
// create a glob of files
const entryArray = glob.sync('./plugins/**/{svg,src}/**/*.*');
/**
* Create a dictionary of entries in format: folder: ['file', 'file2']
* https://webpack.js.org/configuration/entry-context/#entry
*/
var folders = []
var entries = {};
// list unique folders
entryArray.map((item) => {
const folderName = item.split('/')[2];
if (!folders.includes(folderName)) {
folders.push(folderName);
}
});
// assign files to each folder
folders.map((folder) => {
var imports = [];
entryArray.map((item) => {
const folderName = item.split('/')[2];
if (folder == folderName) {
imports.push(item);
}
});
entries[folder] = imports
});
module.exports = {
entry: entries,
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
query: {
presets: [ '@babel/preset-env' ],
},
}
],
},
{
test: /\.css$/,
use: [
'style-loader',
'css-loader',
{
loader: 'postcss-loader',
options: {
plugins: [
require('postcss-nested-ancestors'),
require('postcss-nested')
]
}
}
]
},
{
test: /\.svg$/,
loader: 'svg-inline-loader?removeSVGTagAttrs=false'
}
],
},
output: {
path: exportPath,
filename: '[name]/dist/bundle.js',
libraryTarget: 'umd',
libraryExport: 'default'
}
};