smartbundle 0.14.1 → 0.14.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.
- package/__compiled__/cjs/src/tasks/buildTypesTask/findTypingsPackages.js +49 -17
- package/__compiled__/cjs/src/tasks/buildTypesTask/findTypingsPackages.js.map +1 -1
- package/__compiled__/esm/src/tasks/buildTypesTask/findTypingsPackages.mjs +48 -17
- package/__compiled__/esm/src/tasks/buildTypesTask/findTypingsPackages.mjs.map +1 -1
- package/package.json +1 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const path = require("node:path");
|
|
4
|
+
const fs = require("node:fs");
|
|
4
5
|
function _interopNamespaceDefault(e) {
|
|
5
6
|
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
|
|
6
7
|
if (e) {
|
|
@@ -18,6 +19,7 @@ function _interopNamespaceDefault(e) {
|
|
|
18
19
|
return Object.freeze(n);
|
|
19
20
|
}
|
|
20
21
|
const path__namespace = /* @__PURE__ */ _interopNamespaceDefault(path);
|
|
22
|
+
const fs__namespace = /* @__PURE__ */ _interopNamespaceDefault(fs);
|
|
21
23
|
function createVirtualHostFunctions(ts, virtualFilePath, virtualSourceContent, originalHost) {
|
|
22
24
|
const getSourceFile = (fileName, languageVersion) => {
|
|
23
25
|
if (fileName === virtualFilePath) {
|
|
@@ -48,11 +50,9 @@ function createVirtualHostFunctions(ts, virtualFilePath, virtualSourceContent, o
|
|
|
48
50
|
};
|
|
49
51
|
}
|
|
50
52
|
function createCompilerHostWithVirtualSource(ts, packages, sourceDir) {
|
|
51
|
-
const
|
|
52
|
-
"export const a = 1;\n";
|
|
53
|
-
const virtualFilePath = path__namespace.join(sourceDir, "virtual.ts");
|
|
54
|
-
const originalHost = ts.createCompilerHost({
|
|
53
|
+
const compilerOptions = {
|
|
55
54
|
target: ts.ScriptTarget.ESNext,
|
|
55
|
+
module: ts.ModuleKind.NodeNext,
|
|
56
56
|
moduleResolution: ts.ModuleResolutionKind.NodeNext,
|
|
57
57
|
baseUrl: ".",
|
|
58
58
|
sourceRoot: sourceDir,
|
|
@@ -62,7 +62,11 @@ function createCompilerHostWithVirtualSource(ts, packages, sourceDir) {
|
|
|
62
62
|
paths: {
|
|
63
63
|
"*": ["node_modules/*"]
|
|
64
64
|
}
|
|
65
|
-
}
|
|
65
|
+
};
|
|
66
|
+
const virtualSourceContent = [...packages].map((p) => `import "${p}";`).join("\n") + // for ignoring the `Generated an empty chunk: "."` error
|
|
67
|
+
"export const a = 1;\n";
|
|
68
|
+
const virtualFilePath = path__namespace.join(sourceDir, "virtual.ts");
|
|
69
|
+
const originalHost = ts.createCompilerHost(compilerOptions);
|
|
66
70
|
const virtualFunctions = createVirtualHostFunctions(
|
|
67
71
|
ts,
|
|
68
72
|
virtualFilePath,
|
|
@@ -75,18 +79,40 @@ function createCompilerHostWithVirtualSource(ts, packages, sourceDir) {
|
|
|
75
79
|
return virtualFunctions[prop] || target[prop];
|
|
76
80
|
}
|
|
77
81
|
}),
|
|
78
|
-
virtualFilePath
|
|
82
|
+
virtualFilePath,
|
|
83
|
+
compilerOptions
|
|
79
84
|
};
|
|
80
85
|
}
|
|
86
|
+
function findPackageNameForResolvedFile(resolvedFileName, sourceDir) {
|
|
87
|
+
let currentDir = path__namespace.dirname(resolvedFileName);
|
|
88
|
+
const sourcePackageJsonPath = path__namespace.join(sourceDir, "package.json");
|
|
89
|
+
while (true) {
|
|
90
|
+
const packageJsonPath = path__namespace.join(currentDir, "package.json");
|
|
91
|
+
if (fs__namespace.existsSync(packageJsonPath)) {
|
|
92
|
+
if (packageJsonPath === sourcePackageJsonPath) {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
try {
|
|
96
|
+
const packageJson = JSON.parse(
|
|
97
|
+
fs__namespace.readFileSync(packageJsonPath, "utf-8")
|
|
98
|
+
);
|
|
99
|
+
return typeof packageJson.name === "string" ? packageJson.name : null;
|
|
100
|
+
} catch {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
const parentDir = path__namespace.dirname(currentDir);
|
|
105
|
+
if (parentDir === currentDir) {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
currentDir = parentDir;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
81
111
|
function findTypingsPackages({ ts }, packages, sourceDir) {
|
|
82
|
-
const { host, virtualFilePath } = createCompilerHostWithVirtualSource(
|
|
83
|
-
ts,
|
|
84
|
-
packages,
|
|
85
|
-
sourceDir
|
|
86
|
-
);
|
|
112
|
+
const { host, virtualFilePath, compilerOptions } = createCompilerHostWithVirtualSource(ts, packages, sourceDir);
|
|
87
113
|
const program = ts.createProgram({
|
|
88
114
|
rootNames: [virtualFilePath],
|
|
89
|
-
options:
|
|
115
|
+
options: compilerOptions,
|
|
90
116
|
host
|
|
91
117
|
});
|
|
92
118
|
const sourceFile = program.getSourceFile(virtualFilePath);
|
|
@@ -98,22 +124,28 @@ function findTypingsPackages({ ts }, packages, sourceDir) {
|
|
|
98
124
|
const missingTypings = /* @__PURE__ */ new Set();
|
|
99
125
|
const existingTypingPackages = /* @__PURE__ */ new Set();
|
|
100
126
|
sourceFile.forEachChild((node) => {
|
|
101
|
-
var _a;
|
|
102
127
|
if (ts.isImportDeclaration(node)) {
|
|
103
128
|
const moduleSpecifier = node.moduleSpecifier;
|
|
104
129
|
if (ts.isStringLiteral(moduleSpecifier)) {
|
|
105
130
|
const moduleResolution = ts.resolveModuleName(
|
|
106
131
|
moduleSpecifier.text,
|
|
107
132
|
virtualFilePath,
|
|
108
|
-
|
|
133
|
+
compilerOptions,
|
|
109
134
|
host
|
|
110
135
|
);
|
|
111
|
-
|
|
136
|
+
const resolvedModule = moduleResolution == null ? void 0 : moduleResolution.resolvedModule;
|
|
137
|
+
if (!resolvedModule) {
|
|
112
138
|
missingTypings.add(moduleSpecifier.text);
|
|
113
139
|
} else {
|
|
114
|
-
|
|
115
|
-
|
|
140
|
+
const packageName = findPackageNameForResolvedFile(
|
|
141
|
+
resolvedModule.resolvedFileName,
|
|
142
|
+
sourceDir
|
|
116
143
|
);
|
|
144
|
+
if (packageName) {
|
|
145
|
+
existingTypingPackages.add(packageName);
|
|
146
|
+
} else {
|
|
147
|
+
missingTypings.add(moduleSpecifier.text);
|
|
148
|
+
}
|
|
117
149
|
}
|
|
118
150
|
}
|
|
119
151
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"findTypingsPackages.js","sources":["../../../../../../src/tasks/buildTypesTask/findTypingsPackages.ts"],"sourcesContent":["import type * as ts from \"typescript\";\nimport * as path from \"node:path\";\nimport type { TS } from \"../../detectModules.js\";\n\ntype HostFunctions = {\n getSourceFile: ts.CompilerHost[\"getSourceFile\"];\n fileExists: ts.CompilerHost[\"fileExists\"];\n readFile: ts.CompilerHost[\"readFile\"];\n};\n\nfunction createVirtualHostFunctions(\n ts: TS[\"ts\"],\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 ts: TS[\"ts\"],\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(
|
|
1
|
+
{"version":3,"file":"findTypingsPackages.js","sources":["../../../../../../src/tasks/buildTypesTask/findTypingsPackages.ts"],"sourcesContent":["import type * as ts from \"typescript\";\nimport * as path from \"node:path\";\nimport * as fs from \"node:fs\";\nimport type { TS } from \"../../detectModules.js\";\n\ntype HostFunctions = {\n getSourceFile: ts.CompilerHost[\"getSourceFile\"];\n fileExists: ts.CompilerHost[\"fileExists\"];\n readFile: ts.CompilerHost[\"readFile\"];\n};\n\nfunction createVirtualHostFunctions(\n ts: TS[\"ts\"],\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 ts: TS[\"ts\"],\n packages: Set<string>,\n sourceDir: string,\n) {\n const compilerOptions = {\n target: ts.ScriptTarget.ESNext,\n module: ts.ModuleKind.NodeNext,\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 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(compilerOptions);\n const virtualFunctions = createVirtualHostFunctions(\n ts,\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 compilerOptions,\n };\n}\n\nfunction findPackageNameForResolvedFile(\n resolvedFileName: string,\n sourceDir: string,\n) {\n let currentDir = path.dirname(resolvedFileName);\n const sourcePackageJsonPath = path.join(sourceDir, \"package.json\");\n\n while (true) {\n const packageJsonPath = path.join(currentDir, \"package.json\");\n if (fs.existsSync(packageJsonPath)) {\n if (packageJsonPath === sourcePackageJsonPath) {\n return null;\n }\n\n try {\n const packageJson = JSON.parse(\n fs.readFileSync(packageJsonPath, \"utf-8\"),\n );\n return typeof packageJson.name === \"string\" ? packageJson.name : null;\n } catch {\n return null;\n }\n }\n\n const parentDir = path.dirname(currentDir);\n if (parentDir === currentDir) {\n return null;\n }\n currentDir = parentDir;\n }\n}\n\nexport function findTypingsPackages(\n { ts }: TS,\n packages: Set<string>,\n sourceDir: string,\n) {\n const { host, virtualFilePath, compilerOptions } =\n createCompilerHostWithVirtualSource(ts, packages, sourceDir);\n\n const program = ts.createProgram({\n rootNames: [virtualFilePath],\n options: compilerOptions,\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 compilerOptions,\n host,\n );\n\n const resolvedModule = moduleResolution?.resolvedModule;\n\n if (!resolvedModule) {\n missingTypings.add(moduleSpecifier.text);\n } else {\n const packageName = findPackageNameForResolvedFile(\n resolvedModule.resolvedFileName,\n sourceDir,\n );\n if (packageName) {\n existingTypingPackages.add(packageName);\n } else {\n missingTypings.add(moduleSpecifier.text);\n }\n }\n }\n }\n });\n\n return { missingTypings, existingTypingPackages };\n}\n"],"names":["path","fs"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAWA,SAAS,2BACP,IACA,iBACA,sBACA,cACe;AACT,QAAA,gBAAkD,CACtD,UACA,oBACG;AACH,QAAI,aAAa,iBAAiB;AAChC,aAAO,GAAG;AAAA,QACR;AAAA,QACA;AAAA,QACA;AAAA,MACF;AAAA,IAAA;AAEK,WAAA,aAAa,cAAc,UAAU,eAAe;AAAA,EAC7D;AAEM,QAAA,aAA4C,CAAC,aAAa;AAC9D,QAAI,aAAa,iBAAiB;AACzB,aAAA;AAAA,IAAA;AAEF,WAAA,aAAa,WAAW,QAAQ;AAAA,EACzC;AAEM,QAAA,WAAwC,CAAC,aAAa;AAC1D,QAAI,aAAa,iBAAiB;AACzB,aAAA;AAAA,IAAA;AAEF,WAAA,aAAa,SAAS,QAAQ;AAAA,EACvC;AAEO,SAAA;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAEA,SAAS,oCACP,IACA,UACA,WACA;AACA,QAAM,kBAAkB;AAAA,IACtB,QAAQ,GAAG,aAAa;AAAA,IACxB,QAAQ,GAAG,WAAW;AAAA,IACtB,kBAAkB,GAAG,qBAAqB;AAAA,IAC1C,SAAS;AAAA,IACT,YAAY;AAAA,IACZ,QAAQ;AAAA,IACR,qBAAqB;AAAA,IACrB,eAAe;AAAA,IACf,OAAO;AAAA,MACL,KAAK,CAAC,gBAAgB;AAAA,IAAA;AAAA,EAE1B;AACA,QAAM,uBACJ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;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;AAAA,UACA;AAAA,QACF;AAEA,cAAM,iBAAiB,qDAAkB;AAEzC,YAAI,CAAC,gBAAgB;AACJ,yBAAA,IAAI,gBAAgB,IAAI;AAAA,QAAA,OAClC;AACL,gBAAM,cAAc;AAAA,YAClB,eAAe;AAAA,YACf;AAAA,UACF;AACA,cAAI,aAAa;AACf,mCAAuB,IAAI,WAAW;AAAA,UAAA,OACjC;AACU,2BAAA,IAAI,gBAAgB,IAAI;AAAA,UAAA;AAAA,QACzC;AAAA,MACF;AAAA,IACF;AAAA,EACF,CACD;AAEM,SAAA,EAAE,gBAAgB,uBAAuB;AAClD;;"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as path from "node:path";
|
|
2
|
+
import * as fs from "node:fs";
|
|
2
3
|
function createVirtualHostFunctions(ts, virtualFilePath, virtualSourceContent, originalHost) {
|
|
3
4
|
const getSourceFile = (fileName, languageVersion) => {
|
|
4
5
|
if (fileName === virtualFilePath) {
|
|
@@ -29,11 +30,9 @@ function createVirtualHostFunctions(ts, virtualFilePath, virtualSourceContent, o
|
|
|
29
30
|
};
|
|
30
31
|
}
|
|
31
32
|
function createCompilerHostWithVirtualSource(ts, packages, sourceDir) {
|
|
32
|
-
const
|
|
33
|
-
"export const a = 1;\n";
|
|
34
|
-
const virtualFilePath = path.join(sourceDir, "virtual.ts");
|
|
35
|
-
const originalHost = ts.createCompilerHost({
|
|
33
|
+
const compilerOptions = {
|
|
36
34
|
target: ts.ScriptTarget.ESNext,
|
|
35
|
+
module: ts.ModuleKind.NodeNext,
|
|
37
36
|
moduleResolution: ts.ModuleResolutionKind.NodeNext,
|
|
38
37
|
baseUrl: ".",
|
|
39
38
|
sourceRoot: sourceDir,
|
|
@@ -43,7 +42,11 @@ function createCompilerHostWithVirtualSource(ts, packages, sourceDir) {
|
|
|
43
42
|
paths: {
|
|
44
43
|
"*": ["node_modules/*"]
|
|
45
44
|
}
|
|
46
|
-
}
|
|
45
|
+
};
|
|
46
|
+
const virtualSourceContent = [...packages].map((p) => `import "${p}";`).join("\n") + // for ignoring the `Generated an empty chunk: "."` error
|
|
47
|
+
"export const a = 1;\n";
|
|
48
|
+
const virtualFilePath = path.join(sourceDir, "virtual.ts");
|
|
49
|
+
const originalHost = ts.createCompilerHost(compilerOptions);
|
|
47
50
|
const virtualFunctions = createVirtualHostFunctions(
|
|
48
51
|
ts,
|
|
49
52
|
virtualFilePath,
|
|
@@ -56,18 +59,40 @@ function createCompilerHostWithVirtualSource(ts, packages, sourceDir) {
|
|
|
56
59
|
return virtualFunctions[prop] || target[prop];
|
|
57
60
|
}
|
|
58
61
|
}),
|
|
59
|
-
virtualFilePath
|
|
62
|
+
virtualFilePath,
|
|
63
|
+
compilerOptions
|
|
60
64
|
};
|
|
61
65
|
}
|
|
66
|
+
function findPackageNameForResolvedFile(resolvedFileName, sourceDir) {
|
|
67
|
+
let currentDir = path.dirname(resolvedFileName);
|
|
68
|
+
const sourcePackageJsonPath = path.join(sourceDir, "package.json");
|
|
69
|
+
while (true) {
|
|
70
|
+
const packageJsonPath = path.join(currentDir, "package.json");
|
|
71
|
+
if (fs.existsSync(packageJsonPath)) {
|
|
72
|
+
if (packageJsonPath === sourcePackageJsonPath) {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
try {
|
|
76
|
+
const packageJson = JSON.parse(
|
|
77
|
+
fs.readFileSync(packageJsonPath, "utf-8")
|
|
78
|
+
);
|
|
79
|
+
return typeof packageJson.name === "string" ? packageJson.name : null;
|
|
80
|
+
} catch {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
const parentDir = path.dirname(currentDir);
|
|
85
|
+
if (parentDir === currentDir) {
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
currentDir = parentDir;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
62
91
|
function findTypingsPackages({ ts }, packages, sourceDir) {
|
|
63
|
-
const { host, virtualFilePath } = createCompilerHostWithVirtualSource(
|
|
64
|
-
ts,
|
|
65
|
-
packages,
|
|
66
|
-
sourceDir
|
|
67
|
-
);
|
|
92
|
+
const { host, virtualFilePath, compilerOptions } = createCompilerHostWithVirtualSource(ts, packages, sourceDir);
|
|
68
93
|
const program = ts.createProgram({
|
|
69
94
|
rootNames: [virtualFilePath],
|
|
70
|
-
options:
|
|
95
|
+
options: compilerOptions,
|
|
71
96
|
host
|
|
72
97
|
});
|
|
73
98
|
const sourceFile = program.getSourceFile(virtualFilePath);
|
|
@@ -79,22 +104,28 @@ function findTypingsPackages({ ts }, packages, sourceDir) {
|
|
|
79
104
|
const missingTypings = /* @__PURE__ */ new Set();
|
|
80
105
|
const existingTypingPackages = /* @__PURE__ */ new Set();
|
|
81
106
|
sourceFile.forEachChild((node) => {
|
|
82
|
-
var _a;
|
|
83
107
|
if (ts.isImportDeclaration(node)) {
|
|
84
108
|
const moduleSpecifier = node.moduleSpecifier;
|
|
85
109
|
if (ts.isStringLiteral(moduleSpecifier)) {
|
|
86
110
|
const moduleResolution = ts.resolveModuleName(
|
|
87
111
|
moduleSpecifier.text,
|
|
88
112
|
virtualFilePath,
|
|
89
|
-
|
|
113
|
+
compilerOptions,
|
|
90
114
|
host
|
|
91
115
|
);
|
|
92
|
-
|
|
116
|
+
const resolvedModule = moduleResolution == null ? void 0 : moduleResolution.resolvedModule;
|
|
117
|
+
if (!resolvedModule) {
|
|
93
118
|
missingTypings.add(moduleSpecifier.text);
|
|
94
119
|
} else {
|
|
95
|
-
|
|
96
|
-
|
|
120
|
+
const packageName = findPackageNameForResolvedFile(
|
|
121
|
+
resolvedModule.resolvedFileName,
|
|
122
|
+
sourceDir
|
|
97
123
|
);
|
|
124
|
+
if (packageName) {
|
|
125
|
+
existingTypingPackages.add(packageName);
|
|
126
|
+
} else {
|
|
127
|
+
missingTypings.add(moduleSpecifier.text);
|
|
128
|
+
}
|
|
98
129
|
}
|
|
99
130
|
}
|
|
100
131
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"findTypingsPackages.mjs","sources":["../../../../../../src/tasks/buildTypesTask/findTypingsPackages.ts"],"sourcesContent":["import type * as ts from \"typescript\";\nimport * as path from \"node:path\";\nimport type { TS } from \"../../detectModules.js\";\n\ntype HostFunctions = {\n getSourceFile: ts.CompilerHost[\"getSourceFile\"];\n fileExists: ts.CompilerHost[\"fileExists\"];\n readFile: ts.CompilerHost[\"readFile\"];\n};\n\nfunction createVirtualHostFunctions(\n ts: TS[\"ts\"],\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 ts: TS[\"ts\"],\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(
|
|
1
|
+
{"version":3,"file":"findTypingsPackages.mjs","sources":["../../../../../../src/tasks/buildTypesTask/findTypingsPackages.ts"],"sourcesContent":["import type * as ts from \"typescript\";\nimport * as path from \"node:path\";\nimport * as fs from \"node:fs\";\nimport type { TS } from \"../../detectModules.js\";\n\ntype HostFunctions = {\n getSourceFile: ts.CompilerHost[\"getSourceFile\"];\n fileExists: ts.CompilerHost[\"fileExists\"];\n readFile: ts.CompilerHost[\"readFile\"];\n};\n\nfunction createVirtualHostFunctions(\n ts: TS[\"ts\"],\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 ts: TS[\"ts\"],\n packages: Set<string>,\n sourceDir: string,\n) {\n const compilerOptions = {\n target: ts.ScriptTarget.ESNext,\n module: ts.ModuleKind.NodeNext,\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 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(compilerOptions);\n const virtualFunctions = createVirtualHostFunctions(\n ts,\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 compilerOptions,\n };\n}\n\nfunction findPackageNameForResolvedFile(\n resolvedFileName: string,\n sourceDir: string,\n) {\n let currentDir = path.dirname(resolvedFileName);\n const sourcePackageJsonPath = path.join(sourceDir, \"package.json\");\n\n while (true) {\n const packageJsonPath = path.join(currentDir, \"package.json\");\n if (fs.existsSync(packageJsonPath)) {\n if (packageJsonPath === sourcePackageJsonPath) {\n return null;\n }\n\n try {\n const packageJson = JSON.parse(\n fs.readFileSync(packageJsonPath, \"utf-8\"),\n );\n return typeof packageJson.name === \"string\" ? packageJson.name : null;\n } catch {\n return null;\n }\n }\n\n const parentDir = path.dirname(currentDir);\n if (parentDir === currentDir) {\n return null;\n }\n currentDir = parentDir;\n }\n}\n\nexport function findTypingsPackages(\n { ts }: TS,\n packages: Set<string>,\n sourceDir: string,\n) {\n const { host, virtualFilePath, compilerOptions } =\n createCompilerHostWithVirtualSource(ts, packages, sourceDir);\n\n const program = ts.createProgram({\n rootNames: [virtualFilePath],\n options: compilerOptions,\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 compilerOptions,\n host,\n );\n\n const resolvedModule = moduleResolution?.resolvedModule;\n\n if (!resolvedModule) {\n missingTypings.add(moduleSpecifier.text);\n } else {\n const packageName = findPackageNameForResolvedFile(\n resolvedModule.resolvedFileName,\n sourceDir,\n );\n if (packageName) {\n existingTypingPackages.add(packageName);\n } else {\n missingTypings.add(moduleSpecifier.text);\n }\n }\n }\n }\n });\n\n return { missingTypings, existingTypingPackages };\n}\n"],"names":[],"mappings":";;AAWA,SAAS,2BACP,IACA,iBACA,sBACA,cACe;AACT,QAAA,gBAAkD,CACtD,UACA,oBACG;AACH,QAAI,aAAa,iBAAiB;AAChC,aAAO,GAAG;AAAA,QACR;AAAA,QACA;AAAA,QACA;AAAA,MACF;AAAA,IAAA;AAEK,WAAA,aAAa,cAAc,UAAU,eAAe;AAAA,EAC7D;AAEM,QAAA,aAA4C,CAAC,aAAa;AAC9D,QAAI,aAAa,iBAAiB;AACzB,aAAA;AAAA,IAAA;AAEF,WAAA,aAAa,WAAW,QAAQ;AAAA,EACzC;AAEM,QAAA,WAAwC,CAAC,aAAa;AAC1D,QAAI,aAAa,iBAAiB;AACzB,aAAA;AAAA,IAAA;AAEF,WAAA,aAAa,SAAS,QAAQ;AAAA,EACvC;AAEO,SAAA;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAEA,SAAS,oCACP,IACA,UACA,WACA;AACA,QAAM,kBAAkB;AAAA,IACtB,QAAQ,GAAG,aAAa;AAAA,IACxB,QAAQ,GAAG,WAAW;AAAA,IACtB,kBAAkB,GAAG,qBAAqB;AAAA,IAC1C,SAAS;AAAA,IACT,YAAY;AAAA,IACZ,QAAQ;AAAA,IACR,qBAAqB;AAAA,IACrB,eAAe;AAAA,IACf,OAAO;AAAA,MACL,KAAK,CAAC,gBAAgB;AAAA,IAAA;AAAA,EAE1B;AAC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