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