From 208d5c0f6d274115073a3186e5196166889f93df Mon Sep 17 00:00:00 2001 From: Craigory Coppola Date: Fri, 11 Mar 2022 14:36:56 -0500 Subject: [PATCH] feat(nx-plugin): port compiler option into nx-plugin generator (#9299) --- .../api-nx-plugin/generators/plugin.md | 10 +++ .../src/generators/plugin/plugin.spec.ts | 68 ++++++++++++++++--- .../src/generators/plugin/schema.d.ts | 1 + .../src/generators/plugin/schema.json | 6 ++ 4 files changed, 75 insertions(+), 10 deletions(-) diff --git a/docs/generated/api-nx-plugin/generators/plugin.md b/docs/generated/api-nx-plugin/generators/plugin.md index 7e55a8d6743b2..28cf02cbc201e 100644 --- a/docs/generated/api-nx-plugin/generators/plugin.md +++ b/docs/generated/api-nx-plugin/generators/plugin.md @@ -43,6 +43,16 @@ Type: `string` Plugin name +### compiler + +Default: `tsc` + +Type: `string` + +Possible values: `tsc`, `swc` + +The compiler used by the build and test targets + ### directory Alias(es): d diff --git a/packages/nx-plugin/src/generators/plugin/plugin.spec.ts b/packages/nx-plugin/src/generators/plugin/plugin.spec.ts index a075529dc3b03..f4906533808fc 100644 --- a/packages/nx-plugin/src/generators/plugin/plugin.spec.ts +++ b/packages/nx-plugin/src/generators/plugin/plugin.spec.ts @@ -1,6 +1,20 @@ import { pluginGenerator } from './plugin'; import { Tree, readProjectConfiguration } from '@nrwl/devkit'; import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; +import { Schema } from './schema'; +import { Linter } from '@nrwl/linter'; + +const getSchema: (overrides?: Partial) => Schema = ( + overrides = {} +) => ({ + name: 'my-plugin', + compiler: 'tsc', + skipTsConfig: false, + skipFormat: false, + linter: Linter.EsLint, + unitTestRunner: 'jest', + ...overrides, +}); describe('NxPlugin Plugin Generator', () => { let tree: Tree; @@ -10,7 +24,7 @@ describe('NxPlugin Plugin Generator', () => { }); it('should update the workspace.json file', async () => { - await pluginGenerator(tree, { name: 'myPlugin' } as any); + await pluginGenerator(tree, getSchema()); const project = readProjectConfiguration(tree, 'my-plugin'); expect(project.root).toEqual('libs/my-plugin'); expect(project.targets.build).toEqual({ @@ -63,10 +77,13 @@ describe('NxPlugin Plugin Generator', () => { }); it('should place the plugin in a directory', async () => { - await pluginGenerator(tree, { - name: 'myPlugin', - directory: 'plugins', - } as any); + await pluginGenerator( + tree, + getSchema({ + name: 'myPlugin', + directory: 'plugins', + }) + ); const project = readProjectConfiguration(tree, 'plugins-my-plugin'); const projectE2e = readProjectConfiguration(tree, 'plugins-my-plugin-e2e'); expect(project.root).toEqual('libs/plugins/my-plugin'); @@ -74,7 +91,7 @@ describe('NxPlugin Plugin Generator', () => { }); it('should create schematic and builder files', async () => { - await pluginGenerator(tree, { name: 'myPlugin' } as any); + await pluginGenerator(tree, getSchema({ name: 'myPlugin' })); [ 'libs/my-plugin/project.json', @@ -103,10 +120,13 @@ describe('NxPlugin Plugin Generator', () => { describe('--unitTestRunner', () => { describe('none', () => { it('should not generate test files', async () => { - await pluginGenerator(tree, { - name: 'myPlugin', - unitTestRunner: 'none', - } as any); + await pluginGenerator( + tree, + getSchema({ + name: 'myPlugin', + unitTestRunner: 'none', + }) + ); [ 'libs/my-plugin/src/generators/my-plugin/generator.ts', @@ -120,4 +140,32 @@ describe('NxPlugin Plugin Generator', () => { }); }); }); + + describe('--compiler', () => { + it('should specify tsc as compiler', async () => { + await pluginGenerator( + tree, + getSchema({ + compiler: 'tsc', + }) + ); + + const { build } = readProjectConfiguration(tree, 'my-plugin').targets; + + expect(build.executor).toEqual('@nrwl/js:tsc'); + }); + + it('should specify swc as compiler', async () => { + await pluginGenerator( + tree, + getSchema({ + compiler: 'swc', + }) + ); + + const { build } = readProjectConfiguration(tree, 'my-plugin').targets; + + expect(build.executor).toEqual('@nrwl/js:swc'); + }); + }); }); diff --git a/packages/nx-plugin/src/generators/plugin/schema.d.ts b/packages/nx-plugin/src/generators/plugin/schema.d.ts index 6a1f97960d1fc..db309f41c1d03 100644 --- a/packages/nx-plugin/src/generators/plugin/schema.d.ts +++ b/packages/nx-plugin/src/generators/plugin/schema.d.ts @@ -11,4 +11,5 @@ export interface Schema { linter: Linter; standaloneConfig?: boolean; setParserOptionsProject?: boolean; + compiler: 'swc' | 'tsc'; } diff --git a/packages/nx-plugin/src/generators/plugin/schema.json b/packages/nx-plugin/src/generators/plugin/schema.json index 5d8aee120be12..7911fb22367ce 100644 --- a/packages/nx-plugin/src/generators/plugin/schema.json +++ b/packages/nx-plugin/src/generators/plugin/schema.json @@ -64,6 +64,12 @@ "type": "boolean", "description": "Whether or not to configure the ESLint \"parserOptions.project\" option. We do not do this by default for lint performance reasons.", "default": false + }, + "compiler": { + "type": "string", + "enum": ["tsc", "swc"], + "default": "tsc", + "description": "The compiler used by the build and test targets" } }, "required": ["name"],