@hirarijs/loader 1.0.2 → 1.0.3
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-6ZD2GUQ7.js +219 -0
- package/dist/chunk-QS6DAZFT.js +15 -0
- package/dist/index.js +2 -2
- package/dist/loader.cjs +4 -2
- 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,219 @@
|
|
|
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
|
+
}
|
|
93
|
+
return plugins;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// src/runtime.ts
|
|
97
|
+
import fs3 from "fs";
|
|
98
|
+
import module from "module";
|
|
99
|
+
import { fileURLToPath } from "url";
|
|
100
|
+
import { addHook } from "pirates";
|
|
101
|
+
import * as sourceMapSupport from "source-map-support";
|
|
102
|
+
var map = {};
|
|
103
|
+
function installSourceMaps() {
|
|
104
|
+
sourceMapSupport.install({
|
|
105
|
+
handleUncaughtExceptions: false,
|
|
106
|
+
environment: "node",
|
|
107
|
+
retrieveSourceMap(file) {
|
|
108
|
+
if (map[file]) {
|
|
109
|
+
return { url: file, map: map[file] };
|
|
110
|
+
}
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
var toNodeLoaderFormat = (format) => format === "esm" ? "module" : "commonjs";
|
|
116
|
+
function createRuntime(cwd = process.cwd()) {
|
|
117
|
+
const loaderConfig = loadHirariConfig(cwd);
|
|
118
|
+
const resolvedPlugins = resolvePlugins(loaderConfig, cwd);
|
|
119
|
+
installSourceMaps();
|
|
120
|
+
return {
|
|
121
|
+
cwd,
|
|
122
|
+
loaderConfig,
|
|
123
|
+
resolvedPlugins,
|
|
124
|
+
format: getFormat(loaderConfig)
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
function pickPlugin(filename, plugins) {
|
|
128
|
+
return plugins.find(({ plugin }) => plugin.match(filename));
|
|
129
|
+
}
|
|
130
|
+
function applyPlugin(code, filename, runtime) {
|
|
131
|
+
const match = pickPlugin(filename, runtime.resolvedPlugins);
|
|
132
|
+
if (!match) return { code };
|
|
133
|
+
const ctx = {
|
|
134
|
+
format: runtime.format,
|
|
135
|
+
loaderConfig: runtime.loaderConfig,
|
|
136
|
+
pluginOptions: match.options
|
|
137
|
+
};
|
|
138
|
+
const result = match.plugin.transform(code, filename, ctx);
|
|
139
|
+
if (result.map) {
|
|
140
|
+
map[filename] = result.map;
|
|
141
|
+
}
|
|
142
|
+
return result;
|
|
143
|
+
}
|
|
144
|
+
function collectExtensions(plugins) {
|
|
145
|
+
const set = /* @__PURE__ */ new Set();
|
|
146
|
+
for (const { plugin } of plugins) {
|
|
147
|
+
plugin.extensions.forEach((ext) => set.add(ext));
|
|
148
|
+
}
|
|
149
|
+
return Array.from(set);
|
|
150
|
+
}
|
|
151
|
+
function registerRequireHooks(runtime) {
|
|
152
|
+
const extensions = collectExtensions(runtime.resolvedPlugins);
|
|
153
|
+
const compile = (code, filename) => {
|
|
154
|
+
const result = applyPlugin(code, filename, runtime);
|
|
155
|
+
const banner = `const ${IMPORT_META_URL_VARIABLE} = require('url').pathToFileURL(__filename).href;`;
|
|
156
|
+
if (!result.code.includes(IMPORT_META_URL_VARIABLE)) {
|
|
157
|
+
return `${banner}${result.code}`;
|
|
158
|
+
}
|
|
159
|
+
return result.code;
|
|
160
|
+
};
|
|
161
|
+
const revert = addHook(compile, {
|
|
162
|
+
exts: extensions,
|
|
163
|
+
ignoreNodeModules: runtime.loaderConfig.hookIgnoreNodeModules ?? true
|
|
164
|
+
});
|
|
165
|
+
const extensionsObj = module.Module._extensions;
|
|
166
|
+
const jsHandler = extensionsObj[".js"];
|
|
167
|
+
extensionsObj[".js"] = function(mod, filename) {
|
|
168
|
+
try {
|
|
169
|
+
return jsHandler.call(this, mod, filename);
|
|
170
|
+
} catch (error) {
|
|
171
|
+
if (error && error.code === "ERR_REQUIRE_ESM") {
|
|
172
|
+
const src = fs3.readFileSync(filename, "utf8");
|
|
173
|
+
const result = applyPlugin(src, filename, runtime);
|
|
174
|
+
mod._compile(result.code, filename);
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
throw error;
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
return () => {
|
|
181
|
+
revert();
|
|
182
|
+
extensionsObj[".js"] = jsHandler;
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
async function loaderResolve(specifier, context, next) {
|
|
186
|
+
if (next) return next(specifier, context, next);
|
|
187
|
+
return { url: specifier };
|
|
188
|
+
}
|
|
189
|
+
async function loaderLoad(url, context, next, runtime) {
|
|
190
|
+
const { format: expectedFormat } = runtime;
|
|
191
|
+
const nodeFormat = toNodeLoaderFormat(expectedFormat);
|
|
192
|
+
if (url.startsWith("file://")) {
|
|
193
|
+
const filename = fileURLToPath(url);
|
|
194
|
+
const match = pickPlugin(filename, runtime.resolvedPlugins);
|
|
195
|
+
if (match) {
|
|
196
|
+
const source = fs3.readFileSync(filename, "utf8");
|
|
197
|
+
const result = applyPlugin(source, filename, runtime);
|
|
198
|
+
return {
|
|
199
|
+
format: toNodeLoaderFormat(result.format || expectedFormat),
|
|
200
|
+
source: result.code,
|
|
201
|
+
shortCircuit: true
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
if (next) {
|
|
206
|
+
return next(url, { ...context, format: nodeFormat }, next);
|
|
207
|
+
}
|
|
208
|
+
throw new Error("No default loader available for " + url);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export {
|
|
212
|
+
loadHirariConfig,
|
|
213
|
+
IMPORT_META_URL_VARIABLE,
|
|
214
|
+
resolvePlugins,
|
|
215
|
+
createRuntime,
|
|
216
|
+
registerRequireHooks,
|
|
217
|
+
loaderResolve,
|
|
218
|
+
loaderLoad
|
|
219
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createRuntime,
|
|
3
|
+
registerRequireHooks
|
|
4
|
+
} from "./chunk-6ZD2GUQ7.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
|
+
};
|
package/dist/index.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
2
|
register
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-QS6DAZFT.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-6ZD2GUQ7.js";
|
|
10
10
|
export {
|
|
11
11
|
IMPORT_META_URL_VARIABLE,
|
|
12
12
|
createRuntime,
|
package/dist/loader.cjs
CHANGED
|
@@ -147,6 +147,7 @@ function installSourceMaps() {
|
|
|
147
147
|
}
|
|
148
148
|
});
|
|
149
149
|
}
|
|
150
|
+
var toNodeLoaderFormat = (format) => format === "esm" ? "module" : "commonjs";
|
|
150
151
|
function createRuntime(cwd = process.cwd()) {
|
|
151
152
|
const loaderConfig = loadHirariConfig(cwd);
|
|
152
153
|
const resolvedPlugins = resolvePlugins(loaderConfig, cwd);
|
|
@@ -181,6 +182,7 @@ async function loaderResolve(specifier, context, next) {
|
|
|
181
182
|
}
|
|
182
183
|
async function loaderLoad(url, context, next, runtime2) {
|
|
183
184
|
const { format: expectedFormat } = runtime2;
|
|
185
|
+
const nodeFormat = toNodeLoaderFormat(expectedFormat);
|
|
184
186
|
if (url.startsWith("file://")) {
|
|
185
187
|
const filename = (0, import_url.fileURLToPath)(url);
|
|
186
188
|
const match = pickPlugin(filename, runtime2.resolvedPlugins);
|
|
@@ -188,14 +190,14 @@ async function loaderLoad(url, context, next, runtime2) {
|
|
|
188
190
|
const source = import_fs3.default.readFileSync(filename, "utf8");
|
|
189
191
|
const result = applyPlugin(source, filename, runtime2);
|
|
190
192
|
return {
|
|
191
|
-
format: result.format || expectedFormat,
|
|
193
|
+
format: toNodeLoaderFormat(result.format || expectedFormat),
|
|
192
194
|
source: result.code,
|
|
193
195
|
shortCircuit: true
|
|
194
196
|
};
|
|
195
197
|
}
|
|
196
198
|
}
|
|
197
199
|
if (next) {
|
|
198
|
-
return next(url, { ...context, format:
|
|
200
|
+
return next(url, { ...context, format: nodeFormat }, next);
|
|
199
201
|
}
|
|
200
202
|
throw new Error("No default loader available for " + url);
|
|
201
203
|
}
|
package/dist/loader.js
CHANGED
package/dist/register-auto.js
CHANGED
package/dist/register.js
CHANGED