vue.config.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. // '/pageoffice-api': { // "/dev-api"对应后端项目"http://192.168.1.100:8081"地址
  58. // target: 'http://127.0.0.1:8088',
  59. // ws: true,
  60. // changeOrigin: true,
  61. // pathRewrite: {
  62. // '^/pageoffice-api': ''
  63. // }
  64. // },
  65. },
  66. // before: require('./mock/mock-server.js')
  67. },
  68. transpileDependencies: [/node_modules[/\\\\](element-ui|vuex|vue-baidu-map|)[/\\\\]/], // 默认情况下 babel-loader 会忽略所有 node_modules 中的文件。如果你想要通过 Babel 显式转译一个依赖,可以在 transpileDependencies 选项中列出来。
  69. configureWebpack: config => {
  70. /*
  71. 细节坑。文档里面写着 需要resolve,引入图片生成的位置,
  72. 不加这行会报错。因为github,Readme里面有这句话
  73. resolve contains location of where generated image is
  74. (要把生成的地址resolve到modules里面。不写就错)
  75. 一定要加,血的教训啊
  76. */
  77. config.resolve.modules = ['node_modules', './src/assets/images'];
  78. // 定义一个插件数组。用来覆盖,在里面使用我们的主角
  79. const Plugins = [
  80. new SpritesmithPlugin({
  81. /*
  82. 目标小图标,这里就是你需要整合的小图片的老巢。
  83. 现在是一个个的散兵,把他们位置找到,合成一个
  84. */
  85. src: {
  86. cwd: path.resolve(__dirname, './src/icons/png'),
  87. glob: '*.png'
  88. },
  89. // 输出雪碧图文件及样式文件,这个是打包后,自动生成的雪碧图和样式,自己配置想生成去哪里就去哪里
  90. target: {
  91. image: path.resolve(__dirname, './src/assets/images/sprite.png'),
  92. css: [
  93. [path.resolve(__dirname, './src/assets/css/sprite.scss'), {
  94. // 引用自己的模板
  95. format: 'function_based_template'
  96. }]
  97. ]
  98. },
  99. // 自定义模板入口,我们需要基本的修改webapck生成的样式,上面的大函数就是我们修改的模板
  100. customTemplates: {
  101. 'function_based_template': templateFunction
  102. },
  103. // 样式文件中调用雪碧图地址写法(Readme这么写的)
  104. apiOptions: {
  105. cssImageRef: '~sprite.png'
  106. },
  107. // 让合成的每个图片有一定的距离,否则就会紧挨着,不好使用
  108. spritesmithOptions: {
  109. padding: 20
  110. }
  111. })
  112. ];
  113. // config里面,覆盖掉以前的,要不然不好使
  114. config.plugins = [...config.plugins, ...Plugins];
  115. // provide the app's title in webpack's name field, so that
  116. // it can be accessed in index.html to inject the correct title.
  117. return {
  118. name: name,
  119. resolve: {
  120. alias: {
  121. '@': resolve('src')
  122. }
  123. }
  124. }
  125. },
  126. chainWebpack(config) {
  127. config.plugins.delete('preload'); // TODO: need test
  128. config.plugins.delete('prefetch'); // TODO: need test
  129. // set svg-sprite-loader
  130. config.module
  131. .rule('svg')
  132. .exclude.add(resolve('src/icons'))
  133. .end();
  134. config.module
  135. .rule('icons')
  136. .test(/\.svg$/)
  137. .include.add(resolve('src/icons'))
  138. .end()
  139. .use('svg-sprite-loader')
  140. .loader('svg-sprite-loader')
  141. .options({
  142. symbolId: 'icon-[name]'
  143. })
  144. .end();
  145. // set preserveWhitespace
  146. config.module
  147. .rule('vue')
  148. .use('vue-loader')
  149. .loader('vue-loader')
  150. .tap(options => {
  151. options.compilerOptions.preserveWhitespace = true;
  152. return options
  153. })
  154. .end();
  155. config
  156. // https://webpack.js.org/configuration/devtool/#development
  157. .when(process.env.NODE_ENV === 'development',
  158. config => config.devtool('cheap-source-map')
  159. );
  160. config
  161. .when(process.env.NODE_ENV !== 'development',
  162. config => {
  163. config
  164. .plugin('ScriptExtHtmlWebpackPlugin')
  165. .after('html')
  166. .use('script-ext-html-webpack-plugin', [{
  167. // `runtime` must same as runtimeChunk name. default is `runtime`
  168. inline: /runtime\..*\.js$/
  169. }])
  170. .end();
  171. config
  172. .optimization.splitChunks({
  173. chunks: 'all',
  174. cacheGroups: {
  175. libs: {
  176. name: 'chunk-libs',
  177. test: /[\\/]node_modules[\\/]/,
  178. priority: 10,
  179. chunks: 'initial' // only package third parties that are initially dependent
  180. },
  181. elementUI: {
  182. name: 'chunk-elementUI', // split elementUI into a single package
  183. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  184. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  185. },
  186. commons: {
  187. name: 'chunk-commons',
  188. test: resolve('src/components'), // can customize your rules
  189. minChunks: 3, // minimum common number
  190. priority: 5,
  191. reuseExistingChunk: true
  192. }
  193. }
  194. });
  195. config.optimization.runtimeChunk('single')
  196. }
  197. );
  198. },
  199. css: {
  200. extract: false, //false表示开发环境,true表示生成环境
  201. sourceMap: false,
  202. loaderOptions: {
  203. postcss: {
  204. plugins: [
  205. require('postcss-px-to-viewport')({
  206. unitToConvert: 'px', // 需要转换的单位,默认为"px"
  207. viewportWidth: 1920, // 视窗的宽度,对应pc设计稿的宽度,一般是1920
  208. viewportHeight: 1080, // 视窗的高度,对应的是我们设计稿的高度,我做的是大屏监控,高度就是1080
  209. unitPrecision: 3, // 单位转换后保留的精度
  210. propList: [ // 能转化为vw的属性列表
  211. '*'
  212. ],
  213. viewportUnit: 'vw', // 希望使用的视口单位
  214. fontViewportUnit: 'vw', // 字体使用的视口单位
  215. selectorBlackList: ['div', 'li', '.a', '.b', '.c', '.d', '.e', '.f', '.g',
  216. '.h', '.i', '.j', '.k', '.l', '.m', '.n', '.o', '.p', '.q', '.r', '.s', '.t', '.u', '.v', '.w', '.x', '.y', '.z'], // 需要忽略的CSS选择器,不会转为视口单位,使用原有的px等单位。
  217. minPixelValue: 1, // 设置最小的转换数值,如果为1的话,只有大于1的值会被转换
  218. mediaQuery: false, // 媒体查询里的单位是否需要转换单位
  219. replace: true, // 是否直接更换属性值,而不添加备用属性
  220. exclude: /(\/|\\)(node_modules)(\/|\\)/, // 忽略某些文件夹下的文件或特定文件,例如 'node_modules' 下的文件
  221. })
  222. ]
  223. }
  224. }
  225. }
  226. };
  227. /* 这里是我们自己修改的模板样式,webpack,会自动生成一个sprite.css的样式,有时候生成的不满意,
  228. 我们可以在这里修改,可以自己打印一下 data里面的参数,看着就会大概明白(先看下面的配置,最后看这个模板)
  229. */
  230. var templateFunction = function (data) {
  231. 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)
  232. .replace('H', data.spritesheet.height);
  233. var perSprite = data.sprites.map(function (sprite) {
  234. return '.easy-icon-N { width: Wpx; height: Hpx; background-position: Xpx Ypx; }'
  235. .replace('N', sprite.name)
  236. .replace('W', sprite.width)
  237. .replace('H', sprite.height)
  238. .replace('X', sprite.offset_x)
  239. .replace('Y', sprite.offset_y)
  240. }).join('\n');
  241. return shared + '\n' + perSprite
  242. };