@hirarijs/loader 1.0.7 → 1.0.8
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-NGPAJOT3.js +15 -0
- package/dist/chunk-ZZJ5GQ5Y.js +288 -0
- package/dist/index.js +2 -2
- package/dist/loader.cjs +7 -0
- package/dist/loader.js +1 -1
- 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-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.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
2
|
register
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-NGPAJOT3.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-ZZJ5GQ5Y.js";
|
|
10
10
|
export {
|
|
11
11
|
IMPORT_META_URL_VARIABLE,
|
|
12
12
|
createRuntime,
|
package/dist/loader.cjs
CHANGED
|
@@ -200,11 +200,15 @@ function applyPlugin(code, filename, runtime2) {
|
|
|
200
200
|
return result;
|
|
201
201
|
}
|
|
202
202
|
async function loaderResolve(specifier, context, next, runtime2) {
|
|
203
|
+
const ignoreNodeModules = runtime2.loaderConfig.hookIgnoreNodeModules ?? true;
|
|
203
204
|
const parentUrl = context && context.parentURL;
|
|
204
205
|
const baseDir = parentUrl && typeof parentUrl === "string" && parentUrl.startsWith("file:") ? import_path3.default.dirname((0, import_url.fileURLToPath)(parentUrl)) : process.cwd();
|
|
205
206
|
const tryResolve = (basePath, note) => {
|
|
206
207
|
for (const ext2 of EXTENSION_CANDIDATES) {
|
|
207
208
|
const candidate = basePath + ext2;
|
|
209
|
+
if (ignoreNodeModules && candidate.includes("node_modules")) {
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
208
212
|
if (import_fs3.default.existsSync(candidate) && import_fs3.default.statSync(candidate).isFile()) {
|
|
209
213
|
const url = (0, import_url.pathToFileURL)(candidate).href;
|
|
210
214
|
if (runtime2.loaderConfig.debug) {
|
|
@@ -213,6 +217,9 @@ async function loaderResolve(specifier, context, next, runtime2) {
|
|
|
213
217
|
return { url, shortCircuit: true };
|
|
214
218
|
}
|
|
215
219
|
const indexCandidate = import_path3.default.join(basePath, "index" + ext2);
|
|
220
|
+
if (ignoreNodeModules && indexCandidate.includes("node_modules")) {
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
216
223
|
if (import_fs3.default.existsSync(indexCandidate) && import_fs3.default.statSync(indexCandidate).isFile()) {
|
|
217
224
|
const url = (0, import_url.pathToFileURL)(indexCandidate).href;
|
|
218
225
|
if (runtime2.loaderConfig.debug) {
|
package/dist/loader.js
CHANGED
package/dist/register-auto.js
CHANGED
package/dist/register.js
CHANGED