@hirarijs/loader 1.0.10 → 1.0.12
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-5RAR7I57.js +15 -0
- package/dist/chunk-7FOXDJEC.js +15 -0
- package/dist/chunk-BFYLVEVR.js +306 -0
- package/dist/chunk-C767MMH2.js +292 -0
- package/dist/chunk-HIXSUEUX.js +15 -0
- package/dist/chunk-KAQUSKWG.js +306 -0
- package/dist/index.cjs +23 -6
- package/dist/index.js +2 -2
- package/dist/loader.cjs +26 -9
- package/dist/loader.js +1 -1
- package/dist/register-auto.cjs +23 -6
- package/dist/register-auto.js +2 -2
- package/dist/register.cjs +23 -6
- package/dist/register.js +2 -2
- package/package.json +1 -1
|
@@ -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,15 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createRuntime,
|
|
3
|
+
registerRequireHooks
|
|
4
|
+
} from "./chunk-BFYLVEVR.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,306 @@
|
|
|
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
|
+
let currentCode = code;
|
|
149
|
+
let currentFormat = runtime.format;
|
|
150
|
+
let lastResult = null;
|
|
151
|
+
for (const match of runtime.resolvedPlugins) {
|
|
152
|
+
if (!match.plugin.match(filename)) continue;
|
|
153
|
+
const ctx = {
|
|
154
|
+
format: currentFormat,
|
|
155
|
+
loaderConfig: runtime.loaderConfig,
|
|
156
|
+
pluginOptions: match.options
|
|
157
|
+
};
|
|
158
|
+
const result = match.plugin.transform(currentCode, filename, ctx);
|
|
159
|
+
if (runtime.loaderConfig.debug) {
|
|
160
|
+
console.log(`[hirari-loader][${match.plugin.name}] compiled ${filename}`);
|
|
161
|
+
}
|
|
162
|
+
if (result.map) {
|
|
163
|
+
map[filename] = result.map;
|
|
164
|
+
}
|
|
165
|
+
if (result.format) {
|
|
166
|
+
currentFormat = result.format;
|
|
167
|
+
}
|
|
168
|
+
if (result.continue) {
|
|
169
|
+
lastResult = result;
|
|
170
|
+
if (result.code) {
|
|
171
|
+
currentCode = result.code;
|
|
172
|
+
}
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
return result;
|
|
176
|
+
}
|
|
177
|
+
if (lastResult) {
|
|
178
|
+
return {
|
|
179
|
+
code: lastResult.code ?? currentCode,
|
|
180
|
+
map: lastResult.map,
|
|
181
|
+
format: lastResult.format ?? currentFormat
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
if (runtime.loaderConfig.debug) {
|
|
185
|
+
console.log(`[hirari-loader] no plugin matched ${filename}`);
|
|
186
|
+
}
|
|
187
|
+
return { code: currentCode };
|
|
188
|
+
}
|
|
189
|
+
function pickPlugin(filename, runtime) {
|
|
190
|
+
return runtime.resolvedPlugins.find(({ plugin }) => plugin.match(filename));
|
|
191
|
+
}
|
|
192
|
+
function collectExtensions(plugins) {
|
|
193
|
+
const set = /* @__PURE__ */ new Set();
|
|
194
|
+
for (const { plugin } of plugins) {
|
|
195
|
+
plugin.extensions.forEach((ext) => set.add(ext));
|
|
196
|
+
}
|
|
197
|
+
return Array.from(set);
|
|
198
|
+
}
|
|
199
|
+
function registerRequireHooks(runtime) {
|
|
200
|
+
const extensions = collectExtensions(runtime.resolvedPlugins);
|
|
201
|
+
const compile = (code, filename) => {
|
|
202
|
+
const result = applyPlugin(code, filename, runtime);
|
|
203
|
+
const banner = `const ${IMPORT_META_URL_VARIABLE} = require('url').pathToFileURL(__filename).href;`;
|
|
204
|
+
if (!result.code.includes(IMPORT_META_URL_VARIABLE)) {
|
|
205
|
+
return `${banner}${result.code}`;
|
|
206
|
+
}
|
|
207
|
+
return result.code;
|
|
208
|
+
};
|
|
209
|
+
const revert = addHook(compile, {
|
|
210
|
+
exts: extensions,
|
|
211
|
+
ignoreNodeModules: false
|
|
212
|
+
});
|
|
213
|
+
const extensionsObj = module.Module._extensions;
|
|
214
|
+
const jsHandler = extensionsObj[".js"];
|
|
215
|
+
extensionsObj[".js"] = function(mod, filename) {
|
|
216
|
+
try {
|
|
217
|
+
return jsHandler.call(this, mod, filename);
|
|
218
|
+
} catch (error) {
|
|
219
|
+
if (error && error.code === "ERR_REQUIRE_ESM") {
|
|
220
|
+
const src = fs3.readFileSync(filename, "utf8");
|
|
221
|
+
const result = applyPlugin(src, filename, runtime);
|
|
222
|
+
mod._compile(result.code, filename);
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
throw error;
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
return () => {
|
|
229
|
+
revert();
|
|
230
|
+
extensionsObj[".js"] = jsHandler;
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
async function loaderResolve(specifier, context, next, runtime) {
|
|
234
|
+
const parentUrl = context && context.parentURL;
|
|
235
|
+
const baseDir = parentUrl && typeof parentUrl === "string" && parentUrl.startsWith("file:") ? path3.dirname(fileURLToPath(parentUrl)) : process.cwd();
|
|
236
|
+
const tryResolve = (basePath, note) => {
|
|
237
|
+
for (const ext2 of EXTENSION_CANDIDATES) {
|
|
238
|
+
const candidate = basePath + ext2;
|
|
239
|
+
if (fs3.existsSync(candidate) && fs3.statSync(candidate).isFile()) {
|
|
240
|
+
const url = pathToFileURL(candidate).href;
|
|
241
|
+
if (runtime.loaderConfig.debug) {
|
|
242
|
+
console.log(`[hirari-loader] resolve ${note} ${specifier} -> ${url}`);
|
|
243
|
+
}
|
|
244
|
+
return { url };
|
|
245
|
+
}
|
|
246
|
+
const indexCandidate = path3.join(basePath, "index" + ext2);
|
|
247
|
+
if (fs3.existsSync(indexCandidate) && fs3.statSync(indexCandidate).isFile()) {
|
|
248
|
+
const url = pathToFileURL(indexCandidate).href;
|
|
249
|
+
if (runtime.loaderConfig.debug) {
|
|
250
|
+
console.log(`[hirari-loader] resolve ${note} ${specifier} -> ${url}`);
|
|
251
|
+
}
|
|
252
|
+
return { url };
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return null;
|
|
256
|
+
};
|
|
257
|
+
if (!path3.extname(specifier) && (specifier.startsWith("./") || specifier.startsWith("../") || specifier.startsWith("/") || specifier.startsWith("file:")) && !specifier.startsWith("node:")) {
|
|
258
|
+
const basePath = specifier.startsWith("file:") ? fileURLToPath(specifier) : specifier.startsWith("/") ? specifier : path3.resolve(baseDir, specifier);
|
|
259
|
+
const res = tryResolve(basePath, "extless");
|
|
260
|
+
if (res) return res;
|
|
261
|
+
}
|
|
262
|
+
const ext = path3.extname(specifier);
|
|
263
|
+
if ((ext === ".js" || ext === ".mjs" || ext === ".cjs") && (specifier.startsWith("./") || specifier.startsWith("../") || specifier.startsWith("/") || specifier.startsWith("file:"))) {
|
|
264
|
+
const withoutExt = specifier.slice(0, -ext.length);
|
|
265
|
+
const basePath = specifier.startsWith("file:") ? fileURLToPath(withoutExt) : specifier.startsWith("/") ? withoutExt : path3.resolve(baseDir, withoutExt);
|
|
266
|
+
const res = tryResolve(basePath, "fallback-js");
|
|
267
|
+
if (res) return res;
|
|
268
|
+
}
|
|
269
|
+
if (next) return next(specifier, context);
|
|
270
|
+
return { url: specifier };
|
|
271
|
+
}
|
|
272
|
+
async function loaderLoad(url, context, next, runtime) {
|
|
273
|
+
const { format: expectedFormat } = runtime;
|
|
274
|
+
if (url.startsWith("file://")) {
|
|
275
|
+
const filename = fileURLToPath(url);
|
|
276
|
+
const match = pickPlugin(filename, runtime);
|
|
277
|
+
if (runtime.loaderConfig.debug) {
|
|
278
|
+
console.log(`[hirari-loader] load hook url=${url} match=${!!match}`);
|
|
279
|
+
}
|
|
280
|
+
if (match) {
|
|
281
|
+
const source = fs3.readFileSync(filename, "utf8");
|
|
282
|
+
const result = applyPlugin(source, filename, runtime);
|
|
283
|
+
return {
|
|
284
|
+
format: toNodeLoaderFormat(result.format || expectedFormat),
|
|
285
|
+
source: result.code,
|
|
286
|
+
shortCircuit: true
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
if (!next) {
|
|
291
|
+
throw new Error("No default loader available for " + url);
|
|
292
|
+
}
|
|
293
|
+
const forwarded = await next(url, context);
|
|
294
|
+
if (forwarded) return forwarded;
|
|
295
|
+
throw new Error("Loader did not return a result for " + url);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
export {
|
|
299
|
+
loadHirariConfig,
|
|
300
|
+
IMPORT_META_URL_VARIABLE,
|
|
301
|
+
resolvePlugins,
|
|
302
|
+
createRuntime,
|
|
303
|
+
registerRequireHooks,
|
|
304
|
+
loaderResolve,
|
|
305
|
+
loaderLoad
|
|
306
|
+
};
|
|
@@ -0,0 +1,292 @@
|
|
|
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
|
+
let lastResult = null;
|
|
149
|
+
for (const match of runtime.resolvedPlugins) {
|
|
150
|
+
if (!match.plugin.match(filename)) continue;
|
|
151
|
+
const ctx = {
|
|
152
|
+
format: runtime.format,
|
|
153
|
+
loaderConfig: runtime.loaderConfig,
|
|
154
|
+
pluginOptions: match.options
|
|
155
|
+
};
|
|
156
|
+
const result = match.plugin.transform(code, filename, ctx);
|
|
157
|
+
if (runtime.loaderConfig.debug) {
|
|
158
|
+
console.log(`[hirari-loader][${match.plugin.name}] compiled ${filename}`);
|
|
159
|
+
}
|
|
160
|
+
if (result.map) {
|
|
161
|
+
map[filename] = result.map;
|
|
162
|
+
}
|
|
163
|
+
if (result.continue) {
|
|
164
|
+
lastResult = result;
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
return result;
|
|
168
|
+
}
|
|
169
|
+
if (lastResult) return lastResult;
|
|
170
|
+
if (runtime.loaderConfig.debug) {
|
|
171
|
+
console.log(`[hirari-loader] no plugin matched ${filename}`);
|
|
172
|
+
}
|
|
173
|
+
return { code };
|
|
174
|
+
}
|
|
175
|
+
function pickPlugin(filename, runtime) {
|
|
176
|
+
return runtime.resolvedPlugins.find(({ plugin }) => plugin.match(filename));
|
|
177
|
+
}
|
|
178
|
+
function collectExtensions(plugins) {
|
|
179
|
+
const set = /* @__PURE__ */ new Set();
|
|
180
|
+
for (const { plugin } of plugins) {
|
|
181
|
+
plugin.extensions.forEach((ext) => set.add(ext));
|
|
182
|
+
}
|
|
183
|
+
return Array.from(set);
|
|
184
|
+
}
|
|
185
|
+
function registerRequireHooks(runtime) {
|
|
186
|
+
const extensions = collectExtensions(runtime.resolvedPlugins);
|
|
187
|
+
const compile = (code, filename) => {
|
|
188
|
+
const result = applyPlugin(code, filename, runtime);
|
|
189
|
+
const banner = `const ${IMPORT_META_URL_VARIABLE} = require('url').pathToFileURL(__filename).href;`;
|
|
190
|
+
if (!result.code.includes(IMPORT_META_URL_VARIABLE)) {
|
|
191
|
+
return `${banner}${result.code}`;
|
|
192
|
+
}
|
|
193
|
+
return result.code;
|
|
194
|
+
};
|
|
195
|
+
const revert = addHook(compile, {
|
|
196
|
+
exts: extensions,
|
|
197
|
+
ignoreNodeModules: false
|
|
198
|
+
});
|
|
199
|
+
const extensionsObj = module.Module._extensions;
|
|
200
|
+
const jsHandler = extensionsObj[".js"];
|
|
201
|
+
extensionsObj[".js"] = function(mod, filename) {
|
|
202
|
+
try {
|
|
203
|
+
return jsHandler.call(this, mod, filename);
|
|
204
|
+
} catch (error) {
|
|
205
|
+
if (error && error.code === "ERR_REQUIRE_ESM") {
|
|
206
|
+
const src = fs3.readFileSync(filename, "utf8");
|
|
207
|
+
const result = applyPlugin(src, filename, runtime);
|
|
208
|
+
mod._compile(result.code, filename);
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
throw error;
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
return () => {
|
|
215
|
+
revert();
|
|
216
|
+
extensionsObj[".js"] = jsHandler;
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
async function loaderResolve(specifier, context, next, runtime) {
|
|
220
|
+
const parentUrl = context && context.parentURL;
|
|
221
|
+
const baseDir = parentUrl && typeof parentUrl === "string" && parentUrl.startsWith("file:") ? path3.dirname(fileURLToPath(parentUrl)) : process.cwd();
|
|
222
|
+
const tryResolve = (basePath, note) => {
|
|
223
|
+
for (const ext2 of EXTENSION_CANDIDATES) {
|
|
224
|
+
const candidate = basePath + ext2;
|
|
225
|
+
if (fs3.existsSync(candidate) && fs3.statSync(candidate).isFile()) {
|
|
226
|
+
const url = pathToFileURL(candidate).href;
|
|
227
|
+
if (runtime.loaderConfig.debug) {
|
|
228
|
+
console.log(`[hirari-loader] resolve ${note} ${specifier} -> ${url}`);
|
|
229
|
+
}
|
|
230
|
+
return { url, shortCircuit: true };
|
|
231
|
+
}
|
|
232
|
+
const indexCandidate = path3.join(basePath, "index" + ext2);
|
|
233
|
+
if (fs3.existsSync(indexCandidate) && fs3.statSync(indexCandidate).isFile()) {
|
|
234
|
+
const url = pathToFileURL(indexCandidate).href;
|
|
235
|
+
if (runtime.loaderConfig.debug) {
|
|
236
|
+
console.log(`[hirari-loader] resolve ${note} ${specifier} -> ${url}`);
|
|
237
|
+
}
|
|
238
|
+
return { url, shortCircuit: true };
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return null;
|
|
242
|
+
};
|
|
243
|
+
if (!path3.extname(specifier) && (specifier.startsWith("./") || specifier.startsWith("../") || specifier.startsWith("/") || specifier.startsWith("file:")) && !specifier.startsWith("node:")) {
|
|
244
|
+
const basePath = specifier.startsWith("file:") ? fileURLToPath(specifier) : specifier.startsWith("/") ? specifier : path3.resolve(baseDir, specifier);
|
|
245
|
+
const res = tryResolve(basePath, "extless");
|
|
246
|
+
if (res) return res;
|
|
247
|
+
}
|
|
248
|
+
const ext = path3.extname(specifier);
|
|
249
|
+
if ((ext === ".js" || ext === ".mjs" || ext === ".cjs") && (specifier.startsWith("./") || specifier.startsWith("../") || specifier.startsWith("/") || specifier.startsWith("file:"))) {
|
|
250
|
+
const withoutExt = specifier.slice(0, -ext.length);
|
|
251
|
+
const basePath = specifier.startsWith("file:") ? fileURLToPath(withoutExt) : specifier.startsWith("/") ? withoutExt : path3.resolve(baseDir, withoutExt);
|
|
252
|
+
const res = tryResolve(basePath, "fallback-js");
|
|
253
|
+
if (res) return res;
|
|
254
|
+
}
|
|
255
|
+
if (next) return next(specifier, context);
|
|
256
|
+
return { url: specifier, shortCircuit: true };
|
|
257
|
+
}
|
|
258
|
+
async function loaderLoad(url, context, next, runtime) {
|
|
259
|
+
const { format: expectedFormat } = runtime;
|
|
260
|
+
if (url.startsWith("file://")) {
|
|
261
|
+
const filename = fileURLToPath(url);
|
|
262
|
+
const match = pickPlugin(filename, runtime);
|
|
263
|
+
if (runtime.loaderConfig.debug) {
|
|
264
|
+
console.log(`[hirari-loader] load hook url=${url} match=${!!match}`);
|
|
265
|
+
}
|
|
266
|
+
if (match) {
|
|
267
|
+
const source = fs3.readFileSync(filename, "utf8");
|
|
268
|
+
const result = applyPlugin(source, filename, runtime);
|
|
269
|
+
return {
|
|
270
|
+
format: toNodeLoaderFormat(result.format || expectedFormat),
|
|
271
|
+
source: result.code,
|
|
272
|
+
shortCircuit: true
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
if (!next) {
|
|
277
|
+
throw new Error("No default loader available for " + url);
|
|
278
|
+
}
|
|
279
|
+
const forwarded = await next(url, context);
|
|
280
|
+
if (forwarded) return forwarded;
|
|
281
|
+
throw new Error("Loader did not return a result for " + url);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
export {
|
|
285
|
+
loadHirariConfig,
|
|
286
|
+
IMPORT_META_URL_VARIABLE,
|
|
287
|
+
resolvePlugins,
|
|
288
|
+
createRuntime,
|
|
289
|
+
registerRequireHooks,
|
|
290
|
+
loaderResolve,
|
|
291
|
+
loaderLoad
|
|
292
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createRuntime,
|
|
3
|
+
registerRequireHooks
|
|
4
|
+
} from "./chunk-KAQUSKWG.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,306 @@
|
|
|
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
|
+
let currentCode = code;
|
|
149
|
+
let currentFormat = runtime.format;
|
|
150
|
+
let lastResult = null;
|
|
151
|
+
for (const match of runtime.resolvedPlugins) {
|
|
152
|
+
if (!match.plugin.match(filename)) continue;
|
|
153
|
+
const ctx = {
|
|
154
|
+
format: currentFormat,
|
|
155
|
+
loaderConfig: runtime.loaderConfig,
|
|
156
|
+
pluginOptions: match.options
|
|
157
|
+
};
|
|
158
|
+
const result = match.plugin.transform(currentCode, filename, ctx);
|
|
159
|
+
if (runtime.loaderConfig.debug) {
|
|
160
|
+
console.log(`[hirari-loader][${match.plugin.name}] compiled ${filename}`);
|
|
161
|
+
}
|
|
162
|
+
if (result.map) {
|
|
163
|
+
map[filename] = result.map;
|
|
164
|
+
}
|
|
165
|
+
if (result.format) {
|
|
166
|
+
currentFormat = result.format;
|
|
167
|
+
}
|
|
168
|
+
if (result.continue) {
|
|
169
|
+
lastResult = result;
|
|
170
|
+
if (result.code) {
|
|
171
|
+
currentCode = result.code;
|
|
172
|
+
}
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
return result;
|
|
176
|
+
}
|
|
177
|
+
if (lastResult) {
|
|
178
|
+
return {
|
|
179
|
+
code: lastResult.code ?? currentCode,
|
|
180
|
+
map: lastResult.map,
|
|
181
|
+
format: lastResult.format ?? currentFormat
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
if (runtime.loaderConfig.debug) {
|
|
185
|
+
console.log(`[hirari-loader] no plugin matched ${filename}`);
|
|
186
|
+
}
|
|
187
|
+
return { code: currentCode };
|
|
188
|
+
}
|
|
189
|
+
function pickPlugin(filename, runtime) {
|
|
190
|
+
return runtime.resolvedPlugins.find(({ plugin }) => plugin.match(filename));
|
|
191
|
+
}
|
|
192
|
+
function collectExtensions(plugins) {
|
|
193
|
+
const set = /* @__PURE__ */ new Set();
|
|
194
|
+
for (const { plugin } of plugins) {
|
|
195
|
+
plugin.extensions.forEach((ext) => set.add(ext));
|
|
196
|
+
}
|
|
197
|
+
return Array.from(set);
|
|
198
|
+
}
|
|
199
|
+
function registerRequireHooks(runtime) {
|
|
200
|
+
const extensions = collectExtensions(runtime.resolvedPlugins);
|
|
201
|
+
const compile = (code, filename) => {
|
|
202
|
+
const result = applyPlugin(code, filename, runtime);
|
|
203
|
+
const banner = `const ${IMPORT_META_URL_VARIABLE} = require('url').pathToFileURL(__filename).href;`;
|
|
204
|
+
if (!result.code.includes(IMPORT_META_URL_VARIABLE)) {
|
|
205
|
+
return `${banner}${result.code}`;
|
|
206
|
+
}
|
|
207
|
+
return result.code;
|
|
208
|
+
};
|
|
209
|
+
const revert = addHook(compile, {
|
|
210
|
+
exts: extensions,
|
|
211
|
+
ignoreNodeModules: false
|
|
212
|
+
});
|
|
213
|
+
const extensionsObj = module.Module._extensions;
|
|
214
|
+
const jsHandler = extensionsObj[".js"];
|
|
215
|
+
extensionsObj[".js"] = function(mod, filename) {
|
|
216
|
+
try {
|
|
217
|
+
return jsHandler.call(this, mod, filename);
|
|
218
|
+
} catch (error) {
|
|
219
|
+
if (error && error.code === "ERR_REQUIRE_ESM") {
|
|
220
|
+
const src = fs3.readFileSync(filename, "utf8");
|
|
221
|
+
const result = applyPlugin(src, filename, runtime);
|
|
222
|
+
mod._compile(result.code, filename);
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
throw error;
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
return () => {
|
|
229
|
+
revert();
|
|
230
|
+
extensionsObj[".js"] = jsHandler;
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
async function loaderResolve(specifier, context, next, runtime) {
|
|
234
|
+
const parentUrl = context && context.parentURL;
|
|
235
|
+
const baseDir = parentUrl && typeof parentUrl === "string" && parentUrl.startsWith("file:") ? path3.dirname(fileURLToPath(parentUrl)) : process.cwd();
|
|
236
|
+
const tryResolve = (basePath, note) => {
|
|
237
|
+
for (const ext2 of EXTENSION_CANDIDATES) {
|
|
238
|
+
const candidate = basePath + ext2;
|
|
239
|
+
if (fs3.existsSync(candidate) && fs3.statSync(candidate).isFile()) {
|
|
240
|
+
const url = pathToFileURL(candidate).href;
|
|
241
|
+
if (runtime.loaderConfig.debug) {
|
|
242
|
+
console.log(`[hirari-loader] resolve ${note} ${specifier} -> ${url}`);
|
|
243
|
+
}
|
|
244
|
+
return { url, shortCircuit: true };
|
|
245
|
+
}
|
|
246
|
+
const indexCandidate = path3.join(basePath, "index" + ext2);
|
|
247
|
+
if (fs3.existsSync(indexCandidate) && fs3.statSync(indexCandidate).isFile()) {
|
|
248
|
+
const url = pathToFileURL(indexCandidate).href;
|
|
249
|
+
if (runtime.loaderConfig.debug) {
|
|
250
|
+
console.log(`[hirari-loader] resolve ${note} ${specifier} -> ${url}`);
|
|
251
|
+
}
|
|
252
|
+
return { url, shortCircuit: true };
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return null;
|
|
256
|
+
};
|
|
257
|
+
if (!path3.extname(specifier) && (specifier.startsWith("./") || specifier.startsWith("../") || specifier.startsWith("/") || specifier.startsWith("file:")) && !specifier.startsWith("node:")) {
|
|
258
|
+
const basePath = specifier.startsWith("file:") ? fileURLToPath(specifier) : specifier.startsWith("/") ? specifier : path3.resolve(baseDir, specifier);
|
|
259
|
+
const res = tryResolve(basePath, "extless");
|
|
260
|
+
if (res) return res;
|
|
261
|
+
}
|
|
262
|
+
const ext = path3.extname(specifier);
|
|
263
|
+
if ((ext === ".js" || ext === ".mjs" || ext === ".cjs") && (specifier.startsWith("./") || specifier.startsWith("../") || specifier.startsWith("/") || specifier.startsWith("file:"))) {
|
|
264
|
+
const withoutExt = specifier.slice(0, -ext.length);
|
|
265
|
+
const basePath = specifier.startsWith("file:") ? fileURLToPath(withoutExt) : specifier.startsWith("/") ? withoutExt : path3.resolve(baseDir, withoutExt);
|
|
266
|
+
const res = tryResolve(basePath, "fallback-js");
|
|
267
|
+
if (res) return res;
|
|
268
|
+
}
|
|
269
|
+
if (next) return next(specifier, context);
|
|
270
|
+
return { url: specifier, shortCircuit: true };
|
|
271
|
+
}
|
|
272
|
+
async function loaderLoad(url, context, next, runtime) {
|
|
273
|
+
const { format: expectedFormat } = runtime;
|
|
274
|
+
if (url.startsWith("file://")) {
|
|
275
|
+
const filename = fileURLToPath(url);
|
|
276
|
+
const match = pickPlugin(filename, runtime);
|
|
277
|
+
if (runtime.loaderConfig.debug) {
|
|
278
|
+
console.log(`[hirari-loader] load hook url=${url} match=${!!match}`);
|
|
279
|
+
}
|
|
280
|
+
if (match) {
|
|
281
|
+
const source = fs3.readFileSync(filename, "utf8");
|
|
282
|
+
const result = applyPlugin(source, filename, runtime);
|
|
283
|
+
return {
|
|
284
|
+
format: toNodeLoaderFormat(result.format || expectedFormat),
|
|
285
|
+
source: result.code,
|
|
286
|
+
shortCircuit: true
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
if (!next) {
|
|
291
|
+
throw new Error("No default loader available for " + url);
|
|
292
|
+
}
|
|
293
|
+
const forwarded = await next(url, context);
|
|
294
|
+
if (forwarded) return forwarded;
|
|
295
|
+
throw new Error("Loader did not return a result for " + url);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
export {
|
|
299
|
+
loadHirariConfig,
|
|
300
|
+
IMPORT_META_URL_VARIABLE,
|
|
301
|
+
resolvePlugins,
|
|
302
|
+
createRuntime,
|
|
303
|
+
registerRequireHooks,
|
|
304
|
+
loaderResolve,
|
|
305
|
+
loaderLoad
|
|
306
|
+
};
|
package/dist/index.cjs
CHANGED
|
@@ -174,29 +174,46 @@ function createRuntime(cwd = process.cwd()) {
|
|
|
174
174
|
};
|
|
175
175
|
}
|
|
176
176
|
function applyPlugin(code, filename, runtime) {
|
|
177
|
+
let currentCode = code;
|
|
178
|
+
let currentFormat = runtime.format;
|
|
179
|
+
let lastResult = null;
|
|
177
180
|
for (const match of runtime.resolvedPlugins) {
|
|
178
181
|
if (!match.plugin.match(filename)) continue;
|
|
179
182
|
const ctx = {
|
|
180
|
-
format:
|
|
183
|
+
format: currentFormat,
|
|
181
184
|
loaderConfig: runtime.loaderConfig,
|
|
182
185
|
pluginOptions: match.options
|
|
183
186
|
};
|
|
184
|
-
const result = match.plugin.transform(
|
|
187
|
+
const result = match.plugin.transform(currentCode, filename, ctx);
|
|
185
188
|
if (runtime.loaderConfig.debug) {
|
|
186
189
|
console.log(`[hirari-loader][${match.plugin.name}] compiled ${filename}`);
|
|
187
190
|
}
|
|
188
|
-
if (result.continue) {
|
|
189
|
-
continue;
|
|
190
|
-
}
|
|
191
191
|
if (result.map) {
|
|
192
192
|
map[filename] = result.map;
|
|
193
193
|
}
|
|
194
|
+
if (result.format) {
|
|
195
|
+
currentFormat = result.format;
|
|
196
|
+
}
|
|
197
|
+
if (result.continue) {
|
|
198
|
+
lastResult = result;
|
|
199
|
+
if (result.code) {
|
|
200
|
+
currentCode = result.code;
|
|
201
|
+
}
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
194
204
|
return result;
|
|
195
205
|
}
|
|
206
|
+
if (lastResult) {
|
|
207
|
+
return {
|
|
208
|
+
code: lastResult.code ?? currentCode,
|
|
209
|
+
map: lastResult.map,
|
|
210
|
+
format: lastResult.format ?? currentFormat
|
|
211
|
+
};
|
|
212
|
+
}
|
|
196
213
|
if (runtime.loaderConfig.debug) {
|
|
197
214
|
console.log(`[hirari-loader] no plugin matched ${filename}`);
|
|
198
215
|
}
|
|
199
|
-
return { code };
|
|
216
|
+
return { code: currentCode };
|
|
200
217
|
}
|
|
201
218
|
function collectExtensions(plugins) {
|
|
202
219
|
const set = /* @__PURE__ */ new Set();
|
package/dist/index.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
2
|
register
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-7FOXDJEC.js";
|
|
4
4
|
import {
|
|
5
5
|
IMPORT_META_URL_VARIABLE,
|
|
6
6
|
createRuntime,
|
|
7
7
|
loadHirariConfig,
|
|
8
8
|
resolvePlugins
|
|
9
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-BFYLVEVR.js";
|
|
10
10
|
export {
|
|
11
11
|
IMPORT_META_URL_VARIABLE,
|
|
12
12
|
createRuntime,
|
package/dist/loader.cjs
CHANGED
|
@@ -180,29 +180,46 @@ function createRuntime(cwd = process.cwd()) {
|
|
|
180
180
|
};
|
|
181
181
|
}
|
|
182
182
|
function applyPlugin(code, filename, runtime2) {
|
|
183
|
+
let currentCode = code;
|
|
184
|
+
let currentFormat = runtime2.format;
|
|
185
|
+
let lastResult = null;
|
|
183
186
|
for (const match of runtime2.resolvedPlugins) {
|
|
184
187
|
if (!match.plugin.match(filename)) continue;
|
|
185
188
|
const ctx = {
|
|
186
|
-
format:
|
|
189
|
+
format: currentFormat,
|
|
187
190
|
loaderConfig: runtime2.loaderConfig,
|
|
188
191
|
pluginOptions: match.options
|
|
189
192
|
};
|
|
190
|
-
const result = match.plugin.transform(
|
|
193
|
+
const result = match.plugin.transform(currentCode, filename, ctx);
|
|
191
194
|
if (runtime2.loaderConfig.debug) {
|
|
192
195
|
console.log(`[hirari-loader][${match.plugin.name}] compiled ${filename}`);
|
|
193
196
|
}
|
|
194
|
-
if (result.continue) {
|
|
195
|
-
continue;
|
|
196
|
-
}
|
|
197
197
|
if (result.map) {
|
|
198
198
|
map[filename] = result.map;
|
|
199
199
|
}
|
|
200
|
+
if (result.format) {
|
|
201
|
+
currentFormat = result.format;
|
|
202
|
+
}
|
|
203
|
+
if (result.continue) {
|
|
204
|
+
lastResult = result;
|
|
205
|
+
if (result.code) {
|
|
206
|
+
currentCode = result.code;
|
|
207
|
+
}
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
200
210
|
return result;
|
|
201
211
|
}
|
|
212
|
+
if (lastResult) {
|
|
213
|
+
return {
|
|
214
|
+
code: lastResult.code ?? currentCode,
|
|
215
|
+
map: lastResult.map,
|
|
216
|
+
format: lastResult.format ?? currentFormat
|
|
217
|
+
};
|
|
218
|
+
}
|
|
202
219
|
if (runtime2.loaderConfig.debug) {
|
|
203
220
|
console.log(`[hirari-loader] no plugin matched ${filename}`);
|
|
204
221
|
}
|
|
205
|
-
return { code };
|
|
222
|
+
return { code: currentCode };
|
|
206
223
|
}
|
|
207
224
|
function pickPlugin(filename, runtime2) {
|
|
208
225
|
return runtime2.resolvedPlugins.find(({ plugin }) => plugin.match(filename));
|
|
@@ -218,7 +235,7 @@ async function loaderResolve(specifier, context, next, runtime2) {
|
|
|
218
235
|
if (runtime2.loaderConfig.debug) {
|
|
219
236
|
console.log(`[hirari-loader] resolve ${note} ${specifier} -> ${url}`);
|
|
220
237
|
}
|
|
221
|
-
return { url
|
|
238
|
+
return { url };
|
|
222
239
|
}
|
|
223
240
|
const indexCandidate = import_path3.default.join(basePath, "index" + ext2);
|
|
224
241
|
if (import_fs3.default.existsSync(indexCandidate) && import_fs3.default.statSync(indexCandidate).isFile()) {
|
|
@@ -226,7 +243,7 @@ async function loaderResolve(specifier, context, next, runtime2) {
|
|
|
226
243
|
if (runtime2.loaderConfig.debug) {
|
|
227
244
|
console.log(`[hirari-loader] resolve ${note} ${specifier} -> ${url}`);
|
|
228
245
|
}
|
|
229
|
-
return { url
|
|
246
|
+
return { url };
|
|
230
247
|
}
|
|
231
248
|
}
|
|
232
249
|
return null;
|
|
@@ -244,7 +261,7 @@ async function loaderResolve(specifier, context, next, runtime2) {
|
|
|
244
261
|
if (res) return res;
|
|
245
262
|
}
|
|
246
263
|
if (next) return next(specifier, context);
|
|
247
|
-
return { url: specifier
|
|
264
|
+
return { url: specifier };
|
|
248
265
|
}
|
|
249
266
|
async function loaderLoad(url, context, next, runtime2) {
|
|
250
267
|
const { format: expectedFormat } = runtime2;
|
package/dist/loader.js
CHANGED
package/dist/register-auto.cjs
CHANGED
|
@@ -157,29 +157,46 @@ function createRuntime(cwd = process.cwd()) {
|
|
|
157
157
|
};
|
|
158
158
|
}
|
|
159
159
|
function applyPlugin(code, filename, runtime) {
|
|
160
|
+
let currentCode = code;
|
|
161
|
+
let currentFormat = runtime.format;
|
|
162
|
+
let lastResult = null;
|
|
160
163
|
for (const match of runtime.resolvedPlugins) {
|
|
161
164
|
if (!match.plugin.match(filename)) continue;
|
|
162
165
|
const ctx = {
|
|
163
|
-
format:
|
|
166
|
+
format: currentFormat,
|
|
164
167
|
loaderConfig: runtime.loaderConfig,
|
|
165
168
|
pluginOptions: match.options
|
|
166
169
|
};
|
|
167
|
-
const result = match.plugin.transform(
|
|
170
|
+
const result = match.plugin.transform(currentCode, filename, ctx);
|
|
168
171
|
if (runtime.loaderConfig.debug) {
|
|
169
172
|
console.log(`[hirari-loader][${match.plugin.name}] compiled ${filename}`);
|
|
170
173
|
}
|
|
171
|
-
if (result.continue) {
|
|
172
|
-
continue;
|
|
173
|
-
}
|
|
174
174
|
if (result.map) {
|
|
175
175
|
map[filename] = result.map;
|
|
176
176
|
}
|
|
177
|
+
if (result.format) {
|
|
178
|
+
currentFormat = result.format;
|
|
179
|
+
}
|
|
180
|
+
if (result.continue) {
|
|
181
|
+
lastResult = result;
|
|
182
|
+
if (result.code) {
|
|
183
|
+
currentCode = result.code;
|
|
184
|
+
}
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
177
187
|
return result;
|
|
178
188
|
}
|
|
189
|
+
if (lastResult) {
|
|
190
|
+
return {
|
|
191
|
+
code: lastResult.code ?? currentCode,
|
|
192
|
+
map: lastResult.map,
|
|
193
|
+
format: lastResult.format ?? currentFormat
|
|
194
|
+
};
|
|
195
|
+
}
|
|
179
196
|
if (runtime.loaderConfig.debug) {
|
|
180
197
|
console.log(`[hirari-loader] no plugin matched ${filename}`);
|
|
181
198
|
}
|
|
182
|
-
return { code };
|
|
199
|
+
return { code: currentCode };
|
|
183
200
|
}
|
|
184
201
|
function collectExtensions(plugins) {
|
|
185
202
|
const set = /* @__PURE__ */ new Set();
|
package/dist/register-auto.js
CHANGED
package/dist/register.cjs
CHANGED
|
@@ -169,29 +169,46 @@ function createRuntime(cwd = process.cwd()) {
|
|
|
169
169
|
};
|
|
170
170
|
}
|
|
171
171
|
function applyPlugin(code, filename, runtime) {
|
|
172
|
+
let currentCode = code;
|
|
173
|
+
let currentFormat = runtime.format;
|
|
174
|
+
let lastResult = null;
|
|
172
175
|
for (const match of runtime.resolvedPlugins) {
|
|
173
176
|
if (!match.plugin.match(filename)) continue;
|
|
174
177
|
const ctx = {
|
|
175
|
-
format:
|
|
178
|
+
format: currentFormat,
|
|
176
179
|
loaderConfig: runtime.loaderConfig,
|
|
177
180
|
pluginOptions: match.options
|
|
178
181
|
};
|
|
179
|
-
const result = match.plugin.transform(
|
|
182
|
+
const result = match.plugin.transform(currentCode, filename, ctx);
|
|
180
183
|
if (runtime.loaderConfig.debug) {
|
|
181
184
|
console.log(`[hirari-loader][${match.plugin.name}] compiled ${filename}`);
|
|
182
185
|
}
|
|
183
|
-
if (result.continue) {
|
|
184
|
-
continue;
|
|
185
|
-
}
|
|
186
186
|
if (result.map) {
|
|
187
187
|
map[filename] = result.map;
|
|
188
188
|
}
|
|
189
|
+
if (result.format) {
|
|
190
|
+
currentFormat = result.format;
|
|
191
|
+
}
|
|
192
|
+
if (result.continue) {
|
|
193
|
+
lastResult = result;
|
|
194
|
+
if (result.code) {
|
|
195
|
+
currentCode = result.code;
|
|
196
|
+
}
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
189
199
|
return result;
|
|
190
200
|
}
|
|
201
|
+
if (lastResult) {
|
|
202
|
+
return {
|
|
203
|
+
code: lastResult.code ?? currentCode,
|
|
204
|
+
map: lastResult.map,
|
|
205
|
+
format: lastResult.format ?? currentFormat
|
|
206
|
+
};
|
|
207
|
+
}
|
|
191
208
|
if (runtime.loaderConfig.debug) {
|
|
192
209
|
console.log(`[hirari-loader] no plugin matched ${filename}`);
|
|
193
210
|
}
|
|
194
|
-
return { code };
|
|
211
|
+
return { code: currentCode };
|
|
195
212
|
}
|
|
196
213
|
function collectExtensions(plugins) {
|
|
197
214
|
const set = /* @__PURE__ */ new Set();
|
package/dist/register.js
CHANGED