@hirarijs/loader 1.0.9 → 1.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-2YMWZ4IT.js +289 -0
- package/dist/chunk-5RAR7I57.js +15 -0
- package/dist/chunk-B6QICL5V.js +293 -0
- package/dist/chunk-C767MMH2.js +292 -0
- package/dist/chunk-HIXSUEUX.js +15 -0
- package/dist/chunk-J2O76MZJ.js +15 -0
- package/dist/chunk-JPHIWQ7S.js +15 -0
- package/dist/chunk-KAQUSKWG.js +306 -0
- package/dist/chunk-O7SYNOVB.js +296 -0
- package/dist/chunk-XTXLI7WO.js +15 -0
- package/dist/index.cjs +36 -19
- package/dist/index.d.cts +7 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +2 -2
- package/dist/loader.cjs +39 -26
- package/dist/loader.js +1 -1
- package/dist/register-auto.cjs +36 -19
- package/dist/register-auto.js +2 -2
- package/dist/register.cjs +36 -19
- package/dist/register.js +2 -2
- package/package.json +1 -1
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
// src/config.ts
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import path from "path";
|
|
4
|
+
var DEFAULT_CONFIG = {
|
|
5
|
+
format: "cjs",
|
|
6
|
+
plugins: [
|
|
7
|
+
"@hirarijs/loader-ts",
|
|
8
|
+
"@hirarijs/loader-tsx",
|
|
9
|
+
"@hirarijs/loader-vue",
|
|
10
|
+
"@hirarijs/loader-cjs-interop"
|
|
11
|
+
]
|
|
12
|
+
};
|
|
13
|
+
function loadHirariConfig(cwd = process.cwd()) {
|
|
14
|
+
const configPath = path.join(cwd, "hirari.json");
|
|
15
|
+
if (!fs.existsSync(configPath)) {
|
|
16
|
+
return { ...DEFAULT_CONFIG };
|
|
17
|
+
}
|
|
18
|
+
const raw = fs.readFileSync(configPath, "utf8");
|
|
19
|
+
let parsed;
|
|
20
|
+
try {
|
|
21
|
+
parsed = JSON.parse(raw);
|
|
22
|
+
} catch (error) {
|
|
23
|
+
throw new Error(`Failed to parse hirari.json: ${error.message}`);
|
|
24
|
+
}
|
|
25
|
+
const loaderConfig = parsed.loader || {};
|
|
26
|
+
return {
|
|
27
|
+
...DEFAULT_CONFIG,
|
|
28
|
+
...loaderConfig,
|
|
29
|
+
plugins: loaderConfig.plugins?.length ? loaderConfig.plugins : DEFAULT_CONFIG.plugins
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function getFormat(config) {
|
|
33
|
+
return config.format === "esm" ? "esm" : "cjs";
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// src/constants.ts
|
|
37
|
+
var IMPORT_META_URL_VARIABLE = "__hirari_loader_import_meta_url__";
|
|
38
|
+
|
|
39
|
+
// src/plugin-manager.ts
|
|
40
|
+
import { spawnSync } from "child_process";
|
|
41
|
+
import fs2 from "fs";
|
|
42
|
+
import path2 from "path";
|
|
43
|
+
import { createRequire } from "module";
|
|
44
|
+
var PACKAGE_MANAGERS = [
|
|
45
|
+
{ lock: "pnpm-lock.yaml", command: "pnpm", args: ["add"] },
|
|
46
|
+
{ lock: "yarn.lock", command: "yarn", args: ["add"] },
|
|
47
|
+
{ lock: "package-lock.json", command: "npm", args: ["install"] },
|
|
48
|
+
{ lock: "npm-shrinkwrap.json", command: "npm", args: ["install"] }
|
|
49
|
+
];
|
|
50
|
+
function detectPackageManager(cwd) {
|
|
51
|
+
for (const pm of PACKAGE_MANAGERS) {
|
|
52
|
+
if (fs2.existsSync(path2.join(cwd, pm.lock))) return pm;
|
|
53
|
+
}
|
|
54
|
+
return { command: "npm", args: ["install"] };
|
|
55
|
+
}
|
|
56
|
+
function tryRequire(moduleId, cwd) {
|
|
57
|
+
const req = createRequire(path2.join(cwd, "noop.js"));
|
|
58
|
+
const loaded = req(moduleId);
|
|
59
|
+
return loaded && (loaded.default || loaded);
|
|
60
|
+
}
|
|
61
|
+
function install(pkg, cwd) {
|
|
62
|
+
const pm = detectPackageManager(cwd);
|
|
63
|
+
const result = spawnSync(pm.command, [...pm.args, pkg], {
|
|
64
|
+
cwd,
|
|
65
|
+
stdio: "inherit",
|
|
66
|
+
env: process.env
|
|
67
|
+
});
|
|
68
|
+
if (result.error) {
|
|
69
|
+
throw result.error;
|
|
70
|
+
}
|
|
71
|
+
if (result.status !== 0) {
|
|
72
|
+
throw new Error(`${pm.command} ${pm.args.join(" ")} ${pkg} failed`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
function resolvePlugins(config, cwd) {
|
|
76
|
+
const plugins = [];
|
|
77
|
+
for (const pluginName of config.plugins || []) {
|
|
78
|
+
let loaded = null;
|
|
79
|
+
try {
|
|
80
|
+
loaded = tryRequire(pluginName, cwd);
|
|
81
|
+
} catch (error) {
|
|
82
|
+
if (config.autoInstall) {
|
|
83
|
+
console.log(`[hirari-loader] installing missing plugin ${pluginName}`);
|
|
84
|
+
install(pluginName, cwd);
|
|
85
|
+
loaded = tryRequire(pluginName, cwd);
|
|
86
|
+
} else {
|
|
87
|
+
throw new Error(
|
|
88
|
+
`Plugin "${pluginName}" not found. Enable autoInstall or install manually.`
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (!loaded) continue;
|
|
93
|
+
plugins.push({
|
|
94
|
+
plugin: loaded,
|
|
95
|
+
options: config.pluginOptions?.[pluginName]
|
|
96
|
+
});
|
|
97
|
+
if (config.debug) {
|
|
98
|
+
console.log(`[hirari-loader] loaded plugin ${pluginName}`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return plugins;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// src/runtime.ts
|
|
105
|
+
import fs3 from "fs";
|
|
106
|
+
import module from "module";
|
|
107
|
+
import path3 from "path";
|
|
108
|
+
import { fileURLToPath, pathToFileURL } from "url";
|
|
109
|
+
import { addHook } from "pirates";
|
|
110
|
+
import * as sourceMapSupport from "source-map-support";
|
|
111
|
+
var map = {};
|
|
112
|
+
var EXTENSION_CANDIDATES = [
|
|
113
|
+
".ts",
|
|
114
|
+
".mts",
|
|
115
|
+
".cts",
|
|
116
|
+
".tsx",
|
|
117
|
+
".jsx",
|
|
118
|
+
".vue",
|
|
119
|
+
".js",
|
|
120
|
+
".mjs",
|
|
121
|
+
".cjs"
|
|
122
|
+
];
|
|
123
|
+
function installSourceMaps() {
|
|
124
|
+
sourceMapSupport.install({
|
|
125
|
+
handleUncaughtExceptions: false,
|
|
126
|
+
environment: "node",
|
|
127
|
+
retrieveSourceMap(file) {
|
|
128
|
+
if (map[file]) {
|
|
129
|
+
return { url: file, map: map[file] };
|
|
130
|
+
}
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
var toNodeLoaderFormat = (format) => format === "esm" ? "module" : "commonjs";
|
|
136
|
+
function createRuntime(cwd = process.cwd()) {
|
|
137
|
+
const loaderConfig = loadHirariConfig(cwd);
|
|
138
|
+
const resolvedPlugins = resolvePlugins(loaderConfig, cwd);
|
|
139
|
+
installSourceMaps();
|
|
140
|
+
return {
|
|
141
|
+
cwd,
|
|
142
|
+
loaderConfig,
|
|
143
|
+
resolvedPlugins,
|
|
144
|
+
format: getFormat(loaderConfig)
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
function applyPlugin(code, filename, runtime) {
|
|
148
|
+
for (const match of runtime.resolvedPlugins) {
|
|
149
|
+
if (!match.plugin.match(filename)) continue;
|
|
150
|
+
const ctx = {
|
|
151
|
+
format: runtime.format,
|
|
152
|
+
loaderConfig: runtime.loaderConfig,
|
|
153
|
+
pluginOptions: match.options
|
|
154
|
+
};
|
|
155
|
+
const result = match.plugin.transform(code, filename, ctx);
|
|
156
|
+
if (runtime.loaderConfig.debug) {
|
|
157
|
+
console.log(`[hirari-loader][${match.plugin.name}] compiled ${filename}`);
|
|
158
|
+
}
|
|
159
|
+
if (result.continue) {
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
if (result.map) {
|
|
163
|
+
map[filename] = result.map;
|
|
164
|
+
}
|
|
165
|
+
return result;
|
|
166
|
+
}
|
|
167
|
+
if (runtime.loaderConfig.debug) {
|
|
168
|
+
console.log(`[hirari-loader] no plugin matched ${filename}`);
|
|
169
|
+
}
|
|
170
|
+
return { code };
|
|
171
|
+
}
|
|
172
|
+
function pickPlugin(filename, runtime) {
|
|
173
|
+
return runtime.resolvedPlugins.find(({ plugin }) => plugin.match(filename));
|
|
174
|
+
}
|
|
175
|
+
function collectExtensions(plugins) {
|
|
176
|
+
const set = /* @__PURE__ */ new Set();
|
|
177
|
+
for (const { plugin } of plugins) {
|
|
178
|
+
plugin.extensions.forEach((ext) => set.add(ext));
|
|
179
|
+
}
|
|
180
|
+
return Array.from(set);
|
|
181
|
+
}
|
|
182
|
+
function registerRequireHooks(runtime) {
|
|
183
|
+
const extensions = collectExtensions(runtime.resolvedPlugins);
|
|
184
|
+
const compile = (code, filename) => {
|
|
185
|
+
const result = applyPlugin(code, filename, runtime);
|
|
186
|
+
const banner = `const ${IMPORT_META_URL_VARIABLE} = require('url').pathToFileURL(__filename).href;`;
|
|
187
|
+
if (!result.code.includes(IMPORT_META_URL_VARIABLE)) {
|
|
188
|
+
return `${banner}${result.code}`;
|
|
189
|
+
}
|
|
190
|
+
return result.code;
|
|
191
|
+
};
|
|
192
|
+
const revert = addHook(compile, {
|
|
193
|
+
exts: extensions,
|
|
194
|
+
ignoreNodeModules: false
|
|
195
|
+
});
|
|
196
|
+
const extensionsObj = module.Module._extensions;
|
|
197
|
+
const jsHandler = extensionsObj[".js"];
|
|
198
|
+
extensionsObj[".js"] = function(mod, filename) {
|
|
199
|
+
try {
|
|
200
|
+
return jsHandler.call(this, mod, filename);
|
|
201
|
+
} catch (error) {
|
|
202
|
+
if (error && error.code === "ERR_REQUIRE_ESM") {
|
|
203
|
+
const src = fs3.readFileSync(filename, "utf8");
|
|
204
|
+
const result = applyPlugin(src, filename, runtime);
|
|
205
|
+
mod._compile(result.code, filename);
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
throw error;
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
return () => {
|
|
212
|
+
revert();
|
|
213
|
+
extensionsObj[".js"] = jsHandler;
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
async function loaderResolve(specifier, context, next, runtime) {
|
|
217
|
+
const parentUrl = context && context.parentURL;
|
|
218
|
+
const baseDir = parentUrl && typeof parentUrl === "string" && parentUrl.startsWith("file:") ? path3.dirname(fileURLToPath(parentUrl)) : process.cwd();
|
|
219
|
+
const tryResolve = (basePath, note) => {
|
|
220
|
+
for (const ext2 of EXTENSION_CANDIDATES) {
|
|
221
|
+
const candidate = basePath + ext2;
|
|
222
|
+
if (fs3.existsSync(candidate) && fs3.statSync(candidate).isFile()) {
|
|
223
|
+
const url = pathToFileURL(candidate).href;
|
|
224
|
+
if (runtime.loaderConfig.debug) {
|
|
225
|
+
console.log(`[hirari-loader] resolve ${note} ${specifier} -> ${url}`);
|
|
226
|
+
}
|
|
227
|
+
return { url, shortCircuit: true };
|
|
228
|
+
}
|
|
229
|
+
const indexCandidate = path3.join(basePath, "index" + ext2);
|
|
230
|
+
if (fs3.existsSync(indexCandidate) && fs3.statSync(indexCandidate).isFile()) {
|
|
231
|
+
const url = pathToFileURL(indexCandidate).href;
|
|
232
|
+
if (runtime.loaderConfig.debug) {
|
|
233
|
+
console.log(`[hirari-loader] resolve ${note} ${specifier} -> ${url}`);
|
|
234
|
+
}
|
|
235
|
+
return { url, shortCircuit: true };
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return null;
|
|
239
|
+
};
|
|
240
|
+
if (!path3.extname(specifier) && (specifier.startsWith("./") || specifier.startsWith("../") || specifier.startsWith("/") || specifier.startsWith("file:")) && !specifier.startsWith("node:")) {
|
|
241
|
+
const basePath = specifier.startsWith("file:") ? fileURLToPath(specifier) : specifier.startsWith("/") ? specifier : path3.resolve(baseDir, specifier);
|
|
242
|
+
const res = tryResolve(basePath, "extless");
|
|
243
|
+
if (res) return res;
|
|
244
|
+
}
|
|
245
|
+
const ext = path3.extname(specifier);
|
|
246
|
+
if ((ext === ".js" || ext === ".mjs" || ext === ".cjs") && (specifier.startsWith("./") || specifier.startsWith("../") || specifier.startsWith("/") || specifier.startsWith("file:"))) {
|
|
247
|
+
const withoutExt = specifier.slice(0, -ext.length);
|
|
248
|
+
const basePath = specifier.startsWith("file:") ? fileURLToPath(withoutExt) : specifier.startsWith("/") ? withoutExt : path3.resolve(baseDir, withoutExt);
|
|
249
|
+
const res = tryResolve(basePath, "fallback-js");
|
|
250
|
+
if (res) return res;
|
|
251
|
+
}
|
|
252
|
+
if (next) return next(specifier, context);
|
|
253
|
+
return { url: specifier, shortCircuit: true };
|
|
254
|
+
}
|
|
255
|
+
async function loaderLoad(url, context, next, runtime) {
|
|
256
|
+
const { format: expectedFormat } = runtime;
|
|
257
|
+
if (url.startsWith("file://")) {
|
|
258
|
+
const filename = fileURLToPath(url);
|
|
259
|
+
const match = pickPlugin(filename, runtime);
|
|
260
|
+
if (runtime.loaderConfig.debug) {
|
|
261
|
+
console.log(`[hirari-loader] load hook url=${url} match=${!!match}`);
|
|
262
|
+
}
|
|
263
|
+
if (match) {
|
|
264
|
+
const source = fs3.readFileSync(filename, "utf8");
|
|
265
|
+
const result = applyPlugin(source, filename, runtime);
|
|
266
|
+
return {
|
|
267
|
+
format: toNodeLoaderFormat(result.format || expectedFormat),
|
|
268
|
+
source: result.code,
|
|
269
|
+
shortCircuit: true
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
if (!next) {
|
|
274
|
+
throw new Error("No default loader available for " + url);
|
|
275
|
+
}
|
|
276
|
+
const forwarded = await next(url, context);
|
|
277
|
+
if (forwarded) return forwarded;
|
|
278
|
+
throw new Error("Loader did not return a result for " + url);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
export {
|
|
282
|
+
loadHirariConfig,
|
|
283
|
+
IMPORT_META_URL_VARIABLE,
|
|
284
|
+
resolvePlugins,
|
|
285
|
+
createRuntime,
|
|
286
|
+
registerRequireHooks,
|
|
287
|
+
loaderResolve,
|
|
288
|
+
loaderLoad
|
|
289
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createRuntime,
|
|
3
|
+
registerRequireHooks
|
|
4
|
+
} from "./chunk-C767MMH2.js";
|
|
5
|
+
|
|
6
|
+
// src/register.ts
|
|
7
|
+
function register(cwd = process.cwd()) {
|
|
8
|
+
const runtime = createRuntime(cwd);
|
|
9
|
+
const unregister = registerRequireHooks(runtime);
|
|
10
|
+
return { unregister };
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export {
|
|
14
|
+
register
|
|
15
|
+
};
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
// src/config.ts
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import path from "path";
|
|
4
|
+
var DEFAULT_CONFIG = {
|
|
5
|
+
format: "cjs",
|
|
6
|
+
plugins: [
|
|
7
|
+
"@hirarijs/loader-ts",
|
|
8
|
+
"@hirarijs/loader-tsx",
|
|
9
|
+
"@hirarijs/loader-vue",
|
|
10
|
+
"@hirarijs/loader-cjs-interop"
|
|
11
|
+
]
|
|
12
|
+
};
|
|
13
|
+
function loadHirariConfig(cwd = process.cwd()) {
|
|
14
|
+
const configPath = path.join(cwd, "hirari.json");
|
|
15
|
+
if (!fs.existsSync(configPath)) {
|
|
16
|
+
return { ...DEFAULT_CONFIG };
|
|
17
|
+
}
|
|
18
|
+
const raw = fs.readFileSync(configPath, "utf8");
|
|
19
|
+
let parsed;
|
|
20
|
+
try {
|
|
21
|
+
parsed = JSON.parse(raw);
|
|
22
|
+
} catch (error) {
|
|
23
|
+
throw new Error(`Failed to parse hirari.json: ${error.message}`);
|
|
24
|
+
}
|
|
25
|
+
const loaderConfig = parsed.loader || {};
|
|
26
|
+
return {
|
|
27
|
+
...DEFAULT_CONFIG,
|
|
28
|
+
...loaderConfig,
|
|
29
|
+
plugins: loaderConfig.plugins?.length ? loaderConfig.plugins : DEFAULT_CONFIG.plugins
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function getFormat(config) {
|
|
33
|
+
return config.format === "esm" ? "esm" : "cjs";
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// src/constants.ts
|
|
37
|
+
var IMPORT_META_URL_VARIABLE = "__hirari_loader_import_meta_url__";
|
|
38
|
+
|
|
39
|
+
// src/plugin-manager.ts
|
|
40
|
+
import { spawnSync } from "child_process";
|
|
41
|
+
import fs2 from "fs";
|
|
42
|
+
import path2 from "path";
|
|
43
|
+
import { createRequire } from "module";
|
|
44
|
+
var PACKAGE_MANAGERS = [
|
|
45
|
+
{ lock: "pnpm-lock.yaml", command: "pnpm", args: ["add"] },
|
|
46
|
+
{ lock: "yarn.lock", command: "yarn", args: ["add"] },
|
|
47
|
+
{ lock: "package-lock.json", command: "npm", args: ["install"] },
|
|
48
|
+
{ lock: "npm-shrinkwrap.json", command: "npm", args: ["install"] }
|
|
49
|
+
];
|
|
50
|
+
function detectPackageManager(cwd) {
|
|
51
|
+
for (const pm of PACKAGE_MANAGERS) {
|
|
52
|
+
if (fs2.existsSync(path2.join(cwd, pm.lock))) return pm;
|
|
53
|
+
}
|
|
54
|
+
return { command: "npm", args: ["install"] };
|
|
55
|
+
}
|
|
56
|
+
function tryRequire(moduleId, cwd) {
|
|
57
|
+
const req = createRequire(path2.join(cwd, "noop.js"));
|
|
58
|
+
const loaded = req(moduleId);
|
|
59
|
+
return loaded && (loaded.default || loaded);
|
|
60
|
+
}
|
|
61
|
+
function install(pkg, cwd) {
|
|
62
|
+
const pm = detectPackageManager(cwd);
|
|
63
|
+
const result = spawnSync(pm.command, [...pm.args, pkg], {
|
|
64
|
+
cwd,
|
|
65
|
+
stdio: "inherit",
|
|
66
|
+
env: process.env
|
|
67
|
+
});
|
|
68
|
+
if (result.error) {
|
|
69
|
+
throw result.error;
|
|
70
|
+
}
|
|
71
|
+
if (result.status !== 0) {
|
|
72
|
+
throw new Error(`${pm.command} ${pm.args.join(" ")} ${pkg} failed`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
function resolvePlugins(config, cwd) {
|
|
76
|
+
const plugins = [];
|
|
77
|
+
for (const pluginName of config.plugins || []) {
|
|
78
|
+
let loaded = null;
|
|
79
|
+
try {
|
|
80
|
+
loaded = tryRequire(pluginName, cwd);
|
|
81
|
+
} catch (error) {
|
|
82
|
+
if (config.autoInstall) {
|
|
83
|
+
console.log(`[hirari-loader] installing missing plugin ${pluginName}`);
|
|
84
|
+
install(pluginName, cwd);
|
|
85
|
+
loaded = tryRequire(pluginName, cwd);
|
|
86
|
+
} else {
|
|
87
|
+
throw new Error(
|
|
88
|
+
`Plugin "${pluginName}" not found. Enable autoInstall or install manually.`
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (!loaded) continue;
|
|
93
|
+
plugins.push({
|
|
94
|
+
plugin: loaded,
|
|
95
|
+
options: config.pluginOptions?.[pluginName]
|
|
96
|
+
});
|
|
97
|
+
if (config.debug) {
|
|
98
|
+
console.log(`[hirari-loader] loaded plugin ${pluginName}`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return plugins;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// src/runtime.ts
|
|
105
|
+
import fs3 from "fs";
|
|
106
|
+
import module from "module";
|
|
107
|
+
import path3 from "path";
|
|
108
|
+
import { fileURLToPath, pathToFileURL } from "url";
|
|
109
|
+
import { addHook } from "pirates";
|
|
110
|
+
import * as sourceMapSupport from "source-map-support";
|
|
111
|
+
var map = {};
|
|
112
|
+
var EXTENSION_CANDIDATES = [
|
|
113
|
+
".ts",
|
|
114
|
+
".mts",
|
|
115
|
+
".cts",
|
|
116
|
+
".tsx",
|
|
117
|
+
".jsx",
|
|
118
|
+
".vue",
|
|
119
|
+
".js",
|
|
120
|
+
".mjs",
|
|
121
|
+
".cjs"
|
|
122
|
+
];
|
|
123
|
+
function installSourceMaps() {
|
|
124
|
+
sourceMapSupport.install({
|
|
125
|
+
handleUncaughtExceptions: false,
|
|
126
|
+
environment: "node",
|
|
127
|
+
retrieveSourceMap(file) {
|
|
128
|
+
if (map[file]) {
|
|
129
|
+
return { url: file, map: map[file] };
|
|
130
|
+
}
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
var toNodeLoaderFormat = (format) => format === "esm" ? "module" : "commonjs";
|
|
136
|
+
function createRuntime(cwd = process.cwd()) {
|
|
137
|
+
const loaderConfig = loadHirariConfig(cwd);
|
|
138
|
+
const resolvedPlugins = resolvePlugins(loaderConfig, cwd);
|
|
139
|
+
installSourceMaps();
|
|
140
|
+
return {
|
|
141
|
+
cwd,
|
|
142
|
+
loaderConfig,
|
|
143
|
+
resolvedPlugins,
|
|
144
|
+
format: getFormat(loaderConfig)
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
function applyPlugin(code, filename, runtime) {
|
|
148
|
+
for (const match of runtime.resolvedPlugins) {
|
|
149
|
+
if (!match.plugin.match(filename)) continue;
|
|
150
|
+
const ctx = {
|
|
151
|
+
format: runtime.format,
|
|
152
|
+
loaderConfig: runtime.loaderConfig,
|
|
153
|
+
pluginOptions: match.options
|
|
154
|
+
};
|
|
155
|
+
const result = match.plugin.transform(code, filename, ctx);
|
|
156
|
+
if (runtime.loaderConfig.debug) {
|
|
157
|
+
console.log(`[hirari-loader][${match.plugin.name}] compiled ${filename}`);
|
|
158
|
+
}
|
|
159
|
+
if (result.continue) {
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
if (result.map) {
|
|
163
|
+
map[filename] = result.map;
|
|
164
|
+
}
|
|
165
|
+
return result;
|
|
166
|
+
}
|
|
167
|
+
if (runtime.loaderConfig.debug) {
|
|
168
|
+
console.log(`[hirari-loader] no plugin matched ${filename}`);
|
|
169
|
+
}
|
|
170
|
+
return { code };
|
|
171
|
+
}
|
|
172
|
+
function collectExtensions(plugins) {
|
|
173
|
+
const set = /* @__PURE__ */ new Set();
|
|
174
|
+
for (const { plugin } of plugins) {
|
|
175
|
+
plugin.extensions.forEach((ext) => set.add(ext));
|
|
176
|
+
}
|
|
177
|
+
return Array.from(set);
|
|
178
|
+
}
|
|
179
|
+
function registerRequireHooks(runtime) {
|
|
180
|
+
const extensions = collectExtensions(runtime.resolvedPlugins);
|
|
181
|
+
const compile = (code, filename) => {
|
|
182
|
+
const result = applyPlugin(code, filename, runtime);
|
|
183
|
+
const banner = `const ${IMPORT_META_URL_VARIABLE} = require('url').pathToFileURL(__filename).href;`;
|
|
184
|
+
if (!result.code.includes(IMPORT_META_URL_VARIABLE)) {
|
|
185
|
+
return `${banner}${result.code}`;
|
|
186
|
+
}
|
|
187
|
+
return result.code;
|
|
188
|
+
};
|
|
189
|
+
const revert = addHook(compile, {
|
|
190
|
+
exts: extensions,
|
|
191
|
+
ignoreNodeModules: runtime.loaderConfig.hookIgnoreNodeModules ?? true
|
|
192
|
+
});
|
|
193
|
+
const extensionsObj = module.Module._extensions;
|
|
194
|
+
const jsHandler = extensionsObj[".js"];
|
|
195
|
+
extensionsObj[".js"] = function(mod, filename) {
|
|
196
|
+
try {
|
|
197
|
+
return jsHandler.call(this, mod, filename);
|
|
198
|
+
} catch (error) {
|
|
199
|
+
if (error && error.code === "ERR_REQUIRE_ESM") {
|
|
200
|
+
const src = fs3.readFileSync(filename, "utf8");
|
|
201
|
+
const result = applyPlugin(src, filename, runtime);
|
|
202
|
+
mod._compile(result.code, filename);
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
throw error;
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
return () => {
|
|
209
|
+
revert();
|
|
210
|
+
extensionsObj[".js"] = jsHandler;
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
async function loaderResolve(specifier, context, next, runtime) {
|
|
214
|
+
const ignoreNodeModules = runtime.loaderConfig.hookIgnoreNodeModules ?? true;
|
|
215
|
+
const parentUrl = context && context.parentURL;
|
|
216
|
+
const baseDir = parentUrl && typeof parentUrl === "string" && parentUrl.startsWith("file:") ? path3.dirname(fileURLToPath(parentUrl)) : process.cwd();
|
|
217
|
+
const tryResolve = (basePath, note) => {
|
|
218
|
+
for (const ext2 of EXTENSION_CANDIDATES) {
|
|
219
|
+
const candidate = basePath + ext2;
|
|
220
|
+
if (ignoreNodeModules && candidate.includes("node_modules")) {
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
if (fs3.existsSync(candidate) && fs3.statSync(candidate).isFile()) {
|
|
224
|
+
const url = pathToFileURL(candidate).href;
|
|
225
|
+
if (runtime.loaderConfig.debug) {
|
|
226
|
+
console.log(`[hirari-loader] resolve ${note} ${specifier} -> ${url}`);
|
|
227
|
+
}
|
|
228
|
+
return { url, shortCircuit: true };
|
|
229
|
+
}
|
|
230
|
+
const indexCandidate = path3.join(basePath, "index" + ext2);
|
|
231
|
+
if (ignoreNodeModules && indexCandidate.includes("node_modules")) {
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
if (fs3.existsSync(indexCandidate) && fs3.statSync(indexCandidate).isFile()) {
|
|
235
|
+
const url = pathToFileURL(indexCandidate).href;
|
|
236
|
+
if (runtime.loaderConfig.debug) {
|
|
237
|
+
console.log(`[hirari-loader] resolve ${note} ${specifier} -> ${url}`);
|
|
238
|
+
}
|
|
239
|
+
return { url, shortCircuit: true };
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return null;
|
|
243
|
+
};
|
|
244
|
+
if (!path3.extname(specifier) && (specifier.startsWith("./") || specifier.startsWith("../") || specifier.startsWith("/") || specifier.startsWith("file:")) && !specifier.startsWith("node:")) {
|
|
245
|
+
const basePath = specifier.startsWith("file:") ? fileURLToPath(specifier) : specifier.startsWith("/") ? specifier : path3.resolve(baseDir, specifier);
|
|
246
|
+
const res = tryResolve(basePath, "extless");
|
|
247
|
+
if (res) return res;
|
|
248
|
+
}
|
|
249
|
+
const ext = path3.extname(specifier);
|
|
250
|
+
if ((ext === ".js" || ext === ".mjs" || ext === ".cjs") && (specifier.startsWith("./") || specifier.startsWith("../") || specifier.startsWith("/") || specifier.startsWith("file:"))) {
|
|
251
|
+
const withoutExt = specifier.slice(0, -ext.length);
|
|
252
|
+
const basePath = specifier.startsWith("file:") ? fileURLToPath(withoutExt) : specifier.startsWith("/") ? withoutExt : path3.resolve(baseDir, withoutExt);
|
|
253
|
+
const res = tryResolve(basePath, "fallback-js");
|
|
254
|
+
if (res) return res;
|
|
255
|
+
}
|
|
256
|
+
if (next) return next(specifier, context);
|
|
257
|
+
return { url: specifier, shortCircuit: true };
|
|
258
|
+
}
|
|
259
|
+
async function loaderLoad(url, context, next, runtime) {
|
|
260
|
+
const { format: expectedFormat } = runtime;
|
|
261
|
+
if (url.startsWith("file://")) {
|
|
262
|
+
const filename = fileURLToPath(url);
|
|
263
|
+
const match = pickPlugin(filename, runtime.resolvedPlugins);
|
|
264
|
+
if (runtime.loaderConfig.debug) {
|
|
265
|
+
console.log(`[hirari-loader] load hook url=${url} match=${!!match}`);
|
|
266
|
+
}
|
|
267
|
+
if (match) {
|
|
268
|
+
const source = fs3.readFileSync(filename, "utf8");
|
|
269
|
+
const result = applyPlugin(source, filename, runtime);
|
|
270
|
+
return {
|
|
271
|
+
format: toNodeLoaderFormat(result.format || expectedFormat),
|
|
272
|
+
source: result.code,
|
|
273
|
+
shortCircuit: true
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
if (!next) {
|
|
278
|
+
throw new Error("No default loader available for " + url);
|
|
279
|
+
}
|
|
280
|
+
const forwarded = await next(url, context);
|
|
281
|
+
if (forwarded) return forwarded;
|
|
282
|
+
throw new Error("Loader did not return a result for " + url);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export {
|
|
286
|
+
loadHirariConfig,
|
|
287
|
+
IMPORT_META_URL_VARIABLE,
|
|
288
|
+
resolvePlugins,
|
|
289
|
+
createRuntime,
|
|
290
|
+
registerRequireHooks,
|
|
291
|
+
loaderResolve,
|
|
292
|
+
loaderLoad
|
|
293
|
+
};
|