Skip to content

Commit

Permalink
fix(react): transpile only the config files then disable tsnode service
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 committed Oct 1, 2023
1 parent e29e9f9 commit f5ca733
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 9 deletions.
11 changes: 11 additions & 0 deletions e2e/react-core/src/react-module-federation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,17 @@ describe('React Module Federation', () => {
// check default generated host is built successfully
const buildOutput = runCLI(`run ${shell}:build:development`);
expect(buildOutput).toContain('Successfully ran target build');

// check serves devRemotes ok
const shellProcess = await runCommandUntil(
`serve ${shell} --devRemotes=${remote} --verbose`,
(output) => {
return output.includes(
`All remotes started, server ready at http://localhost:4200`
);
}
);
await killProcessAndPorts(shellProcess.pid, 4200);
}, 500_000);

it('should support different versions workspace libs for host and remote', async () => {
Expand Down
8 changes: 7 additions & 1 deletion packages/angular/src/builders/utilities/module-federation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,19 @@ function getModuleFederationConfig(

let moduleFederationConfigPath = moduleFederationConfigPathJS;

let tsNodeService;
if (existsSync(moduleFederationConfigPathTS)) {
tsNodeRegister(moduleFederationConfigPathTS, tsconfigPath);
tsNodeService = tsNodeRegister(moduleFederationConfigPathTS, tsconfigPath, {
transpileOnly: true,
});
moduleFederationConfigPath = moduleFederationConfigPathTS;
}

try {
const config = require(moduleFederationConfigPath);
if (tsNodeService) {
tsNodeService.enabled(false);
}
return {
mfeConfig: config.default || config,
mfConfigPath: moduleFederationConfigPath,
Expand Down
13 changes: 10 additions & 3 deletions packages/angular/src/builders/utilities/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ export async function mergeCustomWebpackConfig(
}

export function resolveCustomWebpackConfig(path: string, tsConfig: string) {
tsNodeRegister(path, tsConfig);
const tsNodeService = tsNodeRegister(path, tsConfig);

const customWebpackConfig = require(path);
if (tsNodeService) {
tsNodeService.enabled(false);
}
// If the user provides a configuration in TS file
// then there are 2 cases for exporting an object. The first one is:
// `module.exports = { ... }`. And the second one is:
Expand All @@ -42,9 +45,13 @@ export function resolveIndexHtmlTransformer(
tsConfig: string,
target: import('@angular-devkit/architect').Target
) {
tsNodeRegister(path, tsConfig);

const tsNodeService = tsNodeRegister(path, tsConfig);
const indexTransformer = require(path);

if (tsNodeService) {
tsNodeService.enabled(false);
}

const transform = indexTransformer.default ?? indexTransformer;

return (indexHtml) => transform(target, indexHtml);
Expand Down
15 changes: 12 additions & 3 deletions packages/js/src/utils/typescript/tsnode-register.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
export function tsNodeRegister(file: string, tsConfig?: string) {
import type { RegisterOptions, Service } from 'ts-node';

export function tsNodeRegister(
file: string,
tsConfig?: string,
tsNodeOptions?: RegisterOptions
): Service | undefined {
if (!file?.endsWith('.ts')) return;
// Register TS compiler lazily
require('ts-node').register({
const tsNodeService = require('ts-node').register({
project: tsConfig,
compilerOptions: {
module: 'CommonJS',
types: ['node'],
},
...tsNodeOptions,
});

if (!tsConfig) return;
if (!tsConfig) return tsNodeService;

// Register paths in tsConfig
const tsconfigPaths = require('tsconfig-paths');
Expand All @@ -18,4 +25,6 @@ export function tsNodeRegister(file: string, tsConfig?: string) {
if (baseUrl && paths) {
tsconfigPaths.register({ baseUrl, paths });
}

return tsNodeService;
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,20 @@ function getModuleFederationConfig(

let moduleFederationConfigPath = moduleFederationConfigPathJS;

// create a no-op so this can be called with issue
let tsNodeService;
if (existsSync(moduleFederationConfigPathTS)) {
tsNodeRegister(moduleFederationConfigPathTS, tsconfigPath);
tsNodeService = tsNodeRegister(moduleFederationConfigPathTS, tsconfigPath, {
transpileOnly: true,
});
moduleFederationConfigPath = moduleFederationConfigPathTS;
}

try {
const config = require(moduleFederationConfigPath);
if (tsNodeService) {
tsNodeService.enabled(false);
}
return config.default || config;
} catch {
throw new Error(
Expand Down
7 changes: 6 additions & 1 deletion packages/webpack/src/utils/webpack/custom-webpack.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { tsNodeRegister } from '@nx/js/src/utils/typescript/tsnode-register';

export function resolveCustomWebpackConfig(path: string, tsConfig: string) {
tsNodeRegister(path, tsConfig);
const tsNodeService = tsNodeRegister(path, tsConfig);

const customWebpackConfig = require(path);

if (tsNodeService) {
tsNodeService.enabled(false);
}

// If the user provides a configuration in TS file
// then there are 2 cases for exporing an object. The first one is:
// `module.exports = { ... }`. And the second one is:
Expand Down

0 comments on commit f5ca733

Please sign in to comment.