@poppinss/ts-exec 1.3.0 → 1.4.0

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/build/src/loader.js +34 -22
  2. package/package.json +1 -1
@@ -35,10 +35,10 @@ var DEFAULT_CONFIG = {
35
35
  keepClassNames: true
36
36
  }
37
37
  };
38
- function tsConfigToSwcConfig(tsConfig) {
39
- const tsTarget = tsConfig.compilerOptions?.target?.toLowerCase();
38
+ function tsConfigToSwcConfig(tsConfig2) {
39
+ const tsTarget = tsConfig2.compilerOptions?.target?.toLowerCase();
40
40
  return {
41
- sourceMaps: tsConfig.compilerOptions?.sourceMap ?? true,
41
+ sourceMaps: tsConfig2.compilerOptions?.sourceMap ?? true,
42
42
  module: {
43
43
  /**
44
44
  * Since, we will run the SWC transformer via Nodejs loader hooks, the
@@ -48,31 +48,31 @@ function tsConfigToSwcConfig(tsConfig) {
48
48
  },
49
49
  jsc: {
50
50
  target: tsTarget ? tsTarget === "es6" ? "esnext" : tsTarget : "esnext",
51
- baseUrl: tsConfig.compilerOptions?.baseUrl,
51
+ baseUrl: tsConfig2.compilerOptions?.baseUrl,
52
52
  externalHelpers: false,
53
53
  parser: {
54
54
  syntax: "typescript",
55
- tsx: !!tsConfig.compilerOptions?.jsx,
55
+ tsx: !!tsConfig2.compilerOptions?.jsx,
56
56
  decorators: true,
57
57
  dynamicImport: true
58
58
  },
59
59
  transform: {
60
- ...tsConfig.compilerOptions?.emitDecoratorMetadata ? {
60
+ ...tsConfig2.compilerOptions?.emitDecoratorMetadata ? {
61
61
  decoratorMetadata: true
62
62
  } : {},
63
- ...tsConfig.compilerOptions?.experimentalDecorators ? {
63
+ ...tsConfig2.compilerOptions?.experimentalDecorators ? {
64
64
  legacyDecorator: true
65
65
  } : {},
66
- ...tsConfig.compilerOptions?.verbatimModuleSyntax ? {
66
+ ...tsConfig2.compilerOptions?.verbatimModuleSyntax ? {
67
67
  verbatimModuleSyntax: true
68
68
  } : {},
69
- ...tsConfig.compilerOptions?.useDefineForClassFields ? {
69
+ ...tsConfig2.compilerOptions?.useDefineForClassFields ? {
70
70
  useDefineForClassFields: true
71
71
  } : {},
72
72
  react: {
73
73
  runtime: "automatic",
74
- ...tsConfig.compilerOptions?.jsxImportSource ? {
75
- importSource: tsConfig.compilerOptions?.jsxImportSource
74
+ ...tsConfig2.compilerOptions?.jsxImportSource ? {
75
+ importSource: tsConfig2.compilerOptions?.jsxImportSource
76
76
  } : {}
77
77
  }
78
78
  },
@@ -81,10 +81,10 @@ function tsConfigToSwcConfig(tsConfig) {
81
81
  };
82
82
  }
83
83
  function getConfig(searchPath) {
84
- const tsConfig = getTsconfig(
84
+ const tsConfig2 = getTsconfig(
85
85
  typeof searchPath === "string" ? searchPath : fileURLToPath(searchPath)
86
86
  );
87
- if (!tsConfig) {
87
+ if (!tsConfig2) {
88
88
  debug_default("Unable to locate tsconfig file");
89
89
  return {
90
90
  tsConfig: null,
@@ -94,10 +94,10 @@ function getConfig(searchPath) {
94
94
  }
95
95
  };
96
96
  }
97
- debug_default("tsConfig %O", tsConfig.config);
98
- const swcConfig2 = tsConfigToSwcConfig(tsConfig.config);
97
+ debug_default("tsConfig %O", tsConfig2.config);
98
+ const swcConfig2 = tsConfigToSwcConfig(tsConfig2.config);
99
99
  debug_default("swcConfig %O", swcConfig2);
100
- return { tsConfig: tsConfig.config, tsConfigPath: tsConfig.path, swcConfig: swcConfig2 };
100
+ return { tsConfig: tsConfig2.config, tsConfigPath: tsConfig2.path, swcConfig: swcConfig2 };
101
101
  }
102
102
 
103
103
  // src/transform.ts
@@ -111,6 +111,10 @@ function transformSync(source, options) {
111
111
 
112
112
  // src/loader.ts
113
113
  var swcConfig;
114
+ var tsConfig;
115
+ function isTypeScriptFile(filePath) {
116
+ return !filePath.endsWith(".d.ts") && (filePath.endsWith(".ts") || filePath.endsWith(".tsx") || filePath.endsWith(".mts") || filePath.endsWith(".cts"));
117
+ }
114
118
  function isLocalPath(specifier) {
115
119
  return specifier.startsWith("./") || specifier.startsWith("../") || specifier.startsWith("/") || specifier.startsWith("file:");
116
120
  }
@@ -138,24 +142,32 @@ async function initialize() {
138
142
  const searchPath = process.env.TS_EXEC_PWD ?? process.cwd();
139
143
  const config = getConfig(searchPath);
140
144
  swcConfig = config.swcConfig;
145
+ tsConfig = config.tsConfig;
141
146
  }
142
147
  var resolve = async (specifier, context, nextResolve) => {
143
148
  debug_default("resolving specifier %s", specifier);
149
+ const [pathname] = specifier.split("?");
150
+ if (!tsConfig?.compilerOptions?.rewriteRelativeImportExtensions && context.parentURL && isTypeScriptFile(pathname)) {
151
+ throw new Error(
152
+ `Cannot import "${specifier}" using "${extname(pathname)}" extension. Enable "compilerOptions.rewriteRelativeImportExtensions" to import TypeScript files`
153
+ );
154
+ }
144
155
  try {
145
156
  return await nextResolve(specifier, context);
146
157
  } catch (error) {
147
158
  if (error.code === "ERR_MODULE_NOT_FOUND" && error.url && isLocalPath(error.url)) {
148
- let url = error.url;
149
- const fileExtension = extname(url);
159
+ let [urlPathname, ...urlQs] = error.url.split("?");
160
+ const fileExtension = extname(urlPathname);
150
161
  if (fileExtension === ".js") {
151
- url = url.replace(".js", ".ts");
162
+ urlPathname = urlPathname.replace(".js", ".ts");
152
163
  } else if (fileExtension === ".mjs") {
153
- url = url.replace(".mjs", ".mts");
164
+ urlPathname = urlPathname.replace(".mjs", ".mts");
154
165
  } else if (fileExtension === ".cjs") {
155
- url = url.replace(".cjs", ".cts");
166
+ urlPathname = urlPathname.replace(".cjs", ".cts");
156
167
  } else if (fileExtension === ".jsx") {
157
- url = url.replace(".jsx", ".tsx");
168
+ urlPathname = urlPathname.replace(".jsx", ".tsx");
158
169
  }
170
+ const url = urlQs.length ? `${urlPathname}?${urlQs.join("?")}` : urlPathname;
159
171
  debug_default("retrying to resolve file with specifier %s", url);
160
172
  return nextResolve(url, context);
161
173
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@poppinss/ts-exec",
3
3
  "description": "TypeScript JIT compiler built for correctness and supports legacy/experimental decorators",
4
- "version": "1.3.0",
4
+ "version": "1.4.0",
5
5
  "engines": {
6
6
  "node": ">=24.0.0"
7
7
  },