Asper Header  1.0.14
The header injector extension
Loading...
Searching...
No Matches
esbuild.js
Go to the documentation of this file.
1
9import fs from "fs";
10import path from "path";
11import esbuild from "esbuild";
12import { copy } from "esbuild-plugin-copy";
13
14const logLevel = 'info'; // 'silent' | 'info' | 'warning' | 'error'
15
24function walk(dir, suffix) {
25 for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
26 const fullPath = path.join(dir, entry.name);
27 if (entry.isDirectory()) {
28 if (walk(fullPath, suffix)) {
29 return true;
30 }
31 } else if (fullPath.endsWith(suffix.replace("*", ""))) {
32 return true;
33 }
34 }
35 return false;
36}
37
52function patternExists(from) {
53 const patterns = Array.isArray(from) ? from : [from];
54
55 return patterns.some(pat => {
56 // Handle recursive glob like **/*.ext
57 if (pat.includes("**/")) {
58 const [baseDir, suffix] = pat.split("**/");
59 if (!fs.existsSync(baseDir)) {
60 return false;
61 }
62
63 let found = walk(baseDir, suffix);
64 return found;
65 }
66
67 // Handle simple wildcard patterns like *.json, *.txt
68 if (/[?*]/.test(pat)) {
69 const dir = path.dirname(pat);
70 if (!fs.existsSync(dir)) {
71 return false;
72 }
73
74 const basename = path.basename(pat);
75 const files = fs.readdirSync(dir);
76
77 // *.ext → check suffix
78 if (basename.startsWith("*.")) {
79 const ext = basename.slice(1);
80 const foundFile = files.some(f => f.endsWith(ext));
81 return foundFile;
82 }
83
84 // prefix* → check startsWith
85 if (basename.endsWith("*")) {
86 const prefix = basename.slice(0, -1);
87 const foundFile = files.some(f => f.startsWith(prefix));
88 return foundFile;
89 }
90
91 return false;
92 }
93
94 // Direct file path
95 return fs.existsSync(pat);
96 });
97}
98
107function checkedCopy(options) {
108 for (const asset of options.assets || []) {
109 if (!patternExists(asset.from)) {
110 throw new Error(
111 `❌ checkedCopy: No files matched pattern: ${asset.from}`
112 );
113 }
114 }
115 return copy(options);
116}
117
118const production = process.argv.includes('--production');
119const watch = process.argv.includes('--watch');
120
127function minifyJSON(src, dest) {
128 console.log(`Minifying ${src} to ${dest}`);
129 const data = JSON.parse(fs.readFileSync(src, "utf8"));
130 fs.writeFileSync(dest, JSON.stringify(data));
131}
132
133
134// Pre-minify known assets
135minifyJSON("assets/formatingRules/languages.json", "assets/formatingRules/languages.min.json");
136minifyJSON("assets/bonus/ditf.json", "assets/bonus/ditf.min.json");
137minifyJSON("assets/bonus/watermark.json", "assets/bonus/watermark.min.json");
138
139
145 name: 'esbuild-problem-matcher',
146
147 setup(build) {
148 build.onStart(() => {
149 console.log('[watch] build started');
150 });
151 build.onEnd((result) => {
152 if (result.errors.length > 0) {
153 result.errors.forEach(({ text, location }) => {
154 console.error(`✘ [ERROR] ${text}`);
155 if (location) {
156 console.error(` ${location.file}:${location.line}:${location.column}`);
157 }
158 });
159 }
160 console.log('[watch] build finished');
161 });
162 },
163};
164
171async function main() {
172 const ctx = await esbuild.context({
173 entryPoints: [
174 'src/extension.ts'
175 ],
176 bundle: true,
177 format: 'cjs',
178 minify: production,
179 sourcemap: !production,
180 sourcesContent: false,
181 platform: 'node',
182 outfile: 'dist/extension.js',
183 external: [
184 'vscode'//,
185 // 'jsonc-parser'
186 ],
188 plugins: [
189 /* add to the end of plugins array */
192 assets: [
193 {
194 from: ['./assets/formatingRules/languages.min.json'],
195 to: ['./assets/formatingRules/']
196 },
197 {
198 from: ["./assets/asciiArt/**/*.txt"],
199 to: ['./assets/asciiArt/'],
200 },
201 {
202 from: ['./assets/bonus/*.min.json'],
203 to: ['./assets/bonus/'],
204 }
205 ],
206 verbose: true,
207 }),
208 ],
209 });
210 if (watch) {
211 await ctx.watch();
212 } else {
213 await ctx.rebuild();
214 await ctx.dispose();
215 }
216}
217
218// Entrypoint
219main().catch(e => {
220 console.error(e);
221 process.exit(1);
222});
function walk(dir, suffix)
Definition esbuild.js:24
const watch
Definition esbuild.js:119
function patternExists(from)
Definition esbuild.js:52
function checkedCopy(options)
Definition esbuild.js:107
const logLevel
Definition esbuild.js:14
function async main()
Definition esbuild.js:171
function minifyJSON(src, dest)
Definition esbuild.js:127
const esbuildProblemMatcherPlugin
Definition esbuild.js:144
import esbuild from esbuild
Definition esbuild.js:11
import path from path
Definition esbuild.js:10
const production
Definition esbuild.js:118
import fs from fs
Definition esbuild.js:9