Skip to content

Commit

Permalink
feat(styled-components): use SWC Wasm plugin (#2679)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan committed Jun 24, 2024
1 parent bdab462 commit b3adc3c
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 64 deletions.
42 changes: 42 additions & 0 deletions packages/core/tests/mergeConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,46 @@ describe('mergeRsbuildConfig', () => {
},
});
});

it('should merge SWC plugins as expected', () => {
expect(
mergeRsbuildConfig(
{
tools: {
swc: {
jsc: {
experimental: {
plugins: [['@swc/plugin-foo', {}]],
},
},
},
},
},
{
tools: {
swc: {
jsc: {
experimental: {
plugins: [['@swc/plugin-bar', {}]],
},
},
},
},
},
),
).toEqual({
tools: {
swc: {
jsc: {
experimental: {
plugins: [
['@swc/plugin-foo', {}],
['@swc/plugin-bar', {}],
],
},
},
},
},
});
});
});
3 changes: 2 additions & 1 deletion packages/plugin-styled-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"dev": "modern build --watch"
},
"dependencies": {
"@rsbuild/shared": "workspace:*"
"@rsbuild/shared": "workspace:*",
"@swc/plugin-styled-components": "2.0.8"
},
"devDependencies": {
"@rsbuild/core": "workspace:*",
Expand Down
27 changes: 17 additions & 10 deletions packages/plugin-styled-components/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
import { reduceConfigs } from '@rsbuild/core';

/**
* the options of [rspackExperiments.styledComponents](https://rspack.dev/guide/features/builtin-swc-loader#rspackexperimentsstyledcomponents).
* The options of [@swc/plugin-styled-components](https://www.npmjs.com/package/@swc/plugin-styled-components).
*/
export type PluginStyledComponentsOptions = {
displayName?: boolean;
Expand Down Expand Up @@ -60,17 +60,24 @@ export const pluginStyledComponents = (
};

api.modifyRsbuildConfig((userConfig, { mergeRsbuildConfig }) => {
const mergedOptions = getMergedOptions();
if (!mergedOptions) {
return userConfig;
}

const extraConfig: RsbuildConfig = {
tools: {
swc(opts) {
const mergedOptions = getMergedOptions();
if (!mergedOptions) {
return opts;
}

opts.rspackExperiments ??= {};
opts.rspackExperiments.styledComponents = mergedOptions;
return opts;
swc: {
jsc: {
experimental: {
plugins: [
[
require.resolve('@swc/plugin-styled-components'),
mergedOptions,
],
],
},
},
},
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ exports[`plugins/styled-components > should apply styledComponents option to swc
},
"isModule": "unknown",
"jsc": {
"experimental": {
"plugins": [
[
"<ROOT>/node_modules/<PNPM_INNER>/@swc/plugin-styled-components/swc_plugin_styled_components.wasm",
{
"displayName": true,
"pure": false,
"ssr": false,
"transpileTemplateLiterals": true,
},
],
],
},
"externalHelpers": true,
"parser": {
"decorators": true,
Expand All @@ -49,14 +62,6 @@ exports[`plugins/styled-components > should apply styledComponents option to swc
"legacyDecorator": false,
},
},
"rspackExperiments": {
"styledComponents": {
"displayName": true,
"pure": false,
"ssr": false,
"transpileTemplateLiterals": true,
},
},
},
},
],
Expand Down Expand Up @@ -89,6 +94,19 @@ exports[`plugins/styled-components > should enable ssr option when target contai
},
"isModule": "unknown",
"jsc": {
"experimental": {
"plugins": [
[
"<ROOT>/node_modules/<PNPM_INNER>/@swc/plugin-styled-components/swc_plugin_styled_components.wasm",
{
"displayName": true,
"pure": false,
"ssr": false,
"transpileTemplateLiterals": true,
},
],
],
},
"externalHelpers": true,
"parser": {
"decorators": true,
Expand All @@ -101,14 +119,6 @@ exports[`plugins/styled-components > should enable ssr option when target contai
"legacyDecorator": false,
},
},
"rspackExperiments": {
"styledComponents": {
"displayName": true,
"pure": false,
"ssr": true,
"transpileTemplateLiterals": true,
},
},
},
},
],
Expand Down Expand Up @@ -152,6 +162,19 @@ exports[`plugins/styled-components > should enable ssr option when target contai
},
"isModule": "unknown",
"jsc": {
"experimental": {
"plugins": [
[
"<ROOT>/node_modules/<PNPM_INNER>/@swc/plugin-styled-components/swc_plugin_styled_components.wasm",
{
"displayName": true,
"pure": false,
"ssr": false,
"transpileTemplateLiterals": true,
},
],
],
},
"externalHelpers": true,
"parser": {
"decorators": true,
Expand All @@ -164,14 +187,6 @@ exports[`plugins/styled-components > should enable ssr option when target contai
"legacyDecorator": false,
},
},
"rspackExperiments": {
"styledComponents": {
"displayName": true,
"pure": false,
"ssr": true,
"transpileTemplateLiterals": true,
},
},
},
},
],
Expand Down
Loading

0 comments on commit b3adc3c

Please sign in to comment.