Debian系统JS构建工具配置指南
一 环境准备
二 选择并初始化构建工具链
三 示例一 Webpack 配置(含开发与生产)
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = (env, argv) => {
const isProd = argv.mode === 'production';
return {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: isProd ? 'js/bundle.[contenthash:8].js' : 'js/bundle.js',
clean: true,
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: { presets: ['@babel/preset-env'] },
},
},
{
test: /\.css$/i,
use: ['style-loader', 'css-loader'],
},
],
},
plugins: [
new HtmlWebpackPlugin({
template: './public/index.html',
minify: isProd && {
removeComments: true, collapseWhitespace: true, removeRedundantAttributes: true,
},
}),
],
devServer: { static: './dist', port: 3000, open: true, hot: true },
optimization: {
splitChunks: { chunks: 'all' },
},
};
};
{
"scripts": {
"dev": "webpack serve --mode development",
"build": "webpack --mode production"
}
}
四 示例二 Gulp 配置(自动化压缩与合并)
import { src, dest, series } from 'gulp';
import uglify from 'gulp-uglify';
import cssnano from 'gulp-cssnano';
import rename from 'gulp-rename';
import concat from 'gulp-concat';
const jsTask = () =>
src('src/js/*.js')
.pipe(concat('app.min.js'))
.pipe(uglify())
.pipe(rename({ suffix: '.min' }))
.pipe(dest('dist/js'));
const cssTask = () =>
src('src/css/*.css')
.pipe(concat('styles.min.css'))
.pipe(cssnano())
.pipe(rename({ suffix: '.min' }))
.pipe(dest('dist/css'));
export default series(jsTask, cssTask);
五 质量保障与常见故障排查