Skip to content

Commit

Permalink
chore: sink native-federation-typescript capability into dts-kit
Browse files Browse the repository at this point in the history
  • Loading branch information
2heal1 committed Mar 13, 2024
1 parent 5b8ad94 commit 17c50a3
Show file tree
Hide file tree
Showing 32 changed files with 443 additions and 77 deletions.
19 changes: 19 additions & 0 deletions packages/dts-kit/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint", "simple-import-sort"],
"extends": [
"../../.eslintrc.json",
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"ignorePatterns": ["!**/*"],
"rules": {
"@typescript-eslint/explicit-module-boundary-types": 0,
"@typescript-eslint/camelcase": 0,
"@typescript-eslint/no-var-requires": 0,
"@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/no-non-null-assertion": 0,
"@typescript-eslint/no-explicit-any": "off"
}
}
1 change: 1 addition & 0 deletions packages/dts-kit/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @module-federation/dts-kit
21 changes: 21 additions & 0 deletions packages/dts-kit/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Matteo Pietro Dazzi and NearForm Limited

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions packages/dts-kit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @module-federation/dts-kit
39 changes: 39 additions & 0 deletions packages/dts-kit/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "@module-federation/dts-kit",
"version": "0.3.1",
"description": "Bundler federated types",
"keywords": [
"module federation",
"typescript",
"remote types",
"federated types"
],
"files": [
"dist/",
"README.md"
],
"publishConfig": {
"access": "public"
},
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.js",
"types": "./dist/index.d.ts"
}
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"author": "Matteo Pietro Dazzi <[email protected]> (https://github.com/ilteoood)",
"license": "MIT",
"dependencies": {
"adm-zip": "^0.5.10",
"ansi-colors": "^4.1.3",
"axios": "^1.6.7",
"rambda": "^9.1.0"
},
"peerDependencies": {
"typescript": "^4.9.0 || ^5.0.0",
"vue-tsc": "^1.0.24"
}
}
52 changes: 52 additions & 0 deletions packages/dts-kit/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "dts-kit",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/dts-kit/src",
"projectType": "library",
"targets": {
"build": {
"executor": "nx:run-commands",
"outputs": ["{workspaceRoot}/packages/dts-kit/dist"],
"options": {
"parallel": false,
"commands": [
"tsup --config packages/dts-kit/tsup.config.ts",
"cp packages/dts-kit/package.json packages/dts-kit/dist",
"cp packages/dts-kit/*.md packages/dts-kit/dist"
]
}
},
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["packages/dts-kit/**/*.ts"]
}
},
"test": {
"executor": "@nx/vite:test",
"outputs": ["{workspaceRoot}/coverage/packages/dts-kit"]
},
"pre-release": {
"executor": "nx:run-commands",
"options": {
"parallel": false,
"commands": [
{
"command": "nx run dts-kit:test",
"forwardAllArgs": false
},
{
"command": "nx run dts-kit:build",
"forwardAllArgs": false
}
]
}
},
"semantic-release": {
"executor": "@goestav/nx-semantic-release:semantic-release"
}
},
"tags": ["package"],
"implicitDependencies": []
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ describe('hostPlugin', () => {
typesFolder: '@mf-types',
deleteTypesFolder: true,
maxRetries: 3,
implementation: '',
devServer: {
typesReload: true,
},
});

expect(mapRemotesToDownload).toStrictEqual({
Expand All @@ -46,6 +50,10 @@ describe('hostPlugin', () => {
typesFolder: 'custom-types',
deleteTypesFolder: false,
maxRetries: 1,
implementation: '',
devServer: {
typesReload: true,
},
};

const { hostOptions, mapRemotesToDownload } =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ const defaultOptions = {
typesFolder: '@mf-types',
deleteTypesFolder: true,
maxRetries: 3,
implementation: '',
devServer: {
typesReload: true,
},
} satisfies Partial<HostOptions>;

const retrieveRemoteStringUrl = (remote: string) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ describe('hostPlugin', () => {
deleteTypesFolder: true,
moduleFederationConfig,
compilerInstance: 'tsc',
compileInChildProcess: false,
implementation: '',
});
});

Expand Down Expand Up @@ -107,6 +109,8 @@ describe('hostPlugin', () => {
deleteTypesFolder: false,
moduleFederationConfig,
compilerInstance: 'vue-tsc',
compileInChildProcess: false,
implementation: '',
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const defaultOptions = {
deleteTypesFolder: true,
additionalFilesToCompile: [],
compilerInstance: 'tsc' as const,
compileInChildProcess: false,
implementation: '',
} satisfies Partial<RemoteOptions>;

const readTsConfig = ({
Expand Down
9 changes: 9 additions & 0 deletions packages/dts-kit/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export { retrieveRemoteConfig } from './configurations/remotePlugin';
export { retrieveHostConfig } from './configurations/hostPlugin';
export { getDTSManagerConstructor } from './lib/utils';
export { retrieveOriginalOutDir } from './lib/typeScriptCompiler';
export { DTSManager } from './lib/DTSManager';

export { DTSManagerOptions } from './interfaces/DTSManagerOptions';
export { HostOptions } from './interfaces/HostOptions';
export { RemoteOptions } from './interfaces/RemoteOptions';
7 changes: 7 additions & 0 deletions packages/dts-kit/src/interfaces/DTSManagerOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { HostOptions } from './HostOptions';
import { RemoteOptions } from './RemoteOptions';

export interface DTSManagerOptions {
remote?: RemoteOptions;
host?: HostOptions;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ export interface HostOptions {
typesFolder?: string;
deleteTypesFolder?: boolean;
maxRetries?: number;
devServer?: {
typesReload?: boolean;
};
implementation?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ export interface RemoteOptions {
deleteTypesFolder?: boolean;
additionalFilesToCompile?: string[];
compilerInstance?: 'tsc' | 'vue-tsc';
compileInChildProcess?: boolean;
implementation?: string;
}
73 changes: 73 additions & 0 deletions packages/dts-kit/src/lib/DTSManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import ansiColors from 'ansi-colors';
import { rm } from 'fs/promises';
import { retrieveRemoteConfig } from '../configurations/remotePlugin';
import { createTypesArchive, downloadTypesArchive } from './archiveHandler';
import { compileTs, retrieveMfTypesPath } from './typeScriptCompiler';
import { retrieveHostConfig } from '../configurations/hostPlugin';
import { DTSManagerOptions } from '../interfaces/DTSManagerOptions';

class DTSManager {
options: DTSManagerOptions;

constructor(options: DTSManagerOptions) {
this.options = options;
}

async generateTypes() {
try {
const { options } = this;
if (!options.remote) {
throw new Error(
'options.remote is required if you want to generateTypes',
);
}
const { remoteOptions, tsConfig, mapComponentsToExpose } =
retrieveRemoteConfig(options.remote);
compileTs(mapComponentsToExpose, tsConfig, remoteOptions);

await createTypesArchive(tsConfig, remoteOptions);

if (remoteOptions.deleteTypesFolder) {
await rm(retrieveMfTypesPath(tsConfig, remoteOptions), {
recursive: true,
force: true,
});
}
console.log(ansiColors.green('Federated types created correctly'));
} catch (error) {
console.error(
ansiColors.red(`Unable to compile federated types, ${error}`),
);
}
}

async consumeTypes() {
const { options } = this;
if (!options.host) {
throw new Error('options.host is required if you want to consumeTypes');
}
const { hostOptions, mapRemotesToDownload } = retrieveHostConfig(
options.host,
);

if (hostOptions.deleteTypesFolder) {
await rm(hostOptions.typesFolder, {
recursive: true,
force: true,
}).catch((error) =>
console.error(
ansiColors.red(`Unable to remove types folder, ${error}`),
),
);
}

const typesDownloader = downloadTypesArchive(hostOptions);
const downloadPromises =
Object.entries(mapRemotesToDownload).map(typesDownloader);

await Promise.allSettled(downloadPromises);
console.log(ansiColors.green('Federated types extraction completed'));
}
}

export { DTSManager };
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ describe('archiveHandler', () => {
typesFolder: tmpDir,
deleteTypesFolder: true,
maxRetries: 3,
implementation: '',
devServer: {
typesReload: true,
},
};

const destinationFolder = 'typesHostFolder';
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ describe('typeScriptCompiler', () => {
tsConfigPath: './tsconfig.json',
deleteTypesFolder: false,
compilerInstance: 'tsc',
compileInChildProcess: false,
implementation: '',
};

it('retrieveMfTypesPath correctly calculate path', () => {
Expand Down
7 changes: 7 additions & 0 deletions packages/dts-kit/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { DTSManager } from './DTSManager';

export function getDTSManagerConstructor(
implementation?: string,
): typeof DTSManager {
return implementation ? require(implementation) : DTSManager;
}
12 changes: 12 additions & 0 deletions packages/dts-kit/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"target": "es2017",
"module": "esnext",
"lib": ["esnext"],
"moduleResolution": "node",
"esModuleInterop": true,
"strict": true,
"strictNullChecks": true,
"resolveJsonModule": true
}
}
16 changes: 16 additions & 0 deletions packages/dts-kit/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"declaration": true,
"skipLibCheck": true,
"types": []
},
"include": ["**/*.ts"],
"exclude": [
"jest.config.ts",
"**/*.spec.ts",
"**/*.test.ts",
"tsup.config.ts"
]
}
19 changes: 19 additions & 0 deletions packages/dts-kit/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"types": ["vitest/globals", "vitest/importMeta", "vite/client", "node"]
},
"include": [
"vite.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.test.tsx",
"src/**/*.spec.tsx",
"src/**/*.test.js",
"src/**/*.spec.js",
"src/**/*.test.jsx",
"src/**/*.spec.jsx",
"src/**/*.d.ts"
]
}
Loading

0 comments on commit 17c50a3

Please sign in to comment.