config.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. const path = require('path')
  18. const glob = require('globby')
  19. const webpack = require('webpack')
  20. const HtmlWebpackPlugin = require('html-webpack-plugin')
  21. const HtmlWebpackExtPlugin = require('html-webpack-ext-plugin')
  22. const isProduction = process.env.NODE_ENV !== 'development'
  23. const resolve = dir => path.join(__dirname, '..', dir)
  24. const assetsDir = resolve('src')
  25. const distDir = resolve('dist')
  26. const viewDir = resolve('src/view')
  27. function moduleName (modules) {
  28. let filename = path.basename(modules)
  29. let parts = filename.split('.')
  30. parts.pop()
  31. filename = parts.join('.')
  32. return path.dirname(modules) + '/' + filename
  33. }
  34. const jsEntry = (() => {
  35. const obj = {}
  36. const files = glob.sync(['js/conf/*/!(_*).js'], { cwd: assetsDir })
  37. files.forEach(val => {
  38. let parts = val.split(/[\\/]/)
  39. parts.shift()
  40. parts.shift()
  41. let modules = parts.join('/')
  42. let entry = moduleName(modules)
  43. obj[entry] = val
  44. })
  45. return obj
  46. })()
  47. const minifierConfig = isProduction ? {
  48. removeComments: true,
  49. removeCommentsFromCDATA: true,
  50. collapseWhitespace: true,
  51. collapseBooleanAttributes: true,
  52. removeRedundantAttributes: true,
  53. useShortDoctype: true,
  54. minifyJS: true,
  55. removeScriptTypeAttributes: true,
  56. maxLineLength: 1024
  57. } : false
  58. const getPageEntry = view => jsEntry[view] ? view : ''
  59. // 重新定向输出页面
  60. const pageRewriter = {
  61. 'view/home/index.*': 'index.html'
  62. }
  63. const isEmpty = o => {
  64. for (let k in o) {
  65. if (o.hasOwnProperty(k)) {
  66. return
  67. }
  68. }
  69. return true
  70. }
  71. const unixPath = v => v.replace(/\\/g, '/')
  72. const rewriterPath = p => {
  73. if (isEmpty(pageRewriter)) {
  74. return
  75. }
  76. for (let k in pageRewriter) {
  77. let regx = new RegExp(k)
  78. if (regx.test(unixPath(p))) {
  79. return pageRewriter[k]
  80. }
  81. }
  82. }
  83. const pages = glob.sync(['*/!(_*).html'], { cwd: viewDir }).map(p => {
  84. let pagePath = `${path.join(viewDir, p)}`
  85. let newPagePath = rewriterPath(pagePath)
  86. let entry = getPageEntry(p.replace('.html', ''))
  87. let chunks = ['common']
  88. if (entry) {
  89. chunks.push(entry)
  90. }
  91. return new HtmlWebpackPlugin({
  92. filename: newPagePath || path.join('view', p),
  93. template: `html-loader?min=false!${path.join(viewDir, p)}`,
  94. cache: true,
  95. inject: true,
  96. chunks: chunks,
  97. minify: minifierConfig
  98. })
  99. })
  100. const baseConfig = {
  101. entry: jsEntry,
  102. output: {
  103. path: distDir,
  104. publicPath: '/',
  105. filename: 'js/[name].[chunkhash:7].js'
  106. },
  107. devServer: {
  108. historyApiFallback: true,
  109. hot: true,
  110. inline: true,
  111. progress: true
  112. },
  113. module: {
  114. rules: [
  115. {
  116. test: /\.js$/,
  117. exclude: /(node_modules|bower_components)/,
  118. use: [
  119. {
  120. loader: 'babel-loader',
  121. options: {
  122. cacheDirectory: true,
  123. cacheIdentifier: true
  124. }
  125. }
  126. ]
  127. },
  128. {
  129. test: /\.(png|jpe?g|gif|svg|cur)(\?.*)?$/,
  130. loader: 'file-loader',
  131. options: {
  132. name: 'images/[name].[ext]?[hash]'
  133. }
  134. },
  135. {
  136. test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
  137. loader: 'url-loader',
  138. options: {
  139. limit: 10000,
  140. // publicPath: distDir,
  141. name: 'font/[name].[hash:7].[ext]'
  142. }
  143. }
  144. ]
  145. },
  146. resolve: {
  147. modules: [
  148. resolve('node_modules'),
  149. resolve('src'),
  150. resolve('src/js')
  151. ],
  152. alias: {
  153. '@': resolve('src/js'),
  154. '~': resolve('src/lib')
  155. },
  156. extensions: ['.js', 'json', '.vue', '.scss']
  157. },
  158. externals: {
  159. 'vue': 'Vue',
  160. 'vuex': 'Vuex',
  161. 'vue-router': 'VueRouter',
  162. 'jquery': '$',
  163. 'lodash': '_',
  164. 'bootstrap': 'bootstrap',
  165. 'd3': 'd3',
  166. 'canvg': 'canvg',
  167. 'html2canvas': 'html2canvas',
  168. './jsplumb': 'jsPlumb',
  169. './highlight.js': 'highlight.js',
  170. './clipboard': 'clipboard',
  171. './codemirror': 'CodeMirror'
  172. },
  173. plugins: [
  174. new webpack.ProvidePlugin({ vue: 'Vue', _: 'lodash' }),
  175. new HtmlWebpackExtPlugin({
  176. cache: true,
  177. delimiter: '$',
  178. locals: {
  179. NODE_ENV:isProduction
  180. }
  181. }),
  182. ...pages
  183. ]
  184. }
  185. module.exports = {
  186. isProduction,
  187. assetsDir,
  188. distDir,
  189. baseConfig
  190. }