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 6.8KB

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