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

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