javascript - 文件加载器将图像保存到每个文件夹,例如 Project/Module/src/img/example.jpg 到 Project/Module/dist/img/example.jpg
问题描述
您好我正在尝试创建一个 Webpack 配置,为该项目中的每个文件夹生成文件。为每个文件夹制作一个 webpack.config不是一种选择。
我有一个 index.html、JS 和 SASS 都已处理。下一步是图像,我正在努力解决。
目前发生的情况是:
cd /MyProject/ && npm run build
在这个文件夹里面,有“模块”,每个模块都是一个文件夹。
源代码:
/MyProject/MyModule/src/js/app.js
/MyProject/MyModule/src/scss/app.scss
/MyProject/MyModule/src/index.html
/MyProject/MyModule/src/img/example1.jpg
分布:
MyProject/MyModule/dist/(app.css|app.js|index.html|img/example1.jpg)
我的问题
保存图像时,以下面的当前方式,
name: '[path]../../dist/img/[name].[ext]'
我的 HTML 输出是这样的:
<img src="/MyModule/src/img/../dist/img/screenshot.png" alt="Screenshot">
我确实理解为什么会发生这种情况,但我不知道有什么更好的方法。
如果我删除[path]
它当然会保存在 MyProject 而不是 MyModule 中,这是我不想要的。
webpack.config.js
const entryPlus = require('webpack-entry-plus');
const glob = require('glob');
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const fs = require('fs');
const htmlList_exclusions = [
'node_modules'
]
const htmlList = []
const imgList = []
const files = fs.readdirSync(__dirname)
files.forEach(file => {
var filePath = path.join(__dirname, file);
var isDir = fs.statSync(filePath).isDirectory();
if(isDir && !file.startsWith('.') && !file.startsWith('_') && !htmlList_exclusions.includes(file)) {
htmlList.push(filePath + '/src/index.html')
}
});
const entryFiles = [
{
entryFiles: glob.sync('./*/src/js/app.js'),
outputName(item) {
return item.replace('src/js/', 'dist/').replace('.js', '').replace('./', '');
},
},
];
const plugins = [
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
// path: '[folder]tes',
filename: '[name].css',
})
]
for(var i in htmlList) {
plugins.push( new HtmlWebpackPlugin({
template: htmlList[i],
filename: htmlList[i].replace('src/', '/'),
}));
}
module.exports = {
watch: true,
entry: entryPlus(entryFiles),
output: {
path: path.resolve(__dirname, './'),
filename: '[name].js',
publicPath: '/',
},
module: {
rules: [
{
test: /\.(sa|sc|c)ss$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
// you can specify a publicPath here
// by default it use publicPath in webpackOptions.output
// publicPath: '../',
sourceMap: false,
}
},
"css-loader", "sass-loader"
]
},
{
test: /\.js$/,
exclude: /(node_modules)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
plugins: ['@babel/plugin-proposal-object-rest-spread', '@babel/plugin-transform-runtime']
}
}
},
{
test: /\.html$/,
use: ['html-loader'],
},
{
test: /\.(jpg|png|gif)$/,
use: [
{
loader: 'file-loader',
options: {
name: '[path]../dist/img/[name].[ext]',
outputPath: '',
publicPath: '/',
}
}
]
}
]
},
plugins,
resolve: {
alias: {
globalscss: path.resolve(__dirname, './_global/scss/'),
}
}
}
解决方案
我会为更好的 RegExp 工作,但我使用了以下解决方案
{
test: /\.(jpg|png|gif|svg)$/,
use: [
{
loader: 'file-loader',
options: {
regExp: /([^\0]+)\/([^\0]+)\/([^\0]+)\/([^\0]+)\/([^\0]+)\.(jpg|png|gif|svg)$/,
name: '[2]/dist/img/[name].[ext]',
outputPath: '',
publicPath: '/',
}
}
]
}
推荐阅读
- flutter - 冻结 - 创建子类
- php - 制作 CPT 帖子和页面循环(使用 Elementor)
- regex - 检查字符串是否具有以下格式:2 个字母 au max 后跟带有 regexp_like oracle 的数字
- javascript - 如何选择动态创建的输入的 ID?
- regex - htaccess:从缺少值的查询字符串中删除 URL 参数?
- python - Python Spyder - IPython 控制台中的水平滚动条隐藏最后一行
- c# - elasticsearch中长数据类型的通配符搜索问题
- c++ - 在 Visual Studio 19 中将 2 个项目链接在一起
- sql-server - Sql server 循环记录
- sql - 如何将oracle中的多个表导出到Oracle SQL developer中的多个.csv文件中