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

webpack.config.js 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
  6. const PostCSSPresetEnv = require('postcss-preset-env');
  7. const PostCSSSafeParser = require('postcss-safe-parser');
  8. const SpriteLoaderPlugin = require('svg-sprite-loader/plugin');
  9. const TerserPlugin = require('terser-webpack-plugin');
  10. const VueLoaderPlugin = require('vue-loader/lib/plugin');
  11. const { statSync } = require('fs');
  12. const { resolve, parse } = require('path');
  13. const { SourceMapDevToolPlugin } = require('webpack');
  14. const glob = (pattern) => fastGlob.sync(pattern, { cwd: __dirname, absolute: true });
  15. const themes = {};
  16. for (const path of glob('web_src/less/themes/*.less')) {
  17. themes[parse(path).name] = [path];
  18. }
  19. const isProduction = process.env.NODE_ENV !== 'development';
  20. module.exports = {
  21. mode: isProduction ? 'production' : 'development',
  22. entry: {
  23. index: [
  24. resolve(__dirname, 'web_src/js/index.js'),
  25. resolve(__dirname, 'web_src/less/index.less'),
  26. ],
  27. swagger: [
  28. resolve(__dirname, 'web_src/js/standalone/swagger.js'),
  29. ],
  30. jquery: [
  31. resolve(__dirname, 'web_src/js/jquery.js'),
  32. ],
  33. icons: glob('node_modules/@primer/octicons/build/svg/**/*.svg'),
  34. ...themes,
  35. },
  36. devtool: false,
  37. output: {
  38. path: resolve(__dirname, 'public'),
  39. filename: 'js/[name].js',
  40. chunkFilename: 'js/[name].js',
  41. },
  42. optimization: {
  43. minimize: isProduction,
  44. minimizer: [
  45. new TerserPlugin({
  46. sourceMap: true,
  47. extractComments: false,
  48. terserOptions: {
  49. output: {
  50. comments: false,
  51. },
  52. },
  53. }),
  54. new OptimizeCSSAssetsPlugin({
  55. cssProcessor: cssnano,
  56. cssProcessorOptions: {
  57. parser: PostCSSSafeParser,
  58. },
  59. cssProcessorPluginOptions: {
  60. preset: [
  61. 'default',
  62. {
  63. discardComments: {
  64. removeAll: true,
  65. },
  66. },
  67. ],
  68. },
  69. }),
  70. ],
  71. splitChunks: {
  72. chunks: 'async',
  73. name: (_, chunks) => chunks.map((item) => item.name).join('-'),
  74. }
  75. },
  76. module: {
  77. rules: [
  78. {
  79. test: /\.vue$/,
  80. exclude: /node_modules/,
  81. loader: 'vue-loader',
  82. },
  83. {
  84. test: /\.js$/,
  85. exclude: /node_modules/,
  86. use: [
  87. {
  88. loader: 'babel-loader',
  89. options: {
  90. cacheDirectory: true,
  91. cacheCompression: false,
  92. cacheIdentifier: [
  93. resolve(__dirname, 'package.json'),
  94. resolve(__dirname, 'package-lock.json'),
  95. resolve(__dirname, 'webpack.config.js'),
  96. ].map((path) => statSync(path).mtime.getTime()).join(':'),
  97. sourceMaps: true,
  98. presets: [
  99. [
  100. '@babel/preset-env',
  101. {
  102. useBuiltIns: 'usage',
  103. corejs: 3,
  104. },
  105. ],
  106. ],
  107. plugins: [
  108. [
  109. '@babel/plugin-transform-runtime',
  110. {
  111. regenerator: true,
  112. }
  113. ],
  114. '@babel/plugin-proposal-object-rest-spread',
  115. ],
  116. },
  117. },
  118. ],
  119. },
  120. {
  121. test: /\.(less|css)$/i,
  122. use: [
  123. {
  124. loader: MiniCssExtractPlugin.loader,
  125. },
  126. {
  127. loader: 'css-loader',
  128. options: {
  129. importLoaders: 2,
  130. url: false,
  131. }
  132. },
  133. {
  134. loader: 'postcss-loader',
  135. options: {
  136. plugins: () => [
  137. PostCSSPresetEnv(),
  138. ],
  139. },
  140. },
  141. {
  142. loader: 'less-loader',
  143. },
  144. ],
  145. },
  146. {
  147. test: /\.svg$/,
  148. use: [
  149. {
  150. loader: 'svg-sprite-loader',
  151. options: {
  152. extract: true,
  153. spriteFilename: 'img/svg/icons.svg',
  154. symbolId: (path) => {
  155. const { name } = parse(path);
  156. if (/@primer[/\\]octicons/.test(path)) {
  157. return `octicon-${name}`;
  158. }
  159. return name;
  160. },
  161. },
  162. },
  163. {
  164. loader: 'svgo-loader',
  165. },
  166. ],
  167. },
  168. ],
  169. },
  170. plugins: [
  171. new VueLoaderPlugin(),
  172. // avoid generating useless js output files for css- and svg-only chunks
  173. new FixStyleOnlyEntriesPlugin({
  174. extensions: ['less', 'scss', 'css', 'svg'],
  175. silent: true,
  176. }),
  177. new MiniCssExtractPlugin({
  178. filename: 'css/[name].css',
  179. chunkFilename: 'css/[name].css',
  180. }),
  181. new SourceMapDevToolPlugin({
  182. filename: 'js/[name].js.map',
  183. include: [
  184. 'js/index.js',
  185. ],
  186. }),
  187. new SpriteLoaderPlugin({
  188. plainSprite: true,
  189. }),
  190. ],
  191. performance: {
  192. hints: isProduction ? 'warning' : false,
  193. maxEntrypointSize: 512000,
  194. maxAssetSize: 512000,
  195. assetFilter: (filename) => {
  196. if (filename.endsWith('.map')) return false;
  197. if (['js/swagger.js', 'js/highlight.js'].includes(filename)) return false;
  198. return true;
  199. },
  200. },
  201. resolve: {
  202. symlinks: false,
  203. alias: {
  204. vue$: 'vue/dist/vue.esm.js', // needed because vue's default export is the runtime only
  205. },
  206. },
  207. watchOptions: {
  208. ignored: [
  209. 'node_modules/**',
  210. ],
  211. },
  212. };