index.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. const HtmlWebpackInlineSourcePlugin = require('html-webpack-inline-source-plugin')
  2. const tauriConfig = require('../helpers/tauri-config')
  3. const WebpackShellPlugin = require('webpack-shell-plugin')
  4. const safeTap = (options, cb) => {
  5. if (options !== undefined) {
  6. cb()
  7. }
  8. return options
  9. }
  10. module.exports.chain = function (chain) {
  11. const cfg = tauriConfig({
  12. ctx: {
  13. debug: process.env.NODE_ENV !== 'production',
  14. prod: process.env.NODE_ENV === 'production'
  15. }
  16. })
  17. if (!cfg.tauri.embeddedServer.active) {
  18. chain.optimization.splitChunks({
  19. chunks: 'all',
  20. minSize: 0,
  21. maxSize: Infinity,
  22. maxAsyncRequests: 1,
  23. maxInitialRequests: 1,
  24. automaticNameDelimiter: '~',
  25. name: true,
  26. cacheGroups: {
  27. styles: {
  28. name: 'styles',
  29. chunks: 'all'
  30. },
  31. commons: {
  32. name: 'vendors',
  33. chunks: 'all'
  34. }
  35. }
  36. })
  37. chain.output.filename('js/app.js')
  38. if (cfg.ctx.prod) {
  39. if (cfg.build.extractCSS) {
  40. chain.plugin('mini-css-extract')
  41. .tap(options => {
  42. options[0].filename = 'css/app.css'
  43. return options
  44. })
  45. }
  46. chain.plugin('html-webpack')
  47. .tap(options => {
  48. options[0].inlineSource = '.(js|css)$'
  49. return options
  50. })
  51. chain.module.rule('babel')
  52. .use('babel-loader')
  53. .tap(options => safeTap(options, () => {
  54. options.plugins.push([
  55. 'system-import-transformer', { // needs constant attention
  56. modules: 'common'
  57. }
  58. ])
  59. }))
  60. }
  61. const modules = {
  62. images: 'url-loader',
  63. fonts: 'url-loader',
  64. media: 'url-loader'
  65. }
  66. for (const module in modules) {
  67. chain.module.rule(module)
  68. .use(modules[module])
  69. .tap(options => safeTap(options, () => {
  70. options.limit = undefined
  71. }))
  72. }
  73. }
  74. if (cfg.ctx.prod && !cfg.tauri.embeddedServer.active) {
  75. chain.plugin('html-webpack-inline-source')
  76. .use(HtmlWebpackInlineSourcePlugin)
  77. }
  78. if (cfg.tauri.automaticStart.active) {
  79. chain.plugin('webpack-shell-plugin')
  80. .use(WebpackShellPlugin, [{
  81. onBuildEnd: [
  82. cfg.ctx.prod
  83. ? `tauri build${cfg.tauri.automaticStart.buildArgs.join(' ')}`
  84. : `tauri dev${cfg.tauri.automaticStart.devArgs.join(' ')}`
  85. ]
  86. }])
  87. }
  88. }