From 63006b791acfc5b69d9cf6b347394285496cd262 Mon Sep 17 00:00:00 2001 From: Daniel Grant Date: Fri, 6 May 2022 18:46:13 +0100 Subject: [PATCH] fix(js): .swcrc path option should follow existing conventions (#10127) Co-authored-by: Craigory Coppola --- docs/generated/packages/js.json | 5 +-- packages/js/migrations.json | 6 +++ packages/js/src/executors/swc/schema.json | 5 +-- packages/js/src/executors/swc/swc.impl.ts | 5 ++- .../update-swcrc-path.spec.ts | 34 +++++++++++++++++ .../update-14.1.5-beta.0/update-swcrc-path.ts | 37 +++++++++++++++++++ packages/js/src/utils/schema.d.ts | 2 +- 7 files changed, 86 insertions(+), 8 deletions(-) create mode 100644 packages/js/src/migrations/update-14.1.5-beta.0/update-swcrc-path.spec.ts create mode 100644 packages/js/src/migrations/update-14.1.5-beta.0/update-swcrc-path.ts diff --git a/docs/generated/packages/js.json b/docs/generated/packages/js.json index 90a2d984a1374..75e2fbcf5ca6a 100644 --- a/docs/generated/packages/js.json +++ b/docs/generated/packages/js.json @@ -362,10 +362,9 @@ "type": "string", "description": "The path to the Typescript configuration file." }, - "swcrcPath": { + "swcrc": { "type": "string", - "description": "The path to the SWC configuration file.", - "default": ".lib.swcrc" + "description": "The path to the SWC configuration file. Default: .lib.swcrc" }, "assets": { "type": "array", diff --git a/packages/js/migrations.json b/packages/js/migrations.json index bd0ab190cfbb6..569c636498098 100644 --- a/packages/js/migrations.json +++ b/packages/js/migrations.json @@ -23,6 +23,12 @@ "version": "14.0.0-beta.2", "description": "Exclude jest config from .lib.swcrc", "factory": "./src/migrations/update-14-0-0/exclude-jest-config-swcrc" + }, + "update-swcrc-path": { + "cli": "nx", + "version": "14.1.5-beta.0", + "description": "Rename option swcrcPath to swcrc, and resolve relative to workspace root", + "factory": "./src/migrations/update-14.1.5-beta.0/update-swcrc-path" } }, "packageJsonUpdates": {} diff --git a/packages/js/src/executors/swc/schema.json b/packages/js/src/executors/swc/schema.json index a880a1c3f8ae6..2915ad35ceff0 100644 --- a/packages/js/src/executors/swc/schema.json +++ b/packages/js/src/executors/swc/schema.json @@ -17,10 +17,9 @@ "type": "string", "description": "The path to the Typescript configuration file." }, - "swcrcPath": { + "swcrc": { "type": "string", - "description": "The path to the SWC configuration file.", - "default": ".lib.swcrc" + "description": "The path to the SWC configuration file. Default: .lib.swcrc" }, "assets": { "type": "array", diff --git a/packages/js/src/executors/swc/swc.impl.ts b/packages/js/src/executors/swc/swc.impl.ts index b3f2a5404c3cc..8000b027068d6 100644 --- a/packages/js/src/executors/swc/swc.impl.ts +++ b/packages/js/src/executors/swc/swc.impl.ts @@ -45,12 +45,15 @@ 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 = options.swcrc + ? join(contextRoot, options.swcrc) + : join(contextRoot, projectRoot, '.lib.swcrc'); const swcCliOptions = { srcPath: projectDir, destPath: relative(join(contextRoot, swcCwd), outputPath), swcCwd, - swcrcPath: join(contextRoot, projectRoot, options.swcrcPath), + swcrcPath, }; return { diff --git a/packages/js/src/migrations/update-14.1.5-beta.0/update-swcrc-path.spec.ts b/packages/js/src/migrations/update-14.1.5-beta.0/update-swcrc-path.spec.ts new file mode 100644 index 0000000000000..90addfa811f2a --- /dev/null +++ b/packages/js/src/migrations/update-14.1.5-beta.0/update-swcrc-path.spec.ts @@ -0,0 +1,34 @@ +import { + addProjectConfiguration, + readProjectConfiguration, +} from '@nrwl/devkit'; +import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; +import updateSwcrcPath from './update-swcrc-path'; + +describe('update-swcrc-path migration', () => { + it('should replace relative `swcrcPath` option with absolute `swcrc`', async () => { + const tree = createTreeWithEmptyWorkspace(2); + addProjectConfiguration(tree, 'test-package', { + root: 'packages/test-package', + targets: { + build: { + executor: '@nrwl/js:swc', + options: { + swcrcPath: 'config/swcrc.json', + somethingThatShouldNotBeRemoved: true, + }, + }, + }, + }); + + await updateSwcrcPath(tree); + + const { targets, root } = readProjectConfiguration(tree, 'test-package'); + expect(root).toBe('packages/test-package'); + expect(targets.build.options.somethingThatShouldNotBeRemoved).toBeDefined(); + expect(targets.build.options.swcrcPath).toBeUndefined(); + expect(targets.build.options.swcrc).toBe( + 'packages/test-package/config/swcrc.json' + ); + }); +}); diff --git a/packages/js/src/migrations/update-14.1.5-beta.0/update-swcrc-path.ts b/packages/js/src/migrations/update-14.1.5-beta.0/update-swcrc-path.ts new file mode 100644 index 0000000000000..99faf583e5f31 --- /dev/null +++ b/packages/js/src/migrations/update-14.1.5-beta.0/update-swcrc-path.ts @@ -0,0 +1,37 @@ +import { + joinPathFragments, + readProjectConfiguration, + Tree, + updateProjectConfiguration, +} from '@nrwl/devkit'; +import { forEachExecutorOptions } from '@nrwl/workspace/src/utilities/executor-options-utils'; +import { SwcExecutorOptions } from '../../utils/schema'; + +type OldSwcExecutorOptions = SwcExecutorOptions & { swcrcPath?: string }; + +export function updateSwcrcPath(tree: Tree) { + forEachExecutorOptions( + tree, + '@nrwl/js:swc', + (_, projectName, targetName, configurationName) => { + const projectConfig = readProjectConfiguration(tree, projectName); + const executorOptions: OldSwcExecutorOptions = configurationName + ? projectConfig.targets[targetName].configurations[configurationName] + : projectConfig.targets[targetName].options; + + if (!executorOptions.swcrcPath) return; + + const newSwcrcPath = joinPathFragments( + projectConfig.root, + executorOptions.swcrcPath + ); + + delete executorOptions.swcrcPath; + executorOptions.swcrc = newSwcrcPath; + + updateProjectConfiguration(tree, projectName, projectConfig); + } + ); +} + +export default updateSwcrcPath; diff --git a/packages/js/src/utils/schema.d.ts b/packages/js/src/utils/schema.d.ts index 76c788cb1ff22..a3aa39e7df86e 100644 --- a/packages/js/src/utils/schema.d.ts +++ b/packages/js/src/utils/schema.d.ts @@ -36,7 +36,7 @@ export interface ExecutorOptions { main: string; outputPath: string; tsConfig: string; - swcrcPath: string; + swcrc?: string; watch: boolean; transformers: TransformerEntry[]; updateBuildableProjectDepsInPackageJson?: boolean;