config.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 VueLoaderPlugin = require('vue-loader/lib/plugin')
  21. const HtmlWebpackPlugin = require('html-webpack-plugin')
  22. const MiniCssExtractPlugin = require('mini-css-extract-plugin')
  23. const isProduction = process.env.NODE_ENV !== 'development'
  24. const resolve = dir => path.join(__dirname, '..', dir)
  25. const assetsDir = resolve('src')
  26. const distDir = resolve('dist')
  27. const viewDir = resolve('src/view')
  28. function moduleName (modules) {
  29. let filename = path.basename(modules)
  30. let parts = filename.split('.')
  31. parts.pop()
  32. filename = parts.join('.')
  33. return path.dirname(modules) + '/' + filename
  34. }
  35. const jsEntry = (() => {
  36. const obj = {}
  37. const files = glob.sync(['js/conf/*/!(_*).js'], { cwd: assetsDir })
  38. files.forEach(val => {
  39. let parts = val.split(/[\\/]/)
  40. parts.shift()
  41. parts.shift()
  42. let modules = parts.join('/')
  43. let entry = moduleName(modules)
  44. obj[entry] = ['babel-polyfill', val]
  45. })
  46. return obj
  47. })()
  48. const minifierConfig = isProduction ? {
  49. removeComments: true,
  50. removeCommentsFromCDATA: true,
  51. collapseWhitespace: true,
  52. collapseBooleanAttributes: true,
  53. removeRedundantAttributes: true,
  54. useShortDoctype: true,
  55. minifyJS: true,
  56. removeScriptTypeAttributes: true,
  57. maxLineLength: 1024
  58. } : false
  59. const getPageEntry = view => jsEntry[view] ? view : ''
  60. // Redirect output page
  61. const pageRewriter = {
  62. 'view/home/index.*': 'index.html'
  63. }
  64. const isEmpty = o => {
  65. for (let k in o) {
  66. if (o.hasOwnProperty(k)) {
  67. return
  68. }
  69. }
  70. return true
  71. }
  72. const unixPath = v => v.replace(/\\/g, '/')
  73. const rewriterPath = p => {
  74. if (isEmpty(pageRewriter)) {
  75. return
  76. }
  77. for (let k in pageRewriter) {
  78. let regx = new RegExp(k)
  79. if (regx.test(unixPath(p))) {
  80. return pageRewriter[k]
  81. }
  82. }
  83. }
  84. const version = new Date().getTime();
  85. const pages = glob.sync(['*/!(_*).html'], { cwd: viewDir }).map(p => {
  86. let pagePath = `${path.join(viewDir, p)}`
  87. let newPagePath = rewriterPath(pagePath)
  88. let entry = getPageEntry(p.replace('.html', ''))
  89. let chunks = ['common']
  90. if (entry) {
  91. chunks.push(entry)
  92. }
  93. return new HtmlWebpackPlugin({
  94. filename: newPagePath || path.join('view', p),
  95. template: `${path.join('src/view', p)}`,
  96. cache: true,
  97. favicon:'./favicon.png',
  98. inject: true,
  99. hash: version,
  100. chunks: chunks,
  101. minify: minifierConfig
  102. })
  103. })
  104. const baseConfig = {
  105. entry: jsEntry,
  106. output: {
  107. path: distDir,
  108. publicPath: '/',
  109. filename: 'js/[name].[chunkhash:7]'+version+'.js'
  110. },
  111. module: {
  112. rules: [
  113. {
  114. test: /\.(js|vue)$/,
  115. loader: 'eslint-loader',
  116. enforce: 'pre',
  117. include: [resolve('src')],
  118. options: {
  119. formatter: require('eslint-friendly-formatter'),
  120. emitWarning: true
  121. }
  122. },
  123. {
  124. test: /\.vue$/,
  125. loader: 'vue-loader',
  126. options: {
  127. hotReload: !isProduction
  128. }
  129. },
  130. {
  131. test: /\.js$/,
  132. exclude: /(node_modules|bower_components)/,
  133. use: [
  134. {
  135. loader: 'babel-loader',
  136. options: {
  137. cacheDirectory: true,
  138. cacheIdentifier: true
  139. }
  140. }
  141. ]
  142. },
  143. {
  144. test: /\.(sa|sc|c)ss$/,
  145. use: [
  146. {
  147. loader: MiniCssExtractPlugin.loader,
  148. options: {
  149. hmr: !isProduction,
  150. },
  151. },
  152. 'css-loader',
  153. {
  154. loader: 'postcss-loader',
  155. options: {
  156. plugins: (loader) => [
  157. require('autoprefixer')({
  158. overrideBrowserslist: [
  159. "Android 4.1",
  160. "iOS 7.1",
  161. "Chrome > 31",
  162. "ff > 31",
  163. "ie >= 8"
  164. ]
  165. }),
  166. require('cssnano')
  167. ]
  168. }
  169. },
  170. 'sass-loader'
  171. ]
  172. },
  173. {
  174. test: /\.(png|jpe?g|gif|svg|cur)(\?.*)?$/,
  175. loader: 'file-loader',
  176. options: {
  177. esModule: false,
  178. name: 'images/[name].[ext]?[hash]'
  179. }
  180. },
  181. {
  182. test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
  183. loader: 'url-loader',
  184. options: {
  185. esModule: false,
  186. limit: 10000,
  187. // publicPath: distDir,
  188. name: 'font/[name].[hash:7].[ext]'
  189. }
  190. }
  191. ]
  192. },
  193. resolve: {
  194. modules: [
  195. resolve('node_modules'),
  196. resolve('src'),
  197. resolve('src/js')
  198. ],
  199. alias: {
  200. '@': resolve('src/js'),
  201. '~': resolve('src/lib'),
  202. 'jquery':'jquery/dist/jquery.min.js',
  203. 'jquery-ui': 'jquery-ui'
  204. },
  205. extensions: ['*', '.js', 'json', '.vue', '.scss']
  206. },
  207. plugins: [
  208. new VueLoaderPlugin(),
  209. new webpack.ProvidePlugin({ vue: 'Vue', _: 'lodash',jQuery:"jquery/dist/jquery.min.js",$:"jquery/dist/jquery.min.js" }),
  210. new webpack.DefinePlugin({
  211. PUBLIC_PATH: JSON.stringify(process.env.PUBLIC_PATH ? process.env.PUBLIC_PATH : '')
  212. }),
  213. ...pages
  214. ]
  215. }
  216. module.exports = {
  217. isProduction,
  218. assetsDir,
  219. distDir,
  220. baseConfig
  221. }