|
| 1 | +// @ts-check |
| 2 | +/* eslint-disable @typescript-eslint/no-var-requires */ |
| 3 | +/* eslint-disable no-undef */ |
| 4 | +/* eslint-disable no-console */ |
| 5 | + |
| 6 | +const esbuild = require("esbuild"); |
| 7 | +const path = require("path"); |
| 8 | +const process = require("process"); |
| 9 | +const fs = require("fs"); |
| 10 | + |
| 11 | +const args = process.argv.slice(2); |
| 12 | +const prod = process.env.NODE_ENV === "production"; |
| 13 | + |
| 14 | +const entryPoints = [ |
| 15 | + "monaco-editor/esm/vs/language/json/json.worker.js", |
| 16 | + "monaco-editor/esm/vs/editor/editor.worker.js", |
| 17 | + "main.ts", |
| 18 | +]; |
| 19 | + |
| 20 | +if (args.includes("--clean") || args.includes("--rebuild")) { |
| 21 | + // remove dist folder |
| 22 | + try { |
| 23 | + fs.rmSync(path.join(__dirname, "dist"), { recursive: true, force: true }); |
| 24 | + } catch (err) { |
| 25 | + console.log(err); |
| 26 | + } |
| 27 | + if (!args.includes("--rebuild")) { |
| 28 | + process.exit(0); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +/**@type {import('esbuild').BuildOptions}*/ |
| 33 | +const BuildOptions = { |
| 34 | + /** |
| 35 | + * By default esbuild will not bundle the input files. Bundling must be |
| 36 | + * explicitly enabled. |
| 37 | + */ |
| 38 | + bundle: true, |
| 39 | + /** |
| 40 | + * This option controls the file names of the output files corresponding to |
| 41 | + * each input entry point file. |
| 42 | + */ |
| 43 | + entryNames: "[name].bundle", |
| 44 | + /** |
| 45 | + * This is an array of files that each serve as an input to the bundling |
| 46 | + * algorithm. |
| 47 | + */ |
| 48 | + entryPoints: entryPoints, |
| 49 | + /** |
| 50 | + * This sets the output format for the generated JavaScript files. We are |
| 51 | + * using th `iife`, witch format stands for "immediately-invoked function |
| 52 | + * expression". |
| 53 | + */ |
| 54 | + format: "iife", |
| 55 | + /** |
| 56 | + * This option changes how a given input file is interpreted. We use it to |
| 57 | + * copy assets. |
| 58 | + */ |
| 59 | + loader: { |
| 60 | + ".ttf": "file", |
| 61 | + }, |
| 62 | + /** |
| 63 | + * When enabled, the generated code will be minified instead of |
| 64 | + * pretty-printed. We enable this option on production builds. |
| 65 | + */ |
| 66 | + minify: prod, |
| 67 | + /** |
| 68 | + * This option sets the output directory for the build operation. |
| 69 | + */ |
| 70 | + outdir: path.join(__dirname, "dist"), |
| 71 | + /** |
| 72 | + * Configure esbuild's bundler to generate code intended for the browser. |
| 73 | + */ |
| 74 | + platform: "browser", |
| 75 | + /** |
| 76 | + * Generate source maps, witch can make it easier to debug code. |
| 77 | + */ |
| 78 | + sourcemap: true, |
| 79 | + /** |
| 80 | + * This sets the target environment for the generated JavaScript and/or CSS |
| 81 | + * code. The target can either be set to a JavaScript language version such |
| 82 | + * as es2020 or to a list of versions of individual engines ([chrome58, |
| 83 | + * firefox57, safari11, edge16]). |
| 84 | + */ |
| 85 | + target: "ES2015", |
| 86 | + /** |
| 87 | + * Enabling watch mode on the build API tells esbuild to listen for changes |
| 88 | + * on the file system and to rebuild whenever a file changes that could |
| 89 | + * invalidate the build. |
| 90 | + */ |
| 91 | + watch: args.includes("--watch") |
| 92 | +}; |
| 93 | + |
| 94 | +esbuild |
| 95 | + .build(BuildOptions) |
| 96 | + .catch(() => process.exit(1)) |
| 97 | + .then((result) => { |
| 98 | + if (result.errors.length > 0) { |
| 99 | + console.error(result.errors); |
| 100 | + } |
| 101 | + if (result.warnings.length > 0) { |
| 102 | + console.error(result.warnings); |
| 103 | + } |
| 104 | + }); |
0 commit comments