admin管理员组文章数量:1293068
I have library that use ES modules and I want to generate UMD file in ES5. I have config like this:
var path = require('path');
var webpack = require('webpack');
module.exports = {
entry: {
index: path.resolve('./src/index.js')
},
output: {
library: 'name',
libraryTarget: 'umd',
path: path.resolve(__dirname, 'dist'),
filename: 'umd.min.js'
},
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['env']
}
}
]
},
stats: {
colors: true
},
mode: 'production',
devtool: 'source-map'
};
and webpack create file main.js
how can I make output file umd.min.js
?
> webpack --mode production
Hash: 98ccf0949bfdf066246a
Version: webpack 4.44.0
Time: 97ms
Built at: 25.07.2020 16:30:31
Asset Size Chunks Chunk Names
main.js 2.23 KiB 0 [emitted] main
Entrypoint main = main.js
[0] ./src/index.js + 3 modules 3.6 KiB {0} [built]
| ./src/index.js 294 bytes [built]
| ./src/Canvas.js 1.88 KiB [built]
| ./src/Item.js 1.2 KiB [built]
| ./src/constants.js 239 bytes [built]
My package.json look like this:
"type": "module",
"main": "./src/index.js",
"unpkg": "./dist/umd.min.js",
So this a bug? Why the file name is main.js?
I have library that use ES modules and I want to generate UMD file in ES5. I have config like this:
var path = require('path');
var webpack = require('webpack');
module.exports = {
entry: {
index: path.resolve('./src/index.js')
},
output: {
library: 'name',
libraryTarget: 'umd',
path: path.resolve(__dirname, 'dist'),
filename: 'umd.min.js'
},
module: {
loaders: [
{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['env']
}
}
]
},
stats: {
colors: true
},
mode: 'production',
devtool: 'source-map'
};
and webpack create file main.js
how can I make output file umd.min.js
?
> webpack --mode production
Hash: 98ccf0949bfdf066246a
Version: webpack 4.44.0
Time: 97ms
Built at: 25.07.2020 16:30:31
Asset Size Chunks Chunk Names
main.js 2.23 KiB 0 [emitted] main
Entrypoint main = main.js
[0] ./src/index.js + 3 modules 3.6 KiB {0} [built]
| ./src/index.js 294 bytes [built]
| ./src/Canvas.js 1.88 KiB [built]
| ./src/Item.js 1.2 KiB [built]
| ./src/constants.js 239 bytes [built]
My package.json look like this:
"type": "module",
"main": "./src/index.js",
"unpkg": "./dist/umd.min.js",
So this a bug? Why the file name is main.js?
Share Improve this question asked Jul 25, 2020 at 14:32 jcubicjcubic 66.7k58 gold badges249 silver badges453 bronze badges 2-
Which version of webpack are you using? I'm aware of
loaders
is no longer existed to be replaced byrules
instead. To be honest I don't see anything weird in your configuration though. – tmhao2005 Commented Jul 25, 2020 at 17:35 - @tmhao2005 the config was wrong, but it didn't matter it was not loaded at all. – jcubic Commented Jul 25, 2020 at 18:23
2 Answers
Reset to default 5So, by default the webpack emits the chunk name as "main.js", so if you want to rename the chunk name, use the "chunkFilename" option in the output object of your webpack config file, here is the example how i did that:
output: {
filename: env === 'development' ? '[name].js' : '[name].[hash].js',
path: path.resolve(__dirname, '../dist'),
chunkFilename: 'scripts/[name].[hash].js',},
in your case you should do:
chunkFilename: 'umd.min.js'
The problem is that webpack-cli suggest to rename the file to cjs but don't say that it's not loaded by default, you need to explicitly add --config with the filename when running webpack.
See this issue:
https://github./webpack/webpack-cli/issues/1165
本文标签: javascriptHow to create output filename with given name in webpackStack Overflow
版权声明:本文标题:javascript - How to create output filename with given name in webpack? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741569599a2385926.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论