@hirarijs/loader 1.0.7 → 1.0.9
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-DB7XPVAR.js +293 -0
- package/dist/chunk-J3YUVEEK.js +15 -0
- package/dist/chunk-NGPAJOT3.js +15 -0
- package/dist/chunk-ZZJ5GQ5Y.js +288 -0
- package/dist/index.cjs +6 -1
- package/dist/index.js +2 -2
- package/dist/loader.cjs +13 -1
- package/dist/loader.js +1 -1
- package/dist/register-auto.cjs +6 -1
- package/dist/register-auto.js +2 -2
- package/dist/register.cjs +6 -1
- package/dist/register.js +2 -2
- package/package.json +2 -1
|
@@ -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 pickPlugin(filename, plugins) {
|
|
148
|
+
return plugins.find(({ plugin }) => plugin.match(filename));
|
|
149
|
+
}
|
|
150
|
+
function applyPlugin(code, filename, runtime) {
|
|
151
|
+
const match = pickPlugin(filename, runtime.resolvedPlugins);
|
|
152
|
+
if (!match) {
|
|
153
|
+
if (runtime.loaderConfig.debug) {
|
|
154
|
+
console.log(`[hirari-loader] no plugin matched ${filename}`);
|
|
155
|
+
}
|
|
156
|
+
return { code };
|
|
157
|
+
}
|
|
158
|
+
const ctx = {
|
|
159
|
+
format: runtime.format,
|
|
160
|
+
loaderConfig: runtime.loaderConfig,
|
|
161
|
+
pluginOptions: match.options
|
|
162
|
+
};
|
|
163
|
+
const result = match.plugin.transform(code, filename, ctx);
|
|
164
|
+
if (runtime.loaderConfig.debug) {
|
|
165
|
+
console.log(`[hirari-loader][${match.plugin.name}] compiled ${filename}`);
|
|
166
|
+
}
|
|
167
|
+
if (result.map) {
|
|
168
|
+
map[filename] = result.map;
|
|
169
|
+
}
|
|
170
|
+
return result;
|
|
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
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createRuntime,
|
|
3
|
+
registerRequireHooks
|
|
4
|
+
} from "./chunk-DB7XPVAR.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-ZZJ5GQ5Y.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,288 @@
|
|
|
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 ignoreNodeModules = runtime.loaderConfig.hookIgnoreNodeModules ?? true;
|
|
210
|
+
const parentUrl = context && context.parentURL;
|
|
211
|
+
const baseDir = parentUrl && typeof parentUrl === "string" && parentUrl.startsWith("file:") ? path3.dirname(fileURLToPath(parentUrl)) : process.cwd();
|
|
212
|
+
const tryResolve = (basePath, note) => {
|
|
213
|
+
for (const ext2 of EXTENSION_CANDIDATES) {
|
|
214
|
+
const candidate = basePath + ext2;
|
|
215
|
+
if (ignoreNodeModules && candidate.includes("node_modules")) {
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
if (fs3.existsSync(candidate) && fs3.statSync(candidate).isFile()) {
|
|
219
|
+
const url = pathToFileURL(candidate).href;
|
|
220
|
+
if (runtime.loaderConfig.debug) {
|
|
221
|
+
console.log(`[hirari-loader] resolve ${note} ${specifier} -> ${url}`);
|
|
222
|
+
}
|
|
223
|
+
return { url, shortCircuit: true };
|
|
224
|
+
}
|
|
225
|
+
const indexCandidate = path3.join(basePath, "index" + ext2);
|
|
226
|
+
if (ignoreNodeModules && indexCandidate.includes("node_modules")) {
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
if (fs3.existsSync(indexCandidate) && fs3.statSync(indexCandidate).isFile()) {
|
|
230
|
+
const url = pathToFileURL(indexCandidate).href;
|
|
231
|
+
if (runtime.loaderConfig.debug) {
|
|
232
|
+
console.log(`[hirari-loader] resolve ${note} ${specifier} -> ${url}`);
|
|
233
|
+
}
|
|
234
|
+
return { url, shortCircuit: true };
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
return null;
|
|
238
|
+
};
|
|
239
|
+
if (!path3.extname(specifier) && (specifier.startsWith("./") || specifier.startsWith("../") || specifier.startsWith("/") || specifier.startsWith("file:")) && !specifier.startsWith("node:")) {
|
|
240
|
+
const basePath = specifier.startsWith("file:") ? fileURLToPath(specifier) : specifier.startsWith("/") ? specifier : path3.resolve(baseDir, specifier);
|
|
241
|
+
const res = tryResolve(basePath, "extless");
|
|
242
|
+
if (res) return res;
|
|
243
|
+
}
|
|
244
|
+
const ext = path3.extname(specifier);
|
|
245
|
+
if ((ext === ".js" || ext === ".mjs" || ext === ".cjs") && (specifier.startsWith("./") || specifier.startsWith("../") || specifier.startsWith("/") || specifier.startsWith("file:"))) {
|
|
246
|
+
const withoutExt = specifier.slice(0, -ext.length);
|
|
247
|
+
const basePath = specifier.startsWith("file:") ? fileURLToPath(withoutExt) : specifier.startsWith("/") ? withoutExt : path3.resolve(baseDir, withoutExt);
|
|
248
|
+
const res = tryResolve(basePath, "fallback-js");
|
|
249
|
+
if (res) return res;
|
|
250
|
+
}
|
|
251
|
+
if (next) return next(specifier, context);
|
|
252
|
+
return { url: specifier, shortCircuit: true };
|
|
253
|
+
}
|
|
254
|
+
async function loaderLoad(url, context, next, runtime) {
|
|
255
|
+
const { format: expectedFormat } = runtime;
|
|
256
|
+
if (url.startsWith("file://")) {
|
|
257
|
+
const filename = fileURLToPath(url);
|
|
258
|
+
const match = pickPlugin(filename, runtime.resolvedPlugins);
|
|
259
|
+
if (runtime.loaderConfig.debug) {
|
|
260
|
+
console.log(`[hirari-loader] load hook url=${url} match=${!!match}`);
|
|
261
|
+
}
|
|
262
|
+
if (match) {
|
|
263
|
+
const source = fs3.readFileSync(filename, "utf8");
|
|
264
|
+
const result = applyPlugin(source, filename, runtime);
|
|
265
|
+
return {
|
|
266
|
+
format: toNodeLoaderFormat(result.format || expectedFormat),
|
|
267
|
+
source: result.code,
|
|
268
|
+
shortCircuit: true
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
if (!next) {
|
|
273
|
+
throw new Error("No default loader available for " + url);
|
|
274
|
+
}
|
|
275
|
+
const forwarded = await next(url, context);
|
|
276
|
+
if (forwarded) return forwarded;
|
|
277
|
+
throw new Error("Loader did not return a result for " + url);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
export {
|
|
281
|
+
loadHirariConfig,
|
|
282
|
+
IMPORT_META_URL_VARIABLE,
|
|
283
|
+
resolvePlugins,
|
|
284
|
+
createRuntime,
|
|
285
|
+
registerRequireHooks,
|
|
286
|
+
loaderResolve,
|
|
287
|
+
loaderLoad
|
|
288
|
+
};
|
package/dist/index.cjs
CHANGED
|
@@ -44,7 +44,12 @@ var import_fs = __toESM(require("fs"), 1);
|
|
|
44
44
|
var import_path = __toESM(require("path"), 1);
|
|
45
45
|
var DEFAULT_CONFIG = {
|
|
46
46
|
format: "cjs",
|
|
47
|
-
plugins: [
|
|
47
|
+
plugins: [
|
|
48
|
+
"@hirarijs/loader-ts",
|
|
49
|
+
"@hirarijs/loader-tsx",
|
|
50
|
+
"@hirarijs/loader-vue",
|
|
51
|
+
"@hirarijs/loader-cjs-interop"
|
|
52
|
+
]
|
|
48
53
|
};
|
|
49
54
|
function loadHirariConfig(cwd = process.cwd()) {
|
|
50
55
|
const configPath = import_path.default.join(cwd, "hirari.json");
|
package/dist/index.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
2
|
register
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-J3YUVEEK.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-DB7XPVAR.js";
|
|
10
10
|
export {
|
|
11
11
|
IMPORT_META_URL_VARIABLE,
|
|
12
12
|
createRuntime,
|
package/dist/loader.cjs
CHANGED
|
@@ -47,7 +47,12 @@ var import_fs = __toESM(require("fs"), 1);
|
|
|
47
47
|
var import_path = __toESM(require("path"), 1);
|
|
48
48
|
var DEFAULT_CONFIG = {
|
|
49
49
|
format: "cjs",
|
|
50
|
-
plugins: [
|
|
50
|
+
plugins: [
|
|
51
|
+
"@hirarijs/loader-ts",
|
|
52
|
+
"@hirarijs/loader-tsx",
|
|
53
|
+
"@hirarijs/loader-vue",
|
|
54
|
+
"@hirarijs/loader-cjs-interop"
|
|
55
|
+
]
|
|
51
56
|
};
|
|
52
57
|
function loadHirariConfig(cwd = process.cwd()) {
|
|
53
58
|
const configPath = import_path.default.join(cwd, "hirari.json");
|
|
@@ -200,11 +205,15 @@ function applyPlugin(code, filename, runtime2) {
|
|
|
200
205
|
return result;
|
|
201
206
|
}
|
|
202
207
|
async function loaderResolve(specifier, context, next, runtime2) {
|
|
208
|
+
const ignoreNodeModules = runtime2.loaderConfig.hookIgnoreNodeModules ?? true;
|
|
203
209
|
const parentUrl = context && context.parentURL;
|
|
204
210
|
const baseDir = parentUrl && typeof parentUrl === "string" && parentUrl.startsWith("file:") ? import_path3.default.dirname((0, import_url.fileURLToPath)(parentUrl)) : process.cwd();
|
|
205
211
|
const tryResolve = (basePath, note) => {
|
|
206
212
|
for (const ext2 of EXTENSION_CANDIDATES) {
|
|
207
213
|
const candidate = basePath + ext2;
|
|
214
|
+
if (ignoreNodeModules && candidate.includes("node_modules")) {
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
208
217
|
if (import_fs3.default.existsSync(candidate) && import_fs3.default.statSync(candidate).isFile()) {
|
|
209
218
|
const url = (0, import_url.pathToFileURL)(candidate).href;
|
|
210
219
|
if (runtime2.loaderConfig.debug) {
|
|
@@ -213,6 +222,9 @@ async function loaderResolve(specifier, context, next, runtime2) {
|
|
|
213
222
|
return { url, shortCircuit: true };
|
|
214
223
|
}
|
|
215
224
|
const indexCandidate = import_path3.default.join(basePath, "index" + ext2);
|
|
225
|
+
if (ignoreNodeModules && indexCandidate.includes("node_modules")) {
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
216
228
|
if (import_fs3.default.existsSync(indexCandidate) && import_fs3.default.statSync(indexCandidate).isFile()) {
|
|
217
229
|
const url = (0, import_url.pathToFileURL)(indexCandidate).href;
|
|
218
230
|
if (runtime2.loaderConfig.debug) {
|
package/dist/loader.js
CHANGED
package/dist/register-auto.cjs
CHANGED
|
@@ -36,7 +36,12 @@ var import_fs = __toESM(require("fs"), 1);
|
|
|
36
36
|
var import_path = __toESM(require("path"), 1);
|
|
37
37
|
var DEFAULT_CONFIG = {
|
|
38
38
|
format: "cjs",
|
|
39
|
-
plugins: [
|
|
39
|
+
plugins: [
|
|
40
|
+
"@hirarijs/loader-ts",
|
|
41
|
+
"@hirarijs/loader-tsx",
|
|
42
|
+
"@hirarijs/loader-vue",
|
|
43
|
+
"@hirarijs/loader-cjs-interop"
|
|
44
|
+
]
|
|
40
45
|
};
|
|
41
46
|
function loadHirariConfig(cwd = process.cwd()) {
|
|
42
47
|
const configPath = import_path.default.join(cwd, "hirari.json");
|
package/dist/register-auto.js
CHANGED
package/dist/register.cjs
CHANGED
|
@@ -48,7 +48,12 @@ var import_fs = __toESM(require("fs"), 1);
|
|
|
48
48
|
var import_path = __toESM(require("path"), 1);
|
|
49
49
|
var DEFAULT_CONFIG = {
|
|
50
50
|
format: "cjs",
|
|
51
|
-
plugins: [
|
|
51
|
+
plugins: [
|
|
52
|
+
"@hirarijs/loader-ts",
|
|
53
|
+
"@hirarijs/loader-tsx",
|
|
54
|
+
"@hirarijs/loader-vue",
|
|
55
|
+
"@hirarijs/loader-cjs-interop"
|
|
56
|
+
]
|
|
52
57
|
};
|
|
53
58
|
function loadHirariConfig(cwd = process.cwd()) {
|
|
54
59
|
const configPath = import_path.default.join(cwd, "hirari.json");
|
package/dist/register.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hirarijs/loader",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.9",
|
|
4
4
|
"description": "Runtime loader for HirariJS that wires loader plugins and config",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -33,6 +33,7 @@
|
|
|
33
33
|
"source-map-support": "^0.5.21"
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
|
+
"@hirarijs/loader-cjs-interop": "*",
|
|
36
37
|
"@hirarijs/loader-ts": "*",
|
|
37
38
|
"@hirarijs/loader-tsx": "*",
|
|
38
39
|
"@hirarijs/loader-vue": "*"
|