zoukankan      html  css  js  c++  java
  • webpack 使用webpack-merge 分成 prod、dev、common模块

    1、安装项目需要使用的npm包

    yarn add webpack-merge cross-env html-webpack-plugin clean-webpack-plugin babel-loader @babel/core     //cross-env设置打包env
    

    2、在项目根目录下新建build文件夹,里面新建三个webpack.config.js配置文件,分别为webpack.dev.js、webpack.prod.js、webpack.common.js

    //webpack.common.js 公共配置
    const HtmlWebpackPlugin = require('html-webpack-plugin') const { CleanWebpackPlugin } = require('clean-webpack-plugin') const path=require('path') module.exports = { entry: { main: './src/main.js' }, module: { rules: [ { test: /.js$/, exclude: /node_modules/, //exclude 表示不用babel-loader 处理node_modules中的文件,外部引入的文件一般都处理过 loader: "babel-loader" } ] }, plugins: [ new CleanWebpackPlugin(), new HtmlWebpackPlugin({ template: './src/public/index.html' }) ], output: { path: path.join(__dirname,'..', 'dist'), filename: '[name].js' } }
    //webpack.prod.js   生产模式配置
    
    const { merge } = require('webpack-merge');
    const ComConfig = require('./webpack.common');
    const prodConfig = {
        mode: 'development',
        devtool: 'cheap-module-source-map'
    }
    module.exports = merge(prodConfig, ComConfig)
    
    //webpack.dev.js  开发模式配置
    const { merge } = require('webpack-merge');
    const ComConfig = require('./webpack.common');
    const path = require('path')
    const devConfig = {
        mode: 'production',
        devtool: 'eval-cheap-module-source-map',
        devServer: {
            contentBase: path.join(__dirname, "dist"),
            compress: true,
            port: 9000,
            open: true,
        },
        optimization: {
            usedExports: true
        }
    }
    module.exports = merge(devConfig, ComConfig)
    

    3、在项目根目录下的package.json中配置

     "scripts": {
        "build": "cross-env NODE_ENV=production webpack --config ./build/webpack.prod.js",
        "start": "cross-env NODE_ENV=development webpack-dev-server --config ./build/webpack/dev.js"  
     },

    4、webpack分模块配置成功

  • 相关阅读:
    ResultSet转换成List的方法
    恒星英语
    java中Float类型数据四舍五入
    总结JS打印方法
    jquery 点击除操作区域外的任意区域,将操作区域隐藏
    PowerDesigner pdm生成Access的方法
    reportbuilder设置打印页范围技巧_delphi教程
    SQL2000中因为选定的用户拥有对象,所以无法除去该用户.
    delphi 函数指针
    用ADO控件读EXCEL或DBF文件
  • 原文地址:https://www.cnblogs.com/uimeigui/p/13928980.html
Copyright © 2011-2022 走看看