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.
Files changed (2) hide show
  1. package/dist/loader.js +36 -72
  2. 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
- // relative / absolute imports
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
- // non-relative imports: try tsconfig paths
36
- try {
37
- const cfg = await loadTsConfigPaths();
38
- const paths = cfg?.paths || {};
39
- const baseUrl = cfg?.baseUrl || process.cwd();
40
- const keys = Object.keys(paths);
41
- if (keys.length === 0)
42
- return next(specifier, ctx);
43
- for (const key of keys) {
44
- const replacements = paths[key];
45
- const hasStar = key.includes('*');
46
- const pattern = hasStar
47
- ? new RegExp('^' + key.split('*').map((s) => s.replace(/[.*+?^${}()|[\\]\\]/g, '\\$&')).join('(.+)') + '$')
48
- : new RegExp('^' + key.replace(/[.*+?^${}()|[\\]\\]/g, '\\$&') + '$');
49
- const m = specifier.match(pattern);
50
- if (!m)
51
- continue;
52
- for (const rep of replacements) {
53
- const replaced = hasStar ? rep.replace('*', m[1]) : rep;
54
- const abs = path.resolve(baseUrl, replaced);
55
- const tryFiles = [
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
- export async function loadTsConfigPaths() {
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 loadTsConfigPaths();
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: import.meta.dirname,
62
+ baseUrl: process.cwd(),
99
63
  parser: {
100
64
  syntax: 'typescript',
101
65
  tsx: false,
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "nodejs",
9
9
  "typescript"
10
10
  ],
11
- "version": "0.0.13",
11
+ "version": "0.0.14",
12
12
  "type": "module",
13
13
  "bin": {
14
14
  "tsnite": "dist/cli.js"