Skip to content

Commit

Permalink
fix(js): swc executor should support inlining on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 committed Feb 13, 2024
1 parent b468904 commit f9c6c44
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
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 tempSwcrcPath = 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

0 comments on commit f9c6c44

Please sign in to comment.