-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.ts
416 lines (368 loc) · 16.9 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//original version from https://github.com/evanw/esbuild/blob/plugins/docs/plugin-examples.md
import { preprocess, compile, VERSION } from "svelte/compiler";
import { dirname, basename, relative } from "path";
import { promisify } from "util";
import { readFile, statSync } from "fs";
import { originalPositionFor, TraceMap } from "@jridgewell/trace-mapping";
import type { CompileOptions, ModuleCompileOptions, CompileResult } from "svelte/compiler";
import type { PreprocessorGroup } from "svelte/compiler";
import type { OnLoadResult, Plugin, PluginBuild, Location, PartialMessage } from "esbuild";
type Warning = CompileResult["warnings"][number];
interface esbuildSvelteOptions {
/**
* Svelte compiler options
*/
compilerOptions?: CompileOptions;
/**
* Svelte compiler options for module files (*.svelte.js and *.svelte.ts)
*/
moduleCompilerOptions?: ModuleCompileOptions;
/**
* The preprocessor(s) to run the Svelte code through before compiling
*/
preprocess?: PreprocessorGroup | PreprocessorGroup[];
/**
* Attempts to cache compiled files if the mtime of the file hasn't changed since last run.
*
*/
cache?: boolean;
/**
* The regex filter to use when filtering files to compile
* Defaults to `/\.svelte$/`
*/
include?: RegExp;
/**
* A function to filter out warnings
* Defaults to a constant function that returns `true`
*/
filterWarnings?: (warning: Warning) => boolean;
}
interface CacheData {
data: OnLoadResult;
// path, last modified time
dependencies: Map<string, Date>;
}
async function convertMessage(
{ message, start, end }: Warning,
filename: string,
source: string,
sourcemap: any,
): Promise<PartialMessage> {
let location: Partial<Location> | undefined;
if (start && end) {
let lineText = source.split(/\r\n|\r|\n/g)[start.line - 1];
let lineEnd = start.line === end.line ? end.column : lineText.length;
// Adjust the start and end positions based on what the preprocessors did so the positions are correct
if (sourcemap) {
sourcemap = new TraceMap(sourcemap);
const pos = originalPositionFor(sourcemap, {
line: start.line,
column: start.column,
});
if (pos.source) {
start.line = pos.line ?? start.line;
start.column = pos.column ?? start.column;
}
}
location = {
file: filename,
line: start.line,
column: start.column,
length: lineEnd - start.column,
lineText,
};
}
return { text: message, location };
}
//still support old incremental option if possible, but can still be overriden by cache option
const shouldCache = (
build: PluginBuild & {
initialOptions: {
incremental?: boolean;
watch?: boolean;
};
},
) => build.initialOptions?.incremental || build.initialOptions?.watch;
const SVELTE_VERSION = VERSION.split(".").map((v) => parseInt(v))[0];
const SVELTE_JAVASCRIPT_MODULE_FILTER = /\.svelte\.js$/;
const SVELTE_TYPESCRIPT_MODULE_FILTER = /\.svelte\.ts$/;
const SVELTE_MODULE_FILTER = new RegExp(
`(${SVELTE_JAVASCRIPT_MODULE_FILTER.source})|(${SVELTE_TYPESCRIPT_MODULE_FILTER.source})`,
);
const SVELTE_FILE_FILTER = /\.svelte$/;
const SVELTE_FILTER =
SVELTE_VERSION === 5
? new RegExp(`(${SVELTE_FILE_FILTER.source})|${SVELTE_MODULE_FILTER.source}`)
: SVELTE_FILE_FILTER;
const FAKE_CSS_FILTER = /\.esbuild-svelte-fake-css$/;
export default function sveltePlugin(options?: esbuildSvelteOptions): Plugin {
const svelteFilter = options?.include ?? SVELTE_FILTER;
return {
name: "esbuild-svelte",
setup(build) {
if (!options) {
options = {};
}
// see if we are incrementally building or watching for changes and enable the cache
// also checks if it has already been defined and ignores this if it has
if (options.cache == undefined && shouldCache(build)) {
options.cache = true;
}
// by default all warnings are enabled
if (options.filterWarnings == undefined) {
options.filterWarnings = () => true;
}
//Store generated css code for use in fake import
const cssCode = new Map<string, string>();
const fileCache = new Map<string, CacheData>();
//main loader
build.onLoad({ filter: svelteFilter }, async (args) => {
let cachedFile = null;
let previousWatchFiles: string[] = [];
// if told to use the cache, check if it contains the file,
// and if the modified time is not greater than the time when it was cached
// if so, return the cached data
if (options?.cache === true && fileCache.has(args.path)) {
cachedFile = fileCache.get(args.path) || {
dependencies: new Map(),
data: null,
}; // should never hit the null b/c of has check
let cacheValid = true;
//for each dependency check if the mtime is still valid
//if an exception is generated (file was deleted or something) then cache isn't valid
try {
cachedFile.dependencies.forEach((time, path) => {
if (statSync(path).mtime > time) {
cacheValid = false;
}
});
} catch {
cacheValid = false;
}
if (cacheValid) {
return cachedFile.data;
} else {
fileCache.delete(args.path); //can remove from cache if no longer valid
}
}
//reading files
let originalSource = await promisify(readFile)(args.path, "utf8");
let filename = relative(process.cwd(), args.path);
let source = originalSource;
if (SVELTE_TYPESCRIPT_MODULE_FILTER.test(filename)) {
let {
banner,
footer,
bundle,
splitting,
preserveSymlinks,
outfile,
metafile,
outdir,
outbase,
external,
packages,
alias,
resolveExtensions,
mainFields,
conditions,
write,
allowOverwrite,
tsconfig,
outExtension,
publicPath,
entryNames,
chunkNames,
assetNames,
inject,
entryPoints,
stdin,
plugins,
absWorkingDir,
nodePaths,
...transformOptions
} = {
...build.initialOptions,
};
try {
const result = await build.esbuild.transform(originalSource, {
...transformOptions,
loader: "ts",
});
source = result.code;
} catch (e: any) {
let result: OnLoadResult = {};
result.errors = [
await convertMessage(
e,
args.path,
originalSource,
options?.compilerOptions?.sourcemap,
),
];
// only provide if context API is supported or we are caching
if (build.esbuild?.context !== undefined || shouldCache(build)) {
result.watchFiles = previousWatchFiles;
}
return result;
}
}
//file modification time storage
const dependencyModifcationTimes = new Map<string, Date>();
dependencyModifcationTimes.set(args.path, statSync(args.path).mtime); // add the target file
let compilerOptions = {
css: "external" as "external",
...options?.compilerOptions,
};
let moduleCompilerOptions: ModuleCompileOptions = {
...options?.moduleCompilerOptions,
};
//actually compile file
try {
//do preprocessor stuff if it exists
if (options?.preprocess) {
let preprocessResult = null;
try {
preprocessResult = await preprocess(source, options.preprocess, {
filename,
});
} catch (e: any) {
// if preprocess failed there are chances that an external dependency caused exception
// to avoid stop watching those files, we keep the previous dependencies if available
if (cachedFile) {
previousWatchFiles = Array.from(cachedFile.dependencies.keys());
}
throw e;
}
if (preprocessResult.map) {
// normalize the sourcemap 'source' entrys to all match if they are the same file
// needed because of differing handling of file names in preprocessors
let fixedMap = preprocessResult.map as { sources: Array<string> };
for (let index = 0; index < fixedMap?.sources.length; index++) {
if (fixedMap.sources[index] == filename) {
fixedMap.sources[index] = basename(filename);
}
}
compilerOptions.sourcemap = fixedMap;
}
source = preprocessResult.code;
// if caching then we need to store the modifcation times for all dependencies
if (options?.cache === true) {
preprocessResult.dependencies?.forEach((entry) => {
dependencyModifcationTimes.set(entry, statSync(entry).mtime);
});
}
}
let { js, css, warnings } = await (async () => {
if (SVELTE_VERSION === 5 && SVELTE_MODULE_FILTER.test(filename)) {
const { compileModule } = await import("svelte/compiler");
return compileModule(source, {
...moduleCompilerOptions,
filename,
});
}
if (SVELTE_FILE_FILTER.test(filename)) {
return compile(source, {
...compilerOptions,
filename,
});
}
throw new Error(`Cannot compile ${filename}`);
})();
//esbuild doesn't seem to like sourcemaps without "sourcesContent" which Svelte doesn't provide
//so attempt to populate that array if we can find filename in sources
if (compilerOptions.sourcemap) {
if (js.map.sourcesContent == undefined) {
js.map.sourcesContent = [];
}
for (let index = 0; index < js.map.sources.length; index++) {
const element = js.map.sources[index];
if (element == basename(filename)) {
js.map.sourcesContent[index] = originalSource;
index = Infinity; //can break out of loop
}
}
}
let contents = js.code + `\n//# sourceMappingURL=` + js.map.toUrl();
//if svelte emits css seperately, then store it in a map and import it from the js
if (compilerOptions.css === "external" && css?.code) {
let cssPath = args.path
.replace(".svelte", ".esbuild-svelte-fake-css") //TODO append instead of replace to support different svelte filters
.replace(/\\/g, "/");
cssCode.set(
cssPath,
css.code + `/*# sourceMappingURL=${css.map.toUrl()} */`,
);
contents = contents + `\nimport "${cssPath}";`;
}
if (options?.filterWarnings) {
warnings = warnings.filter(options.filterWarnings);
}
const result: OnLoadResult = {
contents,
warnings: await Promise.all(
warnings.map(
async (e) =>
await convertMessage(
e,
args.path,
source,
compilerOptions.sourcemap,
),
),
),
};
// if we are told to cache, then cache
if (options?.cache === true) {
fileCache.set(args.path, {
data: result,
dependencies: dependencyModifcationTimes,
});
}
// make sure to tell esbuild to watch any additional files used if supported
// only provide if context API is supported or we are caching
if (build.esbuild?.context !== undefined || shouldCache(build)) {
result.watchFiles = Array.from(dependencyModifcationTimes.keys());
}
return result;
} catch (e: any) {
let result: OnLoadResult = {};
result.errors = [
await convertMessage(
e,
args.path,
originalSource,
compilerOptions.sourcemap,
),
];
// only provide if context API is supported or we are caching
if (build.esbuild?.context !== undefined || shouldCache(build)) {
result.watchFiles = previousWatchFiles;
}
return result;
}
});
//if the css exists in our map, then output it with the css loader
build.onResolve({ filter: FAKE_CSS_FILTER }, ({ path }) => {
return { path, namespace: "fakecss" };
});
build.onLoad({ filter: FAKE_CSS_FILTER, namespace: "fakecss" }, ({ path }) => {
const css = cssCode.get(path);
return css ? { contents: css, loader: "css", resolveDir: dirname(path) } : null;
});
// this enables the cache at the end of the build. The cache is disabled by default,
// but if this plugin instance is used agian, then the cache will be enabled (because
// we can be confident that the build is incremental or watch).
// This saves enabling caching on every build, which would be a performance hit but
// also makes sure incremental performance is increased.
build.onEnd(() => {
if (!options) {
options = {};
}
if (options.cache === undefined) {
options.cache = true;
}
});
},
};
}