webpack - 开发中的 Webpack 长块名
问题描述
当我在development
. 例如,我创建了一个包含 react 和 react-dom 模块的块:
cacheGroups: {
react: {
test: /[\\/]node_modules[\\/](react|react-dom)[\\/]/,
name: 'vendors~A',
enforce: true,
chunks: 'all',
reuseExistingChunk: true,
priority: 10
},
...
}
在开发中,生成的块的名称变为:vendors~A~._node_modules_react-.eb31e3d6ed9d854e0daa.js
我想删除_node_modules_react-
部分。我怎样才能做到这一点?我以为[name]
给出了文件名?https://webpack.js.org/configuration/output
我的 webpack 配置如下:
module.exports = {
entry: './src/index.tsx',
output: {
filename: '[name].[contenthash].js',
path: path.resolve(__dirname + '../build')
},
devServer: {
contentBase: path.join(__dirname, '../build'),
port: 3030,
historyApiFallback: true,
inline: false,
hot: false
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.json']
},
optimization: {
runtimeChunk: 'single',
splitChunks: {
maxSize: 500000,
minSize: 0,
chunks: 'all',
automaticNameDelimiter: '~',
cacheGroups: {
react: {
test: /[\\/]node_modules[\\/](react|react-dom)[\\/]/,
name: 'vendors~A',
enforce: true,
chunks: 'all',
reuseExistingChunk: true,
priority: 10
},
common: {
test: /[\\/]node_modules[\\/](.*i18next.*|.*redux|react-router.*|@emotion.*|.*redux)[\\/]/,
name: 'vendors~B',
enforce: true,
chunks: 'all',
reuseExistingChunk: true,
priority: 9
},
main: {
test: /[\\/]node_modules[\\/]/,
chunks: 'initial',
name: 'vendors~main',
reuseExistingChunk: true,
enforce: true,
priority: 1
},
default: {
minChunks: 2,
priority: 0,
reuseExistingChunk: true
}
}
},
},
module: {
rules: [
{
enforce: 'pre',
test: /\.js$/,
loader: 'source-map-loader'
},
{
test: /\.svg$/,
loader: 'svg-inline-loader'
}
]
},
resolve: {
alias: {
_components: path.resolve(__dirname, '../src/components/'),
_hooks: path.resolve(__dirname, '../src/hooks/'),
_reducers: path.resolve(__dirname, '../src/reducers/'),
_src: path.resolve(__dirname, '../src/'),
_views: path.resolve(__dirname, '../src/views/')
},
extensions: [".tsx", ".js", ".css", ".scss"]
},
plugins: [
new CleanWebpackPlugin(),
new webpack.HashedModuleIdsPlugin(),
new MiniCssExtractPlugin({
filename: '[name].[contenthash].css',
chunkFilename: '[name].[contenthash].css'
}),
new CopyWebpackPlugin([
{from: 'assets/*.svg', to: 'assets', flatten: true},
{from: 'assets/*.woff', to: 'fonts', flatten: true},
{from: 'locales/**/*.json', to: ''},
{from: 'src/*.html', to: '', flatten: true},
]),
new HtmlWebpackPlugin({
template: path.resolve(__dirname, '../src/index.html'),
filename: 'index.html',
favicon: path.resolve(__dirname, '../assets/favicon.ico'),
inject: true
})
]
};
PS:在production
我得到vendors~A~f734b0c6.c8768b8083857ef954bf.js
,不知道为什么有两个哈希,但这个名字更可取......
解决方案
您可以使用output.chunkFilename。
这是我一直用于我的用例的一个小片段:
output: {
...common.output,
path: resolve(__dirname, '../lib/'),
filename: chunkData => chunkData.chunk.name === 'core' ? 'core.min.js' : '[name].min.js',
library: '[name]',
libraryTarget: 'umd',
chunkFilename: '[name].min.js'
},
推荐阅读
- typescript - 在 onPress 中反应原生 Typescript Formik 的 handleSubmit 类型
- openstreetmap - 打开 OSM pbf 导致 Protobuf 异常
- regex - 哪里有一个字母标签,插入一个
- java - 在我向 TextView 添加一些单词后,我尝试使用一个简单的按钮更改 java android 中字符串的某些部分,但它没有用
- performance - 为什么我的 Haskell 选择排序实现非常快?
- c++ - 如何在 C++ 中克隆未知动态类型的对象?
- asp.net - 使用 PipeTransform 的 groupBy 元素的总和
- flutter - 请解释填充是如何工作的?如果我增加填充,它会变小,如果我减少填充,它会变大?但是为什么?
- regex - Linux find 命令:搜索包含括号的文件名
- java - 检查字符串在java中是否只有一个回文字符不匹配