From 7a2d5709de41bff626fed4a6173a12681bcdb742 Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Mon, 21 Aug 2023 13:30:15 -0400 Subject: [PATCH] fix(web): generate .swcrc file with modern defaults when creating new webapps --- .../application/application.spec.ts | 11 ++++ .../lib/create-application-files.ts | 62 +++++++++---------- .../application/application.spec.ts | 6 ++ .../src/generators/application/application.ts | 10 +++ .../files/app-webpack/.babelrc__tmpl__ | 5 -- 5 files changed, 56 insertions(+), 38 deletions(-) delete mode 100644 packages/web/src/generators/application/files/app-webpack/.babelrc__tmpl__ diff --git a/packages/react/src/generators/application/application.spec.ts b/packages/react/src/generators/application/application.spec.ts index 178dea62abd348..8b89127ad9c9ab 100644 --- a/packages/react/src/generators/application/application.spec.ts +++ b/packages/react/src/generators/application/application.spec.ts @@ -967,6 +967,17 @@ describe('app', () => { 'swc-loader': expect.any(String), }); }); + + it('should add .swcrc when --compiler=swc', async () => { + await applicationGenerator(appTree, { + ...schema, + compiler: 'swc', + }); + + expect(readJson(appTree, '/apps/my-app/.swcrc')).toEqual({ + jsc: { target: 'es2016' }, + }); + }); }); describe('--root-project', () => { diff --git a/packages/react/src/generators/application/lib/create-application-files.ts b/packages/react/src/generators/application/lib/create-application-files.ts index 5c1743be78b3b7..9d9b9797915c49 100644 --- a/packages/react/src/generators/application/lib/create-application-files.ts +++ b/packages/react/src/generators/application/lib/create-application-files.ts @@ -6,6 +6,7 @@ import { Tree, writeJson, } from '@nx/devkit'; +import { type Config as SwcConfig } from '@swc/core'; import { getRelativePathToRootTsConfig } from '@nx/js'; import { join } from 'path'; import { createTsConfig } from '../../../utils/create-ts-config'; @@ -79,39 +80,34 @@ export function createApplicationFiles(host: Tree, options: NormalizedSchema) { : undefined, ].filter(Boolean), }); - } else if ( - options.style === 'styled-components' || - options.style === '@emotion/styled' || - options.style === 'styled-jsx' - ) { - writeJson( - host, - `${options.appProjectRoot}/.swcrc`, - - { - jsc: { - experimental: { - plugins: [ - options.style === 'styled-components' - ? [ - '@swc/plugin-styled-components', - { - displayName: true, - ssr: true, - }, - ] - : undefined, - options.style === 'styled-jsx' - ? ['@swc/plugin-styled-jsx', {}] - : undefined, - options.style === '@emotion/styled' - ? ['@swc/plugin-emotion', {}] - : undefined, - ].filter(Boolean), - }, - }, - } - ); + } else if (options.compiler === 'swc') { + const swcrc: SwcConfig = { + jsc: { + target: 'es2016', + }, + }; + if (options.style === 'styled-components') { + swcrc.jsc.experimental = { + plugins: [ + [ + '@swc/plugin-styled-components', + { + displayName: true, + ssr: true, + }, + ], + ], + }; + } else if (options.style === '@emotion/styled') { + swcrc.jsc.experimental = { + plugins: [['@swc/plugin-emotion', {}]], + }; + } else if (options.style === 'styled-jsx') { + swcrc.jsc.experimental = { + plugins: [['@swc/plugin-styled-jsx', {}]], + }; + } + writeJson(host, `${options.appProjectRoot}/.swcrc`, swcrc); } } else if (options.bundler === 'rspack') { generateFiles( diff --git a/packages/web/src/generators/application/application.spec.ts b/packages/web/src/generators/application/application.spec.ts index 14f4d82428e42c..fcf7ca9554b69f 100644 --- a/packages/web/src/generators/application/application.spec.ts +++ b/packages/web/src/generators/application/application.spec.ts @@ -586,6 +586,9 @@ describe('app', () => { }; " `); + + expect(tree.exists('apps/my-app/.babelrc')).toBeTruthy(); + expect(tree.exists('apps/my-app/.swcrc')).toBeFalsy(); }); it('should support swc compiler', async () => { @@ -609,6 +612,9 @@ describe('app', () => { }; " `); + + expect(tree.exists('apps/my-app/.babelrc')).toBeFalsy(); + expect(tree.exists('apps/my-app/.swcrc')).toBeTruthy(); }); }); diff --git a/packages/web/src/generators/application/application.ts b/packages/web/src/generators/application/application.ts index a1e8051ad3fce9..15edc15fd2ca81 100644 --- a/packages/web/src/generators/application/application.ts +++ b/packages/web/src/generators/application/application.ts @@ -20,6 +20,7 @@ import { Tree, updateNxJson, updateProjectConfiguration, + writeJson, } from '@nx/devkit'; import { swcCoreVersion } from '@nx/js/src/utils/versions'; import type { Linter } from '@nx/linter'; @@ -312,12 +313,21 @@ export async function applicationGenerator(host: Tree, schema: Schema) { } if (options.compiler === 'swc') { + writeJson(host, joinPathFragments(options.appProjectRoot, '.swcrc'), { + jsc: { + target: 'es2016', + }, + }); const installTask = addDependenciesToPackageJson( host, {}, { '@swc/core': swcCoreVersion, 'swc-loader': swcLoaderVersion } ); tasks.push(installTask); + } else { + writeJson(host, joinPathFragments(options.appProjectRoot, '.babelrc'), { + presets: ['@nx/js/babel'], + }); } setDefaults(host, options); diff --git a/packages/web/src/generators/application/files/app-webpack/.babelrc__tmpl__ b/packages/web/src/generators/application/files/app-webpack/.babelrc__tmpl__ deleted file mode 100644 index 19ebd10e1179bf..00000000000000 --- a/packages/web/src/generators/application/files/app-webpack/.babelrc__tmpl__ +++ /dev/null @@ -1,5 +0,0 @@ -{ - "presets": [ - "@nx/js/babel" - ] -}