创建vue-cli3项目和配置

https://segmentfault.com/a/1190000017008697
一、创建项目: 1、创建第一项

vue create hello-world

2、你会被提示选取一个 preset。选择第一项:node-sass\vuex\axios
3、下载所需要的插件:
cnpm i --save-dev compression-webpack-plugin//gzip压缩 cnpm i --save-dev @gfx/zopfli brotli-webpack-plugin//Zopfli压缩 cnpm install node-sass --save-dev //安装node-sass cnpm install sass-loader --save-dev //安装sass-loader cnpm install style-loader --save-dev //安装style-loader

安装sass和配置
二、配置vue.config.js 1、前言:
新版vue-cli,没了webpack.config配置文件,我要配置一个插件怎么办?, 由于vue-cli3内置了webpack的配置,如果需要查看配置查看上一篇,
如果需要自己增加一些配置,则在项目根据下创建文件vue-config.js既可
2、vue.config.js
说明: (1)baseUrl: "./", // 构建好的文件输出到哪里,该属性已被废弃,改成:publicPath:"./"
(2)# vue cli3使用官方方法配置sass全局变量报错
你可能是新版 sass-loader 的受害者。// 旧 data: `@import "~@/assets/scss/variables.scss"; ` // 新 prependData: `@import "~@/assets/scss/variables.scss"; `

源码:
const IS_PROD = ['production', 'prod'].includes(process.env.NODE_ENV); const path =require('path'); const resolve = (dir) => path.join(__dirname, dir); const BundleAnalyzerPlugin = require('webpack-bundle-analyzer'); //打包分析 const UglifyJsPlugin = require('uglifyjs-webpack-plugin'); //去掉console.log // 开启gzip压缩 const CompressionWebpackPlugin = require('compression-webpack-plugin'); const productionGzipExtensions = /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i; //Zopfli压缩 const zopfli = require("@gfx/zopfli"); const BrotliPlugin = require("brotli-webpack-plugin"); module.exports = { publicPath: './', // 默认'/',部署应用包时的基本 URL outputDir: process.env.outputDir || 'dist', // 'dist', 生产环境构建文件的目录 assetsDir: '',// 相对于outputDir的静态资源(js、css、img、fonts)目录 lintOnSave: false, runtimeCompiler: true, // 是否使用包含运行时编译器的 Vue 构建版本 productionSourceMap: false,// 生产环境的 source map parallel: require('os').cpus().length > 1, pwa: {},// 配置proxy跨域 devServer: { overlay: { warnings: true, errors: true }, open: IS_PROD, host: '0.0.0.0', port: 8000, https: false, hotOnly: false, proxy: { '/api': { target: process.env.VUE_APP_BASE_API || 'http://127.0.0.1:8080', changeOrigin: true } } }, // 修复HMR(热更新)失效 chainWebpack: config => { //添加别名 config.resolve.alias .set('@', resolve('src')) .set('assets', resolve('src/assets')) .set('components', resolve('src/components')) .set('layout', resolve('src/layout')) .set('base', resolve('src/base')) .set('static', resolve('src/static')); // 打包分析 if (process.env.IS_ANALYZ) { config.plugin('webpack-report') .use(BundleAnalyzerPlugin, [{ analyzerMode: 'static', }]); }// 修复HMR config.resolve.symlinks(true); }, // 配置externals // 防止将某些 import 的包(package)打包到 bundle 中,而是在运行时(runtime)再去从外部获取这些扩展依赖 configureWebpack: config => { config.externals = { /* 'vue': 'Vue', 'element-ui': 'ELEMENT', 'vue-router': 'VueRouter', 'vuex': 'Vuex', 'axios': 'axios'*/ } // 去掉console.log if (IS_PROD) { const plugins = []; plugins.push( //js混淆和去掉debugger,console.log new UglifyJsPlugin({ uglifyOptions: { compress: { warnings: false, drop_console: true, drop_debugger: false, pure_funcs: ['console.log']//移除console } }, sourceMap: false, parallel: true }) ); plugins.push( //开启Zopfli压缩 new CompressionWebpackPlugin({ algorithm(input, compressionOptions, callback) { return zopfli.gzip(input, compressionOptions, callback); }, compressionOptions: { numiterations: 15 }, minRatio: 0.99, test: productionGzipExtensions }) ); plugins.push( new BrotliPlugin({ test: productionGzipExtensions, minRatio: 0.99 }) ); config.plugins = [ ...config.plugins, ...plugins ]; } }, // 为sass提供全局样式,以及全局变量 css: { modules: false, extract: IS_PROD, sourceMap: false, loaderOptions: { sass: { // 向全局sass样式传入共享的全局变量 prependData: `@import "~assets/scss/variables.scss"; $src: "${process.env.VUE_APP_SRC}"; ` } } } };

其他配置参考如下:vue.config.js
const path = require('path') const CompressionWebpackPlugin = require('compression-webpack-plugin') const UglifyjsWebpackPlugin = require('uglifyjs-webpack-plugin') const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer') const port = process.env.port || process.env.npm_config_port || 8888 const cdnDomian = './' // cdn域名,如果有cdn修改成对应的cdn const name = 'H5Vue' // page title const IS_PRODUCTION = process.env.NODE_ENV === 'production' const cdn = { css: [], js: [ 'https://cdn.bootcss.com/vue/2.6.10/vue.min.js', 'https://cdn.bootcss.com/vue-router/3.0.3/vue-router.min.js', 'https://cdn.bootcss.com/vuex/3.1.0/vuex.min.js', 'https://cdn.bootcss.com/axios/0.19.0-beta.1/axios.min.js', 'https://cdn.bootcss.com/js-cookie/2.2.1/js.cookie.min.js' ] }const externals = { vue: 'Vue', 'vue-router': 'VueRouter', vuex: 'Vuex', axios: 'axios', 'js-cookie': 'Cookies' }function resolve (dir) { return path.join(__dirname, dir) }module.exports = { publicPath: IS_PRODUCTION ? cdnDomian : './',////部署应用包时的基本 URL outputDir: 'dist', ////当运行 vue-cli-service build 时生成的生产环境构建文件的目录 assetsDir: 'static',////放置生成的静态资源 (js、css、img、fonts) 的 (相对于 outputDir 的) 目录 lintOnSave: process.env.NODE_ENV === 'development', // eslint-loader 是否在保存的时候检查 安装@vue/cli-plugin-eslint有效 productionSourceMap: false,//是否使用包含运行时编译器的 Vue 构建版本。设置true后你就可以在使用template devServer: { port: port, open: true, overlay: { warnings: false, errors: true }, proxy: { // change xxx-api/login => mock/login // detail: https://cli.vuejs.org/config/#devserver-proxy [process.env.VUE_APP_BASE_API]: { target: `http://127.0.0.1:${port}/mock`, changeOrigin: true, pathRewrite: { ['^' + process.env.VUE_APP_BASE_API]: '' } } }, after: require('./mock/mock-server.js') }, configureWebpack: { // provide the app's title in webpack's name field, so that // it can be accessed in index.html to inject the correct title. name: name, resolve: { alias: { '@': resolve('src'), // 主目录 'views': resolve('src/views'), // 页面 'components': resolve('src/components'), // 组件 'api': resolve('src/api'), // 接口 'utils': resolve('src/utils'), // 通用功能 'assets': resolve('src/assets'), // 静态资源 'style': resolve('src/style') // 通用样式 } } }, chainWebpack (config) { config.plugins.delete('preload') // TODO: need test config.plugins.delete('prefetch') // TODO: need test// set svg-sprite-loader config.module .rule('svg') .exclude.add(resolve('src/icons')) .end() config.module .rule('icons') .test(/\.svg$/) .include.add(resolve('src/icons')) .end() .use('svg-sprite-loader') .loader('svg-sprite-loader') .options({ symbolId: 'icon-[name]' }) .end()// set preserveWhitespace config.module .rule('vue') .use('vue-loader') .loader('vue-loader') .tap(options => { options.compilerOptions.preserveWhitespace = true return options }) .end()config // https://webpack.js.org/configuration/devtool/#development .when(process.env.NODE_ENV === 'development', config => config.devtool('cheap-source-map') )config .when(process.env.NODE_ENV !== 'development', config => { 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 }, 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') } ) if (IS_PRODUCTION) { config.plugin('analyzer').use(BundleAnalyzerPlugin) config.plugin('html').tap(args => { args[0].cdn = cdn return args }) config.externals(externals) config.plugin('html').tap(args => { args[0].minify.minifyCSS = true // 压缩html中的css return args }) // gzip需要nginx进行配合 config .plugin('compression') .use(CompressionWebpackPlugin) .tap(() => [ { test: /\.js$|\.html$|\.css/, // 匹配文件名 threshold: 10240, // 超过10k进行压缩 deleteOriginalAssets: false // 是否删除源文件 } ]) config.optimization.minimizer([ new UglifyjsWebpackPlugin({ // 生产环境推荐关闭 sourcemap 防止源码泄漏 // 服务端通过前端发送的行列,根据 sourcemap 转为源文件位置 // sourceMap: true, uglifyOptions: { warnings: false, compress: { drop_console: true, drop_debugger: true } } }) ]) } }, css: { // 是否使用css分离插件 ExtractTextPlugin extract: !!IS_PRODUCTION, // 开启 CSS source maps? sourceMap: false, // css预设器配置项 // 启用 CSS modules for all css / pre-processor files. modules: false, loaderOptions: { sass: { data: '@import "style/_mixin.scss"; @import "style/_variables.scss"; @import "style/common.scss"; ' // 全局引入 } } } }

【创建vue-cli3项目和配置】其他链接:https://www.cnblogs.com/wwthuanyu/p/10975134.html

    推荐阅读