1
0

build.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. const esbuild = require('esbuild');
  7. const path = require('path');
  8. const fs = require('fs');
  9. removeDir('dist', (entry) => /index.html$/.test(entry));
  10. const workerEntryPoints = [
  11. 'vs/language/json/json.worker.js',
  12. 'vs/language/css/css.worker.js',
  13. 'vs/language/html/html.worker.js',
  14. 'vs/language/typescript/ts.worker.js',
  15. 'vs/editor/editor.worker.js'
  16. ];
  17. build({
  18. entryPoints: workerEntryPoints.map((entry) => `../node_modules/monaco-editor/esm/${entry}`),
  19. bundle: true,
  20. format: 'iife',
  21. outbase: '../node_modules/monaco-editor/esm/',
  22. outdir: path.join(__dirname, 'dist')
  23. });
  24. build({
  25. entryPoints: ['index.js'],
  26. bundle: true,
  27. format: 'iife',
  28. outdir: path.join(__dirname, 'dist'),
  29. loader: {
  30. '.ttf': 'file'
  31. }
  32. });
  33. /**
  34. * @param {import ('esbuild').BuildOptions} opts
  35. */
  36. function build(opts) {
  37. esbuild.build(opts).then((result) => {
  38. if (result.errors.length > 0) {
  39. console.error(result.errors);
  40. }
  41. if (result.warnings.length > 0) {
  42. console.error(result.warnings);
  43. }
  44. });
  45. }
  46. /**
  47. * Remove a directory and all its contents.
  48. * @param {string} _dirPath
  49. * @param {(filename: string) => boolean} [keep]
  50. */
  51. function removeDir(_dirPath, keep) {
  52. if (typeof keep === 'undefined') {
  53. keep = () => false;
  54. }
  55. const dirPath = path.join(__dirname, _dirPath);
  56. if (!fs.existsSync(dirPath)) {
  57. return;
  58. }
  59. rmDir(dirPath, _dirPath);
  60. console.log(`Deleted ${_dirPath}`);
  61. /**
  62. * @param {string} dirPath
  63. * @param {string} relativeDirPath
  64. * @returns {boolean}
  65. */
  66. function rmDir(dirPath, relativeDirPath) {
  67. let keepsFiles = false;
  68. const entries = fs.readdirSync(dirPath);
  69. for (const entry of entries) {
  70. const filePath = path.join(dirPath, entry);
  71. const relativeFilePath = path.join(relativeDirPath, entry);
  72. if (keep(relativeFilePath)) {
  73. keepsFiles = true;
  74. continue;
  75. }
  76. if (fs.statSync(filePath).isFile()) {
  77. fs.unlinkSync(filePath);
  78. } else {
  79. keepsFiles = rmDir(filePath, relativeFilePath) || keepsFiles;
  80. }
  81. }
  82. if (!keepsFiles) {
  83. fs.rmdirSync(dirPath);
  84. }
  85. return keepsFiles;
  86. }
  87. }