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

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