-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
539 lines (501 loc) · 17.3 KB
/
index.js
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
import { spawn } from "node:child_process";
import { fileURLToPath } from "node:url";
import { promises as fs } from "node:fs";
import path from "node:path";
import { JSONRPCEndpoint } from "ts-lsp-client";
import { normalizePath } from "vite";
import { transform } from "esbuild";
import { filter, map, bufferTime, Subject } from "rxjs";
import colors from "picocolors";
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers
import withResolvers from "promise.withresolvers";
import { codeFrameColumns } from "@babel/code-frame";
withResolvers.shim();
const fsharpFileRegex = /\.(fs|fsx)$/;
const currentDir = path.dirname(fileURLToPath(import.meta.url));
const fableDaemon = path.join(currentDir, "bin/Fable.Daemon.dll");
if (process.env.VITE_PLUGIN_FABLE_DEBUG) {
console.log(
`Running daemon in debug mode, visit http://localhost:9014 to view logs`,
);
}
/**
* @typedef {Object} PluginOptions
* @property {string} [fsproj] - The main fsproj to load
* @property {'transform' | 'preserve' | 'automatic' | null} [jsx] - Apply JSX transformation after Fable compilation: https://esbuild.github.io/api/#transformation
* @property {Boolean} [noReflection] - Pass noReflection value to Fable.Compiler
* @property {string[]} [exclude] - Pass exclude to Fable.Compiler
*/
/** @type {PluginOptions} */
const defaultConfig = { jsx: null, noReflection: false, exclude: [] };
/**
* @function
* @param {PluginOptions} userConfig - The options for configuring the plugin.
* @description Initializes and returns a Vite plugin for to process the incoming F# project.
* @returns {import('vite').Plugin} A Vite plugin object with the standard structure and hooks.
*/
export default function fablePlugin(userConfig) {
/** @type {import("./types.js").PluginState} */
const state = {
config: Object.assign({}, defaultConfig, userConfig),
compilableFiles: new Map(),
sourceFiles: new Set(),
fsproj: null,
configuration: "Debug",
dependentFiles: new Set(),
// @ts-ignore
logger: { info: console.log, warn: console.warn, error: console.error },
dotnetProcess: null,
endpoint: null,
pendingChanges: null,
hotPromiseWithResolvers: null,
isBuild: false,
};
/** @type {Subject<import("./types.js").HookEvent>} **/
const pendingChangesSubject = new Subject();
/**
* @param {String} prefix
* @param {String} message
*/
function logDebug(prefix, message) {
state.logger.info(colors.dim(`[fable]: ${prefix}: ${message}`), {
timestamp: true,
});
}
/**
* @param {String} prefix
* @param {String} message
*/
function logInfo(prefix, message) {
state.logger.info(colors.green(`[fable]: ${prefix}: ${message}`), {
timestamp: true,
});
}
/**
* @param {String} prefix
* @param {String} message
*/
function logWarn(prefix, message) {
state.logger.warn(colors.yellow(`[fable]: ${prefix}: ${message}`), {
timestamp: true,
});
}
/**
* @param {String} prefix
* @param {String} message
*/
function logError(prefix, message) {
state.logger.warn(colors.red(`[fable] ${prefix}: ${message}`), {
timestamp: true,
});
}
/**
* @param {String} prefix
* @param {String} message
*/
function logCritical(prefix, message) {
state.logger.error(colors.red(`[fable] ${prefix}: ${message}`), {
timestamp: true,
});
}
/**
@param {string} configDir - Folder path of the vite.config.js file.
*/
async function findFsProjFile(configDir) {
const files = await fs.readdir(configDir);
const fsprojFiles = files
.filter((file) => file && file.toLocaleLowerCase().endsWith(".fsproj"))
.map((fsProjFile) => {
// Return the full path of the .fsproj file
return normalizePath(path.join(configDir, fsProjFile));
});
return fsprojFiles.length > 0 ? fsprojFiles[0] : null;
}
/**
@returns {Promise<string>}
*/
async function getFableLibrary() {
const fableLibraryInOwnNodeModules = path.join(
currentDir,
"node_modules/@fable-org/fable-library-js",
);
try {
await fs.access(fableLibraryInOwnNodeModules, fs.constants.F_OK);
return normalizePath(fableLibraryInOwnNodeModules);
} catch (e) {
return normalizePath(
path.join(currentDir, "../@fable-org/fable-library-js"),
);
}
}
/**
* Retrieves the project file. At this stage the project is type-checked but Fable did not compile anything.
* @param {string} fableLibrary - Location of the fable-library node module.
* @returns {Promise<import("./types.js").ProjectFileData>} A promise that resolves to an object containing the project options and compiled files.
* @throws {Error} If the result from the endpoint is not a success case.
*/
async function getProjectFile(fableLibrary) {
/** @type {import("./types.js").FSharpDiscriminatedUnion} */
const result = await state.endpoint.send("fable/project-changed", {
configuration: state.configuration,
project: state.fsproj,
fableLibrary,
exclude: state.config.exclude,
noReflection: state.config.noReflection,
});
if (result.case === "Success") {
return {
sourceFiles: result.fields[0],
diagnostics: result.fields[1],
dependentFiles: result.fields[2],
};
} else {
throw new Error(result.fields[0] || "Unknown error occurred");
}
}
/**
* Try and compile the entire project using Fable. The daemon contains all the information at this point to do this.
* No need to pass any additional info.
* @returns {Promise<Map<string, string>>} A promise that resolves a map of compiled files.
* @throws {Error} If the result from the endpoint is not a success case.
*/
async function tryInitialCompile() {
/** @type {import("./types.js").FSharpDiscriminatedUnion} */
const result = await state.endpoint.send("fable/initial-compile");
if (result.case === "Success") {
return result.fields[0];
} else {
throw new Error(result.fields[0] || "Unknown error occurred");
}
}
/**
* @function
* @param {import("./types.js").Diagnostic} diagnostic
* @returns {string}
*/
function formatDiagnostic(diagnostic) {
return `${diagnostic.severity.toUpperCase()} ${diagnostic.errorNumberText}: ${diagnostic.message} ${diagnostic.fileName} (${diagnostic.range.startLine},${diagnostic.range.startColumn}) (${diagnostic.range.endLine},${diagnostic.range.endColumn})`;
}
/**
* @function
* @param {import("./types.js").Diagnostic[]} diagnostics - An array of Diagnostic objects to be logged.
*/
function logDiagnostics(diagnostics) {
for (const diagnostic of diagnostics) {
switch (diagnostic.severity.toLowerCase()) {
case "error":
logError("", formatDiagnostic(diagnostic));
break;
case "warning":
logWarn("", formatDiagnostic(diagnostic));
break;
default:
logInfo("", formatDiagnostic(diagnostic));
break;
}
}
}
/**
* Does a type-check and compilation of the state.fsproj
* @function
* @param {function} addWatchFile
* @returns {Promise}
*/
async function compileProject(addWatchFile) {
logInfo("compileProject", `Full compile started of ${state.fsproj}`);
const fableLibrary = await getFableLibrary();
logDebug("compileProject", `fable-library located at ${fableLibrary}`);
logInfo("compileProject", `about to type-checked ${state.fsproj}.`);
const projectResponse = await getProjectFile(fableLibrary);
logInfo("compileProject", `${state.fsproj} was type-checked.`);
logDiagnostics(projectResponse.diagnostics);
for (const sf of projectResponse.sourceFiles) {
state.sourceFiles.add(normalizePath(sf));
}
for (let dependentFile of projectResponse.dependentFiles) {
dependentFile = normalizePath(dependentFile);
state.dependentFiles.add(dependentFile);
addWatchFile(dependentFile);
}
const compiledFSharpFiles = await tryInitialCompile();
logInfo("compileProject", `Full compile completed of ${state.fsproj}`);
state.sourceFiles.forEach((file) => {
addWatchFile(file);
const normalizedFileName = normalizePath(file);
state.compilableFiles.set(normalizedFileName, compiledFSharpFiles[file]);
});
}
/**
* Either the project or a dependent file changed
* @returns {Promise<void>}
* @param {function} addWatchFile
* @param {Set<String>} projectFiles
*/
async function projectChanged(addWatchFile, projectFiles) {
try {
logInfo(
"projectChanged",
`dependent file ${Array.from(projectFiles).join("\n")} changed.`,
);
state.sourceFiles.clear();
state.compilableFiles.clear();
state.dependentFiles.clear();
await compileProject(addWatchFile);
} catch (e) {
logCritical(
"projectChanged",
`Unexpected failure during projectChanged for ${Array.from(projectFiles)},\n${e}`,
);
}
}
/**
* F# files part of state.compilableFiles have changed.
* @returns {Promise<import("./types.js").Diagnostic[]>}
* @param {String[]} files
*/
async function fsharpFileChanged(files) {
try {
/** @type {import("./types.js").FSharpDiscriminatedUnion} */
const compilationResult = await state.endpoint.send("fable/compile", {
fileNames: files,
});
if (
compilationResult.case === "Success" &&
compilationResult.fields &&
compilationResult.fields.length > 0
) {
const compiledFSharpFiles = compilationResult.fields[0];
logDebug(
"fsharpFileChanged",
`\n${Object.keys(compiledFSharpFiles).join("\n")} compiled`,
);
for (const [key, value] of Object.entries(compiledFSharpFiles)) {
const normalizedFileName = normalizePath(key);
state.compilableFiles.set(normalizedFileName, value);
}
const diagnostics = compilationResult.fields[1];
logDiagnostics(diagnostics);
return diagnostics;
} else {
logError(
"watchChange",
`compilation of ${files} failed, ${compilationResult.fields[0]}`,
);
return [];
}
} catch (e) {
logCritical(
"watchChange",
`compilation of ${files} failed, plugin could not handle this gracefully. ${e}`,
);
return [];
}
}
/**
* @param {import("./types.js").PendingChangesState} acc
* @param {import("./types.js").HookEvent} e
* @return {import("./types.js").PendingChangesState}
*/
function reducePendingChange(acc, e) {
if (e.type === "FSharpFileChanged") {
return {
projectChanged: acc.projectChanged,
fsharpFiles: acc.fsharpFiles.add(e.file),
projectFiles: acc.projectFiles,
};
} else if (e.type === "ProjectFileChanged") {
return {
projectChanged: true,
fsharpFiles: acc.fsharpFiles,
projectFiles: acc.projectFiles.add(e.file),
};
} else {
logWarn("pendingChanges", `Unexpected pending change ${e}`);
return acc;
}
}
/**
* @param {import("./types.js").Diagnostic} diagnostic
* @returns {Promise<import("vite").HMRPayload>}
*/
async function makeHmrError(diagnostic) {
const fileContent = await fs.readFile(diagnostic.fileName, "utf-8");
const frame = codeFrameColumns(fileContent, {
start: {
line: diagnostic.range.startLine,
col: diagnostic.range.startColumn,
},
end: {
line: diagnostic.range.endLine,
col: diagnostic.range.endColumn,
},
});
return {
type: "error",
err: {
message: diagnostic.message,
frame: frame,
stack: "",
id: diagnostic.fileName,
loc: {
file: diagnostic.fileName,
line: diagnostic.range.startLine,
column: diagnostic.range.startColumn,
},
},
};
}
return {
name: "vite-plugin-fable",
enforce: "pre",
configResolved: async function (resolvedConfig) {
state.logger = resolvedConfig.logger;
state.configuration =
resolvedConfig.env.MODE === "production" ? "Release" : "Debug";
state.isBuild = resolvedConfig.command === "build";
logDebug("configResolved", `Configuration: ${state.configuration}`);
const configDir =
resolvedConfig.configFile && path.dirname(resolvedConfig.configFile);
if (state.config && state.config.fsproj) {
state.fsproj = state.config.fsproj;
} else {
state.fsproj = await findFsProjFile(configDir);
}
if (!state.fsproj) {
logCritical(
"configResolved",
`No .fsproj file was found in ${configDir}`,
);
} else {
logInfo("configResolved", `Entry fsproj ${state.fsproj}`);
}
},
buildStart: async function (options) {
try {
logInfo("buildStart", "Starting daemon");
state.dotnetProcess = spawn("dotnet", [fableDaemon, "--stdio"], {
shell: true,
stdio: "pipe",
});
state.endpoint = new JSONRPCEndpoint(
state.dotnetProcess.stdin,
state.dotnetProcess.stdout,
);
if (state.isBuild) {
await projectChanged(
this.addWatchFile.bind(this),
new Set([state.fsproj]),
);
} else {
state.pendingChanges = pendingChangesSubject
.pipe(
bufferTime(50),
map((events) => {
return events.reduce(reducePendingChange, {
projectChanged: false,
fsharpFiles: new Set(),
projectFiles: new Set(),
});
}),
filter(
(state) => state.projectChanged || state.fsharpFiles.size > 0,
),
)
.subscribe(async (pendingChanges) => {
let diagnostics = [];
if (pendingChanges.projectChanged) {
await projectChanged(
this.addWatchFile.bind(this),
pendingChanges.projectFiles,
);
} else {
const files = Array.from(pendingChanges.fsharpFiles);
logDebug("subscribe", files.join("\n"));
diagnostics = await fsharpFileChanged(files);
}
if (state.hotPromiseWithResolvers) {
state.hotPromiseWithResolvers.resolve(diagnostics);
state.hotPromiseWithResolvers = null;
}
});
logDebug("buildStart", "Initial project file change!");
state.hotPromiseWithResolvers = Promise.withResolvers();
pendingChangesSubject.next({
type: "ProjectFileChanged",
file: state.fsproj,
});
await state.hotPromiseWithResolvers.promise;
}
} catch (e) {
logCritical("buildStart", `Unexpected failure during buildStart: ${e}`);
}
},
transform: async function (src, id) {
if (fsharpFileRegex.test(id)) {
logDebug("transform", id);
if (state.compilableFiles.has(id)) {
let code = state.compilableFiles.get(id);
// If Fable outputted JSX, we still need to transform this.
// @vitejs/plugin-react does not do this.
if (state.config.jsx) {
const esbuildResult = await transform(code, {
loader: "jsx",
jsx: state.config.jsx,
});
code = esbuildResult.code;
}
return {
code: code,
map: null,
};
} else {
logWarn("transform", `${id} is not part of compilableFiles.`);
}
}
},
watchChange: async function (id, change) {
if (state.sourceFiles.size !== 0 && state.dependentFiles.has(id)) {
pendingChangesSubject.next({ type: "ProjectFileChanged", file: id });
}
},
handleHotUpdate: async function ({ file, server, modules }) {
if (state.compilableFiles.has(file)) {
logDebug("handleHotUpdate", `enter for ${file}`);
pendingChangesSubject.next({
type: "FSharpFileChanged",
file: file,
});
// handleHotUpdate could be called concurrently because multiple files changed.
if (!state.hotPromiseWithResolvers) {
state.hotPromiseWithResolvers = Promise.withResolvers();
}
// The idea is to wait for a shared promise to resolve.
// This will resolve in the subscription of state.changedFSharpFiles
const diagnostics = await state.hotPromiseWithResolvers.promise;
logDebug("handleHotUpdate", `leave for ${file}`);
const errorDiagnostic = diagnostics.find(
(diag) => diag.severity === "Error",
);
if (errorDiagnostic) {
const msg = await makeHmrError(errorDiagnostic);
console.log(msg);
server.hot.send(msg);
return [];
} else {
// Potentially a file that is not imported in the current graph was changed.
// Vite should not try and hot update that module.
return modules.filter((m) => m.importers.size !== 0);
}
}
},
buildEnd: () => {
logInfo("buildEnd", "Closing daemon");
if (state.dotnetProcess) {
state.dotnetProcess.kill();
}
if (state.pendingChanges) {
state.pendingChanges.unsubscribe();
}
},
};
}