webpack.plugin.config.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. const path = require('path')
  2. const webpack = require('webpack')
  3. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  4. const bundleAnalyzer = new BundleAnalyzerPlugin({
  5. analyzerPort: 0,
  6. })
  7. module.exports = options => {
  8. const sourceMapOptions = {
  9. exclude: [/node_modules/, /vendor/],
  10. filename: '[file].map',
  11. moduleFilenameTemplate: `webpack-tabby-${options.name}:///[resource-path]`,
  12. }
  13. let SourceMapDevToolPlugin = webpack.SourceMapDevToolPlugin
  14. if (process.env.CI) {
  15. sourceMapOptions.append = '\n//# sourceMappingURL=../../../app.asar.unpacked/assets/webpack/[url]'
  16. }
  17. if (process.platform === 'win32' && process.env.TABBY_DEV) {
  18. SourceMapDevToolPlugin = webpack.EvalSourceMapDevToolPlugin
  19. }
  20. const isDev = !!process.env.TABBY_DEV
  21. const config = {
  22. target: 'node',
  23. entry: 'src/index.ts',
  24. context: options.dirname,
  25. devtool: false,
  26. output: {
  27. path: path.resolve(options.dirname, 'dist'),
  28. filename: 'index.js',
  29. pathinfo: true,
  30. libraryTarget: 'umd',
  31. publicPath: 'auto',
  32. },
  33. mode: isDev ? 'development' : 'production',
  34. optimization:{
  35. minimize: false,
  36. },
  37. cache: !isDev ? false : {
  38. type: 'filesystem',
  39. cacheDirectory: path.resolve(options.dirname, 'node_modules', '.webpack-cache'),
  40. },
  41. resolve: {
  42. alias: options.alias ?? {},
  43. modules: ['.', 'src', 'node_modules', '../app/node_modules', '../node_modules'].map(x => path.join(options.dirname, x)),
  44. extensions: ['.ts', '.js'],
  45. },
  46. ignoreWarnings: [/Failed to parse source map/],
  47. module: {
  48. rules: [
  49. ...options.rules ?? [],
  50. {
  51. test: /\.js$/,
  52. enforce: 'pre',
  53. use: {
  54. loader: 'source-map-loader',
  55. options: {
  56. filterSourceMappingUrl: (url, resourcePath) => {
  57. if (/node_modules/.test(resourcePath)) {
  58. return false
  59. }
  60. return true
  61. },
  62. },
  63. },
  64. },
  65. {
  66. test: /\.ts$/,
  67. use: {
  68. loader: 'ts-loader',
  69. options: {
  70. configFile: path.resolve(options.dirname, 'tsconfig.json'),
  71. allowTsInNodeModules: true,
  72. },
  73. },
  74. },
  75. { test: /\.pug$/, use: ['apply-loader', 'pug-loader'] },
  76. { test: /\.scss$/, use: ['@tabby-gang/to-string-loader', 'css-loader', 'sass-loader'], include: /(theme.*|component)\.scss/ },
  77. { test: /\.scss$/, use: ['style-loader', 'css-loader', 'sass-loader'], exclude: /(theme.*|component)\.scss/ },
  78. { test: /\.css$/, use: ['@tabby-gang/to-string-loader', 'css-loader'], include: /component\.css/ },
  79. { test: /\.css$/, use: ['style-loader', 'css-loader'], exclude: /component\.css/ },
  80. { test: /\.yaml$/, use: ['json-loader', 'yaml-loader'] },
  81. { test: /\.svg/, use: ['svg-inline-loader'] },
  82. {
  83. test: /\.(ttf|eot|otf|woff|woff2|ogg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
  84. type: 'asset',
  85. },
  86. ],
  87. },
  88. externals: [
  89. '@electron/remote',
  90. '@serialport/bindings',
  91. 'any-promise',
  92. 'child_process',
  93. 'electron-promise-ipc',
  94. 'electron-updater',
  95. 'electron',
  96. 'fontmanager-redux',
  97. 'fs',
  98. 'keytar',
  99. 'macos-native-processlist',
  100. 'native-process-working-directory',
  101. 'net',
  102. 'ngx-toastr',
  103. 'os',
  104. 'path',
  105. 'readline',
  106. '@luminati-io/socksv5',
  107. 'stream',
  108. 'windows-native-registry',
  109. 'windows-process-tree',
  110. 'windows-process-tree/build/Release/windows_process_tree.node',
  111. /^@angular/,
  112. /^@ng-bootstrap/,
  113. /^rxjs/,
  114. /^tabby-/,
  115. ...options.externals || [],
  116. ],
  117. plugins: [
  118. new SourceMapDevToolPlugin(sourceMapOptions),
  119. ],
  120. }
  121. if (process.env.PLUGIN_BUNDLE_ANALYZER === options.name) {
  122. config.plugins.push(bundleAnalyzer)
  123. config.cache = false
  124. }
  125. return config
  126. }