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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. keep_fnames: /^(HTML|SVG)/, // https://github.com/fgnass/domino/issues/144
  50. output: {
  51. comments: false,
  52. },
  53. },
  54. }),
  55. new OptimizeCSSAssetsPlugin({
  56. cssProcessor: cssnano,
  57. cssProcessorOptions: {
  58. parser: PostCSSSafeParser,
  59. },
  60. cssProcessorPluginOptions: {
  61. preset: [
  62. 'default',
  63. {
  64. discardComments: {
  65. removeAll: true,
  66. },
  67. },
  68. ],
  69. },
  70. }),
  71. ],
  72. splitChunks: {
  73. chunks: 'async',
  74. name: (_, chunks) => chunks.map((item) => item.name).join('-'),
  75. }
  76. },
  77. module: {
  78. rules: [
  79. {
  80. test: /\.vue$/,
  81. exclude: /node_modules/,
  82. loader: 'vue-loader',
  83. },
  84. {
  85. test: require.resolve('jquery-datetimepicker'),
  86. use: 'imports-loader?define=>false,exports=>false',
  87. },
  88. {
  89. test: /\.worker\.js$/,
  90. use: [
  91. {
  92. loader: 'worker-loader',
  93. options: {
  94. name: '[name].js',
  95. inline: true,
  96. fallback: false,
  97. },
  98. },
  99. ],
  100. },
  101. {
  102. test: /\.js$/,
  103. exclude: /node_modules/,
  104. use: [
  105. {
  106. loader: 'babel-loader',
  107. options: {
  108. cacheDirectory: true,
  109. cacheCompression: false,
  110. cacheIdentifier: [
  111. resolve(__dirname, 'package.json'),
  112. resolve(__dirname, 'package-lock.json'),
  113. resolve(__dirname, 'webpack.config.js'),
  114. ].map((path) => statSync(path).mtime.getTime()).join(':'),
  115. sourceMaps: true,
  116. presets: [
  117. [
  118. '@babel/preset-env',
  119. {
  120. useBuiltIns: 'usage',
  121. corejs: 3,
  122. },
  123. ],
  124. ],
  125. plugins: [
  126. [
  127. '@babel/plugin-transform-runtime',
  128. {
  129. regenerator: true,
  130. }
  131. ],
  132. '@babel/plugin-proposal-object-rest-spread',
  133. ],
  134. },
  135. },
  136. ],
  137. },
  138. {
  139. test: /\.(less|css)$/i,
  140. use: [
  141. {
  142. loader: MiniCssExtractPlugin.loader,
  143. },
  144. {
  145. loader: 'css-loader',
  146. options: {
  147. importLoaders: 2,
  148. url: false,
  149. }
  150. },
  151. {
  152. loader: 'postcss-loader',
  153. options: {
  154. plugins: () => [
  155. PostCSSPresetEnv(),
  156. ],
  157. },
  158. },
  159. {
  160. loader: 'less-loader',
  161. },
  162. ],
  163. },
  164. {
  165. test: /\.svg$/,
  166. use: [
  167. {
  168. loader: 'svg-sprite-loader',
  169. options: {
  170. extract: true,
  171. spriteFilename: 'img/svg/icons.svg',
  172. symbolId: (path) => {
  173. const {name} = parse(path);
  174. if (/@primer[/\\]octicons/.test(path)) {
  175. return `octicon-${name}`;
  176. }
  177. return name;
  178. },
  179. },
  180. },
  181. {
  182. loader: 'svgo-loader',
  183. },
  184. ],
  185. },
  186. ],
  187. },
  188. plugins: [
  189. new VueLoaderPlugin(),
  190. // avoid generating useless js output files for css- and svg-only chunks
  191. new FixStyleOnlyEntriesPlugin({
  192. extensions: ['less', 'scss', 'css', 'svg'],
  193. silent: true,
  194. }),
  195. new MiniCssExtractPlugin({
  196. filename: 'css/[name].css',
  197. chunkFilename: 'css/[name].css',
  198. }),
  199. new SourceMapDevToolPlugin({
  200. filename: 'js/[name].js.map',
  201. include: [
  202. 'js/index.js',
  203. ],
  204. }),
  205. new SpriteLoaderPlugin({
  206. plainSprite: true,
  207. }),
  208. ],
  209. performance: {
  210. hints: false,
  211. },
  212. resolve: {
  213. symlinks: false,
  214. alias: {
  215. vue$: 'vue/dist/vue.esm.js', // needed because vue's default export is the runtime only
  216. },
  217. },
  218. watchOptions: {
  219. ignored: [
  220. 'node_modules/**',
  221. ],
  222. },
  223. };