vue.config.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. 'use strict';
  2. const path = require('path');
  3. const defaultSettings = require('./src/settings.js');
  4. const SpritesmithPlugin = require('webpack-spritesmith');
  5. function resolve(dir) {
  6. return path.join(__dirname, dir)
  7. }
  8. const name = defaultSettings.title; // page title
  9. // If your port is set to 80,
  10. // use administrator privileges to execute the command line.
  11. // For example, Mac: sudo npm run
  12. // You can change the port by the following method:
  13. // port = 9518 npm run dev OR npm run dev --port = 9518
  14. const port = process.env.port || process.env.npm_config_port || 9518; // dev port
  15. // All configuration item explanations can be find in https://cli.vuejs.org/config/
  16. module.exports = {
  17. /**
  18. * You will need to set publicPath if you plan to deploy your site under a sub path,
  19. * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
  20. * then publicPath should be set to "/bar/".
  21. * In most cases please use '/' !!!
  22. * Detail: https://cli.vuejs.org/config/#publicpath
  23. */
  24. // publicPath: '/',
  25. publicPath: process.env.NODE_ENV === 'production'
  26. ? '/admin' // [通过 ip:port/admin 访问,并注意配置route 的base] ??history模式下打包有问题, 只能使用'/' 不能是'./'否则静态资源会报错,要直接点访问只能使用hash模式('./')?? https://cli.vuejs.org/zh/config/#publicpath
  27. : '/',
  28. outputDir: 'dist',
  29. assetsDir: 'static',
  30. lintOnSave: false,
  31. productionSourceMap: false,
  32. devServer: {
  33. port: port,
  34. open: true,
  35. overlay: {
  36. warnings: false,
  37. errors: true
  38. },
  39. proxy: {
  40. '/api': {
  41. // prod
  42. target: 'http://127.0.0.1:8088',
  43. changeOrigin: true,
  44. pathRewrite: {}
  45. },
  46. // '/api': {
  47. // // test (docker)
  48. // target: 'http://127.0.0.1:8089',
  49. // changeOrigin: true,
  50. // pathRewrite: {}
  51. // },
  52. '/dfs': {
  53. target: 'http://127.0.0.1:80',
  54. changeOrigin: true,
  55. pathRewrite: {}
  56. }
  57. },
  58. // before: require('./mock/mock-server.js')
  59. },
  60. transpileDependencies: [/node_modules[/\\\\](element-ui|vuex|vue-baidu-map|)[/\\\\]/], // 默认情况下 babel-loader 会忽略所有 node_modules 中的文件。如果你想要通过 Babel 显式转译一个依赖,可以在 transpileDependencies 选项中列出来。
  61. configureWebpack: config => {
  62. /*
  63. 细节坑。文档里面写着 需要resolve,引入图片生成的位置,
  64. 不加这行会报错。因为github,Readme里面有这句话
  65. resolve contains location of where generated image is
  66. (要把生成的地址resolve到modules里面。不写就错)
  67. 一定要加,血的教训啊
  68. */
  69. config.resolve.modules = ['node_modules', './src/assets/images'];
  70. // 定义一个插件数组。用来覆盖,在里面使用我们的主角
  71. const Plugins = [
  72. new SpritesmithPlugin({
  73. /*
  74. 目标小图标,这里就是你需要整合的小图片的老巢。
  75. 现在是一个个的散兵,把他们位置找到,合成一个
  76. */
  77. src: {
  78. cwd: path.resolve(__dirname, './src/icons/png'),
  79. glob: '*.png'
  80. },
  81. // 输出雪碧图文件及样式文件,这个是打包后,自动生成的雪碧图和样式,自己配置想生成去哪里就去哪里
  82. target: {
  83. image: path.resolve(__dirname, './src/assets/images/sprite.png'),
  84. css: [
  85. [path.resolve(__dirname, './src/assets/css/sprite.scss'), {
  86. // 引用自己的模板
  87. format: 'function_based_template'
  88. }]
  89. ]
  90. },
  91. // 自定义模板入口,我们需要基本的修改webapck生成的样式,上面的大函数就是我们修改的模板
  92. customTemplates: {
  93. 'function_based_template': templateFunction
  94. },
  95. // 样式文件中调用雪碧图地址写法(Readme这么写的)
  96. apiOptions: {
  97. cssImageRef: '~sprite.png'
  98. },
  99. // 让合成的每个图片有一定的距离,否则就会紧挨着,不好使用
  100. spritesmithOptions: {
  101. padding: 20
  102. }
  103. })
  104. ];
  105. // config里面,覆盖掉以前的,要不然不好使
  106. config.plugins = [...config.plugins, ...Plugins];
  107. // provide the app's title in webpack's name field, so that
  108. // it can be accessed in index.html to inject the correct title.
  109. return {
  110. name: name,
  111. resolve: {
  112. alias: {
  113. '@': resolve('src')
  114. }
  115. }
  116. }
  117. },
  118. chainWebpack(config) {
  119. config.plugins.delete('preload'); // TODO: need test
  120. config.plugins.delete('prefetch'); // TODO: need test
  121. // set svg-sprite-loader
  122. config.module
  123. .rule('svg')
  124. .exclude.add(resolve('src/icons'))
  125. .end();
  126. config.module
  127. .rule('icons')
  128. .test(/\.svg$/)
  129. .include.add(resolve('src/icons'))
  130. .end()
  131. .use('svg-sprite-loader')
  132. .loader('svg-sprite-loader')
  133. .options({
  134. symbolId: 'icon-[name]'
  135. })
  136. .end();
  137. // set preserveWhitespace
  138. config.module
  139. .rule('vue')
  140. .use('vue-loader')
  141. .loader('vue-loader')
  142. .tap(options => {
  143. options.compilerOptions.preserveWhitespace = true;
  144. return options
  145. })
  146. .end();
  147. config
  148. // https://webpack.js.org/configuration/devtool/#development
  149. .when(process.env.NODE_ENV === 'development',
  150. config => config.devtool('cheap-source-map')
  151. );
  152. config
  153. .when(process.env.NODE_ENV !== 'development',
  154. config => {
  155. config
  156. .plugin('ScriptExtHtmlWebpackPlugin')
  157. .after('html')
  158. .use('script-ext-html-webpack-plugin', [{
  159. // `runtime` must same as runtimeChunk name. default is `runtime`
  160. inline: /runtime\..*\.js$/
  161. }])
  162. .end();
  163. config
  164. .optimization.splitChunks({
  165. chunks: 'all',
  166. cacheGroups: {
  167. libs: {
  168. name: 'chunk-libs',
  169. test: /[\\/]node_modules[\\/]/,
  170. priority: 10,
  171. chunks: 'initial' // only package third parties that are initially dependent
  172. },
  173. elementUI: {
  174. name: 'chunk-elementUI', // split elementUI into a single package
  175. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  176. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  177. },
  178. commons: {
  179. name: 'chunk-commons',
  180. test: resolve('src/components'), // can customize your rules
  181. minChunks: 3, // minimum common number
  182. priority: 5,
  183. reuseExistingChunk: true
  184. }
  185. }
  186. });
  187. config.optimization.runtimeChunk('single')
  188. }
  189. );
  190. },
  191. css: {
  192. extract: false, //false表示开发环境,true表示生成环境
  193. sourceMap: false,
  194. loaderOptions: {
  195. postcss: {
  196. plugins: [
  197. require('postcss-px-to-viewport')({
  198. unitToConvert: 'px', // 需要转换的单位,默认为"px"
  199. viewportWidth: 1920, // 视窗的宽度,对应pc设计稿的宽度,一般是1920
  200. viewportHeight: 1080, // 视窗的高度,对应的是我们设计稿的高度,我做的是大屏监控,高度就是1080
  201. unitPrecision: 3, // 单位转换后保留的精度
  202. propList: [ // 能转化为vw的属性列表
  203. '*'
  204. ],
  205. viewportUnit: 'vw', // 希望使用的视口单位
  206. fontViewportUnit: 'vw', // 字体使用的视口单位
  207. selectorBlackList: ['div', 'li', '.a', '.b', '.c', '.d', '.e', '.f', '.g',
  208. '.h', '.i', '.j', '.k', '.l', '.m', '.n', '.o', '.p', '.q', '.r', '.s', '.t', '.u', '.v', '.w', '.x', '.y', '.z'], // 需要忽略的CSS选择器,不会转为视口单位,使用原有的px等单位。
  209. minPixelValue: 1, // 设置最小的转换数值,如果为1的话,只有大于1的值会被转换
  210. mediaQuery: false, // 媒体查询里的单位是否需要转换单位
  211. replace: true, // 是否直接更换属性值,而不添加备用属性
  212. exclude: /(\/|\\)(node_modules)(\/|\\)/, // 忽略某些文件夹下的文件或特定文件,例如 'node_modules' 下的文件
  213. })
  214. ]
  215. }
  216. }
  217. }
  218. };
  219. /* 这里是我们自己修改的模板样式,webpack,会自动生成一个sprite.css的样式,有时候生成的不满意,
  220. 我们可以在这里修改,可以自己打印一下 data里面的参数,看着就会大概明白(先看下面的配置,最后看这个模板)
  221. */
  222. var templateFunction = function (data) {
  223. var shared = '.easy-icon { background-image: url(I);background-size: Wpx Hpx;display: inline-block;}'.replace('I', data.sprites[0].image).replace('W', data.spritesheet.width)
  224. .replace('H', data.spritesheet.height);
  225. var perSprite = data.sprites.map(function (sprite) {
  226. return '.easy-icon-N { width: Wpx; height: Hpx; background-position: Xpx Ypx; }'
  227. .replace('N', sprite.name)
  228. .replace('W', sprite.width)
  229. .replace('H', sprite.height)
  230. .replace('X', sprite.offset_x)
  231. .replace('Y', sprite.offset_y)
  232. }).join('\n');
  233. return shared + '\n' + perSprite
  234. };