npm - Webpack 基于 webpack.config.js 内部的生产模式对 CSS 和 Javascript 进行优化和压缩
问题描述
我在 package.json 中有以下 NPM 脚本:
"scripts": {
"start": "cross-env NODE_ENV=development webpack --mode development",
"build": "cross-env NODE_ENV=production webpack --mode production"
},
如果我运行
npm run build
(生产模式)我想添加optimization
(见下文)来压缩我的 CSS 和 Uglify Javascript。我该如何做到这一点?优化:{ 最小化器:[new TerserJSPlugin({}), new OptimizeCSSAssetsPlugin({})],
},如果我运行,
npm start
我想要watch
文件并进一步与生产模式相同,除了optimization
. 我正在构建一个 Drupal 站点,因此我需要构建也在开发中的资产。
我webpack.config.js
现在的样子是这样的:
const path = require('path');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CopyPlugin = require('copy-webpack-plugin');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const webpack = require('webpack');
const autoprefixer = require('autoprefixer');
const config = {
entry: {
main: "./src/index.js",
courses: "./src/courses.js",
vendor: "./src/vendor.js"
},
output: {
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'dist')
},
plugins: [
new MiniCssExtractPlugin({ filename: '[name].css' }),
new CopyPlugin([
{ from: './src/assets/images', to: 'assets/images' },
{ from: './src/assets/icons', to: 'assets/icons' }
]),
],
module: {
rules: [
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
{ loader: 'css-loader', options: { importLoaders: 1 } },
'postcss-loader',
]
},
{
test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
use: {
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: './assets/fonts',
},
},
}
]
}
};
module.exports = (env, argv) => {
if (argv.mode === 'development') {
//...
}
if (argv.mode === 'production') {
//...
}
return config;
};
我该如何构建它?
解决方案
我通过如下调整来修复它webpack.config.js
:
const path = require('path');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CopyPlugin = require('copy-webpack-plugin');
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const webpack = require('webpack');
const autoprefixer = require('autoprefixer');
const config = {
//Entry Point
entry: {
main: "./src/index.js",
courses: "./src/courses.js",
vendor: "./src/vendor.js"
},
//Output
output: {
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'dist')
},
//Watch
watch: false,
watchOptions: {
ignored: ['node_modules']
},
//Loader
module: {
rules: [
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
{ loader: 'css-loader', options: { importLoaders: 1 } },
'postcss-loader',
]
},
{
//For fonts
test: /\.(woff|woff2|ttf|otf|eot)$/,
use: [
{
//using file-loader too
loader: "file-loader",
options: {
outputPath: "fonts"
}
}
]
}
]
},
//plugin
plugins: [
new MiniCssExtractPlugin({ filename: '[name].css' }),
new CopyPlugin([
{ from: './src/assets/images', to: 'assets/images' },
{ from: './src/assets/icons', to: 'assets/icons' }
]),
],
};
module.exports = (env, argv) => {
if (argv.mode === 'development') {
//...
config.mode = "development";
config.watch = true;
}
if (argv.mode === 'production') {
//...
config.mode = "production";
config.optimization = {
splitChunks: {
chunks: "all"
},
minimize: true,
minimizer: [
new OptimizeCssAssetsPlugin(),
new TerserPlugin({
cache: true
})
]
};
}
return config;
};
如果有人对上述内容有改进,请告诉我。
推荐阅读
- amazon-web-services - 从 http lambda 端点返回超过 30 秒
- typescript - TypeScript - 在 Jest 中有没有办法模拟/监视 document.fonts API?
- android - react native - 仅以一种方式有条件地添加节点模块(react-native-firebase)
- java - 在 Java 11 中使用 Java AWT 获取 Compent 的 Winows 句柄以进行 OpenOffice 集成
- c# - 将 Blazor WASM 应用连接到 Azure Key Vault
- react-navigation - react-app-rewired 构建抛出尝试导入错误
- javascript - 如何从地址获取 BCH 交易?
- pine-script - 无法将 pine 3 脚本转换为版本 4
- sql - 更改逗号分隔的空检查以包含列名
- python - 反向归一化(Keras/Math)