@hirarijs/loader 1.0.3 → 1.0.4

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,15 @@
1
+ import {
2
+ createRuntime,
3
+ registerRequireHooks
4
+ } from "./chunk-OFSK6WK7.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,221 @@
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
+ throw new Error("No default loader available for " + url);
207
+ }
208
+ const forwarded = await next(url, context, next);
209
+ if (forwarded) return forwarded;
210
+ throw new Error("Loader did not return a result for " + url);
211
+ }
212
+
213
+ export {
214
+ loadHirariConfig,
215
+ IMPORT_META_URL_VARIABLE,
216
+ resolvePlugins,
217
+ createRuntime,
218
+ registerRequireHooks,
219
+ loaderResolve,
220
+ loaderLoad
221
+ };
@@ -0,0 +1,220 @@
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
+ if (url.startsWith("file://")) {
192
+ const filename = fileURLToPath(url);
193
+ const match = pickPlugin(filename, runtime.resolvedPlugins);
194
+ if (match) {
195
+ const source = fs3.readFileSync(filename, "utf8");
196
+ const result = applyPlugin(source, filename, runtime);
197
+ return {
198
+ format: toNodeLoaderFormat(result.format || expectedFormat),
199
+ source: result.code,
200
+ shortCircuit: true
201
+ };
202
+ }
203
+ }
204
+ if (!next) {
205
+ throw new Error("No default loader available for " + url);
206
+ }
207
+ const forwarded = await next(url, context);
208
+ if (forwarded) return forwarded;
209
+ throw new Error("Loader did not return a result for " + url);
210
+ }
211
+
212
+ export {
213
+ loadHirariConfig,
214
+ IMPORT_META_URL_VARIABLE,
215
+ resolvePlugins,
216
+ createRuntime,
217
+ registerRequireHooks,
218
+ loaderResolve,
219
+ loaderLoad
220
+ };
@@ -0,0 +1,15 @@
1
+ import {
2
+ createRuntime,
3
+ registerRequireHooks
4
+ } from "./chunk-VFNKWDE2.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-QS6DAZFT.js";
3
+ } from "./chunk-XOMP5MYL.js";
4
4
  import {
5
5
  IMPORT_META_URL_VARIABLE,
6
6
  createRuntime,
7
7
  loadHirariConfig,
8
8
  resolvePlugins
9
- } from "./chunk-6ZD2GUQ7.js";
9
+ } from "./chunk-VFNKWDE2.js";
10
10
  export {
11
11
  IMPORT_META_URL_VARIABLE,
12
12
  createRuntime,
package/dist/loader.cjs CHANGED
@@ -182,7 +182,6 @@ async function loaderResolve(specifier, context, next) {
182
182
  }
183
183
  async function loaderLoad(url, context, next, runtime2) {
184
184
  const { format: expectedFormat } = runtime2;
185
- const nodeFormat = toNodeLoaderFormat(expectedFormat);
186
185
  if (url.startsWith("file://")) {
187
186
  const filename = (0, import_url.fileURLToPath)(url);
188
187
  const match = pickPlugin(filename, runtime2.resolvedPlugins);
@@ -196,10 +195,12 @@ async function loaderLoad(url, context, next, runtime2) {
196
195
  };
197
196
  }
198
197
  }
199
- if (next) {
200
- return next(url, { ...context, format: nodeFormat }, next);
198
+ if (!next) {
199
+ throw new Error("No default loader available for " + url);
201
200
  }
202
- throw new Error("No default loader available for " + url);
201
+ const forwarded = await next(url, context);
202
+ if (forwarded) return forwarded;
203
+ throw new Error("Loader did not return a result for " + url);
203
204
  }
204
205
 
205
206
  // src/loader.ts
package/dist/loader.js CHANGED
@@ -2,7 +2,7 @@ import {
2
2
  createRuntime,
3
3
  loaderLoad,
4
4
  loaderResolve
5
- } from "./chunk-6ZD2GUQ7.js";
5
+ } from "./chunk-VFNKWDE2.js";
6
6
 
7
7
  // src/loader.ts
8
8
  var runtime = createRuntime();
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  register
3
- } from "./chunk-QS6DAZFT.js";
4
- import "./chunk-6ZD2GUQ7.js";
3
+ } from "./chunk-XOMP5MYL.js";
4
+ import "./chunk-VFNKWDE2.js";
5
5
 
6
6
  // src/register-auto.ts
7
7
  register();
package/dist/register.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  register
3
- } from "./chunk-QS6DAZFT.js";
4
- import "./chunk-6ZD2GUQ7.js";
3
+ } from "./chunk-XOMP5MYL.js";
4
+ import "./chunk-VFNKWDE2.js";
5
5
  export {
6
6
  register
7
7
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hirarijs/loader",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "Runtime loader for HirariJS that wires loader plugins and config",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",