vue.config.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. const webpack = require('webpack')
  2. // gzip压缩插件
  3. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  4. const projectTitle = 'funasr'
  5. // node命令行参数--env xxx=xxx保存至node的环境变量process.env.xxx=xxx
  6. for (const arg of process.argv) {
  7. if (arg.indexOf('=') > 0) {
  8. process.env[arg.split('=')[0]] = arg.split('=')[1]
  9. }
  10. }
  11. module.exports = {
  12. pages: {
  13. index: {
  14. // 本地化的入口,格式/src/main-<配置>.js,当配置对应的入口文件不存在时,使用默认的入口文件./src/main.js
  15. entry: process.argv.indexOf('--mode') !== -1 && require('fs').existsSync('./src/main-' + process.argv[process.argv.indexOf('--mode') + 1] + '.js') ? './src/main-' + process.argv[process.argv.indexOf('--mode') + 1] + '.js' : './src/main.js',
  16. // 模板来源
  17. template: 'public/index.html',
  18. // 在 dist/index.html 的输出
  19. filename: 'index.html',
  20. // 当使用 title 选项时
  21. // template 中的 title 标签需要是 <title><%= htmlWebpackPlugin.options.title %></title>
  22. title: projectTitle
  23. }
  24. },
  25. // 根据环境配置项目名
  26. publicPath: process.env.NODE_ENV === 'production' ? './' : '/funasr-vue',
  27. // 保存时是否eslint检查
  28. lintOnSave: true,
  29. productionSourceMap: false,
  30. devServer: {
  31. // host: '127.0.0.1',
  32. port: 9018,
  33. // https: false, // https:{type:Boolean}
  34. // open: true,
  35. proxy: {
  36. '/api': {
  37. target: 'http://192.168.1.57:20611',
  38. // ws: true,
  39. changeOrigin: true,
  40. // logLevel: 'debug',
  41. pathRewrite: {
  42. '/api': 'http://192.168.1.57:20611/sso-backend'
  43. }
  44. }
  45. },
  46. client: {
  47. overlay: false
  48. }
  49. },
  50. css: {
  51. loaderOptions: {
  52. // 给 sass-loader 传递选项
  53. sass: {
  54. // @/ 是 src/ 的别名
  55. // 所以这里假设你有 `src/variables.scss` 这个文件
  56. additionalData: '@import "~@/assets/css/common.scss";'
  57. }
  58. }
  59. },
  60. // 配置babel-loader 转译 node_modules 里面的文件
  61. transpileDependencies: [
  62. /[/\\]node_modules[/\\](.+?)?plugin-/ // 匹配编译plugin-开头的组件
  63. ],
  64. configureWebpack: (config) => {
  65. const plugins = []
  66. if (process.env.NODE_ENV !== 'production') {
  67. // 开发环境配置
  68. config.devtool = 'eval-source-map'
  69. } else {
  70. // 生产环境配置
  71. // 添加gzip压缩
  72. plugins.push(
  73. new CompressionWebpackPlugin({
  74. filename: '[path].gz[query]',
  75. algorithm: 'gzip',
  76. test: new RegExp('\\.(' + ['js', 'css'].join('|') + ')$'),
  77. threshold: 10240,
  78. minRatio: 0.8
  79. })
  80. )
  81. config.optimization.minimizer[0].options.minimizer.options.compress.drop_console = true
  82. config.optimization.minimizer[0].options.minimizer.options.compress.drop_debugger = true
  83. }
  84. // 配置打包文件git信息
  85. plugins.push(
  86. new webpack.BannerPlugin({
  87. banner: (() => {
  88. try {
  89. const execSync = require('child_process').execSync
  90. return JSON.stringify({
  91. 'remote-origin-url':
  92. '' + execSync('git remote get-url origin'),
  93. branch: '' + execSync('git rev-parse --abbrev-ref HEAD'),
  94. commithash: '' + execSync('git rev-parse HEAD')
  95. })
  96. } catch (e) {
  97. return 'not a git repository'
  98. }
  99. })()
  100. })
  101. )
  102. config.plugins = [...config.plugins, ...plugins]
  103. const sassLoader = require.resolve('sass-loader')
  104. config.module.rules.filter(rule => {
  105. return rule.test.toString().indexOf('scss') !== -1
  106. }).forEach(rule => {
  107. rule.oneOf.forEach(oneOfRule => {
  108. const sassLoaderIndex = oneOfRule.use.findIndex(item => item.loader === sassLoader)
  109. oneOfRule.use.splice(sassLoaderIndex, 0, { loader: require.resolve('css-unicode-loader') })
  110. })
  111. })
  112. },
  113. chainWebpack: (config) => {
  114. // 为了补删除换行而加的配置
  115. // config.module
  116. // .rule('vue')
  117. // .use('vue-loader')
  118. // .loader('vue-loader')
  119. // .tap((options) => {
  120. // // modify the options...
  121. // options.compilerOptions.preserveWhitespace = false
  122. // return options
  123. // })
  124. }
  125. }