vue.config.js 4.2 KB
Newer Older
Mr.奇淼('s avatar
Mr.奇淼( 已提交
1 2 3
'use strict'

const path = require('path')
4

Mr.奇淼('s avatar
Mr.奇淼( 已提交
5 6 7 8
function resolve(dir) {
    return path.join(__dirname, dir)
}
module.exports = {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
9
    // 基础配置 详情看文档
10
    publicPath: './',
Mr.奇淼('s avatar
Mr.奇淼( 已提交
11 12 13 14 15 16 17 18 19 20 21 22
    outputDir: 'dist',
    assetsDir: 'static',
    lintOnSave: process.env.NODE_ENV === 'development',
    productionSourceMap: false,
    devServer: {
        port: 8080,
        open: true,
        overlay: {
            warnings: false,
            errors: true
        },
        proxy: {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
23
            // 把key的路径代理到target位置
Mr.奇淼('s avatar
Mr.奇淼( 已提交
24
            // detail: https://cli.vuejs.org/config/#devserver-proxy
Mr.奇淼('s avatar
Mr.奇淼( 已提交
25 26
            [process.env.VUE_APP_BASE_API]: { //需要代理的路径   例如 '/api'
                target: `http://127.0.0.1:8888`, //代理到 目标路径
Mr.奇淼('s avatar
Mr.奇淼( 已提交
27
                changeOrigin: true,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
28 29
                pathRewrite: { // 修改路径数据
                    ['^' + process.env.VUE_APP_BASE_API]: '' // 举例 '^/api:""' 把路径中的/api字符串删除
Mr.奇淼('s avatar
Mr.奇淼( 已提交
30 31 32 33 34
                }
            }
        },
    },
    configureWebpack: {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
35
        //    @路径走src文件夹
Mr.奇淼('s avatar
Mr.奇淼( 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
        resolve: {
            alias: {
                '@': resolve('src')
            }
        }
    },
    chainWebpack(config) {
        // set preserveWhitespace
        config.module
            .rule('vue')
            .use('vue-loader')
            .loader('vue-loader')
            .tap(options => {
                options.compilerOptions.preserveWhitespace = true
                return options
            })
            .end()
        config
54
            // https://webpack.js.org/configuration/devtool/#development
Mr.奇淼('s avatar
Mr.奇淼( 已提交
55
            .when(process.env.NODE_ENV === 'development',
56 57
                config => config.devtool('cheap-source-map')
            )
Mr.奇淼('s avatar
Mr.奇淼( 已提交
58 59 60 61

        config
            .when(process.env.NODE_ENV !== 'development',
                config => {
62 63 64 65 66 67 68 69 70 71 72

                    // 不打包 begin
                    // 1.目前已经测试通过[vue,axios,echarts]可以cdn引用,其它组件测试通过后可继续添加
                    // 2.此处添加不打包后,需在public/index.html head中添加相应cdn资源链接
                    config.set('externals', {
                        vue: 'Vue',
                        axios: 'axios',
                        echarts: 'echarts'
                    })
                    // 不打包 end

Mr.奇淼('s avatar
Mr.奇淼( 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
                    config
                        .plugin('ScriptExtHtmlWebpackPlugin')
                        .after('html')
                        .use('script-ext-html-webpack-plugin', [{
                            // `runtime` must same as runtimeChunk name. default is `runtime`
                            inline: /runtime\..*\.js$/
                        }])
                        .end()
                    config
                        .optimization.splitChunks({
                            chunks: 'all',
                            cacheGroups: {
                                libs: {
                                    name: 'chunk-libs',
                                    test: /[\\/]node_modules[\\/]/,
                                    priority: 10,
                                    chunks: 'initial' // only package third parties that are initially dependent
                                },
                                elementUI: {
                                    name: 'chunk-elementUI', // split elementUI into a single package
                                    priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
                                    test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
                                },
                                commons: {
                                    name: 'chunk-commons',
                                    test: resolve('src/components'), // can customize your rules
                                    minChunks: 3, //  minimum common number
                                    priority: 5,
                                    reuseExistingChunk: true
                                }
                            }
                        })
                    config.optimization.runtimeChunk('single')
                }
            )
    }
K
klausY 已提交
109
}