gen-esbuild.browser.mjs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /**
  2. * Don't touch this file. It will be regenerated by theia build.
  3. * To customize the build process, change ./esbuild.mjs
  4. */
  5. import { nodeModulesPolyfillPlugin } from 'esbuild-plugins-node-modules-polyfill';
  6. import { copy } from 'esbuild-plugin-copy';
  7. import { compressAssetsPlugin, monacoNlsPlugin, problemMatcherPlugin, sourceMapPathsPlugin } from '@theia/bundle-plugin';
  8. import yargs from 'yargs';
  9. import { hideBin } from 'yargs/helpers';
  10. import resolvePackagePath from 'resolve-package-path';
  11. import path from 'node:path';
  12. import { fileURLToPath } from 'node:url';
  13. export const __dirname = path.dirname(fileURLToPath(import.meta.url));
  14. export function join(...parts) {
  15. return path.join(...parts).replace(/\\/g, '/');
  16. }
  17. const { mode, watch, staticCompression } = yargs(hideBin(process.argv)).option('mode', {
  18. description: "Mode to use",
  19. choices: ["development", "production"],
  20. default: "production"
  21. }).option('watch', {
  22. description: 'Controls whether to enable watch mode',
  23. type: 'boolean',
  24. default: false
  25. }).option('static-compression', {
  26. description: 'Controls whether to emit gzipped copies of the frontend assets. Disabled by default for Electron applications.',
  27. type: 'boolean'
  28. }).argv;
  29. const production = mode === 'production';
  30. const sourcemap = production ? false : 'linked';
  31. const minify = production;
  32. // Electron loads the frontend from the local backend over loopback, where gzip saves nothing
  33. // while the compressed copies still add to the size of the packaged application.
  34. const compression = staticCompression ?? false;
  35. export { mode, watch, sourcemap, minify, compression };
  36. /**
  37. * @type {Record<string, import('esbuild').Loader>}
  38. */
  39. export const loader = {
  40. '.css': 'css',
  41. '.ttf': 'dataurl',
  42. '.eot': 'dataurl',
  43. '.svg': 'dataurl',
  44. '.woff': 'dataurl',
  45. '.woff2': 'dataurl',
  46. '.jpg': 'dataurl',
  47. '.png': 'dataurl',
  48. '.gif': 'dataurl',
  49. '.wasm': 'dataurl',
  50. '.plist': 'dataurl',
  51. '.node': 'file'
  52. };
  53. /**
  54. * @type {import('esbuild').BuildOptions}
  55. */
  56. export const browserOptions = {
  57. entryPoints: {
  58. 'bundle': './src-gen/frontend/index.js',
  59. 'secondary-window': './src-gen/frontend/secondary-index.js',
  60. 'editor.worker': '@theia/monaco-editor-core/esm/vs/editor/common/services/editorWebWorkerMain.js',
  61. 'plugin-worker': '@theia/plugin-ext/lib/hosted/browser/worker/worker-main.js',
  62. },
  63. assetNames: '[name]',
  64. bundle: true,
  65. outdir: 'lib/frontend',
  66. platform: 'browser',
  67. // Support UMD libraries
  68. // but ensure that we also prioritize browser exports where possible
  69. mainFields: ['browser', 'module', 'main'],
  70. loader,
  71. minify,
  72. sourcemap,
  73. plugins: [
  74. problemMatcherPlugin(watch, 'browser'),
  75. monacoNlsPlugin(),
  76. nodeModulesPolyfillPlugin({
  77. globals: {
  78. Buffer: true,
  79. process: false
  80. }
  81. }),
  82. copy({
  83. assets: [
  84. {
  85. // copy secondary window html file to lib folder
  86. from: join(__dirname, 'src-gen/frontend/secondary-window.html'),
  87. to: join(__dirname, 'lib', 'frontend')
  88. },
  89. {
  90. // copy webview files to lib folder
  91. from: join(resolvePackagePath('@theia/plugin-ext', __dirname), '..', 'src', 'main', 'browser', 'webview', 'pre', '*'),
  92. to: join(__dirname, 'lib', 'webview', 'pre')
  93. },
  94. {
  95. // copy frontend plugin host files
  96. from: join(resolvePackagePath('@theia/plugin-ext-vscode', __dirname), '..', 'lib', 'node', 'context', 'plugin-vscode-init-fe.js'),
  97. to: join(__dirname, 'lib', 'frontend', 'context')
  98. }
  99. ]
  100. }),
  101. sourceMapPathsPlugin(),
  102. // Keep last: it compresses whatever the preceding plugins emitted.
  103. compressAssetsPlugin({ compress: compression })
  104. ]
  105. };