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 3, 2023
1 parent e29e9f9 commit fbe26eb
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 16 deletions.
14 changes: 13 additions & 1 deletion e2e/react-core/src/react-module-federation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,13 @@ describe('React Module Federation', () => {
it('should should support generating host and remote apps with the new name and root format', async () => {
const shell = uniq('shell');
const remote = uniq('remote');
const shellPort = 4210;

runCLI(
`generate @nx/react:host ${shell} --project-name-and-root-format=as-provided --no-interactive`
);
runCLI(
`generate @nx/react:remote ${remote} --host=${shell} --project-name-and-root-format=as-provided --no-interactive`
`generate @nx/react:remote ${remote} --host=${shell} --port=${shellPort} --project-name-and-root-format=as-provided --no-interactive`
);

// check files are generated without the layout directory ("apps/") and
Expand All @@ -141,6 +142,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:${shellPort}`
);
}
);
await killProcessAndPorts(shellProcess.pid, shellPort);
}, 500_000);

it('should support different versions workspace libs for host and remote', async () => {
Expand Down
6 changes: 3 additions & 3 deletions e2e/utils/get-env-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ export const packageManagerLockFile = {
pnpm: 'pnpm-lock.yaml',
};

export function ensureCypressInstallation() {
export function ensureCypressInstallation(cwd = tmpProjPath()) {
let cypressVerified = true;
try {
const r = execSync('npx cypress verify', {
stdio: isVerbose() ? 'inherit' : 'pipe',
encoding: 'utf-8',
cwd: tmpProjPath(),
cwd,
});
if (r.indexOf('Verified Cypress!') === -1) {
cypressVerified = false;
Expand All @@ -129,7 +129,7 @@ export function ensureCypressInstallation() {
execSync('npx cypress install', {
stdio: isVerbose() ? 'inherit' : 'pipe',
encoding: 'utf-8',
cwd: tmpProjPath(),
cwd,
});
}
}
Expand Down
12 changes: 12 additions & 0 deletions graph/client-e2e/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default {
transform: {
'^.+\\.[tj]sx?$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'],
maxWorkers: 1,
globals: {},
globalSetup: '../../e2e/utils/global-setup.ts',
globalTeardown: '../../e2e/utils/global-teardown.ts',
displayName: 'e2e-graph-client',
preset: '../../jest.preset.js',
};
10 changes: 10 additions & 0 deletions graph/client-e2e/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@
"sourceRoot": "graph/client-e2e/src",
"projectType": "application",
"targets": {
"pre-e2e": {
"inputs": ["default", "^production", "{workspaceRoot}/jest.preset.js"],
"executor": "@nx/jest:jest",
"options": {
"jestConfig": "{projectRoot}/jest.config.ts",
"passWithNoTests": true
},
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"]
},
"e2e-base": {
"dependsOn": ["pre-e2e"],
"executor": "@nx/cypress:cypress",
"options": {
"testingType": "e2e",
Expand Down
11 changes: 11 additions & 0 deletions graph/client-e2e/src/ensure-cypress.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ensureCypressInstallation } from '@nx/e2e/utils';

describe('Ensure Cypress Binary is Installed', () => {
beforeAll(() => {
ensureCypressInstallation(process.cwd());
});

it('should ensure cypress binary is available for the e2es', () => {
expect(true).toBe(true);
});
});
10 changes: 7 additions & 3 deletions graph/client-e2e/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"sourceMap": false,
"outDir": "../../dist/out-tsc",
"allowJs": true,
"types": ["cypress", "node"],
"types": ["cypress", "jest", "node"],
"lib": ["DOM"]
},
"files": [],
Expand All @@ -16,6 +16,10 @@
"src/**/*.ts",
"src/**/*.js"
],
"references": [],
"exclude": []
"references": [
{
"path": "./tsconfig.spec.json"
}
],
"exclude": ["src/**/*.spec.ts"]
}
10 changes: 10 additions & 0 deletions graph/client-e2e/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": ["src/**/*.spec.ts", "jest.config.ts"],
"files": []
}
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 fbe26eb

Please sign in to comment.