@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.
- package/build/src/loader.js +34 -22
- package/package.json +1 -1
package/build/src/loader.js
CHANGED
|
@@ -35,10 +35,10 @@ var DEFAULT_CONFIG = {
|
|
|
35
35
|
keepClassNames: true
|
|
36
36
|
}
|
|
37
37
|
};
|
|
38
|
-
function tsConfigToSwcConfig(
|
|
39
|
-
const tsTarget =
|
|
38
|
+
function tsConfigToSwcConfig(tsConfig2) {
|
|
39
|
+
const tsTarget = tsConfig2.compilerOptions?.target?.toLowerCase();
|
|
40
40
|
return {
|
|
41
|
-
sourceMaps:
|
|
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:
|
|
51
|
+
baseUrl: tsConfig2.compilerOptions?.baseUrl,
|
|
52
52
|
externalHelpers: false,
|
|
53
53
|
parser: {
|
|
54
54
|
syntax: "typescript",
|
|
55
|
-
tsx: !!
|
|
55
|
+
tsx: !!tsConfig2.compilerOptions?.jsx,
|
|
56
56
|
decorators: true,
|
|
57
57
|
dynamicImport: true
|
|
58
58
|
},
|
|
59
59
|
transform: {
|
|
60
|
-
...
|
|
60
|
+
...tsConfig2.compilerOptions?.emitDecoratorMetadata ? {
|
|
61
61
|
decoratorMetadata: true
|
|
62
62
|
} : {},
|
|
63
|
-
...
|
|
63
|
+
...tsConfig2.compilerOptions?.experimentalDecorators ? {
|
|
64
64
|
legacyDecorator: true
|
|
65
65
|
} : {},
|
|
66
|
-
...
|
|
66
|
+
...tsConfig2.compilerOptions?.verbatimModuleSyntax ? {
|
|
67
67
|
verbatimModuleSyntax: true
|
|
68
68
|
} : {},
|
|
69
|
-
...
|
|
69
|
+
...tsConfig2.compilerOptions?.useDefineForClassFields ? {
|
|
70
70
|
useDefineForClassFields: true
|
|
71
71
|
} : {},
|
|
72
72
|
react: {
|
|
73
73
|
runtime: "automatic",
|
|
74
|
-
...
|
|
75
|
-
importSource:
|
|
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
|
|
84
|
+
const tsConfig2 = getTsconfig(
|
|
85
85
|
typeof searchPath === "string" ? searchPath : fileURLToPath(searchPath)
|
|
86
86
|
);
|
|
87
|
-
if (!
|
|
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",
|
|
98
|
-
const swcConfig2 = tsConfigToSwcConfig(
|
|
97
|
+
debug_default("tsConfig %O", tsConfig2.config);
|
|
98
|
+
const swcConfig2 = tsConfigToSwcConfig(tsConfig2.config);
|
|
99
99
|
debug_default("swcConfig %O", swcConfig2);
|
|
100
|
-
return { tsConfig:
|
|
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
|
|
149
|
-
const fileExtension = extname(
|
|
159
|
+
let [urlPathname, ...urlQs] = error.url.split("?");
|
|
160
|
+
const fileExtension = extname(urlPathname);
|
|
150
161
|
if (fileExtension === ".js") {
|
|
151
|
-
|
|
162
|
+
urlPathname = urlPathname.replace(".js", ".ts");
|
|
152
163
|
} else if (fileExtension === ".mjs") {
|
|
153
|
-
|
|
164
|
+
urlPathname = urlPathname.replace(".mjs", ".mts");
|
|
154
165
|
} else if (fileExtension === ".cjs") {
|
|
155
|
-
|
|
166
|
+
urlPathname = urlPathname.replace(".cjs", ".cts");
|
|
156
167
|
} else if (fileExtension === ".jsx") {
|
|
157
|
-
|
|
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
|
}
|