@hirarijs/loader-ts 1.0.4 → 1.0.6
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/index.cjs +51 -20
- package/dist/index.js +51 -20
- package/package.json +2 -1
package/dist/index.cjs
CHANGED
|
@@ -213379,18 +213379,36 @@ var import_esbuild = require("esbuild");
|
|
|
213379
213379
|
var import_loader = require("@hirarijs/loader");
|
|
213380
213380
|
var import_path = __toESM(require("path"), 1);
|
|
213381
213381
|
var import_typescript = __toESM(require_typescript(), 1);
|
|
213382
|
-
var
|
|
213383
|
-
|
|
213384
|
-
|
|
213385
|
-
|
|
213386
|
-
|
|
213387
|
-
|
|
213388
|
-
|
|
213382
|
+
var resolverCache = /* @__PURE__ */ new Map();
|
|
213383
|
+
var nearestConfigCache = /* @__PURE__ */ new Map();
|
|
213384
|
+
function findNearestTsconfig(start) {
|
|
213385
|
+
if (nearestConfigCache.has(start)) return nearestConfigCache.get(start);
|
|
213386
|
+
let dir = start;
|
|
213387
|
+
while (true) {
|
|
213388
|
+
const candidate = import_path.default.join(dir, "tsconfig.json");
|
|
213389
|
+
if (import_typescript.default.sys.fileExists(candidate)) {
|
|
213390
|
+
nearestConfigCache.set(start, candidate);
|
|
213391
|
+
return candidate;
|
|
213392
|
+
}
|
|
213393
|
+
const parent = import_path.default.dirname(dir);
|
|
213394
|
+
if (parent === dir) break;
|
|
213395
|
+
dir = parent;
|
|
213396
|
+
}
|
|
213397
|
+
nearestConfigCache.set(start, null);
|
|
213398
|
+
return null;
|
|
213399
|
+
}
|
|
213400
|
+
function createTsResolveForConfig(tsconfigPath, debug = false) {
|
|
213401
|
+
if (resolverCache.has(tsconfigPath)) return resolverCache.get(tsconfigPath);
|
|
213402
|
+
const configFile = import_typescript.default.readConfigFile(tsconfigPath, import_typescript.default.sys.readFile);
|
|
213403
|
+
if (configFile.error) {
|
|
213404
|
+
return null;
|
|
213405
|
+
}
|
|
213406
|
+
const parsed = import_typescript.default.parseJsonConfigFileContent(
|
|
213389
213407
|
configFile.config,
|
|
213390
213408
|
import_typescript.default.sys,
|
|
213391
|
-
import_path.default.dirname(
|
|
213409
|
+
import_path.default.dirname(tsconfigPath)
|
|
213392
213410
|
);
|
|
213393
|
-
const options =
|
|
213411
|
+
const options = parsed.options;
|
|
213394
213412
|
const host = {
|
|
213395
213413
|
fileExists: import_typescript.default.sys.fileExists,
|
|
213396
213414
|
readFile: import_typescript.default.sys.readFile,
|
|
@@ -213399,18 +213417,27 @@ function createTsResolve(debug = false) {
|
|
|
213399
213417
|
getCurrentDirectory: import_typescript.default.sys.getCurrentDirectory,
|
|
213400
213418
|
getDirectories: import_typescript.default.sys.getDirectories
|
|
213401
213419
|
};
|
|
213402
|
-
|
|
213420
|
+
const resolver = (specifier, importer) => {
|
|
213403
213421
|
const result = import_typescript.default.resolveModuleName(specifier, importer, options, host);
|
|
213404
213422
|
const resolved = result.resolvedModule?.resolvedFileName;
|
|
213405
|
-
if (resolved &&
|
|
213406
|
-
|
|
213423
|
+
if (resolved && !resolved.endsWith(".d.ts")) {
|
|
213424
|
+
if (debug) {
|
|
213425
|
+
console.log(`[loader-ts] resolved ${specifier} -> ${resolved}`);
|
|
213426
|
+
}
|
|
213427
|
+
return resolved;
|
|
213407
213428
|
}
|
|
213408
|
-
return
|
|
213429
|
+
return null;
|
|
213409
213430
|
};
|
|
213410
213431
|
if (debug) {
|
|
213411
|
-
console.log("[loader-ts] tsconfig:",
|
|
213432
|
+
console.log("[loader-ts] tsconfig:", tsconfigPath);
|
|
213412
213433
|
}
|
|
213413
|
-
|
|
213434
|
+
resolverCache.set(tsconfigPath, resolver);
|
|
213435
|
+
return resolver;
|
|
213436
|
+
}
|
|
213437
|
+
function createTsResolve(importer, debug = false) {
|
|
213438
|
+
const tsconfigPath = findNearestTsconfig(import_path.default.dirname(importer));
|
|
213439
|
+
if (!tsconfigPath) return null;
|
|
213440
|
+
return createTsResolveForConfig(tsconfigPath, debug);
|
|
213414
213441
|
}
|
|
213415
213442
|
function rewriteImports(code, filename, resolve, debug = false) {
|
|
213416
213443
|
const importLike = /(import\s+[^'";]+?from\s+['"]([^'"]+)['"])|(import\s+['"]([^'"]+)['"])|(export\s+[^'";]+?from\s+['"]([^'"]+)['"])/g;
|
|
@@ -213429,16 +213456,16 @@ function rewriteImports(code, filename, resolve, debug = false) {
|
|
|
213429
213456
|
return full.replace(spec, normalized);
|
|
213430
213457
|
});
|
|
213431
213458
|
}
|
|
213432
|
-
var extensions = [".ts", ".mts", ".cts"];
|
|
213459
|
+
var extensions = [".ts", ".mts", ".cts", ".js", ".mjs", ".cjs"];
|
|
213433
213460
|
function runTransform(code, filename, ctx) {
|
|
213434
|
-
const pathResolver = createTsResolve(ctx.loaderConfig.debug);
|
|
213461
|
+
const pathResolver = createTsResolve(filename, ctx.loaderConfig.debug);
|
|
213462
|
+
const rewrittenCode = pathResolver && !filename.includes("node_modules") ? rewriteImports(code, filename, pathResolver, ctx.loaderConfig.debug) : code;
|
|
213435
213463
|
if (ctx.loaderConfig.debug) {
|
|
213436
213464
|
console.log(`[loader-ts] transforming ${filename}`);
|
|
213437
213465
|
if (!pathResolver) {
|
|
213438
|
-
console.log("[loader-ts] no tsconfig resolver created");
|
|
213466
|
+
console.log("[loader-ts] no tsconfig resolver created for", filename);
|
|
213439
213467
|
}
|
|
213440
213468
|
}
|
|
213441
|
-
const rewrittenCode = pathResolver ? rewriteImports(code, filename, pathResolver, ctx.loaderConfig.debug) : code;
|
|
213442
213469
|
const banner = ctx.format === "cjs" ? `const ${import_loader.IMPORT_META_URL_VARIABLE} = require('url').pathToFileURL(__filename).href;` : void 0;
|
|
213443
213470
|
const result = (0, import_esbuild.transformSync)(rewrittenCode, {
|
|
213444
213471
|
loader: "ts",
|
|
@@ -213459,7 +213486,11 @@ function runTransform(code, filename, ctx) {
|
|
|
213459
213486
|
var plugin = {
|
|
213460
213487
|
name: "@hirarijs/loader-ts",
|
|
213461
213488
|
extensions,
|
|
213462
|
-
match: (filename) =>
|
|
213489
|
+
match: (filename) => {
|
|
213490
|
+
const ext = import_path.default.extname(filename);
|
|
213491
|
+
if (!ext) return true;
|
|
213492
|
+
return extensions.includes(ext);
|
|
213493
|
+
},
|
|
213463
213494
|
transform: runTransform
|
|
213464
213495
|
};
|
|
213465
213496
|
var index_default = plugin;
|
package/dist/index.js
CHANGED
|
@@ -213376,18 +213376,36 @@ import {
|
|
|
213376
213376
|
IMPORT_META_URL_VARIABLE
|
|
213377
213377
|
} from "@hirarijs/loader";
|
|
213378
213378
|
import path from "path";
|
|
213379
|
-
var
|
|
213380
|
-
|
|
213381
|
-
|
|
213382
|
-
|
|
213383
|
-
|
|
213384
|
-
|
|
213385
|
-
|
|
213379
|
+
var resolverCache = /* @__PURE__ */ new Map();
|
|
213380
|
+
var nearestConfigCache = /* @__PURE__ */ new Map();
|
|
213381
|
+
function findNearestTsconfig(start) {
|
|
213382
|
+
if (nearestConfigCache.has(start)) return nearestConfigCache.get(start);
|
|
213383
|
+
let dir = start;
|
|
213384
|
+
while (true) {
|
|
213385
|
+
const candidate = path.join(dir, "tsconfig.json");
|
|
213386
|
+
if (import_typescript.default.sys.fileExists(candidate)) {
|
|
213387
|
+
nearestConfigCache.set(start, candidate);
|
|
213388
|
+
return candidate;
|
|
213389
|
+
}
|
|
213390
|
+
const parent = path.dirname(dir);
|
|
213391
|
+
if (parent === dir) break;
|
|
213392
|
+
dir = parent;
|
|
213393
|
+
}
|
|
213394
|
+
nearestConfigCache.set(start, null);
|
|
213395
|
+
return null;
|
|
213396
|
+
}
|
|
213397
|
+
function createTsResolveForConfig(tsconfigPath, debug = false) {
|
|
213398
|
+
if (resolverCache.has(tsconfigPath)) return resolverCache.get(tsconfigPath);
|
|
213399
|
+
const configFile = import_typescript.default.readConfigFile(tsconfigPath, import_typescript.default.sys.readFile);
|
|
213400
|
+
if (configFile.error) {
|
|
213401
|
+
return null;
|
|
213402
|
+
}
|
|
213403
|
+
const parsed = import_typescript.default.parseJsonConfigFileContent(
|
|
213386
213404
|
configFile.config,
|
|
213387
213405
|
import_typescript.default.sys,
|
|
213388
|
-
path.dirname(
|
|
213406
|
+
path.dirname(tsconfigPath)
|
|
213389
213407
|
);
|
|
213390
|
-
const options =
|
|
213408
|
+
const options = parsed.options;
|
|
213391
213409
|
const host = {
|
|
213392
213410
|
fileExists: import_typescript.default.sys.fileExists,
|
|
213393
213411
|
readFile: import_typescript.default.sys.readFile,
|
|
@@ -213396,18 +213414,27 @@ function createTsResolve(debug = false) {
|
|
|
213396
213414
|
getCurrentDirectory: import_typescript.default.sys.getCurrentDirectory,
|
|
213397
213415
|
getDirectories: import_typescript.default.sys.getDirectories
|
|
213398
213416
|
};
|
|
213399
|
-
|
|
213417
|
+
const resolver = (specifier, importer) => {
|
|
213400
213418
|
const result = import_typescript.default.resolveModuleName(specifier, importer, options, host);
|
|
213401
213419
|
const resolved = result.resolvedModule?.resolvedFileName;
|
|
213402
|
-
if (resolved &&
|
|
213403
|
-
|
|
213420
|
+
if (resolved && !resolved.endsWith(".d.ts")) {
|
|
213421
|
+
if (debug) {
|
|
213422
|
+
console.log(`[loader-ts] resolved ${specifier} -> ${resolved}`);
|
|
213423
|
+
}
|
|
213424
|
+
return resolved;
|
|
213404
213425
|
}
|
|
213405
|
-
return
|
|
213426
|
+
return null;
|
|
213406
213427
|
};
|
|
213407
213428
|
if (debug) {
|
|
213408
|
-
console.log("[loader-ts] tsconfig:",
|
|
213429
|
+
console.log("[loader-ts] tsconfig:", tsconfigPath);
|
|
213409
213430
|
}
|
|
213410
|
-
|
|
213431
|
+
resolverCache.set(tsconfigPath, resolver);
|
|
213432
|
+
return resolver;
|
|
213433
|
+
}
|
|
213434
|
+
function createTsResolve(importer, debug = false) {
|
|
213435
|
+
const tsconfigPath = findNearestTsconfig(path.dirname(importer));
|
|
213436
|
+
if (!tsconfigPath) return null;
|
|
213437
|
+
return createTsResolveForConfig(tsconfigPath, debug);
|
|
213411
213438
|
}
|
|
213412
213439
|
function rewriteImports(code, filename, resolve, debug = false) {
|
|
213413
213440
|
const importLike = /(import\s+[^'";]+?from\s+['"]([^'"]+)['"])|(import\s+['"]([^'"]+)['"])|(export\s+[^'";]+?from\s+['"]([^'"]+)['"])/g;
|
|
@@ -213426,16 +213453,16 @@ function rewriteImports(code, filename, resolve, debug = false) {
|
|
|
213426
213453
|
return full.replace(spec, normalized);
|
|
213427
213454
|
});
|
|
213428
213455
|
}
|
|
213429
|
-
var extensions = [".ts", ".mts", ".cts"];
|
|
213456
|
+
var extensions = [".ts", ".mts", ".cts", ".js", ".mjs", ".cjs"];
|
|
213430
213457
|
function runTransform(code, filename, ctx) {
|
|
213431
|
-
const pathResolver = createTsResolve(ctx.loaderConfig.debug);
|
|
213458
|
+
const pathResolver = createTsResolve(filename, ctx.loaderConfig.debug);
|
|
213459
|
+
const rewrittenCode = pathResolver && !filename.includes("node_modules") ? rewriteImports(code, filename, pathResolver, ctx.loaderConfig.debug) : code;
|
|
213432
213460
|
if (ctx.loaderConfig.debug) {
|
|
213433
213461
|
console.log(`[loader-ts] transforming ${filename}`);
|
|
213434
213462
|
if (!pathResolver) {
|
|
213435
|
-
console.log("[loader-ts] no tsconfig resolver created");
|
|
213463
|
+
console.log("[loader-ts] no tsconfig resolver created for", filename);
|
|
213436
213464
|
}
|
|
213437
213465
|
}
|
|
213438
|
-
const rewrittenCode = pathResolver ? rewriteImports(code, filename, pathResolver, ctx.loaderConfig.debug) : code;
|
|
213439
213466
|
const banner = ctx.format === "cjs" ? `const ${IMPORT_META_URL_VARIABLE} = require('url').pathToFileURL(__filename).href;` : void 0;
|
|
213440
213467
|
const result = transformSync(rewrittenCode, {
|
|
213441
213468
|
loader: "ts",
|
|
@@ -213456,7 +213483,11 @@ function runTransform(code, filename, ctx) {
|
|
|
213456
213483
|
var plugin = {
|
|
213457
213484
|
name: "@hirarijs/loader-ts",
|
|
213458
213485
|
extensions,
|
|
213459
|
-
match: (filename) =>
|
|
213486
|
+
match: (filename) => {
|
|
213487
|
+
const ext = path.extname(filename);
|
|
213488
|
+
if (!ext) return true;
|
|
213489
|
+
return extensions.includes(ext);
|
|
213490
|
+
},
|
|
213460
213491
|
transform: runTransform
|
|
213461
213492
|
};
|
|
213462
213493
|
var index_default = plugin;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hirarijs/loader-ts",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "TypeScript loader plugin for HirariJS loader",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@hirarijs/loader": "workspace:*",
|
|
31
|
+
"@types/node": "^25.0.3",
|
|
31
32
|
"tsup": "^8.0.1",
|
|
32
33
|
"typescript": "^5.3.3"
|
|
33
34
|
}
|