npm

每次我们打包或者启动服务的时候,都需要在命令行输入一长串命令,这对我们开发体验也是极其不友好的,那么我们怎么去优化一下呢?

配置启动命令

我们在package.json中配置如下命令:

  1. "scripts": {
  2. "dev": "webpack serve -c ./config/webpack.config.dev.js", // 开发环境
  3. "build": "webpack -c ./config/webpack.config.prod.js" // 生产环境
  4. },

运行

然后我们在控制台输入 npm run dev 来启动服务,发现服务器可以正常启动
image.png

优化

有时候我们会发现在生产环境下进行打包会出现很多警告,告诉我们有些包超过预期,如果我们不希望出现这个警告可以做如下配置:

  1. performance: {
  2. hints: false
  3. }

提取公共配置

我们虽然已经将 开发环境 和 生产环境 配置拆分开来了,但是我们发现两份配置中有很多重复代码,我们来提取一下公共的配置,并改造一下开发环境和生产环境。

公共配置(webpack.config.common.js)

  1. const path = require('path')
  2. const HtmlWebpackPlugin = require('html-webpack-plugin'); // 引入 html-webpack-plugin 插件
  3. const MiniCssExtractPlugin = require('mini-css-extract-plugin'); // 引入 mini-css-extract-plugin 插件
  4. module.exports = {
  5. entry: {
  6. index: './src/index.js', // 配置 index.js 打包入口
  7. another: './src/another.js', // 配置 another.js 打包入口
  8. }, // 入口文件路径
  9. output: { // 出口文件
  10. path: path.resolve(__dirname, '../dist'), // 出口文件路径
  11. clean: true, // 生成新文件的同时是否删除旧文件
  12. },
  13. plugins: [
  14. new HtmlWebpackPlugin({ // 实例化 HtmlWebpackPlugin,它将帮我们生成一个 dist/index.html 文件
  15. template: './index.html', // 指定模板
  16. filename: 'app.html', // 生成的文件名
  17. inject: 'body' // script标签的生成位置
  18. }),
  19. new MiniCssExtractPlugin({
  20. filename: 'styles/[contenthash].css'
  21. })
  22. ],
  23. module: {
  24. rules: [ // 配置资源模块
  25. {
  26. test: /\.css$/, // 利用正则匹配以 .css 结尾的文件
  27. use: [MiniCssExtractPlugin.loader, 'css-loader'] // 使用的 loader
  28. },
  29. {
  30. test: /\.js$/, // 匹配 js 文件
  31. exclude: /node_modules/, // 去除编译 node_modules 包
  32. use: {
  33. loader: 'babel-loader', // 使用 loader
  34. options: {
  35. presets: ['@babel/preset-env'], // 添加预设
  36. plugins: [
  37. [
  38. '@babel/plugin-transform-runtime'
  39. ]
  40. ]
  41. }
  42. }
  43. }
  44. ]
  45. },
  46. optimization: {
  47. splitChunks: {
  48. cacheGroups: {
  49. vendor: {
  50. test: /[\\/]node_modules[\\/]/,
  51. name: 'vendors',
  52. chunks: 'all'
  53. },
  54. },
  55. }
  56. }
  57. }

开发环境(webpack.config.dev.js)

  1. module.exports = {
  2. output: { // 出口文件
  3. filename: './script/[name].js', // 出口文件名
  4. },
  5. mode: 'development', // 设置模式 'none' | 'development' | 'production'
  6. devtool: 'inline-source-map',
  7. devServer: {
  8. static: './dist' // 路径
  9. },
  10. }

生产环境(webpack.config.prod.js)

  1. const CssMinizerPlugin = require('css-minimizer-webpack-plugin'); // 引入 css-minimizer-webpack-plugin 插件
  2. const TerserPlugin = require('terser-webpack-plugin') // 引入 terser-webpack-plugin 插件
  3. module.exports = {
  4. output: { // 出口文件
  5. filename: './script/[name].[contenthash].js', // 出口文件名
  6. publicPath: 'http://localhost:8080/'
  7. },
  8. mode: 'production', // 设置模式 'none' | 'development' | 'production'
  9. optimization: {
  10. minimizer: [
  11. new CssMinizerPlugin(),
  12. new TerserPlugin()
  13. ],
  14. },
  15. performance: {
  16. hints: false
  17. }
  18. }

合并配置文件(webpack-merge)

我们将配置文件拆分为三分,需要让他们之间产生联系,这个工作我们可以借助 webpack-merge 来完成

安装
  1. npm install webpack-merge -D

配置

我们在 config文件 下新建一个 webpack.config.js 文件,配置如下:

  1. const { merge } = require('webpack-merge')
  2. const commonConfig = require('./webpack.config.common')
  3. const prodConfig = require('./webpack.config.prod')
  4. const devConfig = require('./webpack.config.dev')
  5. module.exports = (env) => {
  6. if (env.production) {
  7. return merge(commonConfig, prodConfig)
  8. } else if (env.development) {
  9. return merge(commonConfig, devConfig)
  10. }
  11. return new Error('No matching configuration was found')
  12. }

然后我们改造一下我们的 package.json 如下:

  1. "scripts": {
  2. "dev": "webpack serve -c ./config/webpack.config.js --env development",
  3. "build": "webpack -c ./config/webpack.config.js --env production"
  4. },

运行

分别执行 npm run dev 和 npm run build,效果和我们预期的一样,说明我们合并配置文件生效了