vue.config.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. 'use strict'
  2. const path = require('path')
  3. const defaultSettings = require('./src/settings.js')
  4. const CKEditorWebpackPlugin = require('@ckeditor/ckeditor5-dev-webpack-plugin')
  5. const { styles } = require('@ckeditor/ckeditor5-dev-utils')
  6. function resolve(dir) {
  7. return path.join(__dirname, dir)
  8. }
  9. const name = defaultSettings.title || 'title' // page title
  10. // If your port is set to 80,
  11. // use administrator privileges to execute the command line.
  12. // For example, Mac: sudo npm run
  13. // You can change the port by the following methods:
  14. // port = 9528 npm run dev OR npm run dev --port = 9528
  15. const port = process.env.port || process.env.npm_config_port || 9528 // dev port
  16. // All configuration item explanations can be find in https://cli.vuejs.org/config/
  17. module.exports = {
  18. /**
  19. * You will need to set publicPath if you plan to deploy your site under a sub path,
  20. * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
  21. * then publicPath should be set to "/bar/".
  22. * In most cases please use '/' !!!
  23. * Detail: https://cli.vuejs.org/config/#publicpath
  24. */
  25. publicPath: '/',
  26. outputDir: 'dist',
  27. assetsDir: 'static',
  28. lintOnSave: process.env.NODE_ENV === 'development',
  29. productionSourceMap: false,
  30. devServer: {
  31. port: port,
  32. overlay: {
  33. warnings: false,
  34. errors: true
  35. },
  36. proxy: {
  37. [process.env.VUE_APP_BASE_API]: {
  38. target: `http://127.0.0.1:${port}/`,
  39. changeOrigin: true,
  40. pathRewrite: {
  41. ['^' + process.env.VUE_APP_BASE_API]: ''
  42. }
  43. }
  44. }
  45. },
  46. configureWebpack: {
  47. // provide the app's title in webpack's name field, so that
  48. // it can be accessed in index.html to inject the correct title.
  49. name: name,
  50. resolve: {
  51. alias: {
  52. '@': resolve('src')
  53. }
  54. },
  55. plugins: [
  56. // CKEditor needs its own plugin to be built using webpack.
  57. new CKEditorWebpackPlugin({
  58. // See https://ckeditor.com/docs/ckeditor5/latest/features/ui-language.html
  59. language: 'zh-cn'
  60. })
  61. ]
  62. },
  63. chainWebpack(config) {
  64. config.plugins.delete('preload') // TODO: need test
  65. config.plugins.delete('prefetch') // TODO: need test
  66. const svgRule = config.module.rule('svg')
  67. svgRule.exclude.add(path.join(__dirname, 'node_modules', '@ckeditor'))
  68. // set svg-sprite-loader
  69. config.module
  70. .rule('svg')
  71. .exclude.add(resolve('src/icons'))
  72. .end()
  73. config.module
  74. .rule('icons')
  75. .test(/\.svg$/)
  76. .include.add(resolve('src/icons'))
  77. .end()
  78. .use('svg-sprite-loader')
  79. .loader('svg-sprite-loader')
  80. .options({
  81. symbolId: 'icon-[name]'
  82. })
  83. .end()
  84. // set preserveWhitespace
  85. config.module
  86. .rule('vue')
  87. .use('vue-loader')
  88. .loader('vue-loader')
  89. .tap(options => {
  90. options.compilerOptions.preserveWhitespace = true
  91. return options
  92. })
  93. .end()
  94. config.module
  95. .rule('cke-svg')
  96. .test(/ckeditor5-[^/\\]+[/\\]theme[/\\]icons[/\\][^/\\]+\.svg$/)
  97. .use('raw-loader')
  98. .loader('raw-loader')
  99. config.module
  100. .rule('cke-css')
  101. .test(/ckeditor5-[^/\\]+[/\\].+\.css$/)
  102. .use('postcss-loader')
  103. .loader('postcss-loader')
  104. .tap(() => {
  105. return styles.getPostCssConfig({
  106. themeImporter: {
  107. themePath: require.resolve('@ckeditor/ckeditor5-theme-lark')
  108. },
  109. minify: true
  110. })
  111. })
  112. config
  113. // https://webpack.js.org/configuration/devtool/#development
  114. .when(process.env.NODE_ENV === 'development',
  115. config => config.devtool('cheap-source-map')
  116. )
  117. config
  118. .when(process.env.NODE_ENV !== 'development',
  119. config => {
  120. config
  121. .plugin('ScriptExtHtmlWebpackPlugin')
  122. .after('html')
  123. .use('script-ext-html-webpack-plugin', [{
  124. // `runtime` must same as runtimeChunk name. default is `runtime`
  125. inline: /runtime\..*\.js$/
  126. }])
  127. .end()
  128. config
  129. .optimization.splitChunks({
  130. chunks: 'all',
  131. cacheGroups: {
  132. libs: {
  133. name: 'chunk-libs',
  134. test: /[\\/]node_modules[\\/]/,
  135. priority: 10,
  136. chunks: 'initial' // only package third parties that are initially dependent
  137. },
  138. elementUI: {
  139. name: 'chunk-elementUI', // split elementUI into a single package
  140. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  141. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  142. },
  143. commons: {
  144. name: 'chunk-commons',
  145. test: resolve('src/components'), // can customize your rules
  146. minChunks: 3, // minimum common number
  147. priority: 5,
  148. reuseExistingChunk: true
  149. }
  150. }
  151. })
  152. config.optimization.runtimeChunk('single')
  153. }
  154. )
  155. },
  156. transpileDependencies: [
  157. /ckeditor5-[^/\\]+[/\\]src[/\\].+\.js$/
  158. ]
  159. }