Skip to content

Commit

Permalink
chore(core): add one off exception for next.js (#21572)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Feb 3, 2024
1 parent 7623471 commit 26a815c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 18 deletions.
16 changes: 10 additions & 6 deletions packages/devkit/src/utils/config-utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { dirname, extname, join } from 'path';
import { dirname, extname, join, relative } from 'path';
import { existsSync, readdirSync } from 'fs';
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
import { workspaceRoot } from 'nx/src/devkit-exports';
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
import { registerTsProject } from 'nx/src/plugins/js/utils/register';
import { requireNx } from '../../nx';

const { workspaceRoot, registerTsProject } = requireNx();

export let dynamicImport = new Function(
'modulePath',
Expand All @@ -22,7 +21,12 @@ export async function loadConfigFile<T extends object = any>(
? join(dirname(configFilePath), 'tsconfig.json')
: getRootTsConfigPath();
if (tsConfigPath) {
const unregisterTsProject = registerTsProject(tsConfigPath);
const unregisterTsProject = registerTsProject(
tsConfigPath,
undefined,
// TODO(@AgentEnder): Remove this hack to make sure that e2e loads properly for next.js
relative(workspaceRoot, dirname(configFilePath)) === 'e2e'
);
try {
module = await load(configFilePath);
} finally {
Expand Down
1 change: 1 addition & 0 deletions packages/nx/src/devkit-internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ export {
createProjectRootMappingsFromProjectConfigurations,
findProjectForPath,
} from './project-graph/utils/find-project-for-path';
export { registerTsProject } from './plugins/js/utils/register';
45 changes: 34 additions & 11 deletions packages/nx/src/plugins/js/utils/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,31 @@ export function registerTsProject(tsConfigPath: string): () => void;
* @returns cleanup function
* @deprecated This signature will be removed in Nx v19. You should pass the full path to the tsconfig in the first argument.
*/
export function registerTsProject(path: string, configFilename: string);
export function registerTsProject(
path: string,
configFilename?: string
configFilename: string,
/**
* @deprecated Do not use this.. it's a 1-off exception
*/
useForSwcEnvironmentVariable?: boolean
);
export function registerTsProject(
path: string,
configFilename?: string,
/**
* @deprecated Do not use this.. it's a 1-off exception
*/
useForSwcEnvironmentVariable?: boolean
): () => void {
const tsConfigPath = configFilename ? join(path, configFilename) : path;
const compilerOptions: CompilerOptions = readCompilerOptions(tsConfigPath);

const cleanupFunctions: ((...args: unknown[]) => unknown)[] = [
registerTsConfigPaths(tsConfigPath),
registerTranspiler(compilerOptions),
registerTranspiler(
compilerOptions,
useForSwcEnvironmentVariable ? tsConfigPath : undefined
),
];

// Add ESM support for `.ts` files.
Expand All @@ -66,17 +80,22 @@ export function registerTsProject(
}

export function getSwcTranspiler(
compilerOptions: CompilerOptions
compilerOptions: CompilerOptions,
tsConfigPath?: string
): (...args: unknown[]) => unknown {
type ISwcRegister = typeof import('@swc-node/register/register')['register'];

// These are requires to prevent it from registering when it shouldn't
const register = require('@swc-node/register/register')
.register as ISwcRegister;

let rootTsConfig = join(workspaceRoot, 'tsconfig.base.json');
if (existsSync(rootTsConfig)) {
process.env.SWC_NODE_PROJECT = rootTsConfig;
if (tsConfigPath) {
process.env.SWC_NODE_PROJECT = tsConfigPath;
} else {
let rootTsConfig = join(workspaceRoot, 'tsconfig.base.json');
if (existsSync(rootTsConfig)) {
process.env.SWC_NODE_PROJECT = rootTsConfig;
}
}

const cleanupFn = register(compilerOptions);
Expand Down Expand Up @@ -108,7 +127,10 @@ export function getTsNodeTranspiler(
};
}

export function getTranspiler(compilerOptions: CompilerOptions) {
export function getTranspiler(
compilerOptions: CompilerOptions,
tsConfigPath?: string
) {
const preferTsNode = process.env.NX_PREFER_TS_NODE === 'true';

if (!ts) {
Expand All @@ -122,7 +144,7 @@ export function getTranspiler(compilerOptions: CompilerOptions) {
compilerOptions.skipLibCheck = true;

if (swcNodeInstalled && !preferTsNode) {
return () => getSwcTranspiler(compilerOptions);
return () => getSwcTranspiler(compilerOptions, tsConfigPath);
}

// We can fall back on ts-node if it's available
Expand All @@ -140,10 +162,11 @@ export function getTranspiler(compilerOptions: CompilerOptions) {
* @returns cleanup method
*/
export function registerTranspiler(
compilerOptions: CompilerOptions
compilerOptions: CompilerOptions,
tsConfigPath?: string
): () => void {
// Function to register transpiler that returns cleanup function
const transpiler = getTranspiler(compilerOptions);
const transpiler = getTranspiler(compilerOptions, tsConfigPath);

if (!transpiler) {
warnNoTranspiler();
Expand Down
6 changes: 5 additions & 1 deletion packages/playwright/plugin.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export { createNodes, PlaywrightPluginOptions } from './src/plugins/plugin';
export {
createNodes,
PlaywrightPluginOptions,
createDependencies,
} from './src/plugins/plugin';

0 comments on commit 26a815c

Please sign in to comment.