diff --git a/docs/generated/packages/js/executors/swc.json b/docs/generated/packages/js/executors/swc.json index 2c5289db8ef55..7f93c917c8a1d 100644 --- a/docs/generated/packages/js/executors/swc.json +++ b/docs/generated/packages/js/executors/swc.json @@ -116,7 +116,8 @@ "oneOf": [ { "type": "string", "enum": ["all", "none"] }, { "type": "array", "items": { "type": "string" } } - ] + ], + "x-deprecated": "Make sure all dependencies are buildable by running `nx g @nx/js:setup-build`. This option will be removed in Nx 20." }, "externalBuildTargets": { "type": "array", @@ -131,6 +132,11 @@ "x-priority": "internal" } }, + "stripLeadingPaths": { + "type": "boolean", + "description": "Remove leading directory from output (e.g. src). See: https://swc.rs/docs/usage/cli#--strip-leading-paths", + "default": false + }, "required": ["main", "outputPath", "tsConfig"], "definitions": { "assetPattern": { diff --git a/docs/generated/packages/js/executors/tsc.json b/docs/generated/packages/js/executors/tsc.json index cd05d41f7d8e7..5d41da4d8fc8f 100644 --- a/docs/generated/packages/js/executors/tsc.json +++ b/docs/generated/packages/js/executors/tsc.json @@ -116,7 +116,8 @@ "oneOf": [ { "type": "string", "enum": ["all", "none"] }, { "type": "array", "items": { "type": "string" } } - ] + ], + "x-deprecated": "Make sure all dependencies are buildable by running `nx g @nx/js:setup-build`. This option will be removed in Nx 20." }, "externalBuildTargets": { "type": "array", diff --git a/e2e/js/src/js-executor-swc.test.ts b/e2e/js/src/js-executor-swc.test.ts index 9c287b68cc870..4aa369567679b 100644 --- a/e2e/js/src/js-executor-swc.test.ts +++ b/e2e/js/src/js-executor-swc.test.ts @@ -92,4 +92,19 @@ myLib(); }).toString(); expect(result).toContain('x'); }, 240_000); + + it('should support --strip-leading-paths option', () => { + const lib = uniq('lib'); + runCLI(`generate @nx/js:lib ${lib} --bundler=swc --no-interactive`); + + runCLI(`build ${lib} --stripLeadingPaths`); + + checkFilesExist( + `dist/libs/${lib}/package.json`, + `dist/libs/${lib}/index.js`, + `dist/libs/${lib}/lib/${lib}.js`, + `dist/libs/${lib}/index.d.ts`, + `dist/libs/${lib}/lib/${lib}.d.ts` + ); + }); }); diff --git a/packages/js/migrations.json b/packages/js/migrations.json index e462d9b9efa8c..dcd1bf7f9583c 100644 --- a/packages/js/migrations.json +++ b/packages/js/migrations.json @@ -167,6 +167,15 @@ "alwaysAddToPackageJson": false } } + }, + "18.2.5": { + "version": "18.2.5-beta.0", + "packages": { + "@swc/cli": { + "version": "~0.3.12", + "alwaysAddToPackageJson": false + } + } } } } diff --git a/packages/js/src/executors/swc/schema.json b/packages/js/src/executors/swc/schema.json index 195de7d0086a3..d5c07b2e76128 100644 --- a/packages/js/src/executors/swc/schema.json +++ b/packages/js/src/executors/swc/schema.json @@ -97,7 +97,8 @@ "type": "string" } } - ] + ], + "x-deprecated": "Make sure all dependencies are buildable by running `nx g @nx/js:setup-build`. This option will be removed in Nx 20." }, "externalBuildTargets": { "type": "array", @@ -114,6 +115,11 @@ "x-priority": "internal" } }, + "stripLeadingPaths": { + "type": "boolean", + "description": "Remove leading directory from output (e.g. src). See: https://swc.rs/docs/usage/cli#--strip-leading-paths", + "default": false + }, "required": ["main", "outputPath", "tsConfig"], "definitions": { "assetPattern": { diff --git a/packages/js/src/executors/swc/swc.impl.ts b/packages/js/src/executors/swc/swc.impl.ts index 19002b39e3cb4..0ca1827d54c69 100644 --- a/packages/js/src/executors/swc/swc.impl.ts +++ b/packages/js/src/executors/swc/swc.impl.ts @@ -17,6 +17,7 @@ import { import { copyPackageJson } from '../../utils/package-json'; import { NormalizedSwcExecutorOptions, + SwcCliOptions, SwcExecutorOptions, } from '../../utils/schema'; import { compileSwc, compileSwcWatch } from '../../utils/swc/compile-swc'; @@ -59,20 +60,16 @@ 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(); - // default to current directory if projectRootParts is []. - // Eg: when a project is at the root level, outside of layout dir - const swcCwd = projectRootParts.join('/') || '.'; + // Always execute from root of project, same as with SWC CLI. + const swcCwd = join(root, projectRoot); const { swcrcPath, tmpSwcrcPath } = getSwcrcPath(options, root, projectRoot); const swcCliOptions = { - srcPath: projectDir, - destPath: relative(join(root, swcCwd), outputPath), + srcPath: projectRoot, + destPath: relative(swcCwd, outputPath), swcCwd, swcrcPath, + stripLeadingPaths: Boolean(options.stripLeadingPaths), }; return { @@ -127,11 +124,16 @@ export async function* swcExecutor( ); if (!isInlineGraphEmpty(inlineProjectGraph)) { + if (options.stripLeadingPaths) { + throw new Error(`Cannot use --strip-leading-paths with inlining.`); + } + options.projectRoot = '.'; // set to root of workspace to include other libs for type check // remap paths for SWC compilation - options.swcCliOptions.srcPath = options.swcCliOptions.swcCwd; + options.inline = true; options.swcCliOptions.swcCwd = '.'; + options.swcCliOptions.srcPath = options.swcCliOptions.swcCwd; options.swcCliOptions.destPath = join( options.swcCliOptions.destPath.split(normalize('../')).at(-1), options.swcCliOptions.srcPath diff --git a/packages/js/src/executors/tsc/schema.json b/packages/js/src/executors/tsc/schema.json index 39dc7035a098e..0cc11cf075af1 100644 --- a/packages/js/src/executors/tsc/schema.json +++ b/packages/js/src/executors/tsc/schema.json @@ -85,7 +85,8 @@ "type": "string" } } - ] + ], + "x-deprecated": "Make sure all dependencies are buildable by running `nx g @nx/js:setup-build`. This option will be removed in Nx 20." }, "externalBuildTargets": { "type": "array", diff --git a/packages/js/src/utils/schema.d.ts b/packages/js/src/utils/schema.d.ts index 0a82243ba2e9c..b0caaa031789b 100644 --- a/packages/js/src/utils/schema.d.ts +++ b/packages/js/src/utils/schema.d.ts @@ -51,6 +51,7 @@ export interface ExecutorOptions { external?: 'all' | 'none' | string[]; externalBuildTargets?: string[]; generateLockfile?: boolean; + stripLeadingPaths?: boolean; } export interface NormalizedExecutorOptions extends ExecutorOptions { @@ -75,6 +76,7 @@ export interface SwcCliOptions { destPath: string; swcrcPath: string; swcCwd: string; + stripLeadingPaths: boolean; } export interface NormalizedSwcExecutorOptions @@ -84,4 +86,7 @@ export interface NormalizedSwcExecutorOptions skipTypeCheck: boolean; swcCliOptions: SwcCliOptions; tmpSwcrcPath: string; + sourceRoot?: string; + // TODO(v20): remove inline feature + inline?: boolean; } diff --git a/packages/js/src/utils/swc/compile-swc.ts b/packages/js/src/utils/swc/compile-swc.ts index ede82806fe9b5..90d2fed647f91 100644 --- a/packages/js/src/utils/swc/compile-swc.ts +++ b/packages/js/src/utils/swc/compile-swc.ts @@ -1,35 +1,66 @@ import { cacheDir, ExecutorContext, logger } from '@nx/devkit'; -import { exec, execSync } from 'child_process'; -import { removeSync } from 'fs-extra'; +import { exec, execSync } from 'node:child_process'; +import { join } from 'node:path'; +import { existsSync, removeSync } from 'fs-extra'; import { createAsyncIterable } from '@nx/devkit/src/utils/async-iterable'; import { NormalizedSwcExecutorOptions, SwcCliOptions } from '../schema'; import { printDiagnostics } from '../typescript/print-diagnostics'; import { runTypeCheck, TypeCheckOptions } from '../typescript/run-type-check'; +import { relative } from 'path'; function getSwcCmd( - { swcrcPath, srcPath, destPath }: SwcCliOptions, + { + swcCliOptions: { swcrcPath, destPath, stripLeadingPaths }, + root, + projectRoot, + originalProjectRoot, + sourceRoot, + inline, + }: NormalizedSwcExecutorOptions, watch = false ) { const swcCLI = require.resolve('@swc/cli/bin/swc.js'); + let inputDir: string; + // TODO(v20): remove inline feature + if (inline) { + inputDir = originalProjectRoot.split('/')[0]; + } else { + if (sourceRoot) { + inputDir = relative(projectRoot, sourceRoot); + } else { + // If sourceRoot is not provided, check if `src` exists and use that instead. + // This is important for root projects to avoid compiling too many directories. + inputDir = existsSync(join(root, projectRoot, 'src')) ? 'src' : '.'; + } + } + let swcCmd = `node ${swcCLI} ${ - // TODO(jack): clean this up when we remove inline module support - // Handle root project - srcPath === '.' ? 'src' : srcPath - } -d ${ - srcPath === '.' ? `${destPath}/src` : destPath - } --config-file=${swcrcPath}`; + inputDir || '.' + } -d ${destPath} --config-file=${swcrcPath} ${ + stripLeadingPaths ? '--strip-leading-paths' : '' + }`; return watch ? swcCmd.concat(' --watch') : swcCmd; } function getTypeCheckOptions(normalizedOptions: NormalizedSwcExecutorOptions) { - const { projectRoot, watch, tsConfig, root, outputPath } = normalizedOptions; + const { sourceRoot, projectRoot, watch, tsConfig, root, outputPath } = + normalizedOptions; + const inputDir = + // If `--strip-leading-paths` SWC option is used, we need to transpile from `src` directory. + !normalizedOptions.swcCliOptions.stripLeadingPaths + ? projectRoot + : sourceRoot + ? sourceRoot + : existsSync(join(root, projectRoot, 'src')) + ? join(projectRoot, 'src') + : projectRoot; const typeCheckOptions: TypeCheckOptions = { mode: 'emitDeclarationOnly', tsConfigPath: tsConfig, outDir: outputPath, workspaceRoot: root, - rootDir: projectRoot, + rootDir: inputDir, }; if (watch) { @@ -51,7 +82,7 @@ export async function compileSwc( removeSync(normalizedOptions.outputPath); } - const swcCmdLog = execSync(getSwcCmd(normalizedOptions.swcCliOptions), { + const swcCmdLog = execSync(getSwcCmd(normalizedOptions), { encoding: 'utf8', cwd: normalizedOptions.swcCliOptions.swcCwd, }); @@ -104,10 +135,9 @@ export async function* compileSwcWatch( let stderrOnData: () => void; let watcherOnExit: () => void; - const swcWatcher = exec( - getSwcCmd(normalizedOptions.swcCliOptions, true), - { cwd: normalizedOptions.swcCliOptions.swcCwd } - ); + const swcWatcher = exec(getSwcCmd(normalizedOptions, 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 d0d826e6c3172..bedec52edcb9d 100644 --- a/packages/js/src/utils/versions.ts +++ b/packages/js/src/utils/versions.ts @@ -2,7 +2,7 @@ export const nxVersion = require('../../package.json').version; export const esbuildVersion = '^0.19.2'; export const prettierVersion = '^2.6.2'; -export const swcCliVersion = '~0.1.62'; +export const swcCliVersion = '~0.3.12'; export const swcCoreVersion = '~1.3.85'; export const swcHelpersVersion = '~0.5.2'; export const swcNodeVersion = '~1.8.0';