gen-esbuild.browser.mjs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. Enabled by default in production mode.',
  27. type: 'boolean'
  28. }).argv;
  29. const production = mode === 'production';
  30. const sourcemap = production ? false : 'linked';
  31. const minify = production;
  32. // Compressing on every rebuild would slow down watch mode, so this only pays off for production builds.
  33. const compression = staticCompression ?? production;
  34. export { mode, watch, sourcemap, minify, compression };
  35. /**
  36. * @type {Record<string, import('esbuild').Loader>}
  37. */
  38. export const loader = {
  39. '.css': 'css',
  40. '.ttf': 'dataurl',
  41. '.eot': 'dataurl',
  42. '.svg': 'dataurl',
  43. '.woff': 'dataurl',
  44. '.woff2': 'dataurl',
  45. '.jpg': 'dataurl',
  46. '.png': 'dataurl',
  47. '.gif': 'dataurl',
  48. '.wasm': 'dataurl',
  49. '.plist': 'dataurl',
  50. '.node': 'file'
  51. };
  52. /**
  53. * @type {import('esbuild').BuildOptions}
  54. */
  55. export const browserOptions = {
  56. entryPoints: {
  57. 'bundle': './src-gen/frontend/index.js',
  58. 'secondary-window': './src-gen/frontend/secondary-index.js',
  59. 'editor.worker': '@theia/monaco-editor-core/esm/vs/editor/common/services/editorWebWorkerMain.js',
  60. 'plugin-worker': '@theia/plugin-ext/lib/hosted/browser/worker/worker-main.js',
  61. },
  62. assetNames: '[name]',
  63. bundle: true,
  64. outdir: 'lib/frontend',
  65. platform: 'browser',
  66. // Support UMD libraries
  67. // but ensure that we also prioritize browser exports where possible
  68. mainFields: ['browser', 'module', 'main'],
  69. loader,
  70. minify,
  71. sourcemap,
  72. plugins: [
  73. problemMatcherPlugin(watch, 'browser'),
  74. monacoNlsPlugin(),
  75. nodeModulesPolyfillPlugin({
  76. globals: {
  77. Buffer: true,
  78. process: false
  79. }
  80. }),
  81. copy({
  82. assets: [
  83. {
  84. // copy secondary window html file to lib folder
  85. from: join(__dirname, 'src-gen/frontend/secondary-window.html'),
  86. to: join(__dirname, 'lib', 'frontend')
  87. },
  88. {
  89. // copy webview files to lib folder
  90. from: join(resolvePackagePath('@theia/plugin-ext', __dirname), '..', 'src', 'main', 'browser', 'webview', 'pre', '*'),
  91. to: join(__dirname, 'lib', 'webview', 'pre')
  92. },
  93. {
  94. // copy frontend plugin host files
  95. from: join(resolvePackagePath('@theia/plugin-ext-vscode', __dirname), '..', 'lib', 'node', 'context', 'plugin-vscode-init-fe.js'),
  96. to: join(__dirname, 'lib', 'frontend', 'context')
  97. }
  98. ]
  99. }),
  100. sourceMapPathsPlugin(),
  101. // Keep last: it compresses whatever the preceding plugins emitted.
  102. compressAssetsPlugin({ compress: compression })
  103. ]
  104. };