@touk/federated-types 1.3.0-beta.5 → 1.3.0-beta.6

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.
Files changed (2) hide show
  1. package/cli.js +30 -22
  2. package/package.json +1 -1
package/cli.js CHANGED
@@ -162,9 +162,6 @@ try {
162
162
  fs.unlinkSync(outFile);
163
163
  }
164
164
 
165
- const tsBuildInfoFile = path.resolve(process.cwd(), 'node_modules', '.cache', 'federated-types', `${federationConfig.name}.tsbuildinfo`);
166
- fs.mkdirSync(path.dirname(tsBuildInfoFile), { recursive: true });
167
-
168
165
  // write the typings file
169
166
  const program = ts.createProgram(compileFiles, {
170
167
  outFile,
@@ -172,8 +169,6 @@ try {
172
169
  emitDeclarationOnly: true,
173
170
  skipLibCheck: true,
174
171
  noCheck: true,
175
- incremental: true,
176
- tsBuildInfoFile,
177
172
  jsx: ts.JsxEmit.React,
178
173
  esModuleInterop: true,
179
174
  });
@@ -189,35 +184,48 @@ try {
189
184
  let typing = fs.readFileSync(outFile, { encoding: 'utf8', flag: 'r' });
190
185
 
191
186
  const moduleRegex = RegExp(/declare module "(.*)"/, 'g');
192
- const moduleNames = [];
187
+ const exposedPaths = compileKeys.map((key) => [
188
+ key,
189
+ federationConfig.exposes[key].replace(/\.(tsx?|jsx?)$/, ''),
190
+ ]);
191
+
192
+ // module name emitted by tsc -> name it is declared under in the federated typings
193
+ const renames = new Map();
194
+ const aliasModules = [];
193
195
 
194
196
  let execResults;
195
197
  while ((execResults = moduleRegex.exec(typing)) !== null) {
196
- moduleNames.push(execResults[1]);
197
- }
198
+ const name = execResults[1];
199
+ if (renames.has(name)) {
200
+ continue;
201
+ }
198
202
 
199
- moduleNames.forEach((name) => {
200
203
  // exposeName - relative name of exposed component (if not found - just take moduleName)
201
- const [exposeName = name, ...aliases] = compileKeys.filter((key) => {
202
- const normalizedPath = federationConfig.exposes[key].replace(/\.(tsx?|jsx?)$/, '');
203
- return normalizedPath.endsWith(name);
204
- });
205
- const regex = RegExp(`"${name}"`, 'g');
204
+ const [exposeName = name, ...aliases] = exposedPaths
205
+ .filter(([, normalizedPath]) => normalizedPath.endsWith(name))
206
+ .map(([key]) => key);
206
207
 
207
208
  const moduleDeclareName = getModuleDeclareName(exposeName);
209
+ renames.set(name, moduleDeclareName);
208
210
 
209
211
  // language=TypeScript
210
- const createAliasModule = (name) => `
211
- declare module "${getModuleDeclareName(name)}" {
212
+ aliases.forEach((alias) =>
213
+ aliasModules.push(`
214
+ declare module "${getModuleDeclareName(alias)}" {
212
215
  export * from "${moduleDeclareName}"
213
216
  }
214
- `;
217
+ `)
218
+ );
219
+ }
215
220
 
216
- typing = [
217
- typing.replace(regex, `"${moduleDeclareName}"`),
218
- ...aliases.map(createAliasModule),
219
- ].join('\n');
220
- });
221
+ if (renames.size > 0) {
222
+ // Rename every exact quoted occurrence of a module name in a single pass.
223
+ const escapeRegExp = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
224
+ const namesRegex = RegExp(`"(${Array.from(renames.keys(), escapeRegExp).join('|')})"`, 'g');
225
+ typing = typing.replace(namesRegex, (match, name) => `"${renames.get(name)}"`);
226
+ }
227
+
228
+ typing = [typing, ...aliasModules].join('\n');
221
229
 
222
230
  outputDirs.forEach((_outputDir) => {
223
231
  const _outFile = path.resolve(_outputDir, `${federationConfig.name}.d.ts`);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@touk/federated-types",
3
3
  "description": "share typings for your federated typescript packages around your mono-repo",
4
- "version": "1.3.0-beta.5",
4
+ "version": "1.3.0-beta.6",
5
5
  "author": "Julian Wielga <j@touk.pl>",
6
6
  "bin": {
7
7
  "make-federated-types": "./cli.js"