|
1
|
"use strict"; |
|
2
|
var __create = Object.create; |
|
3
|
var __defProp = Object.defineProperty; |
|
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; |
|
5
|
var __getOwnPropNames = Object.getOwnPropertyNames; |
|
6
|
var __getProtoOf = Object.getPrototypeOf; |
|
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty; |
|
8
|
var __copyProps = (to, from, except, desc) => { |
|
9
|
if (from && typeof from === "object" || typeof from === "function") { |
|
10
|
for (let key of __getOwnPropNames(from)) |
|
11
|
if (!__hasOwnProp.call(to, key) && key !== except) |
|
12
|
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); |
|
13
|
} |
|
14
|
return to; |
|
15
|
}; |
|
16
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( |
|
17
|
// If the importer is in node compatibility mode or this is not an ESM |
|
18
|
// file that has been converted to a CommonJS file using a Babel- |
|
19
|
// compatible transform (i.e. "__esModule" has not been set), then set |
|
20
|
// "default" to the CommonJS "module.exports" for node compatibility. |
|
21
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, |
|
22
|
mod |
|
23
|
)); |
|
24
|
var import_fs = __toESM(require("fs")); |
|
25
|
var import_url = __toESM(require("url")); |
|
26
|
var import_compilationCache = require("./compilationCache"); |
|
27
|
var import_portTransport = require("./portTransport"); |
|
28
|
var import_transform = require("./transform"); |
|
29
|
var import_util = require("../util"); |
|
30
|
const esmPreflightExtension = ".esm.preflight"; |
|
31
|
async function resolve(originalSpecifier, context, defaultResolve) { |
|
32
|
let specifier = originalSpecifier.replace(esmPreflightExtension, ""); |
|
33
|
if (context.parentURL && context.parentURL.startsWith("file://")) { |
|
34
|
const filename = import_url.default.fileURLToPath(context.parentURL); |
|
35
|
const resolved = (0, import_transform.resolveHook)(filename, specifier); |
|
36
|
if (resolved !== void 0) |
|
37
|
specifier = import_url.default.pathToFileURL(resolved).toString(); |
|
38
|
} |
|
39
|
const result = await defaultResolve(specifier, context, defaultResolve); |
|
40
|
if (result?.url && result.url.startsWith("file://")) |
|
41
|
(0, import_compilationCache.currentFileDepsCollector)()?.add(import_url.default.fileURLToPath(result.url)); |
|
42
|
if (originalSpecifier.endsWith(esmPreflightExtension)) |
|
43
|
result.url = result.url + esmPreflightExtension; |
|
44
|
return result; |
|
45
|
} |
|
46
|
const kSupportedFormats = /* @__PURE__ */ new Map([ |
|
47
|
["commonjs", "commonjs"], |
|
48
|
["module", "module"], |
|
49
|
["commonjs-typescript", "commonjs"], |
|
50
|
["module-typescript", "module"], |
|
51
|
[null, null], |
|
52
|
[void 0, void 0] |
|
53
|
]); |
|
54
|
async function load(originalModuleUrl, context, defaultLoad) { |
|
55
|
const moduleUrl = originalModuleUrl.replace(esmPreflightExtension, ""); |
|
56
|
if (!kSupportedFormats.has(context.format)) |
|
57
|
return defaultLoad(moduleUrl, context, defaultLoad); |
|
58
|
if (!moduleUrl.startsWith("file://")) |
|
59
|
return defaultLoad(moduleUrl, context, defaultLoad); |
|
60
|
const filename = import_url.default.fileURLToPath(moduleUrl); |
|
61
|
if (!(0, import_transform.shouldTransform)(filename)) |
|
62
|
return defaultLoad(moduleUrl, context, defaultLoad); |
|
63
|
const code = import_fs.default.readFileSync(filename, "utf-8"); |
|
64
|
const transformed = (0, import_transform.transformHook)(code, filename, moduleUrl); |
|
65
|
if (transformed.serializedCache) |
|
66
|
transport?.post("pushToCompilationCache", { cache: transformed.serializedCache }); |
|
67
|
return { |
|
68
|
format: kSupportedFormats.get(context.format) || ((0, import_util.fileIsModule)(filename) ? "module" : "commonjs"), |
|
69
|
source: originalModuleUrl.endsWith(esmPreflightExtension) ? `void 0;` : transformed.code, |
|
70
|
shortCircuit: true |
|
71
|
}; |
|
72
|
} |
|
73
|
let transport; |
|
74
|
function initialize(data) { |
|
75
|
transport = createTransport(data?.port); |
|
76
|
} |
|
77
|
function createTransport(port) { |
|
78
|
return new import_portTransport.PortTransport(port, async (method, params) => { |
|
79
|
if (method === "setSingleTSConfig") { |
|
80
|
(0, import_transform.setSingleTSConfig)(params.tsconfig); |
|
81
|
return; |
|
82
|
} |
|
83
|
if (method === "setTransformConfig") { |
|
84
|
(0, import_transform.setTransformConfig)(params.config); |
|
85
|
return; |
|
86
|
} |
|
87
|
if (method === "addToCompilationCache") { |
|
88
|
(0, import_compilationCache.addToCompilationCache)(params.cache); |
|
89
|
return; |
|
90
|
} |
|
91
|
if (method === "getCompilationCache") |
|
92
|
return { cache: (0, import_compilationCache.serializeCompilationCache)() }; |
|
93
|
if (method === "startCollectingFileDeps") { |
|
94
|
(0, import_compilationCache.startCollectingFileDeps)(); |
|
95
|
return; |
|
96
|
} |
|
97
|
if (method === "stopCollectingFileDeps") { |
|
98
|
(0, import_compilationCache.stopCollectingFileDeps)(params.file); |
|
99
|
return; |
|
100
|
} |
|
101
|
}); |
|
102
|
} |
|
103
|
module.exports = { initialize, load, resolve }; |
|
104
|
|