smartbundle 0.12.0 → 0.12.1
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/__compiled__/cjs/src/createViteConfig.js +7 -7
- package/__compiled__/cjs/src/createViteConfig.js.map +1 -1
- package/__compiled__/cjs/src/index.js +11 -11
- package/__compiled__/cjs/src/index.js.map +1 -1
- package/__compiled__/cjs/src/resolveDirs.d.ts +2 -0
- package/__compiled__/cjs/src/resolveDirs.js +3 -1
- package/__compiled__/cjs/src/resolveDirs.js.map +1 -1
- package/__compiled__/cjs/src/tasks/buildTypesTask/buildTypesTask.d.ts +6 -3
- package/__compiled__/cjs/src/tasks/buildTypesTask/buildTypesTask.js +23 -14
- package/__compiled__/cjs/src/tasks/buildTypesTask/buildTypesTask.js.map +1 -1
- package/__compiled__/cjs/src/tasks/buildTypesTask/callTypescript.d.ts +7 -3
- package/__compiled__/cjs/src/tasks/buildTypesTask/callTypescript.js +57 -26
- package/__compiled__/cjs/src/tasks/buildTypesTask/callTypescript.js.map +1 -1
- package/__compiled__/cjs/src/tasks/buildTypesTask/findTypingsNames.d.ts +1 -0
- package/__compiled__/cjs/src/tasks/buildTypesTask/findTypingsNames.js +89 -0
- package/__compiled__/cjs/src/tasks/buildTypesTask/findTypingsNames.js.map +1 -0
- package/__compiled__/cjs/src/tasks/buildTypesTask/findTypingsPackages.d.ts +4 -0
- package/__compiled__/cjs/src/tasks/buildTypesTask/findTypingsPackages.js +124 -0
- package/__compiled__/cjs/src/tasks/buildTypesTask/findTypingsPackages.js.map +1 -0
- package/__compiled__/cjs/src/tasks/buildTypesTask/inlineExtensions.d.ts +2 -8
- package/__compiled__/cjs/src/tasks/buildTypesTask/inlineExtensions.js +6 -7
- package/__compiled__/cjs/src/tasks/buildTypesTask/inlineExtensions.js.map +1 -1
- package/__compiled__/esm/src/createViteConfig.mjs +7 -7
- package/__compiled__/esm/src/createViteConfig.mjs.map +1 -1
- package/__compiled__/esm/src/index.mjs +11 -11
- package/__compiled__/esm/src/index.mjs.map +1 -1
- package/__compiled__/esm/src/resolveDirs.d.mts +2 -0
- package/__compiled__/esm/src/resolveDirs.mjs +3 -1
- package/__compiled__/esm/src/resolveDirs.mjs.map +1 -1
- package/__compiled__/esm/src/tasks/buildTypesTask/buildTypesTask.d.mts +6 -3
- package/__compiled__/esm/src/tasks/buildTypesTask/buildTypesTask.mjs +23 -14
- package/__compiled__/esm/src/tasks/buildTypesTask/buildTypesTask.mjs.map +1 -1
- package/__compiled__/esm/src/tasks/buildTypesTask/callTypescript.d.mts +7 -3
- package/__compiled__/esm/src/tasks/buildTypesTask/callTypescript.mjs +57 -26
- package/__compiled__/esm/src/tasks/buildTypesTask/callTypescript.mjs.map +1 -1
- package/__compiled__/esm/src/tasks/buildTypesTask/findTypingsNames.d.mts +1 -0
- package/__compiled__/esm/src/tasks/buildTypesTask/findTypingsNames.mjs +72 -0
- package/__compiled__/esm/src/tasks/buildTypesTask/findTypingsNames.mjs.map +1 -0
- package/__compiled__/esm/src/tasks/buildTypesTask/findTypingsPackages.d.mts +4 -0
- package/__compiled__/esm/src/tasks/buildTypesTask/findTypingsPackages.mjs +106 -0
- package/__compiled__/esm/src/tasks/buildTypesTask/findTypingsPackages.mjs.map +1 -0
- package/__compiled__/esm/src/tasks/buildTypesTask/inlineExtensions.d.mts +2 -8
- package/__compiled__/esm/src/tasks/buildTypesTask/inlineExtensions.mjs +6 -7
- package/__compiled__/esm/src/tasks/buildTypesTask/inlineExtensions.mjs.map +1 -1
- package/package.json +1 -1
@@ -5,6 +5,9 @@ import "node:fs/promises";
|
|
5
5
|
import "zod";
|
6
6
|
import { getMinVersion } from "../../detectModules.mjs";
|
7
7
|
import { BuildError } from "../../error.mjs";
|
8
|
+
import { findTypingsPackages } from "./findTypingsPackages.mjs";
|
9
|
+
import { findTypingsNames } from "./findTypingsNames.mjs";
|
10
|
+
import "../../args.mjs";
|
8
11
|
function makeFileExists(outDir, filePath) {
|
9
12
|
return (p) => {
|
10
13
|
const dir = path.join(outDir, path.dirname(filePath));
|
@@ -13,11 +16,11 @@ function makeFileExists(outDir, filePath) {
|
|
13
16
|
}
|
14
17
|
async function callTypescript({
|
15
18
|
ts,
|
16
|
-
|
17
|
-
|
18
|
-
packageJson
|
19
|
-
outDir
|
19
|
+
dirs,
|
20
|
+
tsEntrypoints,
|
21
|
+
packageJson
|
20
22
|
}) {
|
23
|
+
const { sourceDir, outDir, esmOutDir, cjsOutDir } = dirs;
|
21
24
|
const configPath = path.join(sourceDir, "tsconfig.json");
|
22
25
|
const configFile = ts.readConfigFile(
|
23
26
|
configPath,
|
@@ -46,24 +49,31 @@ async function callTypescript({
|
|
46
49
|
configPath
|
47
50
|
);
|
48
51
|
const host = ts.createCompilerHost(parsedCommandLine.options);
|
49
|
-
const
|
50
|
-
const
|
52
|
+
const sourceToCjsDtsMap = /* @__PURE__ */ new Map();
|
53
|
+
const sourceToEsmDtsMap = /* @__PURE__ */ new Map();
|
54
|
+
const program = ts.createProgram(
|
55
|
+
tsEntrypoints,
|
56
|
+
parsedCommandLine.options,
|
57
|
+
host
|
58
|
+
);
|
51
59
|
program.emit(void 0, (fileName, data) => {
|
52
60
|
const relativePath = path.relative(sourceDir, fileName);
|
53
61
|
const sourceFileName = fileName.replace(/\.d\.ts$/, ".ts");
|
54
|
-
const finalEsmPath = path.join(
|
62
|
+
const finalEsmPath = path.join(esmOutDir, relativePath);
|
55
63
|
const esmFinalPath = finalEsmPath.replace(/\.d\.ts$/, ".d.mts");
|
56
|
-
|
64
|
+
sourceToEsmDtsMap.set(sourceFileName, esmFinalPath);
|
57
65
|
fs.mkdirSync(path.dirname(esmFinalPath), { recursive: true });
|
58
66
|
fs.writeFileSync(esmFinalPath, data);
|
59
|
-
const finalCjsPath = path.join(
|
67
|
+
const finalCjsPath = path.join(cjsOutDir, relativePath);
|
60
68
|
fs.mkdirSync(path.dirname(finalCjsPath), { recursive: true });
|
61
69
|
const cjsFinalPath = finalCjsPath;
|
62
70
|
fs.writeFileSync(cjsFinalPath, data);
|
63
|
-
|
71
|
+
sourceToCjsDtsMap.set(sourceFileName, cjsFinalPath);
|
64
72
|
});
|
65
|
-
const
|
66
|
-
|
73
|
+
for (const file of [
|
74
|
+
...sourceToCjsDtsMap.values(),
|
75
|
+
...sourceToEsmDtsMap.values()
|
76
|
+
]) {
|
67
77
|
const content = fs.readFileSync(file, "utf-8");
|
68
78
|
const relativePath = path.relative(outDir, file);
|
69
79
|
if (file.endsWith(".d.ts")) {
|
@@ -72,10 +82,7 @@ async function callTypescript({
|
|
72
82
|
content,
|
73
83
|
makeFileExists(outDir, relativePath)
|
74
84
|
);
|
75
|
-
|
76
|
-
allImportedLibraries.add(lib);
|
77
|
-
}
|
78
|
-
fs.writeFileSync(file, transformedCode.output);
|
85
|
+
fs.writeFileSync(file, transformedCode);
|
79
86
|
}
|
80
87
|
if (file.endsWith(".d.mts")) {
|
81
88
|
const transformedCode = inlineExtensionsMjs(
|
@@ -83,28 +90,52 @@ async function callTypescript({
|
|
83
90
|
content,
|
84
91
|
makeFileExists(outDir, relativePath)
|
85
92
|
);
|
86
|
-
|
87
|
-
|
93
|
+
fs.writeFileSync(file, transformedCode);
|
94
|
+
}
|
95
|
+
}
|
96
|
+
const packages = /* @__PURE__ */ new Set();
|
97
|
+
for (const sourceEntrypoint of tsEntrypoints) {
|
98
|
+
const esmEntrypoint = sourceToEsmDtsMap.get(sourceEntrypoint);
|
99
|
+
if (esmEntrypoint) {
|
100
|
+
const localPackages = findTypingsNames(
|
101
|
+
esmEntrypoint,
|
102
|
+
esmOutDir,
|
103
|
+
".d.mts"
|
104
|
+
);
|
105
|
+
for (const p of localPackages) {
|
106
|
+
packages.add(p);
|
107
|
+
}
|
108
|
+
}
|
109
|
+
const cjsEntrypoint = sourceToCjsDtsMap.get(sourceEntrypoint);
|
110
|
+
if (cjsEntrypoint) {
|
111
|
+
const localPackages = findTypingsNames(cjsEntrypoint, cjsOutDir, ".d.ts");
|
112
|
+
for (const p of localPackages) {
|
113
|
+
packages.add(p);
|
88
114
|
}
|
89
|
-
fs.writeFileSync(file, transformedCode.output);
|
90
115
|
}
|
91
116
|
}
|
92
|
-
const
|
93
|
-
|
117
|
+
const { missingTypings, existingTypingPackages } = findTypingsPackages(
|
118
|
+
packages,
|
119
|
+
sourceDir
|
120
|
+
);
|
121
|
+
for (const lib of existingTypingPackages) {
|
94
122
|
if (getMinVersion(packageJson, lib, [
|
95
123
|
"optionalDependencies",
|
96
124
|
"devDependencies"
|
97
125
|
]) == null) {
|
98
|
-
|
126
|
+
missingTypings.add(lib);
|
99
127
|
}
|
100
128
|
}
|
101
|
-
if (
|
102
|
-
const libsList = [...
|
129
|
+
if (missingTypings.size > 0) {
|
130
|
+
const libsList = [...missingTypings].map((x) => `"${x}"`).join(", ");
|
103
131
|
throw new BuildError(
|
104
|
-
`
|
132
|
+
`The typings won't installed in bundled package: ${libsList}. Please install them into dependencies or peerDependencies.`
|
105
133
|
);
|
106
134
|
}
|
107
|
-
return
|
135
|
+
return {
|
136
|
+
sourceToCjsDtsMap,
|
137
|
+
sourceToEsmDtsMap
|
138
|
+
};
|
108
139
|
}
|
109
140
|
export {
|
110
141
|
callTypescript
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"callTypescript.mjs","sources":["../../../../../../src/tasks/buildTypesTask/callTypescript.ts"],"sourcesContent":["import * as path from \"node:path\";\nimport * as fs from \"node:fs\";\nimport {\n inlineExtensionsMjs,\n inlineExtensionsCjs,\n} from \"./inlineExtensions.js\";\nimport { type PackageJson } from \"../../packageJson.js\";\nimport { getMinVersion } from \"../../detectModules.js\";\nimport { BuildError } from \"../../error.js\";\n\ntype BuildTypesOptions = {\n ts: typeof import(\"typescript\");\n
|
1
|
+
{"version":3,"file":"callTypescript.mjs","sources":["../../../../../../src/tasks/buildTypesTask/callTypescript.ts"],"sourcesContent":["import * as path from \"node:path\";\nimport * as fs from \"node:fs\";\nimport {\n inlineExtensionsMjs,\n inlineExtensionsCjs,\n} from \"./inlineExtensions.js\";\nimport { type PackageJson } from \"../../packageJson.js\";\nimport { getMinVersion } from \"../../detectModules.js\";\nimport { BuildError } from \"../../error.js\";\nimport { findTypingsPackages } from \"./findTypingsPackages.js\";\nimport { findTypingsNames } from \"./findTypingsNames.js\";\nimport { type Dirs } from \"../../resolveDirs.js\";\n\ntype BuildTypesOptions = {\n ts: typeof import(\"typescript\");\n dirs: Dirs;\n packageJson: PackageJson;\n tsEntrypoints: string[];\n outDir: string;\n};\n\nfunction makeFileExists(outDir: string, filePath: string) {\n return (p: string) => {\n const dir = path.join(outDir, path.dirname(filePath));\n return fs.existsSync(path.join(dir, p));\n };\n}\n\nexport async function callTypescript({\n ts,\n dirs,\n tsEntrypoints,\n packageJson,\n}: BuildTypesOptions) {\n const { sourceDir, outDir, esmOutDir, cjsOutDir } = dirs;\n\n // <build d.ts>\n const configPath = path.join(sourceDir, \"tsconfig.json\");\n const configFile = ts.readConfigFile(configPath, (path) =>\n // https://github.com/XaveScor/bobrik/issues/22\n fs.readFileSync(path, \"utf-8\"),\n );\n\n const parsedCommandLine = ts.parseJsonConfigFileContent(\n configFile.config,\n ts.sys,\n sourceDir,\n {\n declaration: true,\n emitDeclarationOnly: true,\n strict: false,\n strictNullChecks: false,\n strictFunctionTypes: false,\n strictPropertyInitialization: false,\n skipLibCheck: true,\n skipDefaultLibCheck: true,\n outDir: \"\",\n // https://github.com/XaveScor/bobrik/issues/22#issuecomment-2308552352\n noEmit: false,\n },\n configPath,\n );\n\n const host = ts.createCompilerHost(parsedCommandLine.options);\n\n const sourceToCjsDtsMap = new Map<string, string>();\n const sourceToEsmDtsMap = new Map<string, string>();\n const program = ts.createProgram(\n tsEntrypoints,\n parsedCommandLine.options,\n host,\n );\n program.emit(undefined, (fileName, data) => {\n // .d.ts for cjs because \"type\": \"commonjs\" in package.json\n // .d.mts for esm\n const relativePath = path.relative(sourceDir, fileName);\n const sourceFileName = fileName.replace(/\\.d\\.ts$/, \".ts\"); // Assuming source files have .ts extension\n\n const finalEsmPath = path.join(esmOutDir, relativePath);\n const esmFinalPath = finalEsmPath.replace(/\\.d\\.ts$/, \".d.mts\");\n sourceToEsmDtsMap.set(sourceFileName, esmFinalPath);\n fs.mkdirSync(path.dirname(esmFinalPath), { recursive: true });\n fs.writeFileSync(esmFinalPath, data);\n\n const finalCjsPath = path.join(cjsOutDir, relativePath);\n fs.mkdirSync(path.dirname(finalCjsPath), { recursive: true });\n const cjsFinalPath = finalCjsPath;\n fs.writeFileSync(cjsFinalPath, data);\n sourceToCjsDtsMap.set(sourceFileName, cjsFinalPath);\n });\n // </build d.ts>\n\n // <fix vscode typings>\n for (const file of [\n ...sourceToCjsDtsMap.values(),\n ...sourceToEsmDtsMap.values(),\n ]) {\n const content = fs.readFileSync(file, \"utf-8\");\n const relativePath = path.relative(outDir, file);\n if (file.endsWith(\".d.ts\")) {\n const transformedCode = inlineExtensionsCjs(\n ts,\n content,\n makeFileExists(outDir, relativePath),\n );\n fs.writeFileSync(file, transformedCode);\n }\n if (file.endsWith(\".d.mts\")) {\n const transformedCode = inlineExtensionsMjs(\n ts,\n content,\n makeFileExists(outDir, relativePath),\n );\n fs.writeFileSync(file, transformedCode);\n }\n }\n // </fix vscode typings>\n\n // <find all libraries names>\n const packages = new Set<string>();\n for (const sourceEntrypoint of tsEntrypoints) {\n const esmEntrypoint = sourceToEsmDtsMap.get(sourceEntrypoint);\n if (esmEntrypoint) {\n const localPackages = findTypingsNames(\n esmEntrypoint,\n esmOutDir,\n \".d.mts\",\n );\n for (const p of localPackages) {\n packages.add(p);\n }\n }\n\n const cjsEntrypoint = sourceToCjsDtsMap.get(sourceEntrypoint);\n if (cjsEntrypoint) {\n const localPackages = findTypingsNames(cjsEntrypoint, cjsOutDir, \".d.ts\");\n for (const p of localPackages) {\n packages.add(p);\n }\n }\n }\n // </find all libraries names>\n\n // <check not installed typings libraries>\n const { missingTypings, existingTypingPackages } = findTypingsPackages(\n packages,\n sourceDir,\n );\n for (const lib of existingTypingPackages) {\n if (\n getMinVersion(packageJson, lib, [\n \"optionalDependencies\",\n \"devDependencies\",\n ]) == null\n ) {\n missingTypings.add(lib);\n }\n }\n if (missingTypings.size > 0) {\n const libsList = [...missingTypings].map((x) => `\"${x}\"`).join(\", \");\n throw new BuildError(\n `The typings won't installed in bundled package: ${libsList}. Please install them into dependencies or peerDependencies.`,\n );\n }\n // </check not installed typings libraries>\n\n return {\n sourceToCjsDtsMap,\n sourceToEsmDtsMap,\n };\n}\n"],"names":["path"],"mappings":";;;;;;;;;;AAqBA,SAAS,eAAe,QAAgB,UAAkB;AACxD,SAAO,CAAC,MAAc;AACpB,UAAM,MAAM,KAAK,KAAK,QAAQ,KAAK,QAAQ,QAAQ,CAAC;AACpD,WAAO,GAAG,WAAW,KAAK,KAAK,KAAK,CAAC,CAAC;AAAA,EACxC;AACF;AAEA,eAAsB,eAAe;AAAA,EACnC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAAsB;AACpB,QAAM,EAAE,WAAW,QAAQ,WAAW,UAAc,IAAA;AAGpD,QAAM,aAAa,KAAK,KAAK,WAAW,eAAe;AACvD,QAAM,aAAa,GAAG;AAAA,IAAe;AAAA,IAAY,CAACA;AAAAA;AAAAA,MAEhD,GAAG,aAAaA,OAAM,OAAO;AAAA;AAAA,EAC/B;AAEA,QAAM,oBAAoB,GAAG;AAAA,IAC3B,WAAW;AAAA,IACX,GAAG;AAAA,IACH;AAAA,IACA;AAAA,MACE,aAAa;AAAA,MACb,qBAAqB;AAAA,MACrB,QAAQ;AAAA,MACR,kBAAkB;AAAA,MAClB,qBAAqB;AAAA,MACrB,8BAA8B;AAAA,MAC9B,cAAc;AAAA,MACd,qBAAqB;AAAA,MACrB,QAAQ;AAAA;AAAA,MAER,QAAQ;AAAA,IACV;AAAA,IACA;AAAA,EACF;AAEA,QAAM,OAAO,GAAG,mBAAmB,kBAAkB,OAAO;AAEtD,QAAA,wCAAwB,IAAoB;AAC5C,QAAA,wCAAwB,IAAoB;AAClD,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AAkB,IAAI,gBAAgB;AAC5D,QAAI,eAAe;AACjB,YAAM,gBAAgB;AAAA,QACpB;AAAA,QACA;AAAA,QACA;AAAA,MACF;AACA,iBAAW,KAAK,eAAe;AAC7B,iBAAS,IAAI,CAAC;AAAA,MAAA;AAAA,IAChB;AAGI,UAAA,gBAAgB,kBAAkB,IAAI,gBAAgB;AAC5D,QAAI,eAAe;AACjB,YAAM,gBAAgB,iBAAiB,eAAe,WAAW,OAAO;AACxE,iBAAW,KAAK,eAAe;AAC7B,iBAAS,IAAI,CAAC;AAAA,MAAA;AAAA,IAChB;AAAA,EACF;AAKI,QAAA,EAAE,gBAAgB,uBAAA,IAA2B;AAAA,IACjD;AAAA,IACA;AAAA,EACF;AACA,aAAW,OAAO,wBAAwB;AAEtC,QAAA,cAAc,aAAa,KAAK;AAAA,MAC9B;AAAA,MACA;AAAA,IACD,CAAA,KAAK,MACN;AACA,qBAAe,IAAI,GAAG;AAAA,IAAA;AAAA,EACxB;AAEE,MAAA,eAAe,OAAO,GAAG;AAC3B,UAAM,WAAW,CAAC,GAAG,cAAc,EAAE,IAAI,CAAC,MAAM,IAAI,CAAC,GAAG,EAAE,KAAK,IAAI;AACnE,UAAM,IAAI;AAAA,MACR,mDAAmD,QAAQ;AAAA,IAC7D;AAAA,EAAA;AAIK,SAAA;AAAA,IACL;AAAA,IACA;AAAA,EACF;AACF;"}
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare function findTypingsNames(entrypoint: string, sourceDir: string, ext: string): Set<string>;
|
@@ -0,0 +1,72 @@
|
|
1
|
+
import * as ts from "typescript";
|
2
|
+
import { relative, join } from "node:path";
|
3
|
+
import { readdirSync, statSync } from "node:fs";
|
4
|
+
function createCompilerHostWithVirtualSource(sourceDir) {
|
5
|
+
return ts.createCompilerHost({
|
6
|
+
target: ts.ScriptTarget.ESNext,
|
7
|
+
moduleResolution: ts.ModuleResolutionKind.NodeNext,
|
8
|
+
baseUrl: ".",
|
9
|
+
sourceRoot: sourceDir,
|
10
|
+
noEmit: true,
|
11
|
+
emitDeclarationOnly: true,
|
12
|
+
noEmitOnError: true
|
13
|
+
});
|
14
|
+
}
|
15
|
+
function collectAllFilesInDir(sourceDir, ext) {
|
16
|
+
const files = readdirSync(sourceDir);
|
17
|
+
const ret = new Array();
|
18
|
+
for (const file of files) {
|
19
|
+
const stat = statSync(join(sourceDir, file));
|
20
|
+
if (stat.isDirectory()) {
|
21
|
+
ret.push(...collectAllFilesInDir(join(sourceDir, file), ext));
|
22
|
+
} else if (file.endsWith(ext)) {
|
23
|
+
ret.push(join(sourceDir, file));
|
24
|
+
}
|
25
|
+
}
|
26
|
+
return ret;
|
27
|
+
}
|
28
|
+
function findTypingsNames(entrypoint, sourceDir, ext) {
|
29
|
+
const host = createCompilerHostWithVirtualSource(sourceDir);
|
30
|
+
const program = ts.createProgram({
|
31
|
+
rootNames: collectAllFilesInDir(sourceDir, ext),
|
32
|
+
options: {},
|
33
|
+
host
|
34
|
+
});
|
35
|
+
const packages = /* @__PURE__ */ new Set();
|
36
|
+
const processedFiles = /* @__PURE__ */ new Set();
|
37
|
+
const filesQueue = [relative(sourceDir, entrypoint)];
|
38
|
+
function processModuleSpecifier(moduleSpecifier) {
|
39
|
+
const moduleName = moduleSpecifier.text;
|
40
|
+
if (moduleName.startsWith(".")) {
|
41
|
+
filesQueue.push(moduleName.replace(/\.js$/, ext));
|
42
|
+
return;
|
43
|
+
}
|
44
|
+
packages.add(moduleName);
|
45
|
+
}
|
46
|
+
while (filesQueue.length > 0) {
|
47
|
+
let visit = function(node) {
|
48
|
+
if (ts.isImportDeclaration(node) && ts.isStringLiteral(node.moduleSpecifier)) {
|
49
|
+
processModuleSpecifier(node.moduleSpecifier);
|
50
|
+
}
|
51
|
+
if (ts.isImportTypeNode(node) && ts.isLiteralTypeNode(node.argument) && ts.isStringLiteral(node.argument.literal)) {
|
52
|
+
processModuleSpecifier(node.argument.literal);
|
53
|
+
}
|
54
|
+
if (ts.isExportDeclaration(node) && node.moduleSpecifier && ts.isStringLiteral(node.moduleSpecifier)) {
|
55
|
+
processModuleSpecifier(node.moduleSpecifier);
|
56
|
+
}
|
57
|
+
ts.forEachChild(node, visit);
|
58
|
+
};
|
59
|
+
const relativeCurrentFile = filesQueue.pop();
|
60
|
+
const currentFile = join(sourceDir, relativeCurrentFile);
|
61
|
+
if (processedFiles.has(currentFile)) continue;
|
62
|
+
const sourceFile = program.getSourceFile(currentFile);
|
63
|
+
if (!sourceFile) continue;
|
64
|
+
processedFiles.add(currentFile);
|
65
|
+
ts.forEachChild(sourceFile, visit);
|
66
|
+
}
|
67
|
+
return packages;
|
68
|
+
}
|
69
|
+
export {
|
70
|
+
findTypingsNames
|
71
|
+
};
|
72
|
+
//# sourceMappingURL=findTypingsNames.mjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"findTypingsNames.mjs","sources":["../../../../../../src/tasks/buildTypesTask/findTypingsNames.ts"],"sourcesContent":["import * as ts from \"typescript\";\nimport { join, relative } from \"node:path\";\nimport { readdirSync, statSync } from \"node:fs\";\n\nfunction createCompilerHostWithVirtualSource(sourceDir: string) {\n return ts.createCompilerHost({\n target: ts.ScriptTarget.ESNext,\n moduleResolution: ts.ModuleResolutionKind.NodeNext,\n baseUrl: \".\",\n sourceRoot: sourceDir,\n noEmit: true,\n emitDeclarationOnly: true,\n noEmitOnError: true,\n });\n}\n\n/*\nChatGPT told me we need to specify all files in rootNames for the program to work correctly.\n */\nfunction collectAllFilesInDir(sourceDir: string, ext: string) {\n const files = readdirSync(sourceDir);\n const ret = new Array<string>();\n for (const file of files) {\n // check if the file is a directory\n const stat = statSync(join(sourceDir, file));\n if (stat.isDirectory()) {\n ret.push(...collectAllFilesInDir(join(sourceDir, file), ext));\n } else if (file.endsWith(ext)) {\n ret.push(join(sourceDir, file));\n }\n }\n\n return ret;\n}\n\nexport function findTypingsNames(\n entrypoint: string,\n sourceDir: string,\n ext: string,\n) {\n const host = createCompilerHostWithVirtualSource(sourceDir);\n const program = ts.createProgram({\n rootNames: collectAllFilesInDir(sourceDir, ext),\n options: {},\n host,\n });\n\n const packages = new Set<string>();\n const processedFiles = new Set<string>();\n const filesQueue = [relative(sourceDir, entrypoint)];\n\n function processModuleSpecifier(moduleSpecifier: ts.StringLiteral) {\n const moduleName = moduleSpecifier.text;\n if (moduleName.startsWith(\".\")) {\n filesQueue.push(moduleName.replace(/\\.js$/, ext));\n return;\n }\n\n packages.add(moduleName);\n }\n\n while (filesQueue.length > 0) {\n const relativeCurrentFile = filesQueue.pop()!;\n const currentFile = join(sourceDir, relativeCurrentFile);\n if (processedFiles.has(currentFile)) continue;\n\n const sourceFile = program.getSourceFile(currentFile);\n if (!sourceFile) continue;\n\n function visit(node: ts.Node) {\n // import \"moduleSpecifier\";\n if (\n ts.isImportDeclaration(node) &&\n ts.isStringLiteral(node.moduleSpecifier)\n ) {\n processModuleSpecifier(node.moduleSpecifier);\n }\n\n // Generic<import(\"node\")>;\n if (\n ts.isImportTypeNode(node) &&\n ts.isLiteralTypeNode(node.argument) &&\n ts.isStringLiteral(node.argument.literal)\n ) {\n processModuleSpecifier(node.argument.literal);\n }\n\n // export * from \"moduleSpecifier\";\n if (\n ts.isExportDeclaration(node) &&\n node.moduleSpecifier &&\n ts.isStringLiteral(node.moduleSpecifier)\n ) {\n processModuleSpecifier(node.moduleSpecifier);\n }\n\n ts.forEachChild(node, visit);\n }\n\n processedFiles.add(currentFile);\n\n ts.forEachChild(sourceFile, visit);\n }\n\n return packages;\n}\n"],"names":[],"mappings":";;;AAIA,SAAS,oCAAoC,WAAmB;AAC9D,SAAO,GAAG,mBAAmB;AAAA,IAC3B,QAAQ,GAAG,aAAa;AAAA,IACxB,kBAAkB,GAAG,qBAAqB;AAAA,IAC1C,SAAS;AAAA,IACT,YAAY;AAAA,IACZ,QAAQ;AAAA,IACR,qBAAqB;AAAA,IACrB,eAAe;AAAA,EAAA,CAChB;AACH;AAKA,SAAS,qBAAqB,WAAmB,KAAa;AACtD,QAAA,QA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AAe,GACvC;AACA,+BAAuB,KAAK,eAAe;AAAA,MAAA;AAG1C,SAAA,aAAa,MAAM,KAAK;AAAA,IAC7B;AAnCM,UAAA,sBAAsB,WAAW,IAAI;AACrC,UAAA,cAAc,KAAK,WAAW,mBAAmB;AACnD,QAAA,eAAe,IAAI,WAAW,EAAG;AAE/B,UAAA,aAAa,QAAQ,cAAc,WAAW;AACpD,QAAI,CAAC,WAAY;AAgCjB,mBAAe,IAAI,WAAW;AAE3B,OAAA,aAAa,YAAY,KAAK;AAAA,EAAA;AAG5B,SAAA;AACT;"}
|
@@ -0,0 +1,106 @@
|
|
1
|
+
import * as ts from "typescript";
|
2
|
+
import * as path from "node:path";
|
3
|
+
function createVirtualHostFunctions(virtualFilePath, virtualSourceContent, originalHost) {
|
4
|
+
const getSourceFile = (fileName, languageVersion) => {
|
5
|
+
if (fileName === virtualFilePath) {
|
6
|
+
return ts.createSourceFile(
|
7
|
+
fileName,
|
8
|
+
virtualSourceContent,
|
9
|
+
languageVersion
|
10
|
+
);
|
11
|
+
}
|
12
|
+
return originalHost.getSourceFile(fileName, languageVersion);
|
13
|
+
};
|
14
|
+
const fileExists = (fileName) => {
|
15
|
+
if (fileName === virtualFilePath) {
|
16
|
+
return true;
|
17
|
+
}
|
18
|
+
return originalHost.fileExists(fileName);
|
19
|
+
};
|
20
|
+
const readFile = (fileName) => {
|
21
|
+
if (fileName === virtualFilePath) {
|
22
|
+
return virtualSourceContent;
|
23
|
+
}
|
24
|
+
return originalHost.readFile(fileName);
|
25
|
+
};
|
26
|
+
return {
|
27
|
+
getSourceFile,
|
28
|
+
fileExists,
|
29
|
+
readFile
|
30
|
+
};
|
31
|
+
}
|
32
|
+
function createCompilerHostWithVirtualSource(packages, sourceDir) {
|
33
|
+
const virtualSourceContent = [...packages].map((p) => `import "${p}";`).join("\n") + // for ignoring the `Generated an empty chunk: "."` error
|
34
|
+
"export const a = 1;\n";
|
35
|
+
const virtualFilePath = path.join(sourceDir, "virtual.ts");
|
36
|
+
const originalHost = ts.createCompilerHost({
|
37
|
+
target: ts.ScriptTarget.ESNext,
|
38
|
+
moduleResolution: ts.ModuleResolutionKind.NodeNext,
|
39
|
+
baseUrl: ".",
|
40
|
+
sourceRoot: sourceDir,
|
41
|
+
noEmit: true,
|
42
|
+
emitDeclarationOnly: true,
|
43
|
+
noEmitOnError: true,
|
44
|
+
paths: {
|
45
|
+
"*": ["node_modules/*"]
|
46
|
+
}
|
47
|
+
});
|
48
|
+
const virtualFunctions = createVirtualHostFunctions(
|
49
|
+
virtualFilePath,
|
50
|
+
virtualSourceContent,
|
51
|
+
originalHost
|
52
|
+
);
|
53
|
+
return {
|
54
|
+
host: new Proxy(originalHost, {
|
55
|
+
get(target, prop) {
|
56
|
+
return virtualFunctions[prop] || target[prop];
|
57
|
+
}
|
58
|
+
}),
|
59
|
+
virtualFilePath
|
60
|
+
};
|
61
|
+
}
|
62
|
+
function findTypingsPackages(packages, sourceDir) {
|
63
|
+
const { host, virtualFilePath } = createCompilerHostWithVirtualSource(
|
64
|
+
packages,
|
65
|
+
sourceDir
|
66
|
+
);
|
67
|
+
const program = ts.createProgram({
|
68
|
+
rootNames: [virtualFilePath],
|
69
|
+
options: {},
|
70
|
+
host
|
71
|
+
});
|
72
|
+
const sourceFile = program.getSourceFile(virtualFilePath);
|
73
|
+
if (!sourceFile) {
|
74
|
+
throw new Error(
|
75
|
+
"[getSourceFile] Impossible error inside findMissingTypings"
|
76
|
+
);
|
77
|
+
}
|
78
|
+
const missingTypings = /* @__PURE__ */ new Set();
|
79
|
+
const existingTypingPackages = /* @__PURE__ */ new Set();
|
80
|
+
sourceFile.forEachChild((node) => {
|
81
|
+
var _a;
|
82
|
+
if (ts.isImportDeclaration(node)) {
|
83
|
+
const moduleSpecifier = node.moduleSpecifier;
|
84
|
+
if (ts.isStringLiteral(moduleSpecifier)) {
|
85
|
+
const moduleResolution = ts.resolveModuleName(
|
86
|
+
moduleSpecifier.text,
|
87
|
+
virtualFilePath,
|
88
|
+
{},
|
89
|
+
host
|
90
|
+
);
|
91
|
+
if (!((_a = moduleResolution == null ? void 0 : moduleResolution.resolvedModule) == null ? void 0 : _a.packageId)) {
|
92
|
+
missingTypings.add(moduleSpecifier.text);
|
93
|
+
} else {
|
94
|
+
existingTypingPackages.add(
|
95
|
+
moduleResolution.resolvedModule.packageId.name
|
96
|
+
);
|
97
|
+
}
|
98
|
+
}
|
99
|
+
}
|
100
|
+
});
|
101
|
+
return { missingTypings, existingTypingPackages };
|
102
|
+
}
|
103
|
+
export {
|
104
|
+
findTypingsPackages
|
105
|
+
};
|
106
|
+
//# sourceMappingURL=findTypingsPackages.mjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"findTypingsPackages.mjs","sources":["../../../../../../src/tasks/buildTypesTask/findTypingsPackages.ts"],"sourcesContent":["import * as ts from \"typescript\";\nimport * as path from \"node:path\";\n\ntype HostFunctions = {\n getSourceFile: ts.CompilerHost[\"getSourceFile\"];\n fileExists: ts.CompilerHost[\"fileExists\"];\n readFile: ts.CompilerHost[\"readFile\"];\n};\n\nfunction createVirtualHostFunctions(\n virtualFilePath: string,\n virtualSourceContent: string,\n originalHost: ts.CompilerHost,\n): HostFunctions {\n const getSourceFile: ts.CompilerHost[\"getSourceFile\"] = (\n fileName,\n languageVersion,\n ) => {\n if (fileName === virtualFilePath) {\n return ts.createSourceFile(\n fileName,\n virtualSourceContent,\n languageVersion,\n );\n }\n return originalHost.getSourceFile(fileName, languageVersion);\n };\n\n const fileExists: ts.CompilerHost[\"fileExists\"] = (fileName) => {\n if (fileName === virtualFilePath) {\n return true;\n }\n return originalHost.fileExists(fileName);\n };\n\n const readFile: ts.CompilerHost[\"readFile\"] = (fileName) => {\n if (fileName === virtualFilePath) {\n return virtualSourceContent;\n }\n return originalHost.readFile(fileName);\n };\n\n return {\n getSourceFile,\n fileExists,\n readFile,\n };\n}\n\nfunction createCompilerHostWithVirtualSource(\n packages: Set<string>,\n sourceDir: string,\n) {\n const virtualSourceContent =\n [...packages].map((p) => `import \"${p}\";`).join(\"\\n\") +\n // for ignoring the `Generated an empty chunk: \".\"` error\n \"export const a = 1;\\n\";\n const virtualFilePath = path.join(sourceDir, \"virtual.ts\");\n\n const originalHost = ts.createCompilerHost({\n target: ts.ScriptTarget.ESNext,\n moduleResolution: ts.ModuleResolutionKind.NodeNext,\n baseUrl: \".\",\n sourceRoot: sourceDir,\n noEmit: true,\n emitDeclarationOnly: true,\n noEmitOnError: true,\n paths: {\n \"*\": [\"node_modules/*\"],\n },\n });\n const virtualFunctions = createVirtualHostFunctions(\n virtualFilePath,\n virtualSourceContent,\n originalHost,\n );\n\n return {\n host: new Proxy(originalHost, {\n get(target, prop: keyof ts.CompilerHost) {\n return virtualFunctions[prop as keyof HostFunctions] || target[prop];\n },\n }),\n virtualFilePath,\n };\n}\n\nexport function findTypingsPackages(packages: Set<string>, sourceDir: string) {\n const { host, virtualFilePath } = createCompilerHostWithVirtualSource(\n packages,\n sourceDir,\n );\n\n const program = ts.createProgram({\n rootNames: [virtualFilePath],\n options: {},\n host: host,\n });\n\n const sourceFile = program.getSourceFile(virtualFilePath);\n if (!sourceFile) {\n throw new Error(\n \"[getSourceFile] Impossible error inside findMissingTypings\",\n );\n }\n\n const missingTypings = new Set<string>();\n const existingTypingPackages = new Set<string>();\n sourceFile.forEachChild((node) => {\n if (ts.isImportDeclaration(node)) {\n const moduleSpecifier = node.moduleSpecifier;\n if (ts.isStringLiteral(moduleSpecifier)) {\n const moduleResolution = ts.resolveModuleName(\n moduleSpecifier.text,\n virtualFilePath,\n {},\n host,\n );\n\n if (!moduleResolution?.resolvedModule?.packageId) {\n missingTypings.add(moduleSpecifier.text);\n } else {\n existingTypingPackages.add(\n moduleResolution.resolvedModule.packageId.name,\n );\n }\n }\n }\n });\n\n return { missingTypings, existingTypingPackages };\n}\n"],"names":[],"mappings":";;AASA,SAAS,2BACP,iBACA,sBACA,cACe;AACT,QAAA,gBAAkD,CACtD,UACA,oBACG;AACH,QAA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ChC;AAAA,IACA;AAAA,EACF;AAEM,QAAA,UAAU,GAAG,cAAc;AAAA,IAC/B,WAAW,CAAC,eAAe;AAAA,IAC3B,SAAS,CAAC;AAAA,IACV;AAAA,EAAA,CACD;AAEK,QAAA,aAAa,QAAQ,cAAc,eAAe;AACxD,MAAI,CAAC,YAAY;AACf,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EAAA;AAGI,QAAA,qCAAqB,IAAY;AACjC,QAAA,6CAA6B,IAAY;AACpC,aAAA,aAAa,CAAC,SAAS;;AAC5B,QAAA,GAAG,oBAAoB,IAAI,GAAG;AAChC,YAAM,kBAAkB,KAAK;AACzB,UAAA,GAAG,gBAAgB,eAAe,GAAG;AACvC,cAAM,mBAAmB,GAAG;AAAA,UAC1B,gBAAgB;AAAA,UAChB;AAAA,UACA,CAAC;AAAA,UACD;AAAA,QACF;AAEI,YAAA,GAAC,0DAAkB,mBAAlB,mBAAkC,YAAW;AACjC,yBAAA,IAAI,gBAAgB,IAAI;AAAA,QAAA,OAClC;AACkB,iCAAA;AAAA,YACrB,iBAAiB,eAAe,UAAU;AAAA,UAC5C;AAAA,QAAA;AAAA,MACF;AAAA,IACF;AAAA,EACF,CACD;AAEM,SAAA,EAAE,gBAAgB,uBAAuB;AAClD;"}
|
@@ -1,9 +1,3 @@
|
|
1
1
|
export type FileExists = (path: string) => boolean;
|
2
|
-
export declare function inlineExtensionsMjs(ts: typeof import("typescript"), content: string, fileExists: FileExists):
|
3
|
-
|
4
|
-
usedLibraries: Set<string>;
|
5
|
-
};
|
6
|
-
export declare function inlineExtensionsCjs(ts: typeof import("typescript"), content: string, fileExists: FileExists): {
|
7
|
-
output: string;
|
8
|
-
usedLibraries: Set<string>;
|
9
|
-
};
|
2
|
+
export declare function inlineExtensionsMjs(ts: typeof import("typescript"), content: string, fileExists: FileExists): string;
|
3
|
+
export declare function inlineExtensionsCjs(ts: typeof import("typescript"), content: string, fileExists: FileExists): string;
|
@@ -1,21 +1,20 @@
|
|
1
1
|
import { join } from "node:path";
|
2
2
|
function transformAndExtractImports(ts, content, ext, dtsExt, fileExists) {
|
3
|
-
const usedLibraries = /* @__PURE__ */ new Set();
|
4
3
|
function addExtension(ts2, node, ext2, dtsExt2, fileExists2) {
|
5
4
|
const importPath = node.text;
|
6
5
|
if (!importPath.startsWith(".")) {
|
7
|
-
usedLibraries.add(importPath);
|
8
6
|
return node;
|
9
7
|
}
|
10
8
|
if (importPath.endsWith(".cjs") || importPath.endsWith(".mjs") || importPath.endsWith(".js")) {
|
11
9
|
return node;
|
12
10
|
}
|
13
|
-
|
14
|
-
|
11
|
+
const cleanedImportPath = importPath.replace(/\.[cm]?js$/, "");
|
12
|
+
if (fileExists2(`${cleanedImportPath}${dtsExt2}`)) {
|
13
|
+
return ts2.factory.createStringLiteral(`${cleanedImportPath}${ext2}`);
|
15
14
|
}
|
16
|
-
if (fileExists2("./" + join(
|
15
|
+
if (fileExists2("./" + join(cleanedImportPath, `index${dtsExt2}`))) {
|
17
16
|
return ts2.factory.createStringLiteral(
|
18
|
-
"./" + join(
|
17
|
+
"./" + join(cleanedImportPath, `index${ext2}`)
|
19
18
|
);
|
20
19
|
}
|
21
20
|
return node;
|
@@ -97,7 +96,7 @@ function transformAndExtractImports(ts, content, ext, dtsExt, fileExists) {
|
|
97
96
|
const transformedSourceFile = result.transformed[0];
|
98
97
|
const output = printer.printFile(transformedSourceFile);
|
99
98
|
result.dispose();
|
100
|
-
return
|
99
|
+
return output;
|
101
100
|
}
|
102
101
|
function inlineExtensionsMjs(ts, content, fileExists) {
|
103
102
|
return transformAndExtractImports(ts, content, ".mjs", ".d.mts", fileExists);
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"inlineExtensions.mjs","sources":["../../../../../../src/tasks/buildTypesTask/inlineExtensions.ts"],"sourcesContent":["import type * as TS from \"typescript\";\nimport { join } from \"node:path\";\n\n// It needs for VSCode. It cannot resolve the import/export if it has no extension\n\nexport type FileExists = (path: string) => boolean;\n\nfunction transformAndExtractImports(\n ts: typeof import(\"typescript\"),\n content: string,\n ext: string,\n dtsExt: string,\n fileExists: FileExists,\n) {\n
|
1
|
+
{"version":3,"file":"inlineExtensions.mjs","sources":["../../../../../../src/tasks/buildTypesTask/inlineExtensions.ts"],"sourcesContent":["import type * as TS from \"typescript\";\nimport { join } from \"node:path\";\n\n// It needs for VSCode. It cannot resolve the import/export if it has no extension\n\nexport type FileExists = (path: string) => boolean;\n\nfunction transformAndExtractImports(\n ts: typeof import(\"typescript\"),\n content: string,\n ext: string,\n dtsExt: string,\n fileExists: FileExists,\n) {\n function addExtension(\n ts: typeof import(\"typescript\"),\n node: TS.StringLiteral,\n ext: string,\n dtsExt: string,\n fileExists: FileExists,\n ): TS.StringLiteral {\n const importPath = node.text;\n\n if (!importPath.startsWith(\".\")) {\n return node;\n }\n\n if (\n importPath.endsWith(\".cjs\") ||\n importPath.endsWith(\".mjs\") ||\n importPath.endsWith(\".js\")\n ) {\n return node;\n }\n\n const cleanedImportPath = importPath.replace(/\\.[cm]?js$/, \"\");\n\n if (fileExists(`${cleanedImportPath}${dtsExt}`)) {\n return ts.factory.createStringLiteral(`${cleanedImportPath}${ext}`);\n }\n\n if (fileExists(\"./\" + join(cleanedImportPath, `index${dtsExt}`))) {\n return ts.factory.createStringLiteral(\n \"./\" + join(cleanedImportPath, `index${ext}`),\n );\n }\n\n return node; // Return the original node if no modification was made\n }\n\n const sourceFile = ts.createSourceFile(\n \"temp.ts\",\n content,\n ts.ScriptTarget.ESNext,\n true,\n );\n\n const transformer: TS.TransformerFactory<TS.SourceFile> =\n (context) => (rootNode) => {\n function visit(node: TS.Node): TS.Node {\n // export {} from \"moduleSpecifier\";\n if (ts.isExportDeclaration(node)) {\n const moduleSpecifier = node.moduleSpecifier;\n if (moduleSpecifier && ts.isStringLiteral(moduleSpecifier)) {\n const updatedSpecifier = addExtension(\n ts,\n moduleSpecifier,\n ext,\n dtsExt,\n fileExists,\n );\n return ts.factory.updateExportDeclaration(\n node,\n node.modifiers,\n node.isTypeOnly,\n node.exportClause,\n updatedSpecifier,\n node.attributes,\n );\n }\n }\n\n // import {} from \"moduleSpecifier\";\n if (ts.isImportDeclaration(node)) {\n const moduleSpecifier = node.moduleSpecifier;\n if (moduleSpecifier && ts.isStringLiteral(moduleSpecifier)) {\n const updatedSpecifier = addExtension(\n ts,\n moduleSpecifier,\n ext,\n dtsExt,\n fileExists,\n );\n return ts.factory.updateImportDeclaration(\n node,\n node.modifiers,\n node.importClause,\n updatedSpecifier,\n node.attributes,\n );\n }\n }\n\n // import(\"argument\");\n if (\n ts.isCallExpression(node) &&\n node.expression.kind === ts.SyntaxKind.ImportKeyword\n ) {\n const [argument] = node.arguments;\n if (argument && ts.isStringLiteral(argument)) {\n const updatedArgument = addExtension(\n ts,\n argument,\n ext,\n dtsExt,\n fileExists,\n );\n return ts.factory.updateCallExpression(\n node,\n node.expression,\n node.typeArguments,\n [updatedArgument],\n );\n }\n }\n\n // Generic<import(\"node\")>;\n if (\n ts.isStringLiteral(node) &&\n ts.isLiteralTypeNode(node.parent) &&\n node.parent.parent.kind === ts.SyntaxKind.ImportType\n ) {\n return addExtension(ts, node, ext, dtsExt, fileExists);\n }\n\n return ts.visitEachChild(node, visit, context);\n }\n\n return ts.visitNode(rootNode, visit) as TS.SourceFile;\n };\n\n const result = ts.transform(sourceFile, [transformer]);\n const printer = ts.createPrinter();\n const transformedSourceFile = result.transformed[0];\n const output = printer.printFile(transformedSourceFile);\n result.dispose();\n\n return output;\n}\n\nexport function inlineExtensionsMjs(\n ts: typeof import(\"typescript\"),\n content: string,\n fileExists: FileExists,\n) {\n return transformAndExtractImports(ts, content, \".mjs\", \".d.mts\", fileExists);\n}\n\nexport function inlineExtensionsCjs(\n ts: typeof import(\"typescript\"),\n content: string,\n fileExists: FileExists,\n) {\n return transformAndExtractImports(ts, content, \".js\", \".d.ts\", fileExists);\n}\n"],"names":["ts","ext","dtsExt","fileExists"],"mappings":";AAOA,SAAS,2BACP,IACA,SACA,K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