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.

206 line
5.4 KiB

  1. import * as esbuild from 'esbuild';
  2. import { sassPlugin } from 'esbuild-sass-plugin';
  3. import * as fs from 'fs';
  4. import * as path from 'path';
  5. import { minify } from 'terser';
  6. import { URL } from 'url';
  7. const __dirname = new URL('.', import.meta.url).pathname.substring(1);
  8. const args = process.argv.slice(2);
  9. let index = args.indexOf('--mode');
  10. const mode = (index >= 0 ? args[index + 1] : undefined) || 'none';
  11. const watch = args.includes('--watch');
  12. const check = !args.includes('--no-check');
  13. /**
  14. * @param { 'node' | 'webworker' } target
  15. * @param { 'production' | 'development' | 'none' } mode
  16. */
  17. async function buildExtension(target, mode) {
  18. let plugins = [];
  19. // let TypeCheckerPlugin;
  20. // if (check) {
  21. // ({ EsbuildPlugin: TypeCheckerPlugin } = require('vite-esbuild-typescript-checker'));
  22. // plugins.push(
  23. // TypeCheckerPlugin({
  24. // checker: {
  25. // async: false,
  26. // eslint: {
  27. // enabled: true,
  28. // files: 'src/**/*.ts',
  29. // options: {
  30. // // cache: true,
  31. // cacheLocation: path.join(
  32. // __dirname,
  33. // target === 'webworker' ? '.eslintcache.browser' : '.eslintcache',
  34. // ),
  35. // overrideConfigFile: path.join(
  36. // __dirname,
  37. // target === 'webworker' ? '.eslintrc.browser.json' : '.eslintrc.json',
  38. // ),
  39. // },
  40. // },
  41. // formatter: 'basic',
  42. // typescript: {
  43. // configFile: target === 'webworker' ? 'tsconfig.browser.json' : 'tsconfig.json',
  44. // },
  45. // },
  46. // }),
  47. // );
  48. // }
  49. const alias = {
  50. '@env': path.resolve(__dirname, 'src', 'env', target === 'webworker' ? 'browser' : target),
  51. // This dependency is very large, and isn't needed for our use-case
  52. tr46: path.resolve(__dirname, 'patches', 'tr46.js'),
  53. // Stupid dependency that is used by `http-proxy-agent`
  54. debug:
  55. target === 'webworker'
  56. ? path.resolve(__dirname, 'node_modules', 'debug', 'src', 'browser.js')
  57. : path.resolve(__dirname, 'node_modules', 'debug', 'src', 'node.js'),
  58. };
  59. if (target === 'webworker') {
  60. alias.path = 'path-browserify';
  61. alias.os = 'os-browserify/browser';
  62. }
  63. const out = target === 'webworker' ? 'dist/browser' : 'dist';
  64. const result = await esbuild.build({
  65. bundle: true,
  66. entryPoints: ['src/extension.ts'],
  67. entryNames: '[dir]/gitlens',
  68. alias: alias,
  69. drop: ['debugger'],
  70. external: ['vscode'],
  71. format: 'esm',
  72. keepNames: true,
  73. legalComments: 'none',
  74. logLevel: 'info',
  75. mainFields: target === 'webworker' ? ['browser', 'module', 'main'] : ['module', 'main'],
  76. metafile: true,
  77. minify: mode === 'production' ? true : false,
  78. outdir: out,
  79. platform: target === 'webworker' ? 'browser' : target,
  80. sourcemap: mode === 'production' ? false : true,
  81. // splitting: target === 'webworker' ? false : true,
  82. // chunkNames: 'feature-[name]-[hash]',
  83. target: ['es2022', 'chrome102', 'node16.14.2'],
  84. treeShaking: true,
  85. tsconfig: target === 'webworker' ? 'tsconfig.browser.json' : 'tsconfig.json',
  86. // watch: watch,
  87. plugins: plugins,
  88. });
  89. fs.writeFileSync(
  90. path.join('dist', 'meta', `gitlens${target === 'webworker' ? '.browser' : ''}.json`),
  91. JSON.stringify(result.metafile),
  92. );
  93. if (mode === 'production') {
  94. const file = path.join(out, 'gitlens.js');
  95. console.log(`Minifying ${file}...`);
  96. const code = fs.readFileSync(file, 'utf8');
  97. const result = await minify(code, {
  98. compress: {
  99. drop_debugger: true,
  100. ecma: 2020,
  101. module: true,
  102. },
  103. ecma: 2020,
  104. format: {
  105. comments: false,
  106. ecma: 2020,
  107. },
  108. // Keep the class names otherwise @log won't provide a useful name
  109. keep_classnames: true,
  110. module: true,
  111. });
  112. fs.writeFileSync(file, result.code);
  113. }
  114. }
  115. /**
  116. * @param { 'production' | 'development' | 'none' } mode
  117. */
  118. async function buildGraphWebview(mode) {
  119. let plugins = [sassPlugin()];
  120. const out = 'dist/webviews';
  121. const result = await esbuild.build({
  122. bundle: true,
  123. entryPoints: ['src/webviews/apps/plus/graph/graph.tsx'],
  124. entryNames: '[dir]/graph',
  125. alias: {
  126. '@env': path.resolve(__dirname, 'src', 'env', 'browser'),
  127. tslib: path.resolve(__dirname, 'node_modules/tslib/tslib.es6.js'),
  128. '@microsoft/fast-foundation': path.resolve(
  129. __dirname,
  130. 'node_modules/@microsoft/fast-foundation/dist/esm/index.js',
  131. ),
  132. '@microsoft/fast-react-wrapper': path.resolve(
  133. __dirname,
  134. 'node_modules/@microsoft/fast-react-wrapper/dist/esm/index.js',
  135. ),
  136. },
  137. drop: ['debugger'],
  138. external: ['vscode'],
  139. format: 'esm',
  140. legalComments: 'none',
  141. logLevel: 'info',
  142. mainFields: ['browser', 'module', 'main'],
  143. metafile: true,
  144. minify: mode === 'production' ? true : false,
  145. outdir: out,
  146. platform: 'browser',
  147. sourcemap: true,
  148. target: ['es2022', 'chrome102'],
  149. treeShaking: true,
  150. tsconfig: 'src/webviews/apps/tsconfig.json',
  151. // watch: watch,
  152. plugins: plugins,
  153. });
  154. fs.writeFileSync(path.join('dist', 'meta', 'graph.json'), JSON.stringify(result.metafile));
  155. if (mode === 'production') {
  156. const file = path.join(out, 'graph.js');
  157. console.log(`Minifying ${file}...`);
  158. const code = fs.readFileSync(file, 'utf8');
  159. const result = await minify(code, {
  160. compress: {
  161. drop_debugger: true,
  162. ecma: 2020,
  163. module: true,
  164. },
  165. ecma: 2020,
  166. format: {
  167. comments: false,
  168. ecma: 2020,
  169. },
  170. module: true,
  171. });
  172. fs.writeFileSync(file, result.code);
  173. }
  174. }
  175. try {
  176. await Promise.allSettled([
  177. buildExtension('node', mode),
  178. buildExtension('webworker', mode),
  179. buildGraphWebview(mode),
  180. ]);
  181. } catch (ex) {
  182. console.error(ex);
  183. process.exit(1);
  184. }