diff --git a/packages/js/src/executors/swc/schema.json b/packages/js/src/executors/swc/schema.json index 7ee3c69b39ca9..8914c39400f15 100644 --- a/packages/js/src/executors/swc/schema.json +++ b/packages/js/src/executors/swc/schema.json @@ -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"], diff --git a/packages/js/src/executors/swc/swc.impl.ts b/packages/js/src/executors/swc/swc.impl.ts index a5380c699c310..873afa161d338 100644 --- a/packages/js/src/executors/swc/swc.impl.ts +++ b/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, @@ -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 @@ -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') @@ -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 diff --git a/packages/js/src/utils/schema.d.ts b/packages/js/src/utils/schema.d.ts index 3bc3b89f6d375..3fa8af2dbb0ef 100644 --- a/packages/js/src/utils/schema.d.ts +++ b/packages/js/src/utils/schema.d.ts @@ -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; } diff --git a/packages/js/src/utils/swc/compile-swc.ts b/packages/js/src/utils/swc/compile-swc.ts index 9ed34f7b7c504..df24f4d6ae603 100644 --- a/packages/js/src/utils/swc/compile-swc.ts +++ b/packages/js/src/utils/swc/compile-swc.ts @@ -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; } @@ -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'); @@ -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(); diff --git a/packages/js/src/utils/versions.ts b/packages/js/src/utils/versions.ts index 7ad7d62e67e2c..a3f35b44926b8 100644 --- a/packages/js/src/utils/versions.ts +++ b/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';