Skip to content

Commit

Permalink
fix(js): adjust destination directory for swc executor (#9072)
Browse files Browse the repository at this point in the history
* fix(js): bump swc deps versions

* fix(js): change swc cli arguments to ensure it works in parity with tsc

* fix(js): add "dts" flag to swc executor

* fix(js): remove dts option as diff in perf is negligible

* fix(js): change logic around swc cli so that outputPath isn't computed

Co-authored-by: Chau Tran <ctran@Chaus-MacBook-Pro.local>
  • Loading branch information
nartc and Chau Tran committed Feb 23, 2022
1 parent 7c16cc4 commit 5527724
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 25 deletions.
8 changes: 7 additions & 1 deletion packages/js/src/executors/swc/schema.json
Expand Up @@ -38,7 +38,13 @@
"swcExclude": {
"type": "array",
"description": "List of SWC Glob/Regex to be excluded from compilation (https://swc.rs/docs/configuration/compilation#exclude)",
"default": ["./src/**/.*.spec.ts$", "./**/.*.js$"]
"default": [
"./src/**/.*.spec.ts$",
"./**/.*.spec.ts$",
"./src/**/jest-setup.ts$",
"./**/jest-setup.ts$",
"./**/.*.js$"
]
}
},
"required": ["main", "outputPath", "tsConfig"],
Expand Down
34 changes: 26 additions & 8 deletions packages/js/src/executors/swc/swc.impl.ts
@@ -1,4 +1,4 @@
import { ExecutorContext } from '@nrwl/devkit';
import { ExecutorContext, logger } from '@nrwl/devkit';
import {
assetGlobsToFiles,
FileInputOutput,
Expand All @@ -18,6 +18,7 @@ import { watchForSingleFileChanges } from '../../utils/watch-for-single-file-cha

function normalizeOptions(
options: SwcExecutorOptions,
layoutDir: string,
contextRoot: string,
sourceRoot?: string,
projectRoot?: string
Expand All @@ -38,15 +39,21 @@ function normalizeOptions(
outputPath
);

const projectRootParts = projectRoot.split('/');
// We pop the last part of the `projectRoot` to pass
// the last part (projectDir) and the remainder (projectRootParts) to swc
const projectDir = projectRootParts.pop();
const swcCwd = projectRootParts.join('/');

const swcCliOptions = {
projectDir: projectRoot.split('/').pop(),
// TODO: assume consumers put their code in `src`
destPath: `${relative(projectRoot, options.outputPath)}/src`,
srcPath: projectDir,
destPath: relative(join(contextRoot, swcCwd), outputPath),
swcCwd,
swcrcPath: join(projectRoot, '.swcrc'),
};

return {
...options,
swcrcPath: join(projectRoot, '.swcrc'),
mainOutputPath: resolve(
outputPath,
options.main.replace(`${projectRoot}/`, '').replace('.ts', '.js')
Expand Down Expand Up @@ -81,9 +88,20 @@ export async function* swcExecutor(
_options: SwcExecutorOptions,
context: ExecutorContext
) {
const { sourceRoot, root } = context.workspace.projects[context.projectName];
const options = normalizeOptions(_options, context.root, sourceRoot, root);
options.swcrcPath = addTempSwcrc(options);
const { sourceRoot, root, projectType } =
context.workspace.projects[context.projectName];
const layoutDir =
projectType === 'library'
? context.workspace.workspaceLayout.libsDir
: context.workspace.workspaceLayout.appsDir;
const options = normalizeOptions(
_options,
layoutDir,
context.root,
sourceRoot,
root
);
options.swcCliOptions.swcrcPath = addTempSwcrc(options);
const { tmpTsConfig, projectRoot } = checkDependencies(
context,
options.tsConfig
Expand Down
5 changes: 3 additions & 2 deletions packages/js/src/utils/schema.d.ts
Expand Up @@ -52,15 +52,16 @@ export interface SwcExecutorOptions extends ExecutorOptions {
}

export interface SwcCliOptions {
projectDir: string;
srcPath: string;
destPath: string;
swcrcPath: string;
swcCwd: string;
}

export interface NormalizedSwcExecutorOptions
extends NormalizedExecutorOptions {
swcExclude: string[];
skipTypeCheck: boolean;
swcrcPath: string;
swcCliOptions: SwcCliOptions;
}

Expand Down
18 changes: 9 additions & 9 deletions packages/js/src/utils/swc/compile-swc.ts
Expand Up @@ -2,16 +2,15 @@ import { ExecutorContext, logger } from '@nrwl/devkit';
import { cacheDir } from '@nrwl/workspace/src/utilities/cache-directory';
import { exec, execSync } from 'child_process';
import { createAsyncIterable } from '../create-async-iterable/create-async-iteratable';
import { NormalizedSwcExecutorOptions } from '../schema';
import { NormalizedSwcExecutorOptions, SwcCliOptions } from '../schema';
import { printDiagnostics } from '../typescript/print-diagnostics';
import { runTypeCheck, TypeCheckOptions } from '../typescript/run-type-check';

function getSwcCmd(
normalizedOptions: NormalizedSwcExecutorOptions,
{ swcrcPath, srcPath, destPath }: SwcCliOptions,
watch = false
) {
const srcPath = `../${normalizedOptions.swcCliOptions.projectDir}`;
let swcCmd = `npx swc ${srcPath} -d ${normalizedOptions.swcCliOptions.destPath} --source-maps --no-swcrc --config-file=${normalizedOptions.swcrcPath}`;
let swcCmd = `npx swc ${srcPath} -d ${destPath} --source-maps --no-swcrc --config-file=${swcrcPath}`;
return watch ? swcCmd.concat(' --watch') : swcCmd;
}

Expand Down Expand Up @@ -40,8 +39,8 @@ export async function compileSwc(
) {
logger.log(`Compiling with SWC for ${context.projectName}...`);

const swcCmdLog = execSync(getSwcCmd(normalizedOptions), {
cwd: normalizedOptions.projectRoot,
const swcCmdLog = execSync(getSwcCmd(normalizedOptions.swcCliOptions), {
cwd: normalizedOptions.swcCliOptions.swcCwd,
}).toString();
logger.log(swcCmdLog.replace(/\n/, ''));
const isCompileSuccess = swcCmdLog.includes('Successfully compiled');
Expand Down Expand Up @@ -85,9 +84,10 @@ export async function* compileSwcWatch(
let stderrOnData: () => void;
let watcherOnExit: () => void;

const swcWatcher = exec(getSwcCmd(normalizedOptions, true), {
cwd: normalizedOptions.projectRoot,
});
const swcWatcher = exec(
getSwcCmd(normalizedOptions.swcCliOptions, true),
{ cwd: normalizedOptions.swcCliOptions.swcCwd }
);

processOnExit = () => {
swcWatcher.kill();
Expand Down
8 changes: 3 additions & 5 deletions packages/js/src/utils/versions.ts
@@ -1,7 +1,5 @@
export const nxVersion = '*';

export const swcCoreVersion = '1.2.118';

export const swcCliVersion = '~0.1.52';

export const swcHelpersVersion = '~0.2.14';
export const swcCoreVersion = '~1.2.143';
export const swcCliVersion = '~0.1.55';
export const swcHelpersVersion = '~0.3.3';

0 comments on commit 5527724

Please sign in to comment.