Skip to content

Commit

Permalink
fix(uni-builder): styled-components not works when use webpack swc-pl…
Browse files Browse the repository at this point in the history
…ugin (#5325)
  • Loading branch information
9aoy authored Jan 29, 2024
1 parent 67b9f52 commit b79abcf
Show file tree
Hide file tree
Showing 14 changed files with 45 additions and 33 deletions.
7 changes: 7 additions & 0 deletions .changeset/lovely-dolls-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@modern-js/uni-builder': patch
---

fix(uni-builder): styled-components not works when use webpack swc-plugin

fix(uni-builder): 修复在使用 webpack swc 插件时 styled-components 不生效的问题
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { RsbuildPlugin } from '@rsbuild/core';
import { PLUGIN_SWC_NAME } from '@rsbuild/core';
import {
isServerTarget,
mergeChainedOptions,
Expand All @@ -12,6 +13,8 @@ export const pluginStyledComponents = (
): RsbuildPlugin => ({
name: 'uni-builder:styled-components',

pre: [PLUGIN_SWC_NAME, 'uni-builder:babel'],

setup(api) {
api.modifyBundlerChain(async (chain, { CHAIN_ID, isProd }) => {
const isSSR = isServerTarget(api.context.targets);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,12 @@ exports[`plugins/styled-components > should works in webpack swc mode 1`] = `
"libraryName": "@arco-design/web-react/icon",
},
],
"styledComponents": {
"displayName": true,
"pure": false,
"ssr": false,
"transpileTemplateLiterals": true,
},
},
"jsc": {
"externalHelpers": true,
Expand Down
9 changes: 3 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests/e2e/builder/cases/externalHelpers/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import path from 'path';
import { build } from '@scripts/shared';
import { providerType } from '@scripts/helper';
import { expect, test } from '@modern-js/e2e/playwright';
import { builderPluginSwc } from '@modern-js/builder-plugin-swc';
import { pluginSwc } from '@rsbuild/plugin-swc';

test('should externalHelpers by default', async () => {
const builder = await build({
cwd: __dirname,
entry: { index: path.resolve(__dirname, './src/main.ts') },
plugins: providerType === 'rspack' ? [] : [builderPluginSwc()],
plugins: providerType === 'rspack' ? [] : [pluginSwc()],
});
const files = await builder.unwrapOutputJSON(false);

Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/builder/cases/html/minify/index.swc.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { join } from 'path';
import { expect, test } from '@modern-js/e2e/playwright';
import { build, getHrefByEntryName } from '@scripts/shared';
import { builderPluginSwc } from '@modern-js/builder-plugin-swc';
import { pluginSwc } from '@rsbuild/plugin-swc';

const fixtures = __dirname;

Expand All @@ -14,7 +14,7 @@ test('should minify template js & css correctly when use swc-plugin', async ({
main: join(fixtures, 'src/index.ts'),
},
runServer: true,
plugins: [builderPluginSwc()],
plugins: [pluginSwc()],
builderConfig: {
html: {
template: './static/index.html',
Expand Down
6 changes: 3 additions & 3 deletions tests/e2e/builder/cases/lodash/index.swc.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as path from 'path';
import { expect, test } from '@modern-js/e2e/playwright';
import { build } from '@scripts/shared';
import { builderPluginSwc } from '@modern-js/builder-plugin-swc';
import { pluginSwc } from '@rsbuild/plugin-swc';

test('should optimize lodash bundle size when using SWC plugin', async () => {
const builder = await build({
Expand All @@ -21,7 +21,7 @@ test('should optimize lodash bundle size when using SWC plugin', async () => {
},
},
},
plugins: [builderPluginSwc()],
plugins: [pluginSwc()],
runServer: false,
});

Expand Down Expand Up @@ -49,7 +49,7 @@ test('should not optimize lodash bundle size when transformLodash is false and u
},
},
},
plugins: [builderPluginSwc()],
plugins: [pluginSwc()],
runServer: false,
});

Expand Down
6 changes: 3 additions & 3 deletions tests/e2e/builder/cases/performance/removeConsole.swc.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { join } from 'path';
import { expect, test } from '@modern-js/e2e/playwright';
import { build } from '@scripts/shared';
import { builderPluginSwc } from '@modern-js/builder-plugin-swc';
import { pluginSwc } from '@rsbuild/plugin-swc';

const cwd = join(__dirname, 'removeConsole');

Expand All @@ -26,7 +26,7 @@ test('should remove specified console correctly when using SWC plugin', async ()
entry: {
main: join(cwd, 'src/index.js'),
},
plugins: [builderPluginSwc()],
plugins: [pluginSwc()],
builderConfig: {
performance: {
removeConsole: ['log', 'warn'],
Expand All @@ -48,7 +48,7 @@ test('should remove all console correctly when using SWC plugin', async () => {
entry: {
main: join(cwd, 'src/index.js'),
},
plugins: [builderPluginSwc()],
plugins: [pluginSwc()],
builderConfig: {
performance: {
removeConsole: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path';
import { build } from '@scripts/shared';
import { expect, test } from '@modern-js/e2e/playwright';
import { builderPluginSwc } from '@modern-js/builder-plugin-swc';
import { pluginSwc } from '@rsbuild/plugin-swc';
import { findEntry, copyPkgToNodeModules, cases, shareTest } from './helper';

/* webpack can receive Function type configuration */
Expand Down Expand Up @@ -34,7 +34,7 @@ test('should import with function customName', async () => {

const builder = await build({
...setupConfig,
plugins: [builderPluginSwc()],
plugins: [pluginSwc()],
builderConfig: {
source: {
transformImport: [
Expand Down Expand Up @@ -63,7 +63,7 @@ test('should import with template config with SWC', async () => {
const setupConfig = {
cwd: __dirname,
entry: { index: path.resolve(__dirname, './src/index.js') },
plugins: [builderPluginSwc()],
plugins: [pluginSwc()],
};

{
Expand Down Expand Up @@ -110,6 +110,6 @@ cases.forEach(c => {
shareTest(`${name}-webpack`, entry, config);

shareTest(`${name}-webpack-swc`, entry, config, {
plugins: [builderPluginSwc()],
plugins: [pluginSwc()],
});
});
6 changes: 3 additions & 3 deletions tests/e2e/builder/cases/styled-component/index.swc.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path';
import { build } from '@scripts/shared';
import { expect, test } from '@modern-js/e2e/playwright';
import { builderPluginSwc } from '@modern-js/builder-plugin-swc';
import { pluginSwc } from '@rsbuild/plugin-swc';

const commonConfig = {
cwd: __dirname,
Expand Down Expand Up @@ -36,7 +36,7 @@ const noStyledConfig = {
test('should allow to disable styled-components when use swc plugin', async () => {
const builder = await build({
...noStyledConfig,
plugins: [builderPluginSwc()],
plugins: [pluginSwc()],
});
const files = await builder.unwrapOutputJSON();

Expand All @@ -53,7 +53,7 @@ test('should allow to disable styled-components when use swc plugin', async () =
test('should transform styled-components by default when use swc plugin', async () => {
const builder = await build({
...commonConfig,
plugins: [builderPluginSwc()],
plugins: [pluginSwc()],
});
const files = await builder.unwrapOutputJSON();

Expand Down
12 changes: 6 additions & 6 deletions tests/e2e/builder/cases/swc/index.swc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import * as path from 'path';
import { readFileSync } from 'fs';
import { expect, test } from '@modern-js/e2e/playwright';
import { build, getHrefByEntryName } from '@scripts/shared';
import { builderPluginSwc } from '@modern-js/builder-plugin-swc';
import { pluginSwc } from '@rsbuild/plugin-swc';

test('should run SWC compilation correctly', async ({ page }) => {
const builder = await build({
cwd: __dirname,
entry: {
index: path.resolve(__dirname, './src/main.ts'),
},
plugins: [builderPluginSwc()],
plugins: [pluginSwc()],
runServer: true,
});

Expand All @@ -33,7 +33,7 @@ test('should optimize lodash bundle size', async ({ page }) => {
entry: {
index: path.resolve(__dirname, './src/main.ts'),
},
plugins: [builderPluginSwc()],
plugins: [pluginSwc()],
runServer: true,
builderConfig: {
output: {
Expand Down Expand Up @@ -65,7 +65,7 @@ test('should use define for class', async () => {
index: path.resolve(__dirname, './src/main.ts'),
},
plugins: [
builderPluginSwc({
pluginSwc({
overrides: [
{
test: /override.ts/,
Expand Down Expand Up @@ -111,7 +111,7 @@ test('core-js-entry', async () => {
index: path.resolve(__dirname, './src/core-js-entry.ts'),
},
plugins: [
builderPluginSwc({
pluginSwc({
env: {
targets: 'ie 9',
mode: 'entry',
Expand All @@ -136,7 +136,7 @@ test('core-js-usage', async () => {
index: path.resolve(__dirname, './src/core-js-usage.ts'),
},
plugins: [
builderPluginSwc({
pluginSwc({
env: {
targets: 'ie 9',
mode: 'usage',
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/builder/cases/top-level-await/index.swc.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as path from 'path';
import { expect, test } from '@modern-js/e2e/playwright';
import { build, getHrefByEntryName } from '@scripts/shared';
import { builderPluginSwc } from '@modern-js/builder-plugin-swc';
import { pluginSwc } from '@rsbuild/plugin-swc';

test('should run top level await correctly when using SWC', async ({
page,
Expand All @@ -11,7 +11,7 @@ test('should run top level await correctly when using SWC', async ({
entry: {
index: path.resolve(__dirname, './src/index.ts'),
},
plugins: [builderPluginSwc()],
plugins: [pluginSwc()],
runServer: true,
});

Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
"vue": "^3.3.4"
},
"devDependencies": {
"@rsbuild/plugin-swc": "0.3.7",
"@rsbuild/shared": "0.3.7",
"@modern-js/uni-builder": "workspace:*",
"@modern-js/builder-plugin-swc": "workspace:*",
"@modern-js/e2e": "workspace:*",
"@modern-js/utils": "workspace:*",
"@types/node": "^14",
Expand Down
1 change: 0 additions & 1 deletion tests/integration/storybook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"@modern-js/builder-rspack-provider": "workspace:*",
"@modern-js/builder-webpack-provider": "workspace:*",
"@modern-js/storybook": "workspace:*",
"@modern-js/builder-plugin-swc": "workspace:*",
"@types/react": "^18",
"@types/react-dom": "^18",
"prop-types": "^15.8.1"
Expand Down

0 comments on commit b79abcf

Please sign in to comment.