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.
Files changed (2) hide show
  1. package/dist/loader.js +27 -15
  2. 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 path from 'node:path';
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 TS_SOURCE_RE = /\.(cts|mts|tsx|ts)$/;
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(path.join(process.cwd(), 'tsconfig.json'), 'utf-8');
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 ? path.resolve(process.cwd(), baseUrl) : process.cwd();
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 = process.cwd();
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 = path.dirname(parentPath);
105
- const basePath = path.resolve(parentDir, specifier);
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
- path.join(basePath, 'index.ts'),
116
- path.join(basePath, 'index.tsx'),
117
- path.join(basePath, 'index.mts'),
118
- path.join(basePath, 'index.cts'),
119
- path.join(basePath, 'index.js'),
120
- path.join(basePath, 'index.mjs'),
121
- path.join(basePath, 'index.cjs')
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://') || !TS_SOURCE_RE.test(url)) {
150
+ if (!url.startsWith('file://') || !hasTypeScriptExtension(url)) {
139
151
  return next(url, ctx);
140
152
  }
141
153
  const { paths, baseUrl } = await loadTSConfig();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "tsnite",
3
3
  "description": "TypeScript at full throttle—fast, safe, unstoppable. 🚀",
4
- "version": "0.1.1",
4
+ "version": "0.1.2",
5
5
  "keywords": [
6
6
  "cli",
7
7
  "esm",