webpack.plugin.config.mjs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import * as fs from 'fs'
  2. import * as path from 'path'
  3. import wp from 'webpack'
  4. import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer'
  5. import { AngularWebpackPlugin } from '@ngtools/webpack'
  6. const bundleAnalyzer = new BundleAnalyzerPlugin({
  7. analyzerPort: 0,
  8. })
  9. import { createEs2015LinkerPlugin } from '@angular/compiler-cli/linker/babel'
  10. const linkerPlugin = createEs2015LinkerPlugin({
  11. linkerJitMode: true,
  12. fileSystem: {
  13. resolve: path.resolve,
  14. exists: fs.existsSync,
  15. dirname: path.dirname,
  16. relative: path.relative,
  17. readFile: fs.readFileSync,
  18. },
  19. })
  20. export default options => {
  21. const sourceMapOptions = {
  22. exclude: [/node_modules/, /vendor/],
  23. filename: '[file].map',
  24. moduleFilenameTemplate: `webpack-tabby-${options.name}:///[resource-path]`,
  25. }
  26. let devtoolPlugin = wp.SourceMapDevToolPlugin
  27. if (process.env.CI) {
  28. sourceMapOptions.append = '\n//# sourceMappingURL=../../../app.asar.unpacked/assets/webpack/[url]'
  29. }
  30. if ((process.platform === 'win32' || process.platform === 'linux') && process.env.TABBY_DEV) {
  31. devtoolPlugin = wp.EvalSourceMapDevToolPlugin
  32. }
  33. const isDev = !!process.env.TABBY_DEV
  34. const config = {
  35. target: 'node',
  36. entry: 'src/index.ts',
  37. context: options.dirname,
  38. devtool: false,
  39. output: {
  40. path: path.resolve(options.dirname, 'dist'),
  41. filename: 'index.js',
  42. pathinfo: true,
  43. libraryTarget: 'umd',
  44. publicPath: 'auto',
  45. },
  46. mode: isDev ? 'development' : 'production',
  47. optimization:{
  48. minimize: false,
  49. },
  50. cache: !isDev ? false : {
  51. type: 'filesystem',
  52. cacheDirectory: path.resolve(options.dirname, 'node_modules', '.webpack-cache'),
  53. },
  54. resolve: {
  55. alias: options.alias ?? {},
  56. modules: ['.', 'src', 'node_modules', '../app/node_modules', '../node_modules'].map(x => path.join(options.dirname, x)),
  57. extensions: ['.ts', '.js'],
  58. mainFields: ['esm2015', 'browser', 'module', 'main'],
  59. },
  60. ignoreWarnings: [/Failed to parse source map/],
  61. module: {
  62. rules: [
  63. ...options.rules ?? [],
  64. {
  65. test: /\.js$/,
  66. enforce: 'pre',
  67. use: {
  68. loader: 'source-map-loader',
  69. options: {
  70. filterSourceMappingUrl: (url, resourcePath) => {
  71. if (/node_modules/.test(resourcePath) && !resourcePath.includes('xterm')) {
  72. return false
  73. }
  74. return true
  75. },
  76. },
  77. },
  78. },
  79. {
  80. test: /\.(m?)js$/,
  81. loader: 'babel-loader',
  82. options: {
  83. plugins: [linkerPlugin],
  84. compact: false,
  85. cacheDirectory: true,
  86. },
  87. resolve: {
  88. fullySpecified: false,
  89. },
  90. },
  91. {
  92. test: /\.ts$/,
  93. use: [
  94. {
  95. loader: '@ngtools/webpack',
  96. },
  97. ],
  98. },
  99. {
  100. test: /\.pug$/,
  101. use: [
  102. 'apply-loader',
  103. {
  104. loader: 'pug-loader',
  105. options: {
  106. pretty: true,
  107. },
  108. },
  109. ],
  110. },
  111. { test: /\.scss$/, use: ['@tabby-gang/to-string-loader', 'css-loader', 'sass-loader'], include: /(theme.*|component)\.scss/ },
  112. { test: /\.scss$/, use: ['style-loader', 'css-loader', 'sass-loader'], exclude: /(theme.*|component)\.scss/ },
  113. { test: /\.css$/, use: ['@tabby-gang/to-string-loader', 'css-loader'], include: /component\.css/ },
  114. { test: /\.css$/, use: ['style-loader', 'css-loader'], exclude: /component\.css/ },
  115. { test: /\.yaml$/, use: ['yaml-loader'] },
  116. { test: /\.svg/, use: ['svg-inline-loader'] },
  117. {
  118. test: /\.(eot|otf|woff|woff2|ogg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
  119. type: 'asset',
  120. },
  121. {
  122. test: /\.ttf$/,
  123. type: 'asset/inline',
  124. },
  125. {
  126. test: /\.po$/,
  127. use: [
  128. { loader: 'json-loader' },
  129. { loader: 'po-gettext-loader' },
  130. ],
  131. },
  132. ],
  133. },
  134. externals: [
  135. '@electron/remote',
  136. '@serialport/bindings',
  137. '@serialport/bindings-cpp',
  138. 'any-promise',
  139. 'child_process',
  140. 'electron-promise-ipc',
  141. 'electron-updater',
  142. 'electron',
  143. 'fontmanager-redux',
  144. 'fs',
  145. 'keytar',
  146. 'macos-native-processlist',
  147. 'native-process-working-directory',
  148. 'net',
  149. 'ngx-toastr',
  150. 'os',
  151. 'path',
  152. 'readline',
  153. 'russh',
  154. '@luminati-io/socksv5',
  155. 'stream',
  156. 'windows-native-registry',
  157. '@tabby-gang/windows-process-tree',
  158. '@tabby-gang/windows-process-tree/build/Release/windows_process_tree.node',
  159. /^@angular(?!\/common\/locales)/,
  160. /^@ng-bootstrap/,
  161. /^rxjs/,
  162. /^tabby-/,
  163. ...options.externals || [],
  164. ],
  165. plugins: [
  166. new devtoolPlugin(sourceMapOptions),
  167. new AngularWebpackPlugin({
  168. tsconfig: path.resolve(options.dirname, 'tsconfig.json'),
  169. directTemplateLoading: false,
  170. jitMode: true,
  171. })
  172. ],
  173. }
  174. if (process.env.PLUGIN_BUNDLE_ANALYZER === options.name) {
  175. config.plugins.push(bundleAnalyzer)
  176. config.cache = false
  177. }
  178. return config
  179. }