tsnite 0.1.1 → 0.1.2
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/loader.js +27 -15
- package/package.json +1 -1
package/dist/loader.js
CHANGED
|
@@ -1,14 +1,21 @@
|
|
|
1
1
|
import { transformFile } from '@swc/core';
|
|
2
2
|
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
3
3
|
import { readFile, stat, writeFile } from 'node:fs/promises';
|
|
4
|
-
import
|
|
4
|
+
import { extname, join, dirname } from 'node:path';
|
|
5
5
|
import { createHash } from 'node:crypto';
|
|
6
6
|
import { parse } from './parse.js';
|
|
7
7
|
import { clearTranspileCache, ensureTranspileCacheDir, existsWithCache, getTranspileCacheFile, resolveCache } from './cache.js';
|
|
8
8
|
const tsconfigCache = { paths: null, baseUrl: null };
|
|
9
9
|
const transpileCache = new Map();
|
|
10
10
|
const MAX_TRANSPILE_CACHE_ENTRIES = 256;
|
|
11
|
-
const
|
|
11
|
+
const TS_EXTENSIONS = ['.cts', '.mts', '.tsx', '.ts'];
|
|
12
|
+
function hasTypeScriptExtension(value) {
|
|
13
|
+
return TS_EXTENSIONS.some((extension) => value.endsWith(extension));
|
|
14
|
+
}
|
|
15
|
+
function isTypeScriptSpecifier(specifier) {
|
|
16
|
+
const extension = extname(specifier);
|
|
17
|
+
return extension === '' || hasTypeScriptExtension(extension);
|
|
18
|
+
}
|
|
12
19
|
function hash(value) {
|
|
13
20
|
return createHash('sha1').update(value).digest('hex');
|
|
14
21
|
}
|
|
@@ -69,18 +76,20 @@ async function loadTSConfig() {
|
|
|
69
76
|
return tsconfigCache;
|
|
70
77
|
}
|
|
71
78
|
try {
|
|
72
|
-
const data = await readFile(
|
|
79
|
+
const data = await readFile(join(import.meta.dirname, '..', 'tsconfig.json'), 'utf-8');
|
|
73
80
|
const { compilerOptions } = parse(data);
|
|
74
81
|
const paths = compilerOptions?.paths ?? null;
|
|
75
82
|
const baseUrl = compilerOptions?.baseUrl;
|
|
76
83
|
tsconfigCache.paths = paths || null;
|
|
77
84
|
tsconfigCache.baseUrl =
|
|
78
|
-
baseUrl ?
|
|
85
|
+
baseUrl ?
|
|
86
|
+
join(import.meta.dirname, '..', baseUrl)
|
|
87
|
+
: join(import.meta.dirname, '..');
|
|
79
88
|
return tsconfigCache;
|
|
80
89
|
}
|
|
81
90
|
catch {
|
|
82
91
|
tsconfigCache.paths = null;
|
|
83
|
-
tsconfigCache.baseUrl =
|
|
92
|
+
tsconfigCache.baseUrl = join(import.meta.dirname, '..');
|
|
84
93
|
return tsconfigCache;
|
|
85
94
|
}
|
|
86
95
|
}
|
|
@@ -88,6 +97,9 @@ export async function resolve(specifier, ctx, next) {
|
|
|
88
97
|
if (!specifier.startsWith('.') && !specifier.startsWith('/')) {
|
|
89
98
|
return next(specifier, ctx);
|
|
90
99
|
}
|
|
100
|
+
if (!isTypeScriptSpecifier(specifier)) {
|
|
101
|
+
return next(specifier, ctx);
|
|
102
|
+
}
|
|
91
103
|
const cacheKey = `${ctx.parentURL}::${specifier}`;
|
|
92
104
|
const cached = resolveCache.get(cacheKey);
|
|
93
105
|
if (cached !== undefined) {
|
|
@@ -101,8 +113,8 @@ export async function resolve(specifier, ctx, next) {
|
|
|
101
113
|
};
|
|
102
114
|
}
|
|
103
115
|
const parentPath = fileURLToPath(ctx.parentURL);
|
|
104
|
-
const parentDir =
|
|
105
|
-
const basePath =
|
|
116
|
+
const parentDir = dirname(parentPath);
|
|
117
|
+
const basePath = join(parentDir, specifier);
|
|
106
118
|
const tryFiles = [
|
|
107
119
|
basePath,
|
|
108
120
|
basePath + '.ts',
|
|
@@ -112,13 +124,13 @@ export async function resolve(specifier, ctx, next) {
|
|
|
112
124
|
basePath + '.js',
|
|
113
125
|
basePath + '.mjs',
|
|
114
126
|
basePath + '.cjs',
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
127
|
+
join(basePath, 'index.ts'),
|
|
128
|
+
join(basePath, 'index.tsx'),
|
|
129
|
+
join(basePath, 'index.mts'),
|
|
130
|
+
join(basePath, 'index.cts'),
|
|
131
|
+
join(basePath, 'index.js'),
|
|
132
|
+
join(basePath, 'index.mjs'),
|
|
133
|
+
join(basePath, 'index.cjs')
|
|
122
134
|
];
|
|
123
135
|
for (const file of tryFiles) {
|
|
124
136
|
if (await existsWithCache(file)) {
|
|
@@ -135,7 +147,7 @@ export async function resolve(specifier, ctx, next) {
|
|
|
135
147
|
return next(specifier, ctx);
|
|
136
148
|
}
|
|
137
149
|
export async function load(url, ctx, next) {
|
|
138
|
-
if (!url.startsWith('file://') || !
|
|
150
|
+
if (!url.startsWith('file://') || !hasTypeScriptExtension(url)) {
|
|
139
151
|
return next(url, ctx);
|
|
140
152
|
}
|
|
141
153
|
const { paths, baseUrl } = await loadTSConfig();
|