tsnite 0.0.13 → 0.0.14
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 +36 -72
- package/package.json +1 -1
package/dist/loader.js
CHANGED
|
@@ -1,82 +1,46 @@
|
|
|
1
1
|
import { transformFile } from '@swc/core';
|
|
2
2
|
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
3
|
-
import { readFile } from 'node:fs/promises';
|
|
4
|
-
import { existsSync } from 'node:fs';
|
|
5
|
-
import path from 'node:path';
|
|
3
|
+
import { readFile, access, constants } from 'node:fs/promises';
|
|
6
4
|
import json5 from 'json5';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
async function exists(path) {
|
|
7
|
+
try {
|
|
8
|
+
await access(path, constants.F_OK);
|
|
9
|
+
return true;
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
7
15
|
export async function resolve(specifier, ctx, next) {
|
|
8
|
-
|
|
9
|
-
if (specifier.startsWith('.') || specifier.startsWith('/')) {
|
|
10
|
-
const parentPath = fileURLToPath(ctx.parentURL);
|
|
11
|
-
const parentDir = path.dirname(parentPath);
|
|
12
|
-
const basePath = path.resolve(parentDir, specifier);
|
|
13
|
-
const tryFiles = [
|
|
14
|
-
basePath,
|
|
15
|
-
basePath + '.ts',
|
|
16
|
-
basePath + '.tsx',
|
|
17
|
-
basePath + '.js',
|
|
18
|
-
basePath + '.mjs',
|
|
19
|
-
path.join(basePath, 'index.ts'),
|
|
20
|
-
path.join(basePath, 'index.tsx'),
|
|
21
|
-
path.join(basePath, 'index.js'),
|
|
22
|
-
path.join(basePath, 'index.mjs')
|
|
23
|
-
];
|
|
24
|
-
for (const file of tryFiles) {
|
|
25
|
-
if (existsSync(file)) {
|
|
26
|
-
return {
|
|
27
|
-
url: pathToFileURL(file).href,
|
|
28
|
-
format: 'module',
|
|
29
|
-
shortCircuit: true
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
}
|
|
16
|
+
if (!specifier.startsWith('.') && !specifier.startsWith('/')) {
|
|
33
17
|
return next(specifier, ctx);
|
|
34
18
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
abs,
|
|
57
|
-
abs + '.ts',
|
|
58
|
-
abs + '.tsx',
|
|
59
|
-
abs + '.js',
|
|
60
|
-
abs + '.mjs',
|
|
61
|
-
path.join(abs, 'index.ts'),
|
|
62
|
-
path.join(abs, 'index.tsx'),
|
|
63
|
-
path.join(abs, 'index.js'),
|
|
64
|
-
path.join(abs, 'index.mjs')
|
|
65
|
-
];
|
|
66
|
-
for (const file of tryFiles) {
|
|
67
|
-
if (existsSync(file)) {
|
|
68
|
-
return { url: pathToFileURL(file).href, format: 'module', shortCircuit: true };
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
19
|
+
const parentPath = fileURLToPath(ctx.parentURL);
|
|
20
|
+
const parentDir = path.dirname(parentPath);
|
|
21
|
+
const basePath = path.resolve(parentDir, specifier);
|
|
22
|
+
const tryFiles = [
|
|
23
|
+
basePath,
|
|
24
|
+
basePath + '.ts',
|
|
25
|
+
basePath + '.tsx',
|
|
26
|
+
basePath + '.js',
|
|
27
|
+
basePath + '.mjs',
|
|
28
|
+
path.join(basePath, 'index.ts'),
|
|
29
|
+
path.join(basePath, 'index.tsx'),
|
|
30
|
+
path.join(basePath, 'index.js'),
|
|
31
|
+
path.join(basePath, 'index.mjs')
|
|
32
|
+
];
|
|
33
|
+
for (const file of tryFiles) {
|
|
34
|
+
if (await exists(file)) {
|
|
35
|
+
return {
|
|
36
|
+
url: pathToFileURL(file).href,
|
|
37
|
+
format: 'module',
|
|
38
|
+
shortCircuit: true
|
|
39
|
+
};
|
|
72
40
|
}
|
|
73
41
|
}
|
|
74
|
-
catch {
|
|
75
|
-
// ignore
|
|
76
|
-
}
|
|
77
|
-
return next(specifier, ctx);
|
|
78
42
|
}
|
|
79
|
-
|
|
43
|
+
async function loadTsconfigPaths() {
|
|
80
44
|
try {
|
|
81
45
|
const data = await readFile(path.join(process.cwd(), 'tsconfig.json'), 'utf-8');
|
|
82
46
|
const { compilerOptions: { paths } } = json5.parse(data);
|
|
@@ -90,12 +54,12 @@ export async function load(url, ctx, next) {
|
|
|
90
54
|
if (!url.startsWith('file://') || !url.endsWith('.ts')) {
|
|
91
55
|
return next(url, ctx);
|
|
92
56
|
}
|
|
93
|
-
const paths = await
|
|
57
|
+
const paths = await loadTsconfigPaths();
|
|
94
58
|
const filename = fileURLToPath(url);
|
|
95
59
|
const { code } = await transformFile(filename, {
|
|
96
60
|
filename,
|
|
97
61
|
jsc: {
|
|
98
|
-
baseUrl:
|
|
62
|
+
baseUrl: process.cwd(),
|
|
99
63
|
parser: {
|
|
100
64
|
syntax: 'typescript',
|
|
101
65
|
tsx: false,
|