@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.
@@ -0,0 +1,16 @@
1
+ import {
2
+ createRuntime,
3
+ registerRequireHooks
4
+ } from "./chunk-QGYOU3PW.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
+ register();
13
+
14
+ export {
15
+ register
16
+ };
@@ -0,0 +1,15 @@
1
+ import {
2
+ createRuntime,
3
+ registerRequireHooks
4
+ } from "./chunk-QGYOU3PW.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,16 @@
1
+ import {
2
+ createRuntime,
3
+ registerRequireHooks
4
+ } from "./chunk-UYG4DQC7.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
+ register();
13
+
14
+ export {
15
+ register
16
+ };
@@ -0,0 +1,217 @@
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
+ function createRuntime(cwd = process.cwd()) {
116
+ const loaderConfig = loadHirariConfig(cwd);
117
+ const resolvedPlugins = resolvePlugins(loaderConfig, cwd);
118
+ installSourceMaps();
119
+ return {
120
+ cwd,
121
+ loaderConfig,
122
+ resolvedPlugins,
123
+ format: getFormat(loaderConfig)
124
+ };
125
+ }
126
+ function pickPlugin(filename, plugins) {
127
+ return plugins.find(({ plugin }) => plugin.match(filename));
128
+ }
129
+ function applyPlugin(code, filename, runtime) {
130
+ const match = pickPlugin(filename, runtime.resolvedPlugins);
131
+ if (!match) return { code };
132
+ const ctx = {
133
+ format: runtime.format,
134
+ loaderConfig: runtime.loaderConfig,
135
+ pluginOptions: match.options
136
+ };
137
+ const result = match.plugin.transform(code, filename, ctx);
138
+ if (result.map) {
139
+ map[filename] = result.map;
140
+ }
141
+ return result;
142
+ }
143
+ function collectExtensions(plugins) {
144
+ const set = /* @__PURE__ */ new Set();
145
+ for (const { plugin } of plugins) {
146
+ plugin.extensions.forEach((ext) => set.add(ext));
147
+ }
148
+ return Array.from(set);
149
+ }
150
+ function registerRequireHooks(runtime) {
151
+ const extensions = collectExtensions(runtime.resolvedPlugins);
152
+ const compile = (code, filename) => {
153
+ const result = applyPlugin(code, filename, runtime);
154
+ const banner = `const ${IMPORT_META_URL_VARIABLE} = require('url').pathToFileURL(__filename).href;`;
155
+ if (!result.code.includes(IMPORT_META_URL_VARIABLE)) {
156
+ return `${banner}${result.code}`;
157
+ }
158
+ return result.code;
159
+ };
160
+ const revert = addHook(compile, {
161
+ exts: extensions,
162
+ ignoreNodeModules: runtime.loaderConfig.hookIgnoreNodeModules ?? true
163
+ });
164
+ const extensionsObj = module.Module._extensions;
165
+ const jsHandler = extensionsObj[".js"];
166
+ extensionsObj[".js"] = function(mod, filename) {
167
+ try {
168
+ return jsHandler.call(this, mod, filename);
169
+ } catch (error) {
170
+ if (error && error.code === "ERR_REQUIRE_ESM") {
171
+ const src = fs3.readFileSync(filename, "utf8");
172
+ const result = applyPlugin(src, filename, runtime);
173
+ mod._compile(result.code, filename);
174
+ return;
175
+ }
176
+ throw error;
177
+ }
178
+ };
179
+ return () => {
180
+ revert();
181
+ extensionsObj[".js"] = jsHandler;
182
+ };
183
+ }
184
+ async function loaderResolve(specifier, context, next) {
185
+ if (next) return next(specifier, context, next);
186
+ return { url: specifier };
187
+ }
188
+ async function loaderLoad(url, context, next, runtime) {
189
+ const { format: expectedFormat } = runtime;
190
+ if (url.startsWith("file://")) {
191
+ const filename = fileURLToPath(url);
192
+ const match = pickPlugin(filename, runtime.resolvedPlugins);
193
+ if (match) {
194
+ const source = fs3.readFileSync(filename, "utf8");
195
+ const result = applyPlugin(source, filename, runtime);
196
+ return {
197
+ format: result.format || expectedFormat,
198
+ source: result.code,
199
+ shortCircuit: true
200
+ };
201
+ }
202
+ }
203
+ if (next) {
204
+ return next(url, { ...context, format: expectedFormat }, next);
205
+ }
206
+ throw new Error("No default loader available for " + url);
207
+ }
208
+
209
+ export {
210
+ loadHirariConfig,
211
+ IMPORT_META_URL_VARIABLE,
212
+ resolvePlugins,
213
+ createRuntime,
214
+ registerRequireHooks,
215
+ loaderResolve,
216
+ loaderLoad
217
+ };
@@ -0,0 +1,217 @@
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 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
+ function createRuntime(cwd = process.cwd()) {
116
+ const loaderConfig = loadHirariConfig(cwd);
117
+ const resolvedPlugins = resolvePlugins(loaderConfig, cwd);
118
+ installSourceMaps();
119
+ return {
120
+ cwd,
121
+ loaderConfig,
122
+ resolvedPlugins,
123
+ format: getFormat(loaderConfig)
124
+ };
125
+ }
126
+ function pickPlugin(filename, plugins) {
127
+ return plugins.find(({ plugin }) => plugin.match(filename));
128
+ }
129
+ function applyPlugin(code, filename, runtime) {
130
+ const match = pickPlugin(filename, runtime.resolvedPlugins);
131
+ if (!match) return { code };
132
+ const ctx = {
133
+ format: runtime.format,
134
+ loaderConfig: runtime.loaderConfig,
135
+ pluginOptions: match.options
136
+ };
137
+ const result = match.plugin.transform(code, filename, ctx);
138
+ if (result.map) {
139
+ map[filename] = result.map;
140
+ }
141
+ return result;
142
+ }
143
+ function collectExtensions(plugins) {
144
+ const set = /* @__PURE__ */ new Set();
145
+ for (const { plugin } of plugins) {
146
+ plugin.extensions.forEach((ext) => set.add(ext));
147
+ }
148
+ return Array.from(set);
149
+ }
150
+ function registerRequireHooks(runtime) {
151
+ const extensions = collectExtensions(runtime.resolvedPlugins);
152
+ const compile = (code, filename) => {
153
+ const result = applyPlugin(code, filename, runtime);
154
+ const banner = `const ${IMPORT_META_URL_VARIABLE} = require('url').pathToFileURL(__filename).href;`;
155
+ if (!result.code.includes(IMPORT_META_URL_VARIABLE)) {
156
+ return `${banner}${result.code}`;
157
+ }
158
+ return result.code;
159
+ };
160
+ const revert = addHook(compile, {
161
+ exts: extensions,
162
+ ignoreNodeModules: runtime.loaderConfig.hookIgnoreNodeModules ?? true
163
+ });
164
+ const extensionsObj = module.Module._extensions;
165
+ const jsHandler = extensionsObj[".js"];
166
+ extensionsObj[".js"] = function(mod, filename) {
167
+ try {
168
+ return jsHandler.call(this, mod, filename);
169
+ } catch (error) {
170
+ if (error && error.code === "ERR_REQUIRE_ESM") {
171
+ const src = fs3.readFileSync(filename, "utf8");
172
+ const result = applyPlugin(src, filename, runtime);
173
+ mod._compile(result.code, filename);
174
+ return;
175
+ }
176
+ throw error;
177
+ }
178
+ };
179
+ return () => {
180
+ revert();
181
+ extensionsObj[".js"] = jsHandler;
182
+ };
183
+ }
184
+ async function loaderResolve(specifier, context, next) {
185
+ if (next) return next(specifier, context, next);
186
+ return { url: specifier };
187
+ }
188
+ async function loaderLoad(url, context, next, runtime) {
189
+ const { format: expectedFormat } = runtime;
190
+ if (url.startsWith("file://")) {
191
+ const filename = fileURLToPath(url);
192
+ const match = pickPlugin(filename, runtime.resolvedPlugins);
193
+ if (match) {
194
+ const source = fs3.readFileSync(filename, "utf8");
195
+ const result = applyPlugin(source, filename, runtime);
196
+ return {
197
+ format: result.format || expectedFormat,
198
+ source: result.code,
199
+ shortCircuit: true
200
+ };
201
+ }
202
+ }
203
+ if (next) {
204
+ return next(url, { ...context, format: expectedFormat }, next);
205
+ }
206
+ throw new Error("No default loader available for " + url);
207
+ }
208
+
209
+ export {
210
+ loadHirariConfig,
211
+ IMPORT_META_URL_VARIABLE,
212
+ resolvePlugins,
213
+ createRuntime,
214
+ registerRequireHooks,
215
+ loaderResolve,
216
+ loaderLoad
217
+ };