@hirarijs/loader-ts 1.0.2 → 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.
- package/dist/index.cjs +29 -16
- package/dist/index.js +29 -16
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -213379,14 +213379,17 @@ 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
|
-
|
|
213383
|
-
|
|
213382
|
+
var cachedResolve = null;
|
|
213383
|
+
function createTsResolve(debug = false) {
|
|
213384
|
+
if (cachedResolve) return cachedResolve;
|
|
213385
|
+
const configPath = import_typescript.default.findConfigFile(process.cwd(), import_typescript.default.sys.fileExists, "tsconfig.json");
|
|
213384
213386
|
if (!configPath) return null;
|
|
213385
213387
|
const configFile = import_typescript.default.readConfigFile(configPath, import_typescript.default.sys.readFile);
|
|
213386
|
-
const config = import_typescript.default.parseJsonConfigFileContent(
|
|
213387
|
-
|
|
213388
|
-
|
|
213389
|
-
|
|
213388
|
+
const config = import_typescript.default.parseJsonConfigFileContent(
|
|
213389
|
+
configFile.config,
|
|
213390
|
+
import_typescript.default.sys,
|
|
213391
|
+
import_path.default.dirname(configPath)
|
|
213392
|
+
);
|
|
213390
213393
|
const options = config.options;
|
|
213391
213394
|
const host = {
|
|
213392
213395
|
fileExists: import_typescript.default.sys.fileExists,
|
|
@@ -213396,19 +213399,20 @@ function createTsResolve(filename, debug = false) {
|
|
|
213396
213399
|
getCurrentDirectory: import_typescript.default.sys.getCurrentDirectory,
|
|
213397
213400
|
getDirectories: import_typescript.default.sys.getDirectories
|
|
213398
213401
|
};
|
|
213399
|
-
|
|
213402
|
+
cachedResolve = (specifier, importer) => {
|
|
213400
213403
|
const result = import_typescript.default.resolveModuleName(specifier, importer, options, host);
|
|
213401
213404
|
const resolved = result.resolvedModule?.resolvedFileName;
|
|
213402
|
-
if (resolved) {
|
|
213403
|
-
|
|
213404
|
-
console.log(`[loader-ts] resolved ${specifier} -> ${resolved}`);
|
|
213405
|
-
}
|
|
213406
|
-
return resolved;
|
|
213405
|
+
if (resolved && debug) {
|
|
213406
|
+
console.log(`[loader-ts] resolved ${specifier} -> ${resolved}`);
|
|
213407
213407
|
}
|
|
213408
|
-
return null;
|
|
213408
|
+
return resolved || null;
|
|
213409
213409
|
};
|
|
213410
|
+
if (debug) {
|
|
213411
|
+
console.log("[loader-ts] tsconfig:", configPath);
|
|
213412
|
+
}
|
|
213413
|
+
return cachedResolve;
|
|
213410
213414
|
}
|
|
213411
|
-
function rewriteImports(code, filename, resolve) {
|
|
213415
|
+
function rewriteImports(code, filename, resolve, debug = false) {
|
|
213412
213416
|
const importLike = /(import\s+[^'";]+?from\s+['"]([^'"]+)['"])|(import\s+['"]([^'"]+)['"])|(export\s+[^'";]+?from\s+['"]([^'"]+)['"])/g;
|
|
213413
213417
|
return code.replace(importLike, (full, _a, from1, _b, from2, _c, from3) => {
|
|
213414
213418
|
const spec = from1 || from2 || from3;
|
|
@@ -213417,6 +213421,9 @@ function rewriteImports(code, filename, resolve) {
|
|
|
213417
213421
|
}
|
|
213418
213422
|
const resolved = resolve(spec, filename);
|
|
213419
213423
|
if (!resolved) return full;
|
|
213424
|
+
if (debug) {
|
|
213425
|
+
console.log(`[loader-ts] rewrite ${spec} -> ${resolved}`);
|
|
213426
|
+
}
|
|
213420
213427
|
const relative = import_path.default.isAbsolute(resolved) ? import_path.default.relative(import_path.default.dirname(filename), resolved).replace(/\\/g, "/") : resolved;
|
|
213421
213428
|
const normalized = relative.startsWith(".") ? relative : "./" + relative;
|
|
213422
213429
|
return full.replace(spec, normalized);
|
|
@@ -213424,8 +213431,14 @@ function rewriteImports(code, filename, resolve) {
|
|
|
213424
213431
|
}
|
|
213425
213432
|
var extensions = [".ts", ".mts", ".cts"];
|
|
213426
213433
|
function runTransform(code, filename, ctx) {
|
|
213427
|
-
const pathResolver = createTsResolve(
|
|
213428
|
-
|
|
213434
|
+
const pathResolver = createTsResolve(ctx.loaderConfig.debug);
|
|
213435
|
+
if (ctx.loaderConfig.debug) {
|
|
213436
|
+
console.log(`[loader-ts] transforming ${filename}`);
|
|
213437
|
+
if (!pathResolver) {
|
|
213438
|
+
console.log("[loader-ts] no tsconfig resolver created");
|
|
213439
|
+
}
|
|
213440
|
+
}
|
|
213441
|
+
const rewrittenCode = pathResolver ? rewriteImports(code, filename, pathResolver, ctx.loaderConfig.debug) : code;
|
|
213429
213442
|
const banner = ctx.format === "cjs" ? `const ${import_loader.IMPORT_META_URL_VARIABLE} = require('url').pathToFileURL(__filename).href;` : void 0;
|
|
213430
213443
|
const result = (0, import_esbuild.transformSync)(rewrittenCode, {
|
|
213431
213444
|
loader: "ts",
|
package/dist/index.js
CHANGED
|
@@ -213376,14 +213376,17 @@ import {
|
|
|
213376
213376
|
IMPORT_META_URL_VARIABLE
|
|
213377
213377
|
} from "@hirarijs/loader";
|
|
213378
213378
|
import path from "path";
|
|
213379
|
-
|
|
213380
|
-
|
|
213379
|
+
var cachedResolve = null;
|
|
213380
|
+
function createTsResolve(debug = false) {
|
|
213381
|
+
if (cachedResolve) return cachedResolve;
|
|
213382
|
+
const configPath = import_typescript.default.findConfigFile(process.cwd(), import_typescript.default.sys.fileExists, "tsconfig.json");
|
|
213381
213383
|
if (!configPath) return null;
|
|
213382
213384
|
const configFile = import_typescript.default.readConfigFile(configPath, import_typescript.default.sys.readFile);
|
|
213383
|
-
const config = import_typescript.default.parseJsonConfigFileContent(
|
|
213384
|
-
|
|
213385
|
-
|
|
213386
|
-
|
|
213385
|
+
const config = import_typescript.default.parseJsonConfigFileContent(
|
|
213386
|
+
configFile.config,
|
|
213387
|
+
import_typescript.default.sys,
|
|
213388
|
+
path.dirname(configPath)
|
|
213389
|
+
);
|
|
213387
213390
|
const options = config.options;
|
|
213388
213391
|
const host = {
|
|
213389
213392
|
fileExists: import_typescript.default.sys.fileExists,
|
|
@@ -213393,19 +213396,20 @@ function createTsResolve(filename, debug = false) {
|
|
|
213393
213396
|
getCurrentDirectory: import_typescript.default.sys.getCurrentDirectory,
|
|
213394
213397
|
getDirectories: import_typescript.default.sys.getDirectories
|
|
213395
213398
|
};
|
|
213396
|
-
|
|
213399
|
+
cachedResolve = (specifier, importer) => {
|
|
213397
213400
|
const result = import_typescript.default.resolveModuleName(specifier, importer, options, host);
|
|
213398
213401
|
const resolved = result.resolvedModule?.resolvedFileName;
|
|
213399
|
-
if (resolved) {
|
|
213400
|
-
|
|
213401
|
-
console.log(`[loader-ts] resolved ${specifier} -> ${resolved}`);
|
|
213402
|
-
}
|
|
213403
|
-
return resolved;
|
|
213402
|
+
if (resolved && debug) {
|
|
213403
|
+
console.log(`[loader-ts] resolved ${specifier} -> ${resolved}`);
|
|
213404
213404
|
}
|
|
213405
|
-
return null;
|
|
213405
|
+
return resolved || null;
|
|
213406
213406
|
};
|
|
213407
|
+
if (debug) {
|
|
213408
|
+
console.log("[loader-ts] tsconfig:", configPath);
|
|
213409
|
+
}
|
|
213410
|
+
return cachedResolve;
|
|
213407
213411
|
}
|
|
213408
|
-
function rewriteImports(code, filename, resolve) {
|
|
213412
|
+
function rewriteImports(code, filename, resolve, debug = false) {
|
|
213409
213413
|
const importLike = /(import\s+[^'";]+?from\s+['"]([^'"]+)['"])|(import\s+['"]([^'"]+)['"])|(export\s+[^'";]+?from\s+['"]([^'"]+)['"])/g;
|
|
213410
213414
|
return code.replace(importLike, (full, _a, from1, _b, from2, _c, from3) => {
|
|
213411
213415
|
const spec = from1 || from2 || from3;
|
|
@@ -213414,6 +213418,9 @@ function rewriteImports(code, filename, resolve) {
|
|
|
213414
213418
|
}
|
|
213415
213419
|
const resolved = resolve(spec, filename);
|
|
213416
213420
|
if (!resolved) return full;
|
|
213421
|
+
if (debug) {
|
|
213422
|
+
console.log(`[loader-ts] rewrite ${spec} -> ${resolved}`);
|
|
213423
|
+
}
|
|
213417
213424
|
const relative = path.isAbsolute(resolved) ? path.relative(path.dirname(filename), resolved).replace(/\\/g, "/") : resolved;
|
|
213418
213425
|
const normalized = relative.startsWith(".") ? relative : "./" + relative;
|
|
213419
213426
|
return full.replace(spec, normalized);
|
|
@@ -213421,8 +213428,14 @@ function rewriteImports(code, filename, resolve) {
|
|
|
213421
213428
|
}
|
|
213422
213429
|
var extensions = [".ts", ".mts", ".cts"];
|
|
213423
213430
|
function runTransform(code, filename, ctx) {
|
|
213424
|
-
const pathResolver = createTsResolve(
|
|
213425
|
-
|
|
213431
|
+
const pathResolver = createTsResolve(ctx.loaderConfig.debug);
|
|
213432
|
+
if (ctx.loaderConfig.debug) {
|
|
213433
|
+
console.log(`[loader-ts] transforming ${filename}`);
|
|
213434
|
+
if (!pathResolver) {
|
|
213435
|
+
console.log("[loader-ts] no tsconfig resolver created");
|
|
213436
|
+
}
|
|
213437
|
+
}
|
|
213438
|
+
const rewrittenCode = pathResolver ? rewriteImports(code, filename, pathResolver, ctx.loaderConfig.debug) : code;
|
|
213426
213439
|
const banner = ctx.format === "cjs" ? `const ${IMPORT_META_URL_VARIABLE} = require('url').pathToFileURL(__filename).href;` : void 0;
|
|
213427
213440
|
const result = transformSync(rewrittenCode, {
|
|
213428
213441
|
loader: "ts",
|