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.

webpack_pro.config.js 7.2 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. const cssnano = require('cssnano');
  2. const fastGlob = require('fast-glob');
  3. const FixStyleOnlyEntriesPlugin = require('webpack-fix-style-only-entries');
  4. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  5. const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');
  6. const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
  7. const PostCSSPresetEnv = require('postcss-preset-env');
  8. const PostCSSSafeParser = require('postcss-safe-parser');
  9. const SpriteLoaderPlugin = require('svg-sprite-loader/plugin');
  10. const TerserPlugin = require('terser-webpack-plugin');
  11. const VueLoaderPlugin = require('vue-loader/lib/plugin');
  12. const {statSync} = require('fs');
  13. const {resolve, parse} = require('path');
  14. //const {SourceMapDevToolPlugin} = require('webpack');
  15. const glob = (pattern) => fastGlob.sync(pattern, {cwd: __dirname, absolute: true});
  16. const themes = {};
  17. for (const path of glob('web_src/less/themes/*.less')) {
  18. themes[parse(path).name] = [path];
  19. }
  20. const isProduction = process.env.NODE_ENV !== 'development';
  21. module.exports = {
  22. mode: isProduction ? 'production' : 'development',
  23. entry: {
  24. index: [
  25. resolve(__dirname, 'web_src/js/index.js'),
  26. resolve(__dirname, 'web_src/less/index.less'),
  27. ],
  28. swagger: [
  29. resolve(__dirname, 'web_src/js/standalone/swagger.js'),
  30. ],
  31. jquery: [
  32. resolve(__dirname, 'web_src/js/jquery.js'),
  33. ],
  34. icons: glob('node_modules/@primer/octicons/build/svg/**/*.svg'),
  35. ...themes,
  36. },
  37. devtool: false,
  38. output: {
  39. path: resolve(__dirname, 'public'),
  40. filename: 'js/[name].js',
  41. chunkFilename: 'js/[name].js',
  42. },
  43. node:{
  44. fs: 'empty'
  45. },
  46. optimization: {
  47. minimize: isProduction,
  48. minimizer: [
  49. new TerserPlugin({
  50. sourceMap: true,
  51. extractComments: false,
  52. terserOptions: {
  53. keep_fnames: /^(HTML|SVG)/, // https://github.com/fgnass/domino/issues/144
  54. output: {
  55. comments: false,
  56. },
  57. },
  58. }),
  59. new OptimizeCSSAssetsPlugin({
  60. cssProcessor: cssnano,
  61. cssProcessorOptions: {
  62. parser: PostCSSSafeParser,
  63. },
  64. cssProcessorPluginOptions: {
  65. preset: [
  66. 'default',
  67. {
  68. discardComments: {
  69. removeAll: true,
  70. },
  71. },
  72. ],
  73. },
  74. }),
  75. ],
  76. splitChunks: {
  77. chunks: 'async',
  78. name: (_, chunks) => chunks.map((item) => item.name).join('-'),
  79. cacheGroups: {
  80. // this bundles all monaco's languages into one file instead of emitting 1-65.js files
  81. monaco: {
  82. test: /monaco-editor/,
  83. name: 'monaco',
  84. chunks: 'async'
  85. }
  86. }
  87. }
  88. },
  89. module: {
  90. rules: [
  91. {
  92. test: /\.vue$/,
  93. exclude: /node_modules/,
  94. loader: 'vue-loader',
  95. },
  96. {
  97. test: require.resolve('jquery-datetimepicker'),
  98. use: 'imports-loader?define=>false,exports=>false',
  99. },
  100. {
  101. test: /\.worker\.js$/,
  102. exclude: /monaco/,
  103. use: [
  104. {
  105. loader: 'worker-loader',
  106. options: {
  107. name: '[name].js',
  108. inline: true,
  109. fallback: false,
  110. },
  111. },
  112. ],
  113. },
  114. {
  115. test: /\.ts$/,
  116. use: [
  117. {
  118. loader: "ts-loader",
  119. }
  120. ],
  121. exclude: /node_modules/
  122. },
  123. {
  124. test: /\.js$/,
  125. exclude: /node_modules/,
  126. use: [
  127. {
  128. loader: 'babel-loader',
  129. options: {
  130. cacheDirectory: true,
  131. cacheCompression: false,
  132. cacheIdentifier: [
  133. resolve(__dirname, 'package.json'),
  134. resolve(__dirname, 'package-lock.json'),
  135. resolve(__dirname, 'webpack.config.js'),
  136. ].map((path) => statSync(path).mtime.getTime()).join(':'),
  137. sourceMaps: true,
  138. presets: [
  139. [
  140. '@babel/preset-env',
  141. {
  142. useBuiltIns: 'usage',
  143. corejs: 3,
  144. },
  145. ],
  146. ],
  147. plugins: [
  148. [
  149. '@babel/plugin-transform-runtime',
  150. {
  151. regenerator: true,
  152. }
  153. ],
  154. '@babel/plugin-proposal-object-rest-spread',
  155. ],
  156. },
  157. },
  158. ],
  159. },
  160. {
  161. test: /\.(less|css)$/i,
  162. use: [
  163. {
  164. loader: MiniCssExtractPlugin.loader,
  165. },
  166. {
  167. loader: 'css-loader',
  168. options: {
  169. importLoaders: 2,
  170. url: (_url, resourcePath) => {
  171. // only resolve URLs for dependencies
  172. return resourcePath.includes('node_modules');
  173. },
  174. }
  175. },
  176. {
  177. loader: 'postcss-loader',
  178. options: {
  179. plugins: () => [
  180. PostCSSPresetEnv(),
  181. ],
  182. },
  183. },
  184. {
  185. loader: 'less-loader',
  186. },
  187. ],
  188. },
  189. {
  190. test: /\.svg$/,
  191. use: [
  192. {
  193. loader: 'svg-sprite-loader',
  194. options: {
  195. extract: true,
  196. spriteFilename: 'img/svg/icons.svg',
  197. symbolId: (path) => {
  198. const {name} = parse(path);
  199. if (/@primer[/\\]octicons/.test(path)) {
  200. return `octicon-${name}`;
  201. }
  202. return name;
  203. },
  204. },
  205. },
  206. {
  207. loader: 'svgo-loader',
  208. },
  209. ],
  210. },
  211. {
  212. test: /\.(ttf|woff2?)$/,
  213. use: [
  214. {
  215. loader: 'file-loader',
  216. options: {
  217. name: '[name].[ext]',
  218. outputPath: 'fonts/',
  219. publicPath: (url) => `../fonts/${url}`, // seems required for monaco's font
  220. },
  221. },
  222. ],
  223. },
  224. ],
  225. },
  226. plugins: [
  227. new VueLoaderPlugin(),
  228. // avoid generating useless js output files for css- and svg-only chunks
  229. new FixStyleOnlyEntriesPlugin({
  230. extensions: ['less', 'scss', 'css', 'svg'],
  231. silent: true,
  232. }),
  233. new MiniCssExtractPlugin({
  234. filename: 'css/[name].css',
  235. chunkFilename: 'css/[name].css',
  236. }),
  237. //new SourceMapDevToolPlugin({
  238. //filename: 'js/[name].js.map',
  239. //include: [
  240. //'js/index.js',
  241. //],
  242. //}),
  243. new SpriteLoaderPlugin({
  244. plainSprite: true,
  245. }),
  246. new MonacoWebpackPlugin({
  247. filename: 'js/monaco-[name].worker.js',
  248. })
  249. ],
  250. performance: {
  251. hints: false,
  252. maxEntrypointSize: Infinity,
  253. maxAssetSize: Infinity,
  254. },
  255. resolve: {
  256. symlinks: false,
  257. alias: {
  258. vue$: 'vue/dist/vue.esm.js', // needed because vue's default export is the runtime only
  259. },
  260. extensions: ['.tsx', '.ts', '.js']
  261. },
  262. watchOptions: {
  263. ignored: [
  264. 'node_modules/**',
  265. ],
  266. },
  267. stats: {
  268. children: false,
  269. },
  270. };