Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(js): swc executor should support inlining on windows #21801

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions packages/js/src/executors/swc/swc.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ExecutorContext, readJsonFile } from '@nx/devkit';
import { assetGlobsToFiles, FileInputOutput } from '../../utils/assets/assets';
import { removeSync } from 'fs-extra';
import { sync as globSync } from 'fast-glob';
import { dirname, join, relative, resolve } from 'path';
import { dirname, join, relative, resolve, normalize } from 'path';
import { copyAssets } from '../../utils/assets';
import { checkDependencies } from '../../utils/check-dependencies';
import {
Expand Down Expand Up @@ -66,7 +66,7 @@ function normalizeOptions(
// default to current directory if projectRootParts is [].
// Eg: when a project is at the root level, outside of layout dir
const swcCwd = projectRootParts.join('/') || '.';
const swcrcPath = getSwcrcPath(options, root, projectRoot);
const { swcrcPath, tmpSwcrcPath } = getSwcrcPath(options, root, projectRoot);

const swcCliOptions = {
srcPath: projectDir,
Expand All @@ -89,6 +89,7 @@ function normalizeOptions(
outputPath,
tsConfig: join(root, options.tsConfig),
swcCliOptions,
tmpSwcrcPath,
} as NormalizedSwcExecutorOptions;
}

Expand Down Expand Up @@ -131,17 +132,18 @@ export async function* swcExecutor(
// remap paths for SWC compilation
options.swcCliOptions.srcPath = options.swcCliOptions.swcCwd;
options.swcCliOptions.swcCwd = '.';
options.swcCliOptions.destPath = options.swcCliOptions.destPath
.split('../')
.at(-1)
.concat('/', options.swcCliOptions.srcPath);
options.swcCliOptions.destPath = join(
options.swcCliOptions.destPath.split(normalize('../')).at(-1),
options.swcCliOptions.srcPath
);

// tmp swcrc with dependencies to exclude
// - buildable libraries
// - other libraries that are not dependent on the current project
options.swcCliOptions.swcrcPath = generateTmpSwcrc(
inlineProjectGraph,
options.swcCliOptions.swcrcPath
options.swcCliOptions.swcrcPath,
options.tmpSwcrcPath
);
}

Expand Down Expand Up @@ -206,7 +208,10 @@ export async function* swcExecutor(
}

function removeTmpSwcrc(swcrcPath: string) {
if (swcrcPath.includes('tmp/') && swcrcPath.includes('.generated.swcrc')) {
if (
swcrcPath.includes(normalize('tmp/')) &&
swcrcPath.includes('.generated.swcrc')
) {
removeSync(dirname(swcrcPath));
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/js/src/utils/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,5 @@ export interface NormalizedSwcExecutorOptions
swcExclude: string[];
skipTypeCheck: boolean;
swcCliOptions: SwcCliOptions;
tmpSwcrcPath: string;
}
14 changes: 13 additions & 1 deletion packages/js/src/utils/swc/get-swcrc-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,19 @@ export function getSwcrcPath(
contextRoot: string,
projectRoot: string
) {
return options.swcrc
const swcrcPath = options.swcrc
? join(contextRoot, options.swcrc)
: join(contextRoot, projectRoot, '.swcrc');

const tmpSwcrcPath = join(
contextRoot,
projectRoot,
'tmp',
'.generated.swcrc'
);

return {
swcrcPath,
tmpSwcrcPath,
};
}
4 changes: 2 additions & 2 deletions packages/js/src/utils/swc/inline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import type { InlineProjectGraph } from '../inline';

export function generateTmpSwcrc(
inlineProjectGraph: InlineProjectGraph,
swcrcPath: string
swcrcPath: string,
tmpSwcrcPath: string
) {
const swcrc = readJsonFile(swcrcPath);

Expand All @@ -14,7 +15,6 @@ export function generateTmpSwcrc(
'node_modules/**/*.ts$'
);

const tmpSwcrcPath = `tmp${swcrcPath}`;
writeJsonFile(tmpSwcrcPath, swcrc);

return tmpSwcrcPath;
Expand Down