vue.config.js 9.1 KB

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