@hirarijs/loader 1.0.0
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-A4SNU5T3.js +16 -0
- package/dist/chunk-BF2UZLZ5.js +15 -0
- package/dist/chunk-N4I4R23P.js +16 -0
- package/dist/chunk-QGYOU3PW.js +217 -0
- package/dist/chunk-UYG4DQC7.js +217 -0
- package/dist/index.cjs +241 -0
- package/dist/index.d.cts +58 -0
- package/dist/index.d.ts +58 -0
- package/dist/index.js +17 -0
- package/dist/loader.cjs +215 -0
- package/dist/loader.d.cts +4 -0
- package/dist/loader.d.ts +4 -0
- package/dist/loader.js +18 -0
- package/dist/register-auto.cjs +218 -0
- package/dist/register-auto.d.cts +2 -0
- package/dist/register-auto.d.ts +2 -0
- package/dist/register-auto.js +7 -0
- package/dist/register.cjs +231 -0
- package/dist/register.d.cts +5 -0
- package/dist/register.d.ts +5 -0
- package/dist/register.js +7 -0
- package/package.json +44 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
IMPORT_META_URL_VARIABLE: () => IMPORT_META_URL_VARIABLE,
|
|
34
|
+
createRuntime: () => createRuntime,
|
|
35
|
+
loadHirariConfig: () => loadHirariConfig,
|
|
36
|
+
register: () => register,
|
|
37
|
+
registerLoader: () => register,
|
|
38
|
+
resolvePlugins: () => resolvePlugins
|
|
39
|
+
});
|
|
40
|
+
module.exports = __toCommonJS(index_exports);
|
|
41
|
+
|
|
42
|
+
// src/config.ts
|
|
43
|
+
var import_fs = __toESM(require("fs"), 1);
|
|
44
|
+
var import_path = __toESM(require("path"), 1);
|
|
45
|
+
var DEFAULT_CONFIG = {
|
|
46
|
+
format: "cjs",
|
|
47
|
+
plugins: ["@hirarijs/loader-ts", "@hirarijs/loader-tsx", "@hirarijs/loader-vue"]
|
|
48
|
+
};
|
|
49
|
+
function loadHirariConfig(cwd = process.cwd()) {
|
|
50
|
+
const configPath = import_path.default.join(cwd, "hirari.json");
|
|
51
|
+
if (!import_fs.default.existsSync(configPath)) {
|
|
52
|
+
return { ...DEFAULT_CONFIG };
|
|
53
|
+
}
|
|
54
|
+
const raw = import_fs.default.readFileSync(configPath, "utf8");
|
|
55
|
+
let parsed;
|
|
56
|
+
try {
|
|
57
|
+
parsed = JSON.parse(raw);
|
|
58
|
+
} catch (error) {
|
|
59
|
+
throw new Error(`Failed to parse hirari.json: ${error.message}`);
|
|
60
|
+
}
|
|
61
|
+
const loaderConfig = parsed.loader || {};
|
|
62
|
+
return {
|
|
63
|
+
...DEFAULT_CONFIG,
|
|
64
|
+
...loaderConfig,
|
|
65
|
+
plugins: loaderConfig.plugins?.length ? loaderConfig.plugins : DEFAULT_CONFIG.plugins
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function getFormat(config) {
|
|
69
|
+
return config.format === "esm" ? "esm" : "cjs";
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// src/runtime.ts
|
|
73
|
+
var import_fs3 = __toESM(require("fs"), 1);
|
|
74
|
+
var import_module2 = __toESM(require("module"), 1);
|
|
75
|
+
var import_pirates = require("pirates");
|
|
76
|
+
var sourceMapSupport = __toESM(require("source-map-support"), 1);
|
|
77
|
+
|
|
78
|
+
// src/constants.ts
|
|
79
|
+
var IMPORT_META_URL_VARIABLE = "__hirari_loader_import_meta_url__";
|
|
80
|
+
|
|
81
|
+
// src/plugin-manager.ts
|
|
82
|
+
var import_child_process = require("child_process");
|
|
83
|
+
var import_fs2 = __toESM(require("fs"), 1);
|
|
84
|
+
var import_path2 = __toESM(require("path"), 1);
|
|
85
|
+
var import_module = require("module");
|
|
86
|
+
var PACKAGE_MANAGERS = [
|
|
87
|
+
{ lock: "pnpm-lock.yaml", command: "pnpm", args: ["add"] },
|
|
88
|
+
{ lock: "yarn.lock", command: "yarn", args: ["add"] },
|
|
89
|
+
{ lock: "package-lock.json", command: "npm", args: ["install"] },
|
|
90
|
+
{ lock: "npm-shrinkwrap.json", command: "npm", args: ["install"] }
|
|
91
|
+
];
|
|
92
|
+
function detectPackageManager(cwd) {
|
|
93
|
+
for (const pm of PACKAGE_MANAGERS) {
|
|
94
|
+
if (import_fs2.default.existsSync(import_path2.default.join(cwd, pm.lock))) return pm;
|
|
95
|
+
}
|
|
96
|
+
return { command: "npm", args: ["install"] };
|
|
97
|
+
}
|
|
98
|
+
function tryRequire(moduleId, cwd) {
|
|
99
|
+
const req = (0, import_module.createRequire)(import_path2.default.join(cwd, "noop.js"));
|
|
100
|
+
const loaded = req(moduleId);
|
|
101
|
+
return loaded && (loaded.default || loaded);
|
|
102
|
+
}
|
|
103
|
+
function install(pkg, cwd) {
|
|
104
|
+
const pm = detectPackageManager(cwd);
|
|
105
|
+
const result = (0, import_child_process.spawnSync)(pm.command, [...pm.args, pkg], {
|
|
106
|
+
cwd,
|
|
107
|
+
stdio: "inherit",
|
|
108
|
+
env: process.env
|
|
109
|
+
});
|
|
110
|
+
if (result.error) {
|
|
111
|
+
throw result.error;
|
|
112
|
+
}
|
|
113
|
+
if (result.status !== 0) {
|
|
114
|
+
throw new Error(`${pm.command} ${pm.args.join(" ")} ${pkg} failed`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
function resolvePlugins(config, cwd) {
|
|
118
|
+
const plugins = [];
|
|
119
|
+
for (const pluginName of config.plugins || []) {
|
|
120
|
+
let loaded = null;
|
|
121
|
+
try {
|
|
122
|
+
loaded = tryRequire(pluginName, cwd);
|
|
123
|
+
} catch (error) {
|
|
124
|
+
if (config.autoInstall) {
|
|
125
|
+
console.log(`[hirari-loader] installing missing plugin ${pluginName}`);
|
|
126
|
+
install(pluginName, cwd);
|
|
127
|
+
loaded = tryRequire(pluginName, cwd);
|
|
128
|
+
} else {
|
|
129
|
+
throw new Error(
|
|
130
|
+
`Plugin "${pluginName}" not found. Enable autoInstall or install manually.`
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
if (!loaded) continue;
|
|
135
|
+
plugins.push({
|
|
136
|
+
plugin: loaded,
|
|
137
|
+
options: config.pluginOptions?.[pluginName]
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
return plugins;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// src/runtime.ts
|
|
144
|
+
var map = {};
|
|
145
|
+
function installSourceMaps() {
|
|
146
|
+
sourceMapSupport.install({
|
|
147
|
+
handleUncaughtExceptions: false,
|
|
148
|
+
environment: "node",
|
|
149
|
+
retrieveSourceMap(file) {
|
|
150
|
+
if (map[file]) {
|
|
151
|
+
return { url: file, map: map[file] };
|
|
152
|
+
}
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
function createRuntime(cwd = process.cwd()) {
|
|
158
|
+
const loaderConfig = loadHirariConfig(cwd);
|
|
159
|
+
const resolvedPlugins = resolvePlugins(loaderConfig, cwd);
|
|
160
|
+
installSourceMaps();
|
|
161
|
+
return {
|
|
162
|
+
cwd,
|
|
163
|
+
loaderConfig,
|
|
164
|
+
resolvedPlugins,
|
|
165
|
+
format: getFormat(loaderConfig)
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
function pickPlugin(filename, plugins) {
|
|
169
|
+
return plugins.find(({ plugin }) => plugin.match(filename));
|
|
170
|
+
}
|
|
171
|
+
function applyPlugin(code, filename, runtime) {
|
|
172
|
+
const match = pickPlugin(filename, runtime.resolvedPlugins);
|
|
173
|
+
if (!match) return { code };
|
|
174
|
+
const ctx = {
|
|
175
|
+
format: runtime.format,
|
|
176
|
+
loaderConfig: runtime.loaderConfig,
|
|
177
|
+
pluginOptions: match.options
|
|
178
|
+
};
|
|
179
|
+
const result = match.plugin.transform(code, filename, ctx);
|
|
180
|
+
if (result.map) {
|
|
181
|
+
map[filename] = result.map;
|
|
182
|
+
}
|
|
183
|
+
return result;
|
|
184
|
+
}
|
|
185
|
+
function collectExtensions(plugins) {
|
|
186
|
+
const set = /* @__PURE__ */ new Set();
|
|
187
|
+
for (const { plugin } of plugins) {
|
|
188
|
+
plugin.extensions.forEach((ext) => set.add(ext));
|
|
189
|
+
}
|
|
190
|
+
return Array.from(set);
|
|
191
|
+
}
|
|
192
|
+
function registerRequireHooks(runtime) {
|
|
193
|
+
const extensions = collectExtensions(runtime.resolvedPlugins);
|
|
194
|
+
const compile = (code, filename) => {
|
|
195
|
+
const result = applyPlugin(code, filename, runtime);
|
|
196
|
+
const banner = `const ${IMPORT_META_URL_VARIABLE} = require('url').pathToFileURL(__filename).href;`;
|
|
197
|
+
if (!result.code.includes(IMPORT_META_URL_VARIABLE)) {
|
|
198
|
+
return `${banner}${result.code}`;
|
|
199
|
+
}
|
|
200
|
+
return result.code;
|
|
201
|
+
};
|
|
202
|
+
const revert = (0, import_pirates.addHook)(compile, {
|
|
203
|
+
exts: extensions,
|
|
204
|
+
ignoreNodeModules: runtime.loaderConfig.hookIgnoreNodeModules ?? true
|
|
205
|
+
});
|
|
206
|
+
const extensionsObj = import_module2.default.Module._extensions;
|
|
207
|
+
const jsHandler = extensionsObj[".js"];
|
|
208
|
+
extensionsObj[".js"] = function(mod, filename) {
|
|
209
|
+
try {
|
|
210
|
+
return jsHandler.call(this, mod, filename);
|
|
211
|
+
} catch (error) {
|
|
212
|
+
if (error && error.code === "ERR_REQUIRE_ESM") {
|
|
213
|
+
const src = import_fs3.default.readFileSync(filename, "utf8");
|
|
214
|
+
const result = applyPlugin(src, filename, runtime);
|
|
215
|
+
mod._compile(result.code, filename);
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
throw error;
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
return () => {
|
|
222
|
+
revert();
|
|
223
|
+
extensionsObj[".js"] = jsHandler;
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// src/register.ts
|
|
228
|
+
function register(cwd = process.cwd()) {
|
|
229
|
+
const runtime = createRuntime(cwd);
|
|
230
|
+
const unregister = registerRequireHooks(runtime);
|
|
231
|
+
return { unregister };
|
|
232
|
+
}
|
|
233
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
234
|
+
0 && (module.exports = {
|
|
235
|
+
IMPORT_META_URL_VARIABLE,
|
|
236
|
+
createRuntime,
|
|
237
|
+
loadHirariConfig,
|
|
238
|
+
register,
|
|
239
|
+
registerLoader,
|
|
240
|
+
resolvePlugins
|
|
241
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
export { register, register as registerLoader } from './register.cjs';
|
|
2
|
+
|
|
3
|
+
type ModuleFormat = 'cjs' | 'esm';
|
|
4
|
+
interface LoaderPluginContext {
|
|
5
|
+
format: ModuleFormat;
|
|
6
|
+
loaderConfig: LoaderConfig;
|
|
7
|
+
pluginOptions?: Record<string, unknown>;
|
|
8
|
+
}
|
|
9
|
+
interface TransformResult {
|
|
10
|
+
code: string;
|
|
11
|
+
map?: string;
|
|
12
|
+
format?: ModuleFormat;
|
|
13
|
+
}
|
|
14
|
+
interface LoaderPlugin {
|
|
15
|
+
name: string;
|
|
16
|
+
/**
|
|
17
|
+
* File extensions this plugin is responsible for, including leading dot.
|
|
18
|
+
*/
|
|
19
|
+
extensions: string[];
|
|
20
|
+
/**
|
|
21
|
+
* Match is called with the absolute filename. Return true when the plugin should run.
|
|
22
|
+
*/
|
|
23
|
+
match: (filename: string) => boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Synchronous transform hook. Should return already-transformed JS.
|
|
26
|
+
*/
|
|
27
|
+
transform: (code: string, filename: string, ctx: LoaderPluginContext) => TransformResult;
|
|
28
|
+
}
|
|
29
|
+
interface LoaderConfig {
|
|
30
|
+
format?: ModuleFormat;
|
|
31
|
+
plugins?: string[];
|
|
32
|
+
pluginOptions?: Record<string, Record<string, unknown>>;
|
|
33
|
+
autoInstall?: boolean;
|
|
34
|
+
hookIgnoreNodeModules?: boolean;
|
|
35
|
+
}
|
|
36
|
+
interface HirariConfig {
|
|
37
|
+
loader?: LoaderConfig;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
declare function loadHirariConfig(cwd?: string): LoaderConfig;
|
|
41
|
+
|
|
42
|
+
interface ResolvedPlugin {
|
|
43
|
+
plugin: LoaderPlugin;
|
|
44
|
+
options: Record<string, unknown> | undefined;
|
|
45
|
+
}
|
|
46
|
+
declare function resolvePlugins(config: LoaderConfig, cwd: string): ResolvedPlugin[];
|
|
47
|
+
|
|
48
|
+
interface RuntimeContext {
|
|
49
|
+
cwd: string;
|
|
50
|
+
loaderConfig: LoaderConfig;
|
|
51
|
+
resolvedPlugins: ResolvedPlugin[];
|
|
52
|
+
format: ModuleFormat;
|
|
53
|
+
}
|
|
54
|
+
declare function createRuntime(cwd?: string): RuntimeContext;
|
|
55
|
+
|
|
56
|
+
declare const IMPORT_META_URL_VARIABLE = "__hirari_loader_import_meta_url__";
|
|
57
|
+
|
|
58
|
+
export { type HirariConfig, IMPORT_META_URL_VARIABLE, type LoaderConfig, type LoaderPlugin, type LoaderPluginContext, type ModuleFormat, type TransformResult, createRuntime, loadHirariConfig, resolvePlugins };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
export { register, register as registerLoader } from './register.js';
|
|
2
|
+
|
|
3
|
+
type ModuleFormat = 'cjs' | 'esm';
|
|
4
|
+
interface LoaderPluginContext {
|
|
5
|
+
format: ModuleFormat;
|
|
6
|
+
loaderConfig: LoaderConfig;
|
|
7
|
+
pluginOptions?: Record<string, unknown>;
|
|
8
|
+
}
|
|
9
|
+
interface TransformResult {
|
|
10
|
+
code: string;
|
|
11
|
+
map?: string;
|
|
12
|
+
format?: ModuleFormat;
|
|
13
|
+
}
|
|
14
|
+
interface LoaderPlugin {
|
|
15
|
+
name: string;
|
|
16
|
+
/**
|
|
17
|
+
* File extensions this plugin is responsible for, including leading dot.
|
|
18
|
+
*/
|
|
19
|
+
extensions: string[];
|
|
20
|
+
/**
|
|
21
|
+
* Match is called with the absolute filename. Return true when the plugin should run.
|
|
22
|
+
*/
|
|
23
|
+
match: (filename: string) => boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Synchronous transform hook. Should return already-transformed JS.
|
|
26
|
+
*/
|
|
27
|
+
transform: (code: string, filename: string, ctx: LoaderPluginContext) => TransformResult;
|
|
28
|
+
}
|
|
29
|
+
interface LoaderConfig {
|
|
30
|
+
format?: ModuleFormat;
|
|
31
|
+
plugins?: string[];
|
|
32
|
+
pluginOptions?: Record<string, Record<string, unknown>>;
|
|
33
|
+
autoInstall?: boolean;
|
|
34
|
+
hookIgnoreNodeModules?: boolean;
|
|
35
|
+
}
|
|
36
|
+
interface HirariConfig {
|
|
37
|
+
loader?: LoaderConfig;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
declare function loadHirariConfig(cwd?: string): LoaderConfig;
|
|
41
|
+
|
|
42
|
+
interface ResolvedPlugin {
|
|
43
|
+
plugin: LoaderPlugin;
|
|
44
|
+
options: Record<string, unknown> | undefined;
|
|
45
|
+
}
|
|
46
|
+
declare function resolvePlugins(config: LoaderConfig, cwd: string): ResolvedPlugin[];
|
|
47
|
+
|
|
48
|
+
interface RuntimeContext {
|
|
49
|
+
cwd: string;
|
|
50
|
+
loaderConfig: LoaderConfig;
|
|
51
|
+
resolvedPlugins: ResolvedPlugin[];
|
|
52
|
+
format: ModuleFormat;
|
|
53
|
+
}
|
|
54
|
+
declare function createRuntime(cwd?: string): RuntimeContext;
|
|
55
|
+
|
|
56
|
+
declare const IMPORT_META_URL_VARIABLE = "__hirari_loader_import_meta_url__";
|
|
57
|
+
|
|
58
|
+
export { type HirariConfig, IMPORT_META_URL_VARIABLE, type LoaderConfig, type LoaderPlugin, type LoaderPluginContext, type ModuleFormat, type TransformResult, createRuntime, loadHirariConfig, resolvePlugins };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import {
|
|
2
|
+
register
|
|
3
|
+
} from "./chunk-BF2UZLZ5.js";
|
|
4
|
+
import {
|
|
5
|
+
IMPORT_META_URL_VARIABLE,
|
|
6
|
+
createRuntime,
|
|
7
|
+
loadHirariConfig,
|
|
8
|
+
resolvePlugins
|
|
9
|
+
} from "./chunk-QGYOU3PW.js";
|
|
10
|
+
export {
|
|
11
|
+
IMPORT_META_URL_VARIABLE,
|
|
12
|
+
createRuntime,
|
|
13
|
+
loadHirariConfig,
|
|
14
|
+
register,
|
|
15
|
+
register as registerLoader,
|
|
16
|
+
resolvePlugins
|
|
17
|
+
};
|
package/dist/loader.cjs
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/loader.ts
|
|
31
|
+
var loader_exports = {};
|
|
32
|
+
__export(loader_exports, {
|
|
33
|
+
load: () => load,
|
|
34
|
+
resolve: () => resolve
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(loader_exports);
|
|
37
|
+
|
|
38
|
+
// src/runtime.ts
|
|
39
|
+
var import_fs3 = __toESM(require("fs"), 1);
|
|
40
|
+
var import_url = require("url");
|
|
41
|
+
var import_pirates = require("pirates");
|
|
42
|
+
var sourceMapSupport = __toESM(require("source-map-support"), 1);
|
|
43
|
+
|
|
44
|
+
// src/config.ts
|
|
45
|
+
var import_fs = __toESM(require("fs"), 1);
|
|
46
|
+
var import_path = __toESM(require("path"), 1);
|
|
47
|
+
var DEFAULT_CONFIG = {
|
|
48
|
+
format: "cjs",
|
|
49
|
+
plugins: ["@hirarijs/loader-ts", "@hirarijs/loader-tsx", "@hirarijs/loader-vue"]
|
|
50
|
+
};
|
|
51
|
+
function loadHirariConfig(cwd = process.cwd()) {
|
|
52
|
+
const configPath = import_path.default.join(cwd, "hirari.json");
|
|
53
|
+
if (!import_fs.default.existsSync(configPath)) {
|
|
54
|
+
return { ...DEFAULT_CONFIG };
|
|
55
|
+
}
|
|
56
|
+
const raw = import_fs.default.readFileSync(configPath, "utf8");
|
|
57
|
+
let parsed;
|
|
58
|
+
try {
|
|
59
|
+
parsed = JSON.parse(raw);
|
|
60
|
+
} catch (error) {
|
|
61
|
+
throw new Error(`Failed to parse hirari.json: ${error.message}`);
|
|
62
|
+
}
|
|
63
|
+
const loaderConfig = parsed.loader || {};
|
|
64
|
+
return {
|
|
65
|
+
...DEFAULT_CONFIG,
|
|
66
|
+
...loaderConfig,
|
|
67
|
+
plugins: loaderConfig.plugins?.length ? loaderConfig.plugins : DEFAULT_CONFIG.plugins
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function getFormat(config) {
|
|
71
|
+
return config.format === "esm" ? "esm" : "cjs";
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// src/plugin-manager.ts
|
|
75
|
+
var import_child_process = require("child_process");
|
|
76
|
+
var import_fs2 = __toESM(require("fs"), 1);
|
|
77
|
+
var import_path2 = __toESM(require("path"), 1);
|
|
78
|
+
var import_module = require("module");
|
|
79
|
+
var PACKAGE_MANAGERS = [
|
|
80
|
+
{ lock: "pnpm-lock.yaml", command: "pnpm", args: ["add"] },
|
|
81
|
+
{ lock: "yarn.lock", command: "yarn", args: ["add"] },
|
|
82
|
+
{ lock: "package-lock.json", command: "npm", args: ["install"] },
|
|
83
|
+
{ lock: "npm-shrinkwrap.json", command: "npm", args: ["install"] }
|
|
84
|
+
];
|
|
85
|
+
function detectPackageManager(cwd) {
|
|
86
|
+
for (const pm of PACKAGE_MANAGERS) {
|
|
87
|
+
if (import_fs2.default.existsSync(import_path2.default.join(cwd, pm.lock))) return pm;
|
|
88
|
+
}
|
|
89
|
+
return { command: "npm", args: ["install"] };
|
|
90
|
+
}
|
|
91
|
+
function tryRequire(moduleId, cwd) {
|
|
92
|
+
const req = (0, import_module.createRequire)(import_path2.default.join(cwd, "noop.js"));
|
|
93
|
+
const loaded = req(moduleId);
|
|
94
|
+
return loaded && (loaded.default || loaded);
|
|
95
|
+
}
|
|
96
|
+
function install(pkg, cwd) {
|
|
97
|
+
const pm = detectPackageManager(cwd);
|
|
98
|
+
const result = (0, import_child_process.spawnSync)(pm.command, [...pm.args, pkg], {
|
|
99
|
+
cwd,
|
|
100
|
+
stdio: "inherit",
|
|
101
|
+
env: process.env
|
|
102
|
+
});
|
|
103
|
+
if (result.error) {
|
|
104
|
+
throw result.error;
|
|
105
|
+
}
|
|
106
|
+
if (result.status !== 0) {
|
|
107
|
+
throw new Error(`${pm.command} ${pm.args.join(" ")} ${pkg} failed`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
function resolvePlugins(config, cwd) {
|
|
111
|
+
const plugins = [];
|
|
112
|
+
for (const pluginName of config.plugins || []) {
|
|
113
|
+
let loaded = null;
|
|
114
|
+
try {
|
|
115
|
+
loaded = tryRequire(pluginName, cwd);
|
|
116
|
+
} catch (error) {
|
|
117
|
+
if (config.autoInstall) {
|
|
118
|
+
console.log(`[hirari-loader] installing missing plugin ${pluginName}`);
|
|
119
|
+
install(pluginName, cwd);
|
|
120
|
+
loaded = tryRequire(pluginName, cwd);
|
|
121
|
+
} else {
|
|
122
|
+
throw new Error(
|
|
123
|
+
`Plugin "${pluginName}" not found. Enable autoInstall or install manually.`
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (!loaded) continue;
|
|
128
|
+
plugins.push({
|
|
129
|
+
plugin: loaded,
|
|
130
|
+
options: config.pluginOptions?.[pluginName]
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
return plugins;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// src/runtime.ts
|
|
137
|
+
var map = {};
|
|
138
|
+
function installSourceMaps() {
|
|
139
|
+
sourceMapSupport.install({
|
|
140
|
+
handleUncaughtExceptions: false,
|
|
141
|
+
environment: "node",
|
|
142
|
+
retrieveSourceMap(file) {
|
|
143
|
+
if (map[file]) {
|
|
144
|
+
return { url: file, map: map[file] };
|
|
145
|
+
}
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
function createRuntime(cwd = process.cwd()) {
|
|
151
|
+
const loaderConfig = loadHirariConfig(cwd);
|
|
152
|
+
const resolvedPlugins = resolvePlugins(loaderConfig, cwd);
|
|
153
|
+
installSourceMaps();
|
|
154
|
+
return {
|
|
155
|
+
cwd,
|
|
156
|
+
loaderConfig,
|
|
157
|
+
resolvedPlugins,
|
|
158
|
+
format: getFormat(loaderConfig)
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
function pickPlugin(filename, plugins) {
|
|
162
|
+
return plugins.find(({ plugin }) => plugin.match(filename));
|
|
163
|
+
}
|
|
164
|
+
function applyPlugin(code, filename, runtime2) {
|
|
165
|
+
const match = pickPlugin(filename, runtime2.resolvedPlugins);
|
|
166
|
+
if (!match) return { code };
|
|
167
|
+
const ctx = {
|
|
168
|
+
format: runtime2.format,
|
|
169
|
+
loaderConfig: runtime2.loaderConfig,
|
|
170
|
+
pluginOptions: match.options
|
|
171
|
+
};
|
|
172
|
+
const result = match.plugin.transform(code, filename, ctx);
|
|
173
|
+
if (result.map) {
|
|
174
|
+
map[filename] = result.map;
|
|
175
|
+
}
|
|
176
|
+
return result;
|
|
177
|
+
}
|
|
178
|
+
async function loaderResolve(specifier, context, next) {
|
|
179
|
+
if (next) return next(specifier, context, next);
|
|
180
|
+
return { url: specifier };
|
|
181
|
+
}
|
|
182
|
+
async function loaderLoad(url, context, next, runtime2) {
|
|
183
|
+
const { format: expectedFormat } = runtime2;
|
|
184
|
+
if (url.startsWith("file://")) {
|
|
185
|
+
const filename = (0, import_url.fileURLToPath)(url);
|
|
186
|
+
const match = pickPlugin(filename, runtime2.resolvedPlugins);
|
|
187
|
+
if (match) {
|
|
188
|
+
const source = import_fs3.default.readFileSync(filename, "utf8");
|
|
189
|
+
const result = applyPlugin(source, filename, runtime2);
|
|
190
|
+
return {
|
|
191
|
+
format: result.format || expectedFormat,
|
|
192
|
+
source: result.code,
|
|
193
|
+
shortCircuit: true
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (next) {
|
|
198
|
+
return next(url, { ...context, format: expectedFormat }, next);
|
|
199
|
+
}
|
|
200
|
+
throw new Error("No default loader available for " + url);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// src/loader.ts
|
|
204
|
+
var runtime = createRuntime();
|
|
205
|
+
async function resolve(specifier, context, next) {
|
|
206
|
+
return loaderResolve(specifier, context, next);
|
|
207
|
+
}
|
|
208
|
+
async function load(url, context, next) {
|
|
209
|
+
return loaderLoad(url, context, next, runtime);
|
|
210
|
+
}
|
|
211
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
212
|
+
0 && (module.exports = {
|
|
213
|
+
load,
|
|
214
|
+
resolve
|
|
215
|
+
});
|
package/dist/loader.d.ts
ADDED
package/dist/loader.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createRuntime,
|
|
3
|
+
loaderLoad,
|
|
4
|
+
loaderResolve
|
|
5
|
+
} from "./chunk-QGYOU3PW.js";
|
|
6
|
+
|
|
7
|
+
// src/loader.ts
|
|
8
|
+
var runtime = createRuntime();
|
|
9
|
+
async function resolve(specifier, context, next) {
|
|
10
|
+
return loaderResolve(specifier, context, next);
|
|
11
|
+
}
|
|
12
|
+
async function load(url, context, next) {
|
|
13
|
+
return loaderLoad(url, context, next, runtime);
|
|
14
|
+
}
|
|
15
|
+
export {
|
|
16
|
+
load,
|
|
17
|
+
resolve
|
|
18
|
+
};
|