Skip to content

Commit

Permalink
fix(web): generate .swcrc file with modern defaults when creating new…
Browse files Browse the repository at this point in the history
… webapps (#18749)
  • Loading branch information
jaysoo authored Aug 22, 2023
1 parent 791171e commit 750f485
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 39 deletions.
2 changes: 1 addition & 1 deletion e2e/web/src/web.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('Web Components Applications', () => {
customElements.define('app-root', AppElement);
`
);
runCLI(`build ${appName} --outputHashing none --compiler babel`);
runCLI(`build ${appName} --outputHashing none`);
checkFilesExist(
`dist/apps/${appName}/index.html`,
`dist/apps/${appName}/runtime.js`,
Expand Down
11 changes: 11 additions & 0 deletions packages/react/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,39 +79,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: any = {
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(
Expand Down
6 changes: 6 additions & 0 deletions packages/web/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -609,6 +612,9 @@ describe('app', () => {
};
"
`);

expect(tree.exists('apps/my-app/.babelrc')).toBeFalsy();
expect(tree.exists('apps/my-app/.swcrc')).toBeTruthy();
});
});

Expand Down
13 changes: 13 additions & 0 deletions packages/web/src/generators/application/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -312,12 +313,24 @@ export async function applicationGenerator(host: Tree, schema: Schema) {
}

if (options.compiler === 'swc') {
writeJson(host, joinPathFragments(options.appProjectRoot, '.swcrc'), {
jsc: {
parser: {
syntax: 'typescript',
},
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);
Expand Down

This file was deleted.

1 comment on commit 750f485

@vercel
Copy link

@vercel vercel bot commented on 750f485 Aug 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx.dev
nx-five.vercel.app

Please sign in to comment.