webpack.plugin.config.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. mainFields: ['esm2015', 'browser', 'module', 'main'],
  46. },
  47. ignoreWarnings: [/Failed to parse source map/],
  48. module: {
  49. rules: [
  50. ...options.rules ?? [],
  51. {
  52. test: /\.js$/,
  53. enforce: 'pre',
  54. use: {
  55. loader: 'source-map-loader',
  56. options: {
  57. filterSourceMappingUrl: (url, resourcePath) => {
  58. if (/node_modules/.test(resourcePath)) {
  59. return false
  60. }
  61. return true
  62. },
  63. },
  64. },
  65. },
  66. {
  67. test: /\.ts$/,
  68. use: {
  69. loader: 'ts-loader',
  70. options: {
  71. configFile: path.resolve(options.dirname, 'tsconfig.json'),
  72. allowTsInNodeModules: true,
  73. },
  74. },
  75. },
  76. { test: /\.pug$/, use: ['apply-loader', 'pug-loader'] },
  77. { test: /\.scss$/, use: ['@tabby-gang/to-string-loader', 'css-loader', 'sass-loader'], include: /(theme.*|component)\.scss/ },
  78. { test: /\.scss$/, use: ['style-loader', 'css-loader', 'sass-loader'], exclude: /(theme.*|component)\.scss/ },
  79. { test: /\.css$/, use: ['@tabby-gang/to-string-loader', 'css-loader'], include: /component\.css/ },
  80. { test: /\.css$/, use: ['style-loader', 'css-loader'], exclude: /component\.css/ },
  81. { test: /\.yaml$/, use: ['json-loader', 'yaml-loader'] },
  82. { test: /\.svg/, use: ['svg-inline-loader'] },
  83. {
  84. test: /\.(ttf|eot|otf|woff|woff2|ogg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
  85. type: 'asset',
  86. },
  87. {
  88. test: /\.po$/,
  89. use: [
  90. { loader: 'json-loader' },
  91. { loader: 'po-gettext-loader' },
  92. ],
  93. },
  94. ],
  95. },
  96. externals: [
  97. '@electron/remote',
  98. '@serialport/bindings',
  99. '@serialport/bindings-cpp',
  100. 'any-promise',
  101. 'child_process',
  102. 'electron-promise-ipc',
  103. 'electron-updater',
  104. 'electron',
  105. 'fontmanager-redux',
  106. 'fs',
  107. 'keytar',
  108. 'macos-native-processlist',
  109. 'native-process-working-directory',
  110. 'net',
  111. 'ngx-toastr',
  112. 'os',
  113. 'path',
  114. 'readline',
  115. '@luminati-io/socksv5',
  116. 'stream',
  117. 'windows-native-registry',
  118. 'windows-process-tree',
  119. 'windows-process-tree/build/Release/windows_process_tree.node',
  120. /^@angular(?!\/common\/locales)/,
  121. /^@ng-bootstrap/,
  122. /^rxjs/,
  123. /^tabby-/,
  124. ...options.externals || [],
  125. ],
  126. plugins: [
  127. new SourceMapDevToolPlugin(sourceMapOptions),
  128. ],
  129. }
  130. if (process.env.PLUGIN_BUNDLE_ANALYZER === options.name) {
  131. config.plugins.push(bundleAnalyzer)
  132. config.cache = false
  133. }
  134. return config
  135. }