中的错误
在配置中警告尚未设置"模式"选项。将"模式"选项设置为"开发"或"生产",以实现此环境的默认值。多数./src/index.js ./dist/bundle.js
以下是WebPack的帮助消息,通过在 WebPack 4
中键入webpack --help
。 Usage without config file: webpack <entry> [<entry>] --output [-o] <output>
注意: - 输出需要明确指定
解决方案:
webpack src/index.js --output dist/bundle.js --mode development
,如果您使用的是webpack.config.js
:
const path = require('path');
module.exports = {
mode: 'development', // set mode option, 'development' or 'production'
entry: './src/index.js',
output: {
path: path.resolve(__dirname,'dist'),
filename: "bundle.js"
}
};
我不确定这里的确切问题是什么,但是我也有此警告并通过在 webpack配置文件中设置mode
属性来解决它们
package.json
{
"name": "my-awesome-project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "NODE_ENV=production webpack",
},
...
"devDependencies": {
...
},
"dependencies": {
...
}
}
webpack.config.js
const path = require('path');
const HtmlWebPackPlugin = require('html-webpack-plugin');
const distDir = path.join(__dirname, 'dist');
const config = {
mode: 'development',
entry: ['./src/js/app.js'],
output: {
path: distDir,
filename: 'js/[name].js',
},
module: {
rules: [
...
]
},
plugins: [
...
],
devtool: "eval-source-map", // Default development sourcemap
};
// Check if build is running in production mode, then change the sourcemap type
if (process.env.NODE_ENV === 'production') {
config.devtool = ''; // No sourcemap for production
config.mode = 'production';
// Add more configuration for production here like
// Uglify plugin
// Offline plugin
// Etc,
}
module.exports = config;
希望它有帮助。
配置文件中的设置模式有时不起作用。只是添加--mode=development
才能正确。
"scripts": {
"start": "webpack --config webpack.config.js --hot --mode=development",
}