简体   繁体   English

Webpack 5 多个 javascript 文件在生产模式下产生 0 字节包

[英]Webpack 5 multiple javascript files produces 0 bytes bundle in production mode

I just need to minify and bundle all my js files into one file.我只需要将我的所有 js 文件缩小并捆绑到一个文件中。 So prepared a simple configuration as;于是准备了一个简单的配置为;

const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');

module.exports = {
    mode: 'production',
    resolve: {
        extensions: ['.js']
    },
    entry: [
        './src/main/resources/static/js/app/context.js',
        './src/main/resources/static/js/app/pagemanager.js'
    ],
    plugins: [
        new CleanWebpackPlugin()
    ],
    output: {
        path: path.resolve(__dirname, 'dist'),
        publicPath: '/dist/',
        filename: 'bundle.js'
    }
};

but if I execute webpack --mode production it produces 0 bytes and bundle.js is empty但是如果我执行 webpack --mode production 它会产生 0 个字节并且 bundle.js 是空的

asset bundle.js 0 bytes [emitted] [minimized] (name: main)
./src/main/resources/static/js/app/context.js 9.74 KiB [built] [code generated]
./src/main/resources/static/js/app/pagemanager.js 27.9 KiB [built] [code generated]
webpack 5.20.2 compiled successfully in 299 ms

but with webpack --mode development it creates a file that contains js code.但是使用 webpack --mode development 它会创建一个包含 js 代码的文件。

What may be the reason leading to this?导致这种情况的原因可能是什么? Thank you.谢谢你。

I think that if your file don't have any code to execute, such as我认为如果您的文件没有任何代码可以执行,例如

// just define a var
const foo = 1;
// or just a string 
"console.log('something');"

and the webpack mode is "production"(if you don't set it, the default value is this),the result will be empty, asset main.js 0 bytes.而webpack模式为“生产”(如果不设置,默认值为this),结果为空,资产main.js 0字节。 The solution is changing the code to this.解决方案是将代码更改为此。

console.log('something');

Answering my question for those who is facing the same problem, There should be "export" syntax, that refers what you export from these js files to solve this problem.为那些面临同样问题的人回答我的问题,应该有“导出”语法,指的是你从这些 js 文件中导出的内容来解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM