美文网首页
vue-cli4.0 配置文件 vue.config.js

vue-cli4.0 配置文件 vue.config.js

作者: Michael113c | 来源:发表于2020-06-13 14:23 被阅读0次

vue-cli4没有config文件,解决方法如下

在根目录加上vue.config.js文件

const vuxLoader = require('vux-loader')
module.exports = {
  // 将部署应用程序的基本URL
  // 将部署应用程序的基本URL。
  // 默认情况下,Vue CLI假设您的应用程序将部署在域的根目录下。
  // https://www.my-app.com/。如果应用程序部署在子路径上,则需要使用此选项指定子路径。
  // 例如,如果您的应用程序部署在https://www.foobar.com/my-app/,集baseUrl到'/my-app/'.
  publicPath: process.env.NODE_ENV === 'production' ? './' : './',
  // outputDir: 在npm run build时 生成文件的目录 type:string, default:'dist'
  outputDir: 'dist',
  // pages:{ type:Object,Default:undfind } 
/*
构建多页面模式的应用程序.每个“页面”都应该有一个相应的JavaScript条目文件。该值应该是一
个对象,其中键是条目的名称,而该值要么是指定其条目、模板和文件名的对象,要么是指定其条目
的字符串,
注意:请保证pages里配置的路径和文件名 在你的文档目录都存在 否则启动服务会报错的
*/
  pages: {
      index: {
          //entry for the page
          entry: 'src/main.js',
          //the source template
          template: 'public/index.html',
          //output as dist/index.html
          filename: 'index.html'
      },
      // when using the entry-only string format,
      // template is inferred to be `public/subpage.html`
      // and falls back to `public/index.html` if not found.
      // Output filename is inferred to be `subpage.html`.
      // subpage: 'src/subpage/main.js'
 },

  //   lintOnSave:{ type:Boolean default:true } 问你是否使用eslint
  lintOnSave: true,
  // productionSourceMap:{ type:Bollean,default:true } 生产源映射
  // 如果您不需要生产时的源映射,那么将此设置为false可以加速生产构建
  productionSourceMap: false,
  // devServer:{type:Object} 3个属性host,port,https
  // 它支持webPack-dev-server的所有选项
  devServer: {
      port: 8090, // 端口号
      host: 'localhost',
      https: false, // https:{type:Boolean}
      open: true, //配置自动启动浏览器
      // proxy: 'http://localhost:4000' // 配置跨域处理,只有一个代理
  },
  css: {
    loaderOptions: {
    css: {},
    postcss: {
        plugins: [
        require('postcss-px2rem')({
            remUnit: 37.5
        })
        ]
        }
    }
  },
}

另附一份升级版

const path = require('path')
const CompressionPlugin = require('compression-webpack-plugin')
const isProd = process.env.NODE_ENV === 'production'
const webpack = require('webpack')
// 项目目录
const baseUrl = '/'
function resolve (dir) {
  return path.join(__dirname, dir)
}
module.exports = {
  publicPath: isProd ? baseUrl : '/',
  productionSourceMap: false,
  lintOnSave: !isProd,
  // 开启gzip 压缩
  configureWebpack: () => {
    const plugins = [
    ]
    if (isProd) {
      plugins.push(
        new CompressionPlugin({
          test: /\.js$|\.html$|\.css$/, // 匹配的文件名
          threshold: 8192, // 对 超过8K的数据进行压缩
          deleteOriginalAssets: false // 是否删除源文件
        })
      )
    }
    return {
      plugins
    }
  },
  chainWebpack: config => {
    const types = ['vue-modules', 'vue', 'normal-modules', 'normal']
    types.forEach(type => addStyleResource(config.module.rule('scss').oneOf(type)))
    // svg
    const svgRule = config.module.rule('svg')
    svgRule.uses.clear()
    svgRule
      .include
      .add(resolve('src/assets/svg-icons/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'admin-[name]'
      })
      .end()
    config
    // 开发环境
      .when(process.env.NODE_ENV === 'development',
        // sourcemap不包含列信息
        config => config.devtool('cheap-source-map')
      )
    // image exclude
    const imagesRule = config.module.rule('images')
    imagesRule
      .test(/\.(png|jpe?g|gif|webp|svg)(\?.*)?$/)
      .exclude
      .add(resolve('src/assets/svg-icons/icons'))
      .end()
    config.resolve.alias
      .set('@', resolve('src'))
      .set('assets', resolve('src/assets'))
      .set('utils', resolve('src/utils'))
    config
      .plugin('html')
      .tap((args) => {
        args[0].title = 'Vue Demo'
        return args
      })
  },
  devServer: {
    disableHostCheck: true
  },
  pluginOptions: {
    'style-resources-loader': {
      preProcessor: 'scss',
      patterns: []
    }
  }
}
function addStyleResource (rule) {
  rule.use('style-resource')
    .loader('style-resources-loader')
    .options({
      patterns: [
        // 全局共享样式
        path.resolve(__dirname, './src/css/_variables.scss'),
        path.resolve(__dirname, './src/css/mixins.scss')
      ]
    })
}

相关文章

网友评论

      本文标题:vue-cli4.0 配置文件 vue.config.js

      本文链接:https://www.haomeiwen.com/subject/gjgatktx.html