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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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: require.resolve('jquery-datetimepicker'),
  102. use: 'imports-loader?define=>false,exports=>false',
  103. },
  104. {
  105. test: /\.worker\.js$/,
  106. exclude: /monaco/,
  107. use: [
  108. {
  109. loader: 'worker-loader',
  110. options: {
  111. name: '[name].js',
  112. inline: true,
  113. fallback: false,
  114. },
  115. },
  116. ],
  117. },
  118. {
  119. test: /\.js$/,
  120. exclude: /node_modules/,
  121. use: [
  122. {
  123. loader: 'babel-loader',
  124. options: {
  125. cacheDirectory: true,
  126. cacheCompression: false,
  127. cacheIdentifier: [
  128. resolve(__dirname, 'package.json'),
  129. resolve(__dirname, 'package-lock.json'),
  130. resolve(__dirname, 'webpack.config.js'),
  131. ].map((path) => statSync(path).mtime.getTime()).join(':'),
  132. sourceMaps: true,
  133. presets: [
  134. [
  135. '@babel/preset-env',
  136. {
  137. useBuiltIns: 'usage',
  138. corejs: 3,
  139. },
  140. ],
  141. ],
  142. plugins: [
  143. [
  144. '@babel/plugin-transform-runtime',
  145. {
  146. regenerator: true,
  147. }
  148. ],
  149. '@babel/plugin-proposal-object-rest-spread',
  150. ],
  151. },
  152. },
  153. ],
  154. },
  155. {
  156. test: /\.(less|css)$/i,
  157. use: [
  158. {
  159. loader: MiniCssExtractPlugin.loader,
  160. },
  161. {
  162. loader: 'css-loader',
  163. options: {
  164. importLoaders: 2,
  165. url: (_url, resourcePath) => {
  166. // only resolve URLs for dependencies
  167. return resourcePath.includes('node_modules');
  168. },
  169. }
  170. },
  171. {
  172. loader: 'postcss-loader',
  173. options: {
  174. plugins: () => [
  175. PostCSSPresetEnv(),
  176. ],
  177. },
  178. },
  179. {
  180. loader: 'less-loader',
  181. },
  182. ],
  183. },
  184. {
  185. test: /\.svg$/,
  186. use: [
  187. {
  188. loader: 'svg-sprite-loader',
  189. options: {
  190. extract: true,
  191. spriteFilename: 'img/svg/icons.svg',
  192. symbolId: (path) => {
  193. const {name} = parse(path);
  194. if (/@primer[/\\]octicons/.test(path)) {
  195. return `octicon-${name}`;
  196. }
  197. return name;
  198. },
  199. },
  200. },
  201. {
  202. loader: 'svgo-loader',
  203. },
  204. ],
  205. },
  206. {
  207. test: /\.(ttf|woff2?)$/,
  208. use: [
  209. {
  210. loader: 'file-loader',
  211. options: {
  212. name: '[name].[ext]',
  213. outputPath: 'fonts/',
  214. publicPath: (url) => `../fonts/${url}`, // seems required for monaco's font
  215. },
  216. },
  217. ],
  218. },
  219. ],
  220. },
  221. plugins: [
  222. new VueLoaderPlugin(),
  223. // avoid generating useless js output files for css- and svg-only chunks
  224. new FixStyleOnlyEntriesPlugin({
  225. extensions: ['less', 'scss', 'css', 'svg'],
  226. silent: true,
  227. }),
  228. new MiniCssExtractPlugin({
  229. filename: 'css/[name].css',
  230. chunkFilename: 'css/[name].css',
  231. }),
  232. new SourceMapDevToolPlugin({
  233. filename: 'js/[name].js.map',
  234. include: [
  235. 'js/index.js',
  236. ],
  237. }),
  238. new SpriteLoaderPlugin({
  239. plainSprite: true,
  240. }),
  241. new MonacoWebpackPlugin({
  242. filename: 'js/monaco-[name].worker.js',
  243. }),
  244. ],
  245. performance: {
  246. hints: false,
  247. maxEntrypointSize: Infinity,
  248. maxAssetSize: Infinity,
  249. },
  250. resolve: {
  251. symlinks: false,
  252. alias: {
  253. vue$: 'vue/dist/vue.esm.js', // needed because vue's default export is the runtime only
  254. },
  255. },
  256. watchOptions: {
  257. ignored: [
  258. 'node_modules/**',
  259. ],
  260. },
  261. stats: {
  262. children: false,
  263. },
  264. };