Skip to content

Commit 42e2141

Browse files
jaysooFrozenPandaz
authored andcommittedAug 23, 2023
fix(web): generate .swcrc file with modern defaults when creating new webapps (#18749)
(cherry picked from commit 750f485)
1 parent 7859d45 commit 42e2141

File tree

6 files changed

+59
-39
lines changed

6 files changed

+59
-39
lines changed
 

‎e2e/web/src/web.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ describe('Web Components Applications', () => {
7575
customElements.define('app-root', AppElement);
7676
`
7777
);
78-
runCLI(`build ${appName} --outputHashing none --compiler babel`);
78+
runCLI(`build ${appName} --outputHashing none`);
7979
checkFilesExist(
8080
`dist/apps/${appName}/index.html`,
8181
`dist/apps/${appName}/runtime.js`,

‎packages/react/src/generators/application/application.spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,17 @@ describe('app', () => {
966966
'swc-loader': expect.any(String),
967967
});
968968
});
969+
970+
it('should add .swcrc when --compiler=swc', async () => {
971+
await applicationGenerator(appTree, {
972+
...schema,
973+
compiler: 'swc',
974+
});
975+
976+
expect(readJson(appTree, '/apps/my-app/.swcrc')).toEqual({
977+
jsc: { target: 'es2016' },
978+
});
979+
});
969980
});
970981

971982
describe('--root-project', () => {

‎packages/react/src/generators/application/lib/create-application-files.ts

+28-33
Original file line numberDiff line numberDiff line change
@@ -79,39 +79,34 @@ export function createApplicationFiles(host: Tree, options: NormalizedSchema) {
7979
: undefined,
8080
].filter(Boolean),
8181
});
82-
} else if (
83-
options.style === 'styled-components' ||
84-
options.style === '@emotion/styled' ||
85-
options.style === 'styled-jsx'
86-
) {
87-
writeJson(
88-
host,
89-
`${options.appProjectRoot}/.swcrc`,
90-
91-
{
92-
jsc: {
93-
experimental: {
94-
plugins: [
95-
options.style === 'styled-components'
96-
? [
97-
'@swc/plugin-styled-components',
98-
{
99-
displayName: true,
100-
ssr: true,
101-
},
102-
]
103-
: undefined,
104-
options.style === 'styled-jsx'
105-
? ['@swc/plugin-styled-jsx', {}]
106-
: undefined,
107-
options.style === '@emotion/styled'
108-
? ['@swc/plugin-emotion', {}]
109-
: undefined,
110-
].filter(Boolean),
111-
},
112-
},
113-
}
114-
);
82+
} else if (options.compiler === 'swc') {
83+
const swcrc: any = {
84+
jsc: {
85+
target: 'es2016',
86+
},
87+
};
88+
if (options.style === 'styled-components') {
89+
swcrc.jsc.experimental = {
90+
plugins: [
91+
[
92+
'@swc/plugin-styled-components',
93+
{
94+
displayName: true,
95+
ssr: true,
96+
},
97+
],
98+
],
99+
};
100+
} else if (options.style === '@emotion/styled') {
101+
swcrc.jsc.experimental = {
102+
plugins: [['@swc/plugin-emotion', {}]],
103+
};
104+
} else if (options.style === 'styled-jsx') {
105+
swcrc.jsc.experimental = {
106+
plugins: [['@swc/plugin-styled-jsx', {}]],
107+
};
108+
}
109+
writeJson(host, `${options.appProjectRoot}/.swcrc`, swcrc);
115110
}
116111
} else if (options.bundler === 'rspack') {
117112
generateFiles(

‎packages/web/src/generators/application/application.spec.ts

+6
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,9 @@ describe('app', () => {
586586
};
587587
"
588588
`);
589+
590+
expect(tree.exists('apps/my-app/.babelrc')).toBeTruthy();
591+
expect(tree.exists('apps/my-app/.swcrc')).toBeFalsy();
589592
});
590593

591594
it('should support swc compiler', async () => {
@@ -609,6 +612,9 @@ describe('app', () => {
609612
};
610613
"
611614
`);
615+
616+
expect(tree.exists('apps/my-app/.babelrc')).toBeFalsy();
617+
expect(tree.exists('apps/my-app/.swcrc')).toBeTruthy();
612618
});
613619
});
614620

‎packages/web/src/generators/application/application.ts

+13
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
Tree,
2121
updateNxJson,
2222
updateProjectConfiguration,
23+
writeJson,
2324
} from '@nx/devkit';
2425
import { swcCoreVersion } from '@nx/js/src/utils/versions';
2526
import type { Linter } from '@nx/linter';
@@ -312,12 +313,24 @@ export async function applicationGenerator(host: Tree, schema: Schema) {
312313
}
313314

314315
if (options.compiler === 'swc') {
316+
writeJson(host, joinPathFragments(options.appProjectRoot, '.swcrc'), {
317+
jsc: {
318+
parser: {
319+
syntax: 'typescript',
320+
},
321+
target: 'es2016',
322+
},
323+
});
315324
const installTask = addDependenciesToPackageJson(
316325
host,
317326
{},
318327
{ '@swc/core': swcCoreVersion, 'swc-loader': swcLoaderVersion }
319328
);
320329
tasks.push(installTask);
330+
} else {
331+
writeJson(host, joinPathFragments(options.appProjectRoot, '.babelrc'), {
332+
presets: ['@nx/js/babel'],
333+
});
321334
}
322335

323336
setDefaults(host, options);

‎packages/web/src/generators/application/files/app-webpack/.babelrc__tmpl__

-5
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.