ts-arc 1.1.20 → 1.1.22

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 +31 -27
  2. package/package.json +1 -1
package/dist/loader.js CHANGED
@@ -4,7 +4,9 @@
4
4
  import * as fs from "fs";
5
5
  import * as path from "path";
6
6
  import * as url from "url";
7
- import { transformSync } from "esbuild";
7
+ import { createRequire } from "module";
8
+ var require2 = createRequire(import.meta.url);
9
+ var { transformSync } = require2("esbuild");
8
10
  var config = {
9
11
  baseUrl: null,
10
12
  paths: {},
@@ -20,7 +22,7 @@ function getEffectiveBase() {
20
22
  }
21
23
  return null;
22
24
  }
23
- async function resolveLocal(baseDir, relativePath) {
25
+ function resolveLocalSync(baseDir, relativePath) {
24
26
  const fullPath = path.resolve(baseDir, relativePath);
25
27
  const candidates = [
26
28
  fullPath,
@@ -32,15 +34,15 @@ async function resolveLocal(baseDir, relativePath) {
32
34
  path.join(fullPath, "page.tsx")
33
35
  ];
34
36
  for (const candidate of candidates) {
35
- try {
36
- if (fs.existsSync(candidate) && fs.statSync(candidate).isFile()) {
37
- return { url: url.pathToFileURL(candidate).href };
38
- }
39
- } catch {
37
+ if (fs.existsSync(candidate) && fs.statSync(candidate).isFile()) {
38
+ return { url: url.pathToFileURL(candidate).href };
40
39
  }
41
40
  }
42
41
  throw Object.assign(new Error(`Cannot find module '${relativePath}'`), { code: "ERR_MODULE_NOT_FOUND" });
43
42
  }
43
+ async function resolveLocal(baseDir, relativePath) {
44
+ return resolveLocalSync(baseDir, relativePath);
45
+ }
44
46
  async function resolve2(specifier, context, nextResolve) {
45
47
  let parentPath = process.cwd();
46
48
  if (context.parentURL) {
@@ -123,26 +125,28 @@ const require = createRequire(import.meta.url);`
123
125
  shortCircuit: true
124
126
  };
125
127
  }
126
- function resolveLocalSync(baseDir, relativePath) {
127
- const fullPath = path.resolve(baseDir, relativePath);
128
- const candidates = [
129
- fullPath,
130
- fullPath + ".ts",
131
- fullPath + ".tsx",
132
- path.join(fullPath, "index.ts"),
133
- path.join(fullPath, "index.tsx"),
134
- path.join(fullPath, "page.ts"),
135
- path.join(fullPath, "page.tsx")
136
- ];
137
- for (const candidate of candidates) {
138
- if (fs.existsSync(candidate) && fs.statSync(candidate).isFile()) {
139
- return { url: url.pathToFileURL(candidate).href };
140
- }
128
+ function loadSync(urlStr, context, nextLoadSync) {
129
+ if (!urlStr.endsWith(".ts") && !urlStr.endsWith(".tsx")) {
130
+ return nextLoadSync(urlStr, context);
141
131
  }
142
- throw Object.assign(
143
- new Error(`Cannot find module '${relativePath}'`),
144
- { code: "ERR_MODULE_NOT_FOUND" }
145
- );
132
+ const esbuildLoader = urlStr.endsWith(".tsx") ? "tsx" : "ts";
133
+ const filePath = url.fileURLToPath(urlStr);
134
+ const rawSource = fs.readFileSync(filePath, "utf8");
135
+ const { code } = transformSync(rawSource, {
136
+ loader: esbuildLoader,
137
+ format: "esm",
138
+ target: `node${process.versions.node}`,
139
+ sourcemap: "inline",
140
+ sourcefile: filePath,
141
+ banner: `
142
+ import { createRequire } from 'module';
143
+ const require = createRequire(import.meta.url);`
144
+ });
145
+ return {
146
+ format: "module",
147
+ source: code,
148
+ shortCircuit: true
149
+ };
146
150
  }
147
151
  function resolveSync(specifier, context) {
148
152
  let parentPath = process.cwd();
@@ -206,7 +210,7 @@ function resolveSync(specifier, context) {
206
210
  export {
207
211
  initialize,
208
212
  load,
213
+ loadSync,
209
214
  resolve2 as resolve,
210
- resolveLocalSync,
211
215
  resolveSync
212
216
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-arc",
3
- "version": "1.1.20",
3
+ "version": "1.1.22",
4
4
  "type": "module",
5
5
  "description": "A simple typescript runtime.",
6
6
  "main": "dist/bin.js",