@module-federation/third-party-dts-extractor 0.0.0-chore-bump-node-22-20260710161714

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/dist/index.mjs ADDED
@@ -0,0 +1,206 @@
1
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
+ }) : x)(function(x) {
4
+ if (typeof require !== "undefined")
5
+ return require.apply(this, arguments);
6
+ throw Error('Dynamic require of "' + x + '" is not supported');
7
+ });
8
+
9
+ // src/ThirdPartyExtractor.ts
10
+ import { up as findPkgUp } from "empathic/package";
11
+ import { copyFile, lstat, mkdir, readdir } from "fs/promises";
12
+ import { existsSync, readFileSync } from "fs";
13
+ import path2 from "path";
14
+ import { resolveModulePath } from "exsolve";
15
+
16
+ // src/utils.ts
17
+ import path from "path";
18
+ import fs from "fs";
19
+ function getTypedName(name) {
20
+ return `@types/${name.replace(/^@/, "").replace("/", "__")}`;
21
+ }
22
+ function getPackageRootDir(packageName) {
23
+ if (!packageName) {
24
+ throw new Error("No package name provided.");
25
+ }
26
+ const entryFile = __require.resolve(packageName);
27
+ const entryDir = path.dirname(entryFile);
28
+ let fallbackPackageJsonPath = path.join(
29
+ entryDir,
30
+ "package.json"
31
+ );
32
+ if (!fs.existsSync(fallbackPackageJsonPath)) {
33
+ fallbackPackageJsonPath = null;
34
+ }
35
+ const match = packageName.match(/^@[^/]+\/(.+)$/);
36
+ const localName = match ? match[1] : packageName;
37
+ let currentDir = entryDir;
38
+ while (path.basename(currentDir) !== localName && // Keep going if names differ
39
+ path.dirname(currentDir) !== currentDir) {
40
+ try {
41
+ currentDir = path.dirname(currentDir);
42
+ } catch (err) {
43
+ if (err.code === "EACCES") {
44
+ continue;
45
+ } else {
46
+ throw err;
47
+ }
48
+ }
49
+ }
50
+ if (path.basename(currentDir) === localName) {
51
+ const candidate = path.join(currentDir, "package.json");
52
+ if (fs.existsSync(candidate)) {
53
+ const pkgContent = JSON.parse(fs.readFileSync(candidate, "utf8"));
54
+ if (pkgContent.name === packageName) {
55
+ return currentDir;
56
+ }
57
+ }
58
+ }
59
+ if (fallbackPackageJsonPath) {
60
+ return entryDir;
61
+ }
62
+ throw new Error(
63
+ `Could not find a matching package.json for "${packageName}" and no fallback was found.`
64
+ );
65
+ }
66
+
67
+ // src/ThirdPartyExtractor.ts
68
+ var ignoredPkgs = ["typescript"];
69
+ var isNodeUtils = (pkgJsonPath, importPath) => {
70
+ return pkgJsonPath === importPath;
71
+ };
72
+ var ThirdPartyExtractor = class {
73
+ constructor({
74
+ destDir,
75
+ context = process.cwd(),
76
+ exclude = []
77
+ }) {
78
+ this.destDir = destDir;
79
+ this.context = context;
80
+ this.pkgs = {};
81
+ this.pattern = /(from|import\()\s*['"]([^'"]+)['"]/g;
82
+ this.exclude = exclude;
83
+ }
84
+ addPkgs(pkgName, dirName) {
85
+ if (ignoredPkgs.includes(pkgName)) {
86
+ return;
87
+ }
88
+ if (this.exclude.some((pattern) => {
89
+ if (typeof pattern === "string") {
90
+ return new RegExp(pattern).test(pkgName);
91
+ } else {
92
+ return pattern.test(pkgName);
93
+ }
94
+ })) {
95
+ return;
96
+ }
97
+ this.pkgs[pkgName] = dirName;
98
+ }
99
+ inferPkgDir(importPath) {
100
+ if (this.pkgs[importPath]) {
101
+ return;
102
+ }
103
+ if (path2.isAbsolute(importPath)) {
104
+ return;
105
+ }
106
+ if (importPath.startsWith(".")) {
107
+ return;
108
+ }
109
+ try {
110
+ const importEntry = __require.resolve(importPath, {
111
+ paths: [this.context]
112
+ });
113
+ if (isNodeUtils(importEntry, importPath)) {
114
+ return;
115
+ }
116
+ const packageDir = getPackageRootDir(importPath);
117
+ const pkgJsonPath = path2.join(packageDir, "package.json");
118
+ const dir = path2.dirname(pkgJsonPath);
119
+ const pkg = JSON.parse(readFileSync(pkgJsonPath, "utf-8"));
120
+ const types = pkg.types || pkg.typings;
121
+ if (dir === this.context) {
122
+ return;
123
+ }
124
+ if (types) {
125
+ const typesDir = path2.dirname(path2.resolve(dir, types));
126
+ this.addPkgs(pkg.name, typesDir);
127
+ return typesDir;
128
+ } else if (existsSync(path2.resolve(dir, "index.d.ts"))) {
129
+ this.addPkgs(pkg.name, dir);
130
+ return dir;
131
+ } else {
132
+ const typedPkgName = getTypedName(pkg.name);
133
+ const typedPkgJsonPath = findPkgUp({
134
+ cwd: path2.dirname(
135
+ resolveModulePath(`${typedPkgName}/package.json`, {
136
+ from: this.context
137
+ })
138
+ )
139
+ });
140
+ const typedDir = path2.dirname(typedPkgJsonPath);
141
+ readFileSync(typedPkgJsonPath, "utf-8");
142
+ this.addPkgs(typedPkgName, typedDir);
143
+ return typedDir;
144
+ }
145
+ } catch (_err) {
146
+ return;
147
+ }
148
+ }
149
+ collectTypeImports(str) {
150
+ const { pattern } = this;
151
+ let match;
152
+ const imports = /* @__PURE__ */ new Set();
153
+ while ((match = pattern.exec(str)) !== null) {
154
+ imports.add(match[2]);
155
+ }
156
+ return [...imports];
157
+ }
158
+ collectPkgs(str) {
159
+ const imports = this.collectTypeImports(str);
160
+ imports.forEach((importPath) => {
161
+ this.inferPkgDir(importPath);
162
+ });
163
+ }
164
+ async copyDts() {
165
+ if (!Object.keys(this.pkgs).length) {
166
+ return;
167
+ }
168
+ const ensureDir = async (dir) => mkdir(dir, { recursive: true });
169
+ const copyFiles = async (srcDir, destDir) => {
170
+ if (srcDir.startsWith(".")) {
171
+ return;
172
+ }
173
+ const files = await readdir(srcDir);
174
+ await Promise.all(
175
+ files.map(async (file) => {
176
+ const fullPath = path2.join(srcDir, file);
177
+ if (["node_modules", "bin"].includes(file)) {
178
+ return;
179
+ }
180
+ const stats = await lstat(fullPath);
181
+ if (stats.isDirectory()) {
182
+ const destFullPath = path2.join(destDir, file);
183
+ await ensureDir(destFullPath);
184
+ await copyFiles(fullPath, destFullPath);
185
+ } else {
186
+ if (fullPath.endsWith(".d.ts") || fullPath.endsWith("package.json")) {
187
+ await copyFile(fullPath, path2.join(destDir, file));
188
+ }
189
+ }
190
+ })
191
+ );
192
+ };
193
+ await ensureDir(this.destDir);
194
+ await Promise.all(
195
+ Object.keys(this.pkgs).map(async (pkgName) => {
196
+ const pkgDir = this.pkgs[pkgName];
197
+ const destDir = path2.resolve(this.destDir, pkgName);
198
+ await ensureDir(destDir);
199
+ await copyFiles(pkgDir, destDir);
200
+ })
201
+ );
202
+ }
203
+ };
204
+ export {
205
+ ThirdPartyExtractor
206
+ };
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@module-federation/third-party-dts-extractor",
3
+ "version": "0.0.0-chore-bump-node-22-20260710161714",
4
+ "files": [
5
+ "dist/",
6
+ "README.md"
7
+ ],
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/module-federation/core.git",
11
+ "directory": "packages/third-party-dts-extractor"
12
+ },
13
+ "publishConfig": {
14
+ "access": "public"
15
+ },
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "import": "./dist/index.mjs",
20
+ "require": "./dist/index.js"
21
+ }
22
+ },
23
+ "typesVersions": {
24
+ "*": {
25
+ ".": [
26
+ "./dist/index.d.ts"
27
+ ]
28
+ }
29
+ },
30
+ "main": "./dist/index.js",
31
+ "types": "./dist/index.d.ts",
32
+ "author": "hanric <hanric.zhang@gmail.com>",
33
+ "license": "MIT",
34
+ "dependencies": {
35
+ "empathic": "2.0.0",
36
+ "exsolve": "1.0.8"
37
+ },
38
+ "devDependencies": {
39
+ "@rstest/core": "^0.10.6",
40
+ "@types/node": "^20.19.5",
41
+ "@types/react": "^18.3.11",
42
+ "react": "^18",
43
+ "tsup": "7.3.0"
44
+ },
45
+ "scripts": {
46
+ "build": "tsup --config tsup.config.ts --outDir dist && cp package.json dist && cp *.md dist",
47
+ "lint": "ESLINT_USE_FLAT_CONFIG=false pnpm exec eslint --ignore-pattern node_modules \"**/*.ts\"",
48
+ "test": "pnpm exec rstest --passWithNoTests",
49
+ "pre-release": "pnpm run test && pnpm run build"
50
+ }
51
+ }
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@module-federation/third-party-dts-extractor",
3
+ "version": "0.0.0-chore-bump-node-22-20260710161714",
4
+ "files": [
5
+ "dist/",
6
+ "README.md"
7
+ ],
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/module-federation/core.git",
11
+ "directory": "packages/third-party-dts-extractor"
12
+ },
13
+ "publishConfig": {
14
+ "access": "public"
15
+ },
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "import": "./dist/index.mjs",
20
+ "require": "./dist/index.js"
21
+ }
22
+ },
23
+ "typesVersions": {
24
+ "*": {
25
+ ".": [
26
+ "./dist/index.d.ts"
27
+ ]
28
+ }
29
+ },
30
+ "main": "./dist/index.js",
31
+ "types": "./dist/index.d.ts",
32
+ "author": "hanric <hanric.zhang@gmail.com>",
33
+ "license": "MIT",
34
+ "dependencies": {
35
+ "empathic": "2.0.0",
36
+ "exsolve": "1.0.8"
37
+ },
38
+ "devDependencies": {
39
+ "@rstest/core": "^0.10.6",
40
+ "@types/node": "^20.19.5",
41
+ "@types/react": "^18.3.11",
42
+ "react": "^18",
43
+ "tsup": "7.3.0"
44
+ },
45
+ "scripts": {
46
+ "build": "tsup --config tsup.config.ts --outDir dist && cp package.json dist && cp *.md dist",
47
+ "lint": "ESLINT_USE_FLAT_CONFIG=false pnpm exec eslint --ignore-pattern node_modules \"**/*.ts\"",
48
+ "test": "pnpm exec rstest --passWithNoTests",
49
+ "pre-release": "pnpm run test && pnpm run build"
50
+ }
51
+ }