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