Skip to content

Commit

Permalink
fix(core): unregister in-process ts transpilers when workspace files …
Browse files Browse the repository at this point in the history
…are retrieved
  • Loading branch information
Cammisuli committed Sep 15, 2023
1 parent e98221e commit 78047b3
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 158 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
"@supabase/supabase-js": "^2.26.0",
"@svgr/rollup": "^8.0.1",
"@svgr/webpack": "^8.0.1",
"@swc-node/register": "^1.4.2",
"@swc-node/register": "~1.6.7",
"@swc/cli": "0.1.62",
"@swc/core": "^1.3.51",
"@swc/jest": "^0.2.20",
Expand Down
9 changes: 9 additions & 0 deletions packages/js/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@
"version": "~5.1.3"
}
}
},
"16.9.0": {
"version": "16.9.0-beta.1",
"packages": {
"@swc/register": {
"version": "~1.6.7",
"alwaysAddToPackageJson": false
}
}
}
}
}
2 changes: 1 addition & 1 deletion packages/js/src/utils/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const prettierVersion = '^2.6.2';
export const swcCliVersion = '~0.1.62';
export const swcCoreVersion = '~1.3.51';
export const swcHelpersVersion = '~0.5.0';
export const swcNodeVersion = '~1.4.2';
export const swcNodeVersion = '~1.6.7';
export const tsLibVersion = '^2.3.0';
export const typesNodeVersion = '18.7.1';
export const verdaccioVersion = '^5.0.4';
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"node-machine-id": "1.1.12"
},
"peerDependencies": {
"@swc-node/register": "^1.4.2",
"@swc-node/register": "~1.6.7",
"@swc/core": "^1.2.173"
},
"peerDependenciesMeta": {
Expand Down
4 changes: 3 additions & 1 deletion packages/nx/src/plugins/js/utils/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ export function getTsNodeTranspiler(
warnTsNodeUsage();
}

return () => {};
return () => {
service.enabled(false);
};
}

export function getTranspiler(compilerOptions: CompilerOptions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
loadNxPlugins,
loadNxPluginsSync,
NxPluginV2,
unregisterPluginTSTranspiler,
} from '../../utils/nx-plugin';
import { CreateProjectJsonProjectsPlugin } from '../../plugins/project-json/build-nodes/project-json';
import {
Expand Down Expand Up @@ -84,6 +85,8 @@ export async function retrieveWorkspaceFiles(
'get-workspace-files:end'
);

unregisterPluginTSTranspiler();

return {
allWorkspaceFiles: buildAllWorkspaceFiles(projectFileMap, globalFiles),
projectFileMap,
Expand Down
29 changes: 22 additions & 7 deletions packages/nx/src/utils/nx-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ function getPluginPathAndName(

// Register the ts-transpiler if we are pointing to a
// plain ts file that's not part of a plugin project
if (extension === '.ts' && !tsNodeAndPathsRegistered) {
if (extension === '.ts' && !tsNodeAndPathsUnregisterCallback) {
registerPluginTSTranspiler();
}

Expand Down Expand Up @@ -368,14 +368,14 @@ export function resolveLocalNxPlugin(
return localPluginCache[importPath];
}

let tsNodeAndPathsRegistered = false;
let tsNodeAndPathsUnregisterCallback = undefined;

/**
* Register swc-node or ts-node if they are not currently registered
* with some default settings which work well for Nx plugins.
*/
export function registerPluginTSTranspiler() {
if (!tsNodeAndPathsRegistered) {
if (!tsNodeAndPathsUnregisterCallback) {
// nx-ignore-next-line
const ts: typeof import('typescript') = require('typescript');

Expand All @@ -389,19 +389,33 @@ export function registerPluginTSTranspiler() {
? readTsConfig(tsConfigName)
: {};

registerTsConfigPaths(tsConfigName);
registerTranspiler({
const unregisterTsConfigPaths = registerTsConfigPaths(tsConfigName);
const unregisterTranspiler = registerTranspiler({
experimentalDecorators: true,
emitDecoratorMetadata: true,
...tsConfig.options,
lib: ['es2021'],
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES2021,
inlineSourceMap: true,
esModuleInterop: true,
skipLibCheck: true,
});
tsNodeAndPathsUnregisterCallback = () => {
unregisterTsConfigPaths();
unregisterTranspiler();
};
}
}

/**
* Unregister the ts-node transpiler if it is registered
*/
export function unregisterPluginTSTranspiler() {
if (tsNodeAndPathsUnregisterCallback) {
tsNodeAndPathsUnregisterCallback();
tsNodeAndPathsUnregisterCallback = undefined;
}
tsNodeAndPathsRegistered = true;
}

function lookupLocalPlugin(importPath: string, root = workspaceRoot) {
Expand All @@ -411,7 +425,7 @@ function lookupLocalPlugin(importPath: string, root = workspaceRoot) {
return null;
}

if (!tsNodeAndPathsRegistered) {
if (!tsNodeAndPathsUnregisterCallback) {
registerPluginTSTranspiler();
}

Expand Down Expand Up @@ -451,6 +465,7 @@ function findNxProjectForImportPath(
}

let tsconfigPaths: Record<string, string[]>;

function readTsConfigPaths(root: string = workspaceRoot) {
if (!tsconfigPaths) {
const tsconfigPath: string | null = ['tsconfig.base.json', 'tsconfig.json']
Expand Down
294 changes: 147 additions & 147 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

0 comments on commit 78047b3

Please sign in to comment.