@module-federation/dts-plugin 0.3.0 → 0.3.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/dist/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # @module-federation/dts-plugin
2
2
 
3
+ ## 0.3.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 0a9adaf: chore: remove useless log
8
+ - @module-federation/sdk@0.3.1
9
+ - @module-federation/managers@0.3.1
10
+ - @module-federation/third-party-dts-extractor@0.3.1
11
+
3
12
  ## 0.3.0
4
13
 
5
14
  ### Minor Changes
package/dist/core.js CHANGED
@@ -229,9 +229,6 @@ var compileTs = /* @__PURE__ */ __name((mapComponentsToExpose, tsConfig, remoteO
229
229
  }
230
230
  yield (0, import_promises.rm)(tempTsConfigJsonPath);
231
231
  } catch (err) {
232
- if (isDebugMode()) {
233
- console.log("tsconfig: ", JSON.stringify(tsConfig, null, 2));
234
- }
235
232
  throw err;
236
233
  }
237
234
  }), "compileTs");
@@ -28,150 +28,118 @@ import {
28
28
  } from "./chunk-6DND574L.js";
29
29
 
30
30
  // packages/dts-plugin/src/core/lib/typeScriptCompiler.ts
31
- import { ensureDirSync, writeFileSync, existsSync as existsSync2 } from "fs-extra";
31
+ import { ensureDirSync, writeFileSync, existsSync } from "fs-extra";
32
32
  import crypto from "crypto";
33
- import { stat, readdir, writeFile, rm as rm3, readFile } from "fs/promises";
34
- import { dirname as dirname2, join as join3, normalize, relative, resolve as resolve3, sep, extname as extname2, isAbsolute } from "path";
35
- import { ThirdPartyExtractor as ThirdPartyExtractor2 } from "@module-federation/third-party-dts-extractor";
33
+ import { stat, readdir, writeFile, rm, readFile } from "fs/promises";
34
+ import { dirname, join, normalize, relative, resolve, sep, extname, isAbsolute } from "path";
35
+ import { ThirdPartyExtractor } from "@module-federation/third-party-dts-extractor";
36
36
  import { exec } from "child_process";
37
37
  import util from "util";
38
38
  import { TEMP_DIR } from "@module-federation/sdk";
39
-
40
- // packages/dts-plugin/src/core/lib/utils.ts
41
- import fs3 from "fs";
42
- import path3 from "path";
43
- import axios from "axios";
44
- import http from "http";
45
- import https from "https";
46
- import ansiColors2 from "ansi-colors";
47
-
48
- // packages/dts-plugin/src/core/configurations/remotePlugin.ts
49
- import { existsSync } from "fs";
50
- import { dirname, join, resolve, extname } from "path";
51
- import { utils } from "@module-federation/managers";
52
- import typescript from "typescript";
53
- var defaultOptions = {
54
- tsConfigPath: "./tsconfig.json",
55
- typesFolder: "@mf-types",
56
- compiledTypesFolder: "compiled-types",
57
- hostRemoteTypesFolder: "@mf-types",
58
- deleteTypesFolder: true,
59
- additionalFilesToCompile: [],
60
- compilerInstance: "tsc",
61
- compileInChildProcess: false,
62
- implementation: "",
63
- generateAPITypes: false,
64
- context: process.cwd(),
65
- abortOnError: true,
66
- extractRemoteTypes: false,
67
- extractThirdParty: false
68
- };
69
- function getEffectiveRootDir(parsedCommandLine) {
70
- const compilerOptions = parsedCommandLine.options;
71
- if (compilerOptions.rootDir) {
72
- return compilerOptions.rootDir;
73
- }
74
- const files = parsedCommandLine.fileNames;
75
- if (files.length > 0) {
76
- const commonRoot = files.map((file) => dirname(file)).reduce((commonPath, fileDir) => {
77
- while (!fileDir.startsWith(commonPath)) {
78
- commonPath = dirname(commonPath);
79
- }
80
- return commonPath;
81
- }, files[0]);
82
- return commonRoot;
83
- }
84
- throw new Error("Can not get effective rootDir, please set compilerOptions.rootDir !");
39
+ var STARTS_WITH_SLASH = /^\//;
40
+ var DEFINITION_FILE_EXTENSION = ".d.ts";
41
+ var retrieveMfTypesPath = /* @__PURE__ */ __name((tsConfig, remoteOptions) => normalize(tsConfig.compilerOptions.outDir.replace(remoteOptions.compiledTypesFolder, "")), "retrieveMfTypesPath");
42
+ var retrieveOriginalOutDir = /* @__PURE__ */ __name((tsConfig, remoteOptions) => normalize(tsConfig.compilerOptions.outDir.replace(remoteOptions.compiledTypesFolder, "").replace(remoteOptions.typesFolder, "")), "retrieveOriginalOutDir");
43
+ var retrieveMfAPITypesPath = /* @__PURE__ */ __name((tsConfig, remoteOptions) => join(retrieveOriginalOutDir(tsConfig, remoteOptions), `${remoteOptions.typesFolder}.d.ts`), "retrieveMfAPITypesPath");
44
+ function writeTempTsConfig(tsConfig, context, name) {
45
+ const createHash = /* @__PURE__ */ __name((contents) => {
46
+ return crypto.createHash("md5").update(contents).digest("hex");
47
+ }, "createHash");
48
+ const hash = createHash(`${JSON.stringify(tsConfig)}${name}`);
49
+ const tempTsConfigJsonPath = resolve(context, "node_modules", TEMP_DIR, `tsconfig.${hash}.json`);
50
+ ensureDirSync(dirname(tempTsConfigJsonPath));
51
+ writeFileSync(tempTsConfigJsonPath, JSON.stringify(tsConfig, null, 2));
52
+ return tempTsConfigJsonPath;
85
53
  }
86
- __name(getEffectiveRootDir, "getEffectiveRootDir");
87
- var readTsConfig = /* @__PURE__ */ __name(({ tsConfigPath, typesFolder, compiledTypesFolder, context, additionalFilesToCompile }, mapComponentsToExpose) => {
88
- var _a2;
89
- const resolvedTsConfigPath = resolve(context, tsConfigPath);
90
- const readResult = typescript.readConfigFile(resolvedTsConfigPath, typescript.sys.readFile);
91
- if (readResult.error) {
92
- throw new Error(readResult.error.messageText.toString());
54
+ __name(writeTempTsConfig, "writeTempTsConfig");
55
+ var removeExt = /* @__PURE__ */ __name((f) => {
56
+ const ext = extname(f);
57
+ const regexPattern = new RegExp(`\\${ext}$`);
58
+ return f.replace(regexPattern, "");
59
+ }, "removeExt");
60
+ function getExposeKey(options) {
61
+ const { filePath, rootDir, outDir, mapExposeToEntry } = options;
62
+ const relativeFilePath = removeExt(relative(outDir, filePath.replace(new RegExp(`\\.d.ts$`), "")));
63
+ return mapExposeToEntry[relativeFilePath];
64
+ }
65
+ __name(getExposeKey, "getExposeKey");
66
+ var processTypesFile = /* @__PURE__ */ __name((options) => __async(void 0, null, function* () {
67
+ const { outDir, filePath, rootDir, cb, mapExposeToEntry, mfTypePath } = options;
68
+ if (!existsSync(filePath)) {
69
+ return;
93
70
  }
94
- const rawTsConfigJson = readResult.config;
95
- const configContent = typescript.parseJsonConfigFileContent(rawTsConfigJson, typescript.sys, dirname(resolvedTsConfigPath));
96
- const rootDir = getEffectiveRootDir(configContent);
97
- const outDir = resolve(context, configContent.options.outDir || "dist", typesFolder, compiledTypesFolder);
98
- const defaultCompilerOptions = {
99
- rootDir,
100
- emitDeclarationOnly: true,
101
- noEmit: false,
102
- declaration: true,
103
- outDir
104
- };
105
- rawTsConfigJson.compilerOptions = rawTsConfigJson.compilerOptions || {};
106
- rawTsConfigJson.compilerOptions = __spreadValues(__spreadValues({}, rawTsConfigJson.compilerOptions), defaultCompilerOptions);
107
- (_a2 = rawTsConfigJson.compilerOptions) == null ? true : delete _a2.paths;
108
- const filesToCompile = [
109
- ...Object.values(mapComponentsToExpose),
110
- ...additionalFilesToCompile
111
- ];
112
- rawTsConfigJson.include = [];
113
- rawTsConfigJson.files = filesToCompile;
114
- rawTsConfigJson.exclude = [];
115
- "references" in rawTsConfigJson && delete rawTsConfigJson.references;
116
- rawTsConfigJson.extends = resolvedTsConfigPath;
117
- return rawTsConfigJson;
118
- }, "readTsConfig");
119
- var TS_EXTENSIONS = [
120
- "ts",
121
- "tsx",
122
- "vue",
123
- "svelte"
124
- ];
125
- var resolveWithExtension = /* @__PURE__ */ __name((exposedPath, context) => {
126
- if (extname(exposedPath)) {
127
- return resolve(context, exposedPath);
71
+ const stats = yield stat(filePath);
72
+ if (stats.isDirectory()) {
73
+ const files = yield readdir(filePath);
74
+ yield Promise.all(files.map((file) => processTypesFile(__spreadProps(__spreadValues({}, options), {
75
+ filePath: join(filePath, file)
76
+ }))));
77
+ } else if (filePath.endsWith(".d.ts")) {
78
+ const exposeKey = getExposeKey({
79
+ filePath,
80
+ rootDir,
81
+ outDir,
82
+ mapExposeToEntry
83
+ });
84
+ if (exposeKey) {
85
+ const sourceEntry = exposeKey === "." ? "index" : exposeKey;
86
+ const mfeTypeEntry = join(mfTypePath, `${sourceEntry}${DEFINITION_FILE_EXTENSION}`);
87
+ const mfeTypeEntryDirectory = dirname(mfeTypeEntry);
88
+ const relativePathToOutput = relative(mfeTypeEntryDirectory, filePath).replace(DEFINITION_FILE_EXTENSION, "").replace(STARTS_WITH_SLASH, "").split(sep).join("/");
89
+ ensureDirSync(mfeTypeEntryDirectory);
90
+ yield writeFile(mfeTypeEntry, `export * from './${relativePathToOutput}';
91
+ export { default } from './${relativePathToOutput}';`);
92
+ }
93
+ const content = yield readFile(filePath, "utf8");
94
+ cb(content);
128
95
  }
129
- for (const extension of TS_EXTENSIONS) {
130
- const exposedPathWithExtension = resolve(context, `${exposedPath}.${extension}`);
131
- if (existsSync(exposedPathWithExtension)) {
132
- return exposedPathWithExtension;
96
+ }), "processTypesFile");
97
+ var compileTs = /* @__PURE__ */ __name((mapComponentsToExpose, tsConfig, remoteOptions) => __async(void 0, null, function* () {
98
+ if (!Object.keys(mapComponentsToExpose).length) {
99
+ return;
100
+ }
101
+ const { compilerOptions } = tsConfig;
102
+ const tempTsConfigJsonPath = writeTempTsConfig(tsConfig, remoteOptions.context, remoteOptions.moduleFederationConfig.name || "mf");
103
+ try {
104
+ const mfTypePath = retrieveMfTypesPath(tsConfig, remoteOptions);
105
+ const thirdPartyExtractor = new ThirdPartyExtractor(resolve(mfTypePath, "node_modules"), remoteOptions.context);
106
+ const execPromise = util.promisify(exec);
107
+ const cmd = `npx ${remoteOptions.compilerInstance} --project ${tempTsConfigJsonPath}`;
108
+ try {
109
+ yield execPromise(cmd);
110
+ } catch (err) {
111
+ throw new Error(`compile TS failed, the original command is '${cmd}'`);
133
112
  }
113
+ const mapExposeToEntry = Object.fromEntries(Object.entries(mapComponentsToExpose).map(([exposed, filename]) => {
114
+ const normalizedFileName = normalize(filename);
115
+ let relativeFileName = "";
116
+ if (isAbsolute(normalizedFileName)) {
117
+ relativeFileName = relative(tsConfig.compilerOptions.rootDir, normalizedFileName);
118
+ } else {
119
+ relativeFileName = relative(tsConfig.compilerOptions.rootDir, resolve(remoteOptions.context, normalizedFileName));
120
+ }
121
+ return [
122
+ removeExt(relativeFileName),
123
+ exposed
124
+ ];
125
+ }));
126
+ const cb = remoteOptions.extractThirdParty ? thirdPartyExtractor.collectPkgs.bind(thirdPartyExtractor) : () => void 0;
127
+ yield processTypesFile({
128
+ outDir: compilerOptions.outDir,
129
+ filePath: compilerOptions.outDir,
130
+ rootDir: compilerOptions.rootDir,
131
+ mfTypePath,
132
+ cb,
133
+ mapExposeToEntry
134
+ });
135
+ if (remoteOptions.extractThirdParty) {
136
+ yield thirdPartyExtractor.copyDts();
137
+ }
138
+ yield rm(tempTsConfigJsonPath);
139
+ } catch (err) {
140
+ throw err;
134
141
  }
135
- return void 0;
136
- }, "resolveWithExtension");
137
- var resolveExposes = /* @__PURE__ */ __name((remoteOptions) => {
138
- const parsedOptions = utils.parseOptions(remoteOptions.moduleFederationConfig.exposes || {}, (item, key) => ({
139
- exposePath: Array.isArray(item) ? item[0] : item,
140
- key
141
- }), (item, key) => ({
142
- exposePath: Array.isArray(item.import) ? item.import[0] : item.import[0],
143
- key
144
- }));
145
- return parsedOptions.reduce((accumulator, item) => {
146
- const { exposePath, key } = item[1];
147
- accumulator[key] = resolveWithExtension(exposePath, remoteOptions.context) || resolveWithExtension(join(exposePath, "index"), remoteOptions.context) || exposePath;
148
- return accumulator;
149
- }, {});
150
- }, "resolveExposes");
151
- var retrieveRemoteConfig = /* @__PURE__ */ __name((options) => {
152
- validateOptions(options);
153
- const remoteOptions = __spreadValues(__spreadValues({}, defaultOptions), options);
154
- const mapComponentsToExpose = resolveExposes(remoteOptions);
155
- const tsConfig = readTsConfig(remoteOptions, mapComponentsToExpose);
156
- return {
157
- tsConfig,
158
- mapComponentsToExpose,
159
- remoteOptions
160
- };
161
- }, "retrieveRemoteConfig");
162
-
163
- // packages/dts-plugin/src/core/lib/DTSManager.ts
164
- import ansiColors from "ansi-colors";
165
- import path2 from "path";
166
- import { rm as rm2 } from "fs/promises";
167
- import fs2 from "fs";
168
- import { MANIFEST_EXT, inferAutoPublicPath } from "@module-federation/sdk";
169
- import { ThirdPartyExtractor } from "@module-federation/third-party-dts-extractor";
170
-
171
- // packages/dts-plugin/src/core/lib/archiveHandler.ts
172
- import AdmZip from "adm-zip";
173
- import { resolve as resolve2, join as join2 } from "path";
174
- import { rm } from "fs/promises";
142
+ }), "compileTs");
175
143
 
176
144
  // packages/dts-plugin/src/server/DevServer.ts
177
145
  import WebSocket from "isomorphic-ws";
@@ -575,59 +543,140 @@ function createKoaServer(options) {
575
543
  __name(createKoaServer, "createKoaServer");
576
544
 
577
545
  // packages/dts-plugin/src/core/lib/archiveHandler.ts
578
- var retrieveTypesZipPath = /* @__PURE__ */ __name((mfTypesPath, remoteOptions) => join2(mfTypesPath.replace(remoteOptions.typesFolder, ""), `${remoteOptions.typesFolder}.zip`), "retrieveTypesZipPath");
579
- var createTypesArchive = /* @__PURE__ */ __name((tsConfig, remoteOptions) => __async(void 0, null, function* () {
580
- const mfTypesPath = retrieveMfTypesPath(tsConfig, remoteOptions);
581
- const zip = new AdmZip();
582
- zip.addLocalFolder(mfTypesPath);
583
- return zip.writeZipPromise(retrieveTypesZipPath(mfTypesPath, remoteOptions));
584
- }), "createTypesArchive");
585
- var downloadErrorLogger = /* @__PURE__ */ __name((destinationFolder, fileToDownload) => (reason) => {
586
- throw __spreadProps(__spreadValues({}, reason), {
587
- message: `Network error: Unable to download federated mocks for '${destinationFolder}' from '${fileToDownload}' because '${reason.message}'`
588
- });
589
- }, "downloadErrorLogger");
590
- var retrieveTypesArchiveDestinationPath = /* @__PURE__ */ __name((hostOptions, destinationFolder) => {
591
- return resolve2(hostOptions.context, hostOptions.typesFolder, destinationFolder);
592
- }, "retrieveTypesArchiveDestinationPath");
593
- var downloadTypesArchive = /* @__PURE__ */ __name((hostOptions) => {
594
- let retries = 0;
595
- return (_0) => __async(void 0, [_0], function* ([destinationFolder, fileToDownload]) {
596
- const destinationPath = retrieveTypesArchiveDestinationPath(hostOptions, destinationFolder);
597
- while (retries++ < hostOptions.maxRetries) {
598
- try {
599
- const url = fileToDownload;
600
- const response = yield axiosGet(url, {
601
- responseType: "arraybuffer"
602
- }).catch(downloadErrorLogger(destinationFolder, url));
603
- try {
604
- if (hostOptions.deleteTypesFolder) {
605
- yield rm(destinationPath, {
606
- recursive: true,
607
- force: true
608
- });
609
- }
610
- } catch (error) {
611
- fileLog(`Unable to remove types folder, ${error}`, "downloadTypesArchive", "error");
612
- }
613
- const zip = new AdmZip(Buffer.from(response.data));
614
- zip.extractAllTo(destinationPath, true);
615
- return [
616
- destinationFolder,
617
- destinationPath
618
- ];
619
- } catch (error) {
620
- fileLog(`Error during types archive download: ${(error == null ? void 0 : error.message) || "unknown error"}`, "downloadTypesArchive", "error");
621
- if (retries >= hostOptions.maxRetries) {
622
- if (hostOptions.abortOnError !== false) {
623
- throw error;
624
- }
625
- return void 0;
626
- }
546
+ import AdmZip from "adm-zip";
547
+ import { resolve as resolve3, join as join3 } from "path";
548
+ import { rm as rm3 } from "fs/promises";
549
+
550
+ // packages/dts-plugin/src/core/lib/utils.ts
551
+ import fs3 from "fs";
552
+ import path3 from "path";
553
+ import axios from "axios";
554
+ import http from "http";
555
+ import https from "https";
556
+ import ansiColors2 from "ansi-colors";
557
+
558
+ // packages/dts-plugin/src/core/configurations/remotePlugin.ts
559
+ import { existsSync as existsSync2 } from "fs";
560
+ import { dirname as dirname2, join as join2, resolve as resolve2, extname as extname2 } from "path";
561
+ import { utils } from "@module-federation/managers";
562
+ import typescript from "typescript";
563
+ var defaultOptions = {
564
+ tsConfigPath: "./tsconfig.json",
565
+ typesFolder: "@mf-types",
566
+ compiledTypesFolder: "compiled-types",
567
+ hostRemoteTypesFolder: "@mf-types",
568
+ deleteTypesFolder: true,
569
+ additionalFilesToCompile: [],
570
+ compilerInstance: "tsc",
571
+ compileInChildProcess: false,
572
+ implementation: "",
573
+ generateAPITypes: false,
574
+ context: process.cwd(),
575
+ abortOnError: true,
576
+ extractRemoteTypes: false,
577
+ extractThirdParty: false
578
+ };
579
+ function getEffectiveRootDir(parsedCommandLine) {
580
+ const compilerOptions = parsedCommandLine.options;
581
+ if (compilerOptions.rootDir) {
582
+ return compilerOptions.rootDir;
583
+ }
584
+ const files = parsedCommandLine.fileNames;
585
+ if (files.length > 0) {
586
+ const commonRoot = files.map((file) => dirname2(file)).reduce((commonPath, fileDir) => {
587
+ while (!fileDir.startsWith(commonPath)) {
588
+ commonPath = dirname2(commonPath);
627
589
  }
590
+ return commonPath;
591
+ }, files[0]);
592
+ return commonRoot;
593
+ }
594
+ throw new Error("Can not get effective rootDir, please set compilerOptions.rootDir !");
595
+ }
596
+ __name(getEffectiveRootDir, "getEffectiveRootDir");
597
+ var readTsConfig = /* @__PURE__ */ __name(({ tsConfigPath, typesFolder, compiledTypesFolder, context, additionalFilesToCompile }, mapComponentsToExpose) => {
598
+ var _a2;
599
+ const resolvedTsConfigPath = resolve2(context, tsConfigPath);
600
+ const readResult = typescript.readConfigFile(resolvedTsConfigPath, typescript.sys.readFile);
601
+ if (readResult.error) {
602
+ throw new Error(readResult.error.messageText.toString());
603
+ }
604
+ const rawTsConfigJson = readResult.config;
605
+ const configContent = typescript.parseJsonConfigFileContent(rawTsConfigJson, typescript.sys, dirname2(resolvedTsConfigPath));
606
+ const rootDir = getEffectiveRootDir(configContent);
607
+ const outDir = resolve2(context, configContent.options.outDir || "dist", typesFolder, compiledTypesFolder);
608
+ const defaultCompilerOptions = {
609
+ rootDir,
610
+ emitDeclarationOnly: true,
611
+ noEmit: false,
612
+ declaration: true,
613
+ outDir
614
+ };
615
+ rawTsConfigJson.compilerOptions = rawTsConfigJson.compilerOptions || {};
616
+ rawTsConfigJson.compilerOptions = __spreadValues(__spreadValues({}, rawTsConfigJson.compilerOptions), defaultCompilerOptions);
617
+ (_a2 = rawTsConfigJson.compilerOptions) == null ? true : delete _a2.paths;
618
+ const filesToCompile = [
619
+ ...Object.values(mapComponentsToExpose),
620
+ ...additionalFilesToCompile
621
+ ];
622
+ rawTsConfigJson.include = [];
623
+ rawTsConfigJson.files = filesToCompile;
624
+ rawTsConfigJson.exclude = [];
625
+ "references" in rawTsConfigJson && delete rawTsConfigJson.references;
626
+ rawTsConfigJson.extends = resolvedTsConfigPath;
627
+ return rawTsConfigJson;
628
+ }, "readTsConfig");
629
+ var TS_EXTENSIONS = [
630
+ "ts",
631
+ "tsx",
632
+ "vue",
633
+ "svelte"
634
+ ];
635
+ var resolveWithExtension = /* @__PURE__ */ __name((exposedPath, context) => {
636
+ if (extname2(exposedPath)) {
637
+ return resolve2(context, exposedPath);
638
+ }
639
+ for (const extension of TS_EXTENSIONS) {
640
+ const exposedPathWithExtension = resolve2(context, `${exposedPath}.${extension}`);
641
+ if (existsSync2(exposedPathWithExtension)) {
642
+ return exposedPathWithExtension;
628
643
  }
629
- });
630
- }, "downloadTypesArchive");
644
+ }
645
+ return void 0;
646
+ }, "resolveWithExtension");
647
+ var resolveExposes = /* @__PURE__ */ __name((remoteOptions) => {
648
+ const parsedOptions = utils.parseOptions(remoteOptions.moduleFederationConfig.exposes || {}, (item, key) => ({
649
+ exposePath: Array.isArray(item) ? item[0] : item,
650
+ key
651
+ }), (item, key) => ({
652
+ exposePath: Array.isArray(item.import) ? item.import[0] : item.import[0],
653
+ key
654
+ }));
655
+ return parsedOptions.reduce((accumulator, item) => {
656
+ const { exposePath, key } = item[1];
657
+ accumulator[key] = resolveWithExtension(exposePath, remoteOptions.context) || resolveWithExtension(join2(exposePath, "index"), remoteOptions.context) || exposePath;
658
+ return accumulator;
659
+ }, {});
660
+ }, "resolveExposes");
661
+ var retrieveRemoteConfig = /* @__PURE__ */ __name((options) => {
662
+ validateOptions(options);
663
+ const remoteOptions = __spreadValues(__spreadValues({}, defaultOptions), options);
664
+ const mapComponentsToExpose = resolveExposes(remoteOptions);
665
+ const tsConfig = readTsConfig(remoteOptions, mapComponentsToExpose);
666
+ return {
667
+ tsConfig,
668
+ mapComponentsToExpose,
669
+ remoteOptions
670
+ };
671
+ }, "retrieveRemoteConfig");
672
+
673
+ // packages/dts-plugin/src/core/lib/DTSManager.ts
674
+ import ansiColors from "ansi-colors";
675
+ import path2 from "path";
676
+ import { rm as rm2 } from "fs/promises";
677
+ import fs2 from "fs";
678
+ import { MANIFEST_EXT, inferAutoPublicPath } from "@module-federation/sdk";
679
+ import { ThirdPartyExtractor as ThirdPartyExtractor2 } from "@module-federation/third-party-dts-extractor";
631
680
 
632
681
  // packages/dts-plugin/src/core/configurations/hostPlugin.ts
633
682
  import { parseEntry, ENCODE_NAME_PREFIX, decodeName } from "@module-federation/sdk";
@@ -900,7 +949,7 @@ var DTSManager = (_a = class {
900
949
  const apiTypeFileName = path2.join(hostOptions.context, hostOptions.typesFolder, HOST_API_TYPES_FILE_NAME);
901
950
  try {
902
951
  const existedFile = fs2.readFileSync(apiTypeFileName, "utf-8");
903
- const existedImports = new ThirdPartyExtractor("").collectTypeImports(existedFile);
952
+ const existedImports = new ThirdPartyExtractor2("").collectTypeImports(existedFile);
904
953
  existedImports.forEach((existedImport) => {
905
954
  const alias = existedImport.split("./").slice(1).join("./").replace("/apis.d.ts", "");
906
955
  this.loadedRemoteAPIAlias.add(alias);
@@ -1168,114 +1217,60 @@ function axiosGet(url, config) {
1168
1217
  }
1169
1218
  __name(axiosGet, "axiosGet");
1170
1219
 
1171
- // packages/dts-plugin/src/core/lib/typeScriptCompiler.ts
1172
- var STARTS_WITH_SLASH = /^\//;
1173
- var DEFINITION_FILE_EXTENSION = ".d.ts";
1174
- var retrieveMfTypesPath = /* @__PURE__ */ __name((tsConfig, remoteOptions) => normalize(tsConfig.compilerOptions.outDir.replace(remoteOptions.compiledTypesFolder, "")), "retrieveMfTypesPath");
1175
- var retrieveOriginalOutDir = /* @__PURE__ */ __name((tsConfig, remoteOptions) => normalize(tsConfig.compilerOptions.outDir.replace(remoteOptions.compiledTypesFolder, "").replace(remoteOptions.typesFolder, "")), "retrieveOriginalOutDir");
1176
- var retrieveMfAPITypesPath = /* @__PURE__ */ __name((tsConfig, remoteOptions) => join3(retrieveOriginalOutDir(tsConfig, remoteOptions), `${remoteOptions.typesFolder}.d.ts`), "retrieveMfAPITypesPath");
1177
- function writeTempTsConfig(tsConfig, context, name) {
1178
- const createHash = /* @__PURE__ */ __name((contents) => {
1179
- return crypto.createHash("md5").update(contents).digest("hex");
1180
- }, "createHash");
1181
- const hash = createHash(`${JSON.stringify(tsConfig)}${name}`);
1182
- const tempTsConfigJsonPath = resolve3(context, "node_modules", TEMP_DIR, `tsconfig.${hash}.json`);
1183
- ensureDirSync(dirname2(tempTsConfigJsonPath));
1184
- writeFileSync(tempTsConfigJsonPath, JSON.stringify(tsConfig, null, 2));
1185
- return tempTsConfigJsonPath;
1186
- }
1187
- __name(writeTempTsConfig, "writeTempTsConfig");
1188
- var removeExt = /* @__PURE__ */ __name((f) => {
1189
- const ext = extname2(f);
1190
- const regexPattern = new RegExp(`\\${ext}$`);
1191
- return f.replace(regexPattern, "");
1192
- }, "removeExt");
1193
- function getExposeKey(options) {
1194
- const { filePath, rootDir, outDir, mapExposeToEntry } = options;
1195
- const relativeFilePath = removeExt(relative(outDir, filePath.replace(new RegExp(`\\.d.ts$`), "")));
1196
- return mapExposeToEntry[relativeFilePath];
1197
- }
1198
- __name(getExposeKey, "getExposeKey");
1199
- var processTypesFile = /* @__PURE__ */ __name((options) => __async(void 0, null, function* () {
1200
- const { outDir, filePath, rootDir, cb, mapExposeToEntry, mfTypePath } = options;
1201
- if (!existsSync2(filePath)) {
1202
- return;
1203
- }
1204
- const stats = yield stat(filePath);
1205
- if (stats.isDirectory()) {
1206
- const files = yield readdir(filePath);
1207
- yield Promise.all(files.map((file) => processTypesFile(__spreadProps(__spreadValues({}, options), {
1208
- filePath: join3(filePath, file)
1209
- }))));
1210
- } else if (filePath.endsWith(".d.ts")) {
1211
- const exposeKey = getExposeKey({
1212
- filePath,
1213
- rootDir,
1214
- outDir,
1215
- mapExposeToEntry
1216
- });
1217
- if (exposeKey) {
1218
- const sourceEntry = exposeKey === "." ? "index" : exposeKey;
1219
- const mfeTypeEntry = join3(mfTypePath, `${sourceEntry}${DEFINITION_FILE_EXTENSION}`);
1220
- const mfeTypeEntryDirectory = dirname2(mfeTypeEntry);
1221
- const relativePathToOutput = relative(mfeTypeEntryDirectory, filePath).replace(DEFINITION_FILE_EXTENSION, "").replace(STARTS_WITH_SLASH, "").split(sep).join("/");
1222
- ensureDirSync(mfeTypeEntryDirectory);
1223
- yield writeFile(mfeTypeEntry, `export * from './${relativePathToOutput}';
1224
- export { default } from './${relativePathToOutput}';`);
1225
- }
1226
- const content = yield readFile(filePath, "utf8");
1227
- cb(content);
1228
- }
1229
- }), "processTypesFile");
1230
- var compileTs = /* @__PURE__ */ __name((mapComponentsToExpose, tsConfig, remoteOptions) => __async(void 0, null, function* () {
1231
- if (!Object.keys(mapComponentsToExpose).length) {
1232
- return;
1233
- }
1234
- const { compilerOptions } = tsConfig;
1235
- const tempTsConfigJsonPath = writeTempTsConfig(tsConfig, remoteOptions.context, remoteOptions.moduleFederationConfig.name || "mf");
1236
- try {
1237
- const mfTypePath = retrieveMfTypesPath(tsConfig, remoteOptions);
1238
- const thirdPartyExtractor = new ThirdPartyExtractor2(resolve3(mfTypePath, "node_modules"), remoteOptions.context);
1239
- const execPromise = util.promisify(exec);
1240
- const cmd = `npx ${remoteOptions.compilerInstance} --project ${tempTsConfigJsonPath}`;
1241
- try {
1242
- yield execPromise(cmd);
1243
- } catch (err) {
1244
- throw new Error(`compile TS failed, the original command is '${cmd}'`);
1245
- }
1246
- const mapExposeToEntry = Object.fromEntries(Object.entries(mapComponentsToExpose).map(([exposed, filename]) => {
1247
- const normalizedFileName = normalize(filename);
1248
- let relativeFileName = "";
1249
- if (isAbsolute(normalizedFileName)) {
1250
- relativeFileName = relative(tsConfig.compilerOptions.rootDir, normalizedFileName);
1251
- } else {
1252
- relativeFileName = relative(tsConfig.compilerOptions.rootDir, resolve3(remoteOptions.context, normalizedFileName));
1220
+ // packages/dts-plugin/src/core/lib/archiveHandler.ts
1221
+ var retrieveTypesZipPath = /* @__PURE__ */ __name((mfTypesPath, remoteOptions) => join3(mfTypesPath.replace(remoteOptions.typesFolder, ""), `${remoteOptions.typesFolder}.zip`), "retrieveTypesZipPath");
1222
+ var createTypesArchive = /* @__PURE__ */ __name((tsConfig, remoteOptions) => __async(void 0, null, function* () {
1223
+ const mfTypesPath = retrieveMfTypesPath(tsConfig, remoteOptions);
1224
+ const zip = new AdmZip();
1225
+ zip.addLocalFolder(mfTypesPath);
1226
+ return zip.writeZipPromise(retrieveTypesZipPath(mfTypesPath, remoteOptions));
1227
+ }), "createTypesArchive");
1228
+ var downloadErrorLogger = /* @__PURE__ */ __name((destinationFolder, fileToDownload) => (reason) => {
1229
+ throw __spreadProps(__spreadValues({}, reason), {
1230
+ message: `Network error: Unable to download federated mocks for '${destinationFolder}' from '${fileToDownload}' because '${reason.message}'`
1231
+ });
1232
+ }, "downloadErrorLogger");
1233
+ var retrieveTypesArchiveDestinationPath = /* @__PURE__ */ __name((hostOptions, destinationFolder) => {
1234
+ return resolve3(hostOptions.context, hostOptions.typesFolder, destinationFolder);
1235
+ }, "retrieveTypesArchiveDestinationPath");
1236
+ var downloadTypesArchive = /* @__PURE__ */ __name((hostOptions) => {
1237
+ let retries = 0;
1238
+ return (_0) => __async(void 0, [_0], function* ([destinationFolder, fileToDownload]) {
1239
+ const destinationPath = retrieveTypesArchiveDestinationPath(hostOptions, destinationFolder);
1240
+ while (retries++ < hostOptions.maxRetries) {
1241
+ try {
1242
+ const url = fileToDownload;
1243
+ const response = yield axiosGet(url, {
1244
+ responseType: "arraybuffer"
1245
+ }).catch(downloadErrorLogger(destinationFolder, url));
1246
+ try {
1247
+ if (hostOptions.deleteTypesFolder) {
1248
+ yield rm3(destinationPath, {
1249
+ recursive: true,
1250
+ force: true
1251
+ });
1252
+ }
1253
+ } catch (error) {
1254
+ fileLog(`Unable to remove types folder, ${error}`, "downloadTypesArchive", "error");
1255
+ }
1256
+ const zip = new AdmZip(Buffer.from(response.data));
1257
+ zip.extractAllTo(destinationPath, true);
1258
+ return [
1259
+ destinationFolder,
1260
+ destinationPath
1261
+ ];
1262
+ } catch (error) {
1263
+ fileLog(`Error during types archive download: ${(error == null ? void 0 : error.message) || "unknown error"}`, "downloadTypesArchive", "error");
1264
+ if (retries >= hostOptions.maxRetries) {
1265
+ if (hostOptions.abortOnError !== false) {
1266
+ throw error;
1267
+ }
1268
+ return void 0;
1269
+ }
1253
1270
  }
1254
- return [
1255
- removeExt(relativeFileName),
1256
- exposed
1257
- ];
1258
- }));
1259
- const cb = remoteOptions.extractThirdParty ? thirdPartyExtractor.collectPkgs.bind(thirdPartyExtractor) : () => void 0;
1260
- yield processTypesFile({
1261
- outDir: compilerOptions.outDir,
1262
- filePath: compilerOptions.outDir,
1263
- rootDir: compilerOptions.rootDir,
1264
- mfTypePath,
1265
- cb,
1266
- mapExposeToEntry
1267
- });
1268
- if (remoteOptions.extractThirdParty) {
1269
- yield thirdPartyExtractor.copyDts();
1270
- }
1271
- yield rm3(tempTsConfigJsonPath);
1272
- } catch (err) {
1273
- if (isDebugMode()) {
1274
- console.log("tsconfig: ", JSON.stringify(tsConfig, null, 2));
1275
1271
  }
1276
- throw err;
1277
- }
1278
- }), "compileTs");
1272
+ });
1273
+ }, "downloadTypesArchive");
1279
1274
 
1280
1275
  // packages/dts-plugin/src/core/lib/generateTypes.ts
1281
1276
  function generateTypes(options) {
@@ -4,7 +4,7 @@ import {
4
4
  exposeRpc,
5
5
  getDTSManagerConstructor,
6
6
  isDebugMode
7
- } from "./chunk-SFEGBRA3.js";
7
+ } from "./chunk-7AXI7WOP.js";
8
8
  import {
9
9
  __async,
10
10
  __export,
package/dist/esm/core.js CHANGED
@@ -3,7 +3,7 @@ import {
3
3
  consumeTypes,
4
4
  generateTypesInChildProcess,
5
5
  rpc_exports
6
- } from "./chunk-DEW2PNCR.js";
6
+ } from "./chunk-VFGWGY54.js";
7
7
  import {
8
8
  DTSManager,
9
9
  HOST_API_TYPES_FILE_NAME,
@@ -19,7 +19,7 @@ import {
19
19
  retrieveTypesAssetsInfo,
20
20
  retrieveTypesZipPath,
21
21
  validateOptions
22
- } from "./chunk-SFEGBRA3.js";
22
+ } from "./chunk-7AXI7WOP.js";
23
23
  import "./chunk-HKRTV6ZH.js";
24
24
  import "./chunk-6DND574L.js";
25
25
  export {
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  rpc_exports
3
- } from "./chunk-DEW2PNCR.js";
3
+ } from "./chunk-VFGWGY54.js";
4
4
  import {
5
5
  ModuleFederationDevServer,
6
6
  createKoaServer,
@@ -9,7 +9,7 @@ import {
9
9
  retrieveMfTypesPath,
10
10
  retrieveRemoteConfig,
11
11
  retrieveTypesZipPath
12
- } from "./chunk-SFEGBRA3.js";
12
+ } from "./chunk-7AXI7WOP.js";
13
13
  import {
14
14
  fileLog,
15
15
  getIPV4
@@ -2,7 +2,7 @@ import {
2
2
  RpcGMCallTypes,
3
3
  exposeRpc,
4
4
  generateTypes
5
- } from "./chunk-SFEGBRA3.js";
5
+ } from "./chunk-7AXI7WOP.js";
6
6
  import "./chunk-HKRTV6ZH.js";
7
7
  import {
8
8
  __async,
package/dist/esm/index.js CHANGED
@@ -2,14 +2,14 @@ import {
2
2
  consumeTypes,
3
3
  generateTypesInChildProcess,
4
4
  rpc_exports
5
- } from "./chunk-DEW2PNCR.js";
5
+ } from "./chunk-VFGWGY54.js";
6
6
  import {
7
7
  cloneDeepOptions,
8
8
  generateTypes,
9
9
  isTSProject,
10
10
  retrieveTypesAssetsInfo,
11
11
  validateOptions
12
- } from "./chunk-SFEGBRA3.js";
12
+ } from "./chunk-7AXI7WOP.js";
13
13
  import {
14
14
  getIPV4
15
15
  } from "./chunk-HKRTV6ZH.js";
@@ -210,9 +210,6 @@ var compileTs = /* @__PURE__ */ __name((mapComponentsToExpose, tsConfig, remoteO
210
210
  }
211
211
  yield (0, import_promises.rm)(tempTsConfigJsonPath);
212
212
  } catch (err) {
213
- if (isDebugMode()) {
214
- console.log("tsconfig: ", JSON.stringify(tsConfig, null, 2));
215
- }
216
213
  throw err;
217
214
  }
218
215
  }), "compileTs");
@@ -387,9 +387,6 @@ var compileTs = /* @__PURE__ */ __name((mapComponentsToExpose, tsConfig, remoteO
387
387
  }
388
388
  yield (0, import_promises.rm)(tempTsConfigJsonPath);
389
389
  } catch (err) {
390
- if (isDebugMode()) {
391
- console.log("tsconfig: ", JSON.stringify(tsConfig, null, 2));
392
- }
393
390
  throw err;
394
391
  }
395
392
  }), "compileTs");
package/dist/index.js CHANGED
@@ -223,9 +223,6 @@ var compileTs = /* @__PURE__ */ __name((mapComponentsToExpose, tsConfig, remoteO
223
223
  }
224
224
  yield (0, import_promises.rm)(tempTsConfigJsonPath);
225
225
  } catch (err) {
226
- if (isDebugMode()) {
227
- console.log("tsconfig: ", JSON.stringify(tsConfig, null, 2));
228
- }
229
226
  throw err;
230
227
  }
231
228
  }), "compileTs");
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/dts-plugin",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "author": "hanric <hanric.zhang@gmail.com>",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/dts-plugin",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "author": "hanric <hanric.zhang@gmail.com>",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -55,9 +55,9 @@
55
55
  "log4js": "6.9.1",
56
56
  "node-schedule": "2.1.1",
57
57
  "ws": "8.17.1",
58
- "@module-federation/sdk": "0.3.0",
59
- "@module-federation/managers": "0.3.0",
60
- "@module-federation/third-party-dts-extractor": "0.3.0"
58
+ "@module-federation/sdk": "0.3.1",
59
+ "@module-federation/managers": "0.3.1",
60
+ "@module-federation/third-party-dts-extractor": "0.3.1"
61
61
  },
62
62
  "devDependencies": {
63
63
  "@types/ws": "8.5.10",
@@ -66,7 +66,7 @@
66
66
  "vue": "^3.4.29",
67
67
  "@vue/tsconfig": "^0.5.1",
68
68
  "vue-tsc": "^2.0.26",
69
- "@module-federation/runtime": "0.3.0"
69
+ "@module-federation/runtime": "0.3.1"
70
70
  },
71
71
  "peerDependencies": {
72
72
  "typescript": "^4.9.0 || ^5.0.0",