webpack.config.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. * @author oldj
  3. * @blog http://oldj.net
  4. */
  5. 'use strict'
  6. const path = require('path')
  7. const webpack = require('webpack')
  8. //const moment = require('moment')
  9. const WebpackNotifierPlugin = require('webpack-notifier')
  10. const MiniCssExtractPlugin = require('mini-css-extract-plugin')
  11. const LESSPluginLists = require('less-plugin-lists')
  12. const TerserPlugin = require('terser-webpack-plugin')
  13. const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin')
  14. //const version = require('../app/version').join('.')
  15. const UpVersionPlugin = require('./webpack_up_version')
  16. const basedir = path.dirname(__dirname)
  17. const mini_css_loader = {
  18. loader: MiniCssExtractPlugin.loader,
  19. options: {
  20. // you can specify a publicPath here
  21. // by default it uses publicPath in webpackOptions.output
  22. //publicPath: '../',
  23. hmr: process.env.NODE_ENV === 'development'
  24. }
  25. }
  26. module.exports = {
  27. mode: 'production',
  28. entry: {
  29. app: './app-ui/index.jsx'
  30. //, vendor: ['react', 'antd']
  31. },
  32. devtool: 'source-map',
  33. output: {
  34. path: path.join(__dirname, '..', 'app', 'ui'),
  35. filename: '[name].js',
  36. sourceMapFilename: '[name].js.map'
  37. },
  38. resolve: {
  39. extensions: ['.js', '.jsx']
  40. },
  41. module: {
  42. rules: [
  43. {
  44. test: /\.jsx?$/,
  45. //exclude: [/node_modules/],
  46. use: ['babel-loader?sourceMap']
  47. },
  48. {
  49. test: /\.css$/,
  50. exclude: /(codemirror|animate\.css)/,
  51. loader: [
  52. mini_css_loader,
  53. {
  54. loader: 'css-loader',
  55. options: {
  56. importLoaders: 1,
  57. sourceMap: true
  58. }
  59. }
  60. ]
  61. },
  62. {
  63. test: /\.less$/,
  64. exclude: /(node_modules|antd)/,
  65. loader: [
  66. mini_css_loader,
  67. //'css-loader?minimize&modules&sourceMap&localIdentName=[path][name]--[local]',
  68. //'style-loader',
  69. {
  70. loader: 'css-loader',
  71. options: {
  72. importLoaders: 2,
  73. //modules: true,
  74. //camelCase: true,
  75. //javascriptEnabled: true,
  76. sourceMap: true,
  77. modules: {
  78. mode: 'local',
  79. //context: path.resolve(__dirname, 'src'),
  80. //hashPrefix: 'wonderpen-',
  81. //localIdentName: '[path][name]__[local]--[hash:base64:5]',
  82. localIdentName: '[folder]__[name]__[local]'
  83. }
  84. }
  85. },
  86. {
  87. loader: 'less-loader?outputStyle=expanded',
  88. options: {
  89. javascriptEnabled: true,
  90. plugins: [
  91. new LESSPluginLists({advanced: true})
  92. ]
  93. }
  94. }
  95. ]
  96. },
  97. {
  98. test: /codemirror\b.*\.css|animate\.css$/,
  99. loader: [
  100. mini_css_loader,
  101. {
  102. loader: 'css-loader',
  103. options: {
  104. importLoaders: 1,
  105. sourceMap: true
  106. }
  107. }
  108. ]
  109. },
  110. {
  111. test: /antd\.less$/,
  112. loader: [
  113. mini_css_loader,
  114. //'css-loader?minimize&sourceMap',
  115. {
  116. loader: 'css-loader',
  117. options: {
  118. //importLoaders: 1,
  119. sourceMap: true
  120. }
  121. },
  122. {
  123. loader: 'less-loader',
  124. options: {
  125. javascriptEnabled: true
  126. }
  127. }
  128. ]
  129. },
  130. {
  131. test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
  132. use: [
  133. {
  134. loader: 'babel-loader'
  135. },
  136. {
  137. loader: '@svgr/webpack',
  138. options: {
  139. babel: false,
  140. icon: true,
  141. replaceAttrValues: {
  142. '#000000': 'currentColor',
  143. '#000': 'currentColor'
  144. }
  145. }
  146. }
  147. ]
  148. },
  149. {
  150. test: /\.(eot|woff|woff2|ttf|png|jpg|svg\?bg)$/,
  151. //use: 'url-loader?limit=30000&name=[name]-[hash].[ext]'
  152. use: [
  153. {
  154. loader: 'url-loader',
  155. options: {
  156. limit: 30000,
  157. name: '[name]-[hash].[ext]'
  158. }
  159. }
  160. ]
  161. }
  162. ],
  163. noParse: [/\bAgent\b/]
  164. },
  165. optimization: {
  166. minimizer: [
  167. new TerserPlugin({
  168. terserOptions: {
  169. output: {
  170. ecma: 5,
  171. comments: false,
  172. ascii_only: true
  173. }
  174. },
  175. parallel: true,
  176. cache: true,
  177. sourceMap: true
  178. }),
  179. new OptimizeCSSAssetsPlugin({})
  180. ]
  181. },
  182. plugins: [
  183. new webpack.DefinePlugin({
  184. 'process.env': {
  185. NODE_ENV: JSON.stringify('production')
  186. }
  187. }),
  188. new UpVersionPlugin({
  189. fn: path.join(basedir, 'app', 'version.js'),
  190. packages: [path.join(basedir, 'package.json'), path.join(basedir, 'app', 'package.json')],
  191. }),
  192. new MiniCssExtractPlugin({
  193. // Options similar to the same options in webpackOptions.output
  194. // both options are optional
  195. filename: '[name].css',
  196. chunkFilename: '[id].css'
  197. }),
  198. new webpack.DllReferencePlugin({
  199. context: __dirname,
  200. manifest: require('../tmp/manifest.json')
  201. }),
  202. new webpack.IgnorePlugin(new RegExp('^(electron|fs|path)$')),
  203. new WebpackNotifierPlugin({
  204. title: 'SwitchHosts!',
  205. alwaysNotify: true,
  206. excludeWarnings: true
  207. })
  208. //new webpack.BannerPlugin(`SwitchHosts! [file] v${version}, ${moment().format('YYYY-MM-DD HH:mm:ss')}`)
  209. ]
  210. }