You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

vue.config.js 6.2 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*eslint-disable*/
  2. const path = require('path');
  3. const sass = require('sass');
  4. const Promise = require('bluebird');
  5. const fs = require('fs-extra');
  6. const { match } = require('path-to-regexp');
  7. const proxy = require('express-http-proxy');
  8. const defaultSettings = require('./src/settings.js');
  9. function resolve(dir) {
  10. return path.join(__dirname, dir);
  11. }
  12. const name = defaultSettings.title; // 网址标题
  13. const port = 8013; // 端口配置
  14. // All configuration item explanations can be find in https://cli.vuejs.org/config/
  15. module.exports = {
  16. publicPath: '/',
  17. outputDir: 'dist',
  18. assetsDir: 'static',
  19. lintOnSave: process.env.NODE_ENV === 'development',
  20. productionSourceMap: false,
  21. devServer: {
  22. port,
  23. open: false,
  24. overlay: {
  25. warnings: false,
  26. errors: true,
  27. },
  28. before (app){
  29. function requireUncached(module) {
  30. try {
  31. // 删除缓存,动态加载
  32. delete require.cache[require.resolve(module)];
  33. return require(module);
  34. } catch (e) {
  35. // console.log(`can't load module in ${module}`);
  36. return false
  37. }
  38. }
  39. // 根据 mock 请求发送响应
  40. function sendValue(req, res, value) {
  41. if (typeof value === 'function') {
  42. value = value(req, res);
  43. }
  44. if (value.$$header) {
  45. Object.keys(value.$$header).forEach(key => {
  46. res.setHeader(key, value.$$header[key]);
  47. });
  48. }
  49. const delay = value.$$delay || 0;
  50. delete value.$$header;
  51. delete value.$$delay;
  52. Promise.delay(delay, value).then(result => {
  53. res.send(result);
  54. });
  55. }
  56. // 分解mockPath
  57. const splitUrl = resouce => {
  58. const splitUrl = resouce.split('::');
  59. let verb = 'get', url = '';
  60. if(splitUrl.length > 2) {
  61. throw new Error('url 格式不对');
  62. }
  63. if(splitUrl.length === 2) {
  64. [verb, url] = splitUrl
  65. verb = splitUrl[0].toLowerCase();
  66. url = splitUrl[1];
  67. }else if(splitUrl.length === 1){
  68. verb = 'get';
  69. url = splitUrl[0];
  70. }
  71. return [verb, url];
  72. }
  73. // 处理 restful mock 接口
  74. const mockMap = require(path.join(__dirname, 'mock/mock-map'));
  75. // 根据用户是否添加 mock 文件来决定走本地 mock 或者转发到 dev 接口
  76. app.use('/mock', proxy(process.env.VUE_APP_BASE_API, {
  77. filter: function(req, res){
  78. // 是否匹配到本地 rest 风格 api mockUrl
  79. const matchRESTApi = Object.keys(mockMap).findIndex(d => {
  80. const [,uri] = splitUrl(d);
  81. const matcher = match(uri, { decode: decodeURIComponent })
  82. return matcher(req.path)
  83. }) > -1
  84. // 如果匹配到 restApi 走本地 mock
  85. if(matchRESTApi) return false
  86. // 其他路径
  87. const mockPath = path.join(__dirname, 'mock', req.path);
  88. const value = requireUncached(mockPath);
  89. return value === false
  90. }
  91. }));
  92. // 对于每个 mock 请求,require mock 文件夹下的对应路径文件,并返回响应
  93. Object.keys(mockMap).forEach(mockPath => {
  94. const [verb, uri] = splitUrl(mockPath);
  95. app[verb](path.posix.join('/mock', uri), function(req, res) {
  96. const value = requireUncached(path.join(__dirname, 'mock', mockMap[mockPath]))
  97. sendValue(req, res, value)
  98. })
  99. })
  100. app.all('/mock/*', function(req, res) {
  101. const mockPath = path.join(__dirname, req.path)
  102. const value = requireUncached(mockPath)
  103. if (value) {
  104. sendValue(req, res, value)
  105. } else {
  106. res.sendStatus(404)
  107. }
  108. })
  109. },
  110. },
  111. css: {
  112. loaderOptions: {
  113. sass: {
  114. implementation: sass,
  115. },
  116. },
  117. },
  118. configureWebpack: {
  119. // provide the app's title in webpack's name field, so that
  120. // it can be accessed in index.html to inject the correct title.
  121. name,
  122. resolve: {
  123. alias: {
  124. '@': resolve('src'),
  125. '@crud': resolve('src/components/Crud'),
  126. },
  127. },
  128. },
  129. chainWebpack(config) {
  130. config.plugins.delete('preload');
  131. config.plugins.delete('prefetch');
  132. // set preserveWhitespace
  133. config.module
  134. .rule('vue')
  135. .use('vue-loader')
  136. .loader('vue-loader')
  137. .tap(options => {
  138. options.compilerOptions.preserveWhitespace = true;
  139. return options;
  140. })
  141. .end();
  142. config
  143. // https://webpack.js.org/configuration/devtool/#development
  144. .when(process.env.NODE_ENV === 'development',
  145. config => config.devtool('eval-source-map'),
  146. );
  147. config
  148. .when(process.env.NODE_ENV !== 'development',
  149. config => {
  150. config
  151. .plugin('ScriptExtHtmlWebpackPlugin')
  152. .after('html')
  153. .use('script-ext-html-webpack-plugin', [{
  154. // `runtime` must same as runtimeChunk name. default is `runtime`
  155. inline: /runtime\..*\.js$/,
  156. }])
  157. .end();
  158. config
  159. .optimization.splitChunks({
  160. chunks: 'all',
  161. cacheGroups: {
  162. libs: {
  163. name: 'chunk-libs',
  164. test: /[\\/]node_modules[\\/]/,
  165. priority: 10,
  166. chunks: 'initial', // only package third parties that are initially dependent
  167. },
  168. elementUI: {
  169. name: 'chunk-elementUI', // split elementUI into a single package
  170. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  171. test: /[\\/]node_modules[\\/]_?element-ui(.*)/, // in order to adapt to cnpm
  172. },
  173. commons: {
  174. name: 'chunk-commons',
  175. test: resolve('src/components'), // can customize your rules
  176. minChunks: 3, // minimum common number
  177. priority: 5,
  178. reuseExistingChunk: true,
  179. },
  180. },
  181. });
  182. config.optimization.runtimeChunk('single');
  183. },
  184. );
  185. },
  186. };

一站式算法开发平台、高性能分布式深度学习框架、先进算法模型库、视觉模型炼知平台、数据可视化分析平台等一系列平台及工具,在模型高效分布式训练、数据处理和可视分析、模型炼知和轻量化等技术上形成独特优势,目前已在产学研等各领域近千家单位及个人提供AI应用赋能