esbuild-webview-common.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. // @ts-check
  6. /**
  7. * @fileoverview Common build script for extension scripts used in in webviews.
  8. */
  9. const path = require('path');
  10. const esbuild = require('esbuild');
  11. /**
  12. * @typedef {Partial<import('esbuild').BuildOptions> & {
  13. * entryPoints: string[] | Record<string, string> | { in: string, out: string }[];
  14. * outdir: string;
  15. * }} BuildOptions
  16. */
  17. /**
  18. * Build the source code once using esbuild.
  19. *
  20. * @param {BuildOptions} options
  21. * @param {(outDir: string) => unknown} [didBuild]
  22. */
  23. async function build(options, didBuild) {
  24. await esbuild.build({
  25. bundle: true,
  26. minify: true,
  27. sourcemap: false,
  28. format: 'esm',
  29. platform: 'browser',
  30. target: ['es2020'],
  31. ...options,
  32. });
  33. await didBuild?.(options.outdir);
  34. }
  35. /**
  36. * Build the source code once using esbuild, logging errors instead of throwing.
  37. *
  38. * @param {BuildOptions} options
  39. * @param {(outDir: string) => unknown} [didBuild]
  40. */
  41. async function tryBuild(options, didBuild) {
  42. try {
  43. await build(options, didBuild);
  44. } catch (err) {
  45. console.error(err);
  46. }
  47. }
  48. /**
  49. * @param {{
  50. * srcDir: string;
  51. * outdir: string;
  52. * entryPoints: string[] | Record<string, string> | { in: string, out: string }[];
  53. * additionalOptions?: Partial<import('esbuild').BuildOptions>
  54. * }} config
  55. * @param {string[]} args
  56. * @param {(outDir: string) => unknown} [didBuild]
  57. */
  58. module.exports.run = async function (config, args, didBuild) {
  59. let outdir = config.outdir;
  60. const outputRootIndex = args.indexOf('--outputRoot');
  61. if (outputRootIndex >= 0) {
  62. const outputRoot = args[outputRootIndex + 1];
  63. const outputDirName = path.basename(outdir);
  64. outdir = path.join(outputRoot, outputDirName);
  65. }
  66. /** @type {BuildOptions} */
  67. const resolvedOptions = {
  68. entryPoints: config.entryPoints,
  69. outdir,
  70. ...(config.additionalOptions || {}),
  71. };
  72. const isWatch = args.indexOf('--watch') >= 0;
  73. if (isWatch) {
  74. await tryBuild(resolvedOptions, didBuild);
  75. const watcher = require('@parcel/watcher');
  76. watcher.subscribe(config.srcDir, () => tryBuild(resolvedOptions, didBuild));
  77. } else {
  78. return build(resolvedOptions, didBuild).catch(() => process.exit(1));
  79. }
  80. };