From 14b09067987d9b9350c9122f14d427c6c243fedc Mon Sep 17 00:00:00 2001 From: Fy <1114550440@qq.com> Date: Wed, 20 Sep 2023 11:14:42 +0800 Subject: [PATCH] fix(swc): transform core-js-pure incorrectly, allow using new decorator for js (#4680) --- .changeset/shiny-deers-live.md | 7 ++ packages/builder/plugin-swc/package.json | 2 +- .../extensions/lock-corejs-versions/actual.js | 7 ++ .../lock-corejs-versions/expected.js | 4 + .../lock-corejs-versions/option.json | 8 ++ .../libuild/libuild-plugin-swc/package.json | 2 +- pnpm-lock.yaml | 78 ++++++++++++------- .../decorator/legacy/modern.config.ts | 19 +++++ .../fixtures/decorator/legacy/package.json | 10 +++ .../fixtures/decorator/legacy/src/index.ts | 12 +++ .../fixtures/decorator/new/modern.config.ts | 19 +++++ .../swc/fixtures/decorator/new/package.json | 10 +++ .../swc/fixtures/decorator/new/src/index.js | 12 +++ tests/integration/swc/tests/decorator.test.ts | 36 +++++++++ 14 files changed, 194 insertions(+), 32 deletions(-) create mode 100644 .changeset/shiny-deers-live.md create mode 100644 packages/builder/plugin-swc/tests/fixtures/extensions/lock-corejs-versions/actual.js create mode 100644 packages/builder/plugin-swc/tests/fixtures/extensions/lock-corejs-versions/expected.js create mode 100644 packages/builder/plugin-swc/tests/fixtures/extensions/lock-corejs-versions/option.json create mode 100644 tests/integration/swc/fixtures/decorator/legacy/modern.config.ts create mode 100644 tests/integration/swc/fixtures/decorator/legacy/package.json create mode 100644 tests/integration/swc/fixtures/decorator/legacy/src/index.ts create mode 100644 tests/integration/swc/fixtures/decorator/new/modern.config.ts create mode 100644 tests/integration/swc/fixtures/decorator/new/package.json create mode 100644 tests/integration/swc/fixtures/decorator/new/src/index.js create mode 100644 tests/integration/swc/tests/decorator.test.ts diff --git a/.changeset/shiny-deers-live.md b/.changeset/shiny-deers-live.md new file mode 100644 index 000000000000..ac0122bc4e1f --- /dev/null +++ b/.changeset/shiny-deers-live.md @@ -0,0 +1,7 @@ +--- +'@modern-js/libuild-plugin-swc': patch +'@modern-js/builder-plugin-swc': patch +--- + +fix(swc): fix transform core-js-pure incorrectly, allow using new decorator for js +fix(swc): 修复误转换core-js-pure,对js允许使用新 decorator diff --git a/packages/builder/plugin-swc/package.json b/packages/builder/plugin-swc/package.json index b1fd193b4941..5df7d7f3d732 100644 --- a/packages/builder/plugin-swc/package.json +++ b/packages/builder/plugin-swc/package.json @@ -64,7 +64,7 @@ "@babel/preset-react": "^7.22.15", "@babel/preset-typescript": "^7.22.15", "@modern-js/builder-shared": "workspace:*", - "@modern-js/swc-plugins": "0.6.0", + "@modern-js/swc-plugins": "0.6.3", "@modern-js/utils": "workspace:*", "@swc/helpers": "0.5.1", "core-js": "~3.32.1" diff --git a/packages/builder/plugin-swc/tests/fixtures/extensions/lock-corejs-versions/actual.js b/packages/builder/plugin-swc/tests/fixtures/extensions/lock-corejs-versions/actual.js new file mode 100644 index 000000000000..9ed4dd5256f5 --- /dev/null +++ b/packages/builder/plugin-swc/tests/fixtures/extensions/lock-corejs-versions/actual.js @@ -0,0 +1,7 @@ +/** should be transformed */ +import 'core-js/foo/bar' +import '@swc/helpers/foo/bar' + +/** should stay as is */ +import 'core-js-pure/foo/bar' +import '@swc/helpers-custom/foo/bar' diff --git a/packages/builder/plugin-swc/tests/fixtures/extensions/lock-corejs-versions/expected.js b/packages/builder/plugin-swc/tests/fixtures/extensions/lock-corejs-versions/expected.js new file mode 100644 index 000000000000..1e8b5715c856 --- /dev/null +++ b/packages/builder/plugin-swc/tests/fixtures/extensions/lock-corejs-versions/expected.js @@ -0,0 +1,4 @@ +/** should be transformed */ import "/path/to/corejs/foo/bar"; +import "/path/to/helper/foo/bar"; +/** should stay as is */ import "-pure/foo/bar"; +import "-custom/foo/bar"; diff --git a/packages/builder/plugin-swc/tests/fixtures/extensions/lock-corejs-versions/option.json b/packages/builder/plugin-swc/tests/fixtures/extensions/lock-corejs-versions/option.json new file mode 100644 index 000000000000..6ddb90b57ebb --- /dev/null +++ b/packages/builder/plugin-swc/tests/fixtures/extensions/lock-corejs-versions/option.json @@ -0,0 +1,8 @@ +{ + "extensions": { + "lockCorejsVersion": { + "corejs": "/path/to/corejs", + "swcHelpers": "/path/to/helper" + } + } +} diff --git a/packages/libuild/libuild-plugin-swc/package.json b/packages/libuild/libuild-plugin-swc/package.json index 091b8f25394c..7e99036da9c7 100644 --- a/packages/libuild/libuild-plugin-swc/package.json +++ b/packages/libuild/libuild-plugin-swc/package.json @@ -21,7 +21,7 @@ "@modern-js/libuild-utils": "workspace:*" }, "dependencies": { - "@modern-js/swc-plugins": "0.6.0", + "@modern-js/swc-plugins": "0.6.3", "chalk": "4.1.0" }, "files": [ diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 37938f78abdc..81056e041dbd 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -617,8 +617,8 @@ importers: specifier: workspace:* version: link:../builder-shared '@modern-js/swc-plugins': - specifier: 0.6.0 - version: 0.6.0(@swc/helpers@0.5.1) + specifier: 0.6.3 + version: 0.6.3(@swc/helpers@0.5.1) '@modern-js/utils': specifier: workspace:* version: link:../../toolkit/utils @@ -3405,8 +3405,8 @@ importers: packages/libuild/libuild-plugin-swc: dependencies: '@modern-js/swc-plugins': - specifier: 0.6.0 - version: 0.6.0(@swc/helpers@0.5.1) + specifier: 0.6.3 + version: 0.6.3(@swc/helpers@0.5.1) chalk: specifier: 4.1.0 version: 4.1.0 @@ -8414,6 +8414,24 @@ importers: specifier: ^14 version: 14.18.35 + tests/integration/swc/fixtures/decorator/legacy: + devDependencies: + '@modern-js/app-tools': + specifier: workspace:* + version: link:../../../../../../packages/solutions/app-tools + '@modern-js/plugin-swc': + specifier: workspace:* + version: link:../../../../../../packages/cli/plugin-swc + + tests/integration/swc/fixtures/decorator/new: + devDependencies: + '@modern-js/app-tools': + specifier: workspace:* + version: link:../../../../../../packages/solutions/app-tools + '@modern-js/plugin-swc': + specifier: workspace:* + version: link:../../../../../../packages/cli/plugin-swc + tests/integration/swc/fixtures/minify-css: dependencies: '@modern-js/runtime': @@ -12568,8 +12586,8 @@ packages: - supports-color dev: false - /@modern-js/swc-plugins-darwin-arm64@0.6.0: - resolution: {integrity: sha512-LFJTagB66tv/BW21F+A1BlnhaCqQLjlvHCQfLvlV7n+UJz/FrTc+/BfcXnaj6gV2Opppof31eMb0V0ncyu/suA==} + /@modern-js/swc-plugins-darwin-arm64@0.6.3: + resolution: {integrity: sha512-LzBNgNl9CBeBK0BpyBCybXzpH4Jj95Kad/QjJkVy8gzdFISI6uTpTC/OG3+sGYquvITLjTA9HYNpR1QPS+YTyg==} engines: {node: '>=14.12'} cpu: [arm64] os: [darwin] @@ -12577,8 +12595,8 @@ packages: dev: false optional: true - /@modern-js/swc-plugins-darwin-x64@0.6.0: - resolution: {integrity: sha512-MOF3dQYyRyKbuEtMAdbVyuSKfM7XCWwdUMPlRikyoegMhgzsZE5TBpiefoRGviDqXG0Iko0SoMIoimkr+uQdcQ==} + /@modern-js/swc-plugins-darwin-x64@0.6.3: + resolution: {integrity: sha512-k1l3eIuuKFKsepC+yQEFtNS7SKs+0Y5O6Xl489+ifZnMFQ6EAgKx2mx0x9Fb6icFFdDfOv6fNspYuGIZ+5Phzg==} engines: {node: '>=14.12'} cpu: [x64] os: [darwin] @@ -12586,8 +12604,8 @@ packages: dev: false optional: true - /@modern-js/swc-plugins-linux-arm64-gnu@0.6.0: - resolution: {integrity: sha512-hi3RH0vxKcAvfsYXYyNhzkg9zBLog/p95p15FQQmqzPVhd1zg3JBCKjuP9lO0ZLSrVcjJzkOiQMsEuPxwp0YEw==} + /@modern-js/swc-plugins-linux-arm64-gnu@0.6.3: + resolution: {integrity: sha512-ds0NqYwYysayELzNE1b2uCPE6ZuhhGDEoQHt2czjKKtQ4yt7hLqEmkuSlYGjYuTm39yLl4/IKEMk7bI3TcuOGg==} engines: {node: '>=14.12'} cpu: [arm64] os: [linux] @@ -12595,8 +12613,8 @@ packages: dev: false optional: true - /@modern-js/swc-plugins-linux-arm64-musl@0.6.0: - resolution: {integrity: sha512-EBuBF11zTjBd1tn2KijQretTc80X/uDtxjXO8gLyZ9vNHK9QBuYP2BigupCdLDfgdWOBiVhNsnfwqWBHvrSUHA==} + /@modern-js/swc-plugins-linux-arm64-musl@0.6.3: + resolution: {integrity: sha512-YC/RMox7uGz+ZEHO4xfeDJHCpnwcBkOS2tNqLFkm/D+OotjEjvA9lLCSfaPF9D68XdmQCQCH6rJcqhdTidVa1g==} engines: {node: '>=14.12'} cpu: [arm64] os: [linux] @@ -12604,8 +12622,8 @@ packages: dev: false optional: true - /@modern-js/swc-plugins-linux-x64-gnu@0.6.0: - resolution: {integrity: sha512-Ysx5QUt7GjpBUqwD2NuFdgHUTdM3RyVo4xzkKdb/4F3rERD6JmYH127kjMrGwE5SCsCK493ZtRe6kcO8mabokQ==} + /@modern-js/swc-plugins-linux-x64-gnu@0.6.3: + resolution: {integrity: sha512-YJ9Xt6E0TbEckGCtj6+T1Ek4S3ljoOrdav9qp++X5FuaUzoT+DUf/pHIdnuWbxye1/6i4aF9z2kRIaOrZ6wpVA==} engines: {node: '>=14.12'} cpu: [x64] os: [linux] @@ -12613,8 +12631,8 @@ packages: dev: false optional: true - /@modern-js/swc-plugins-linux-x64-musl@0.6.0: - resolution: {integrity: sha512-Gd6XHoft+Ur7zOiRXTwDgZvHBcmLKxqG+bZHYTYQB7GehnveGNSha4tYbLgNDebJj64UPYH92IuGl7odlN1sUQ==} + /@modern-js/swc-plugins-linux-x64-musl@0.6.3: + resolution: {integrity: sha512-8MNk8trv48hSKpW6E8Xl3dZfG3L84k56GqfPv47EpkCTfR/gBJAdhyBnMwv5r6lXdvNTbd9qmvIQnMfQAwFG2g==} engines: {node: '>=14.12'} cpu: [x64] os: [linux] @@ -12622,8 +12640,8 @@ packages: dev: false optional: true - /@modern-js/swc-plugins-win32-arm64-msvc@0.6.0: - resolution: {integrity: sha512-rfPf1uJpgLA7UHJkWOsU1tSTU1YGt+gSQIocU0Fa5LXMvkT5cwZ4MfdCAbjMnxqQtPaHy3QDO02qe6QE2nvH4Q==} + /@modern-js/swc-plugins-win32-arm64-msvc@0.6.3: + resolution: {integrity: sha512-E8V6ZNp4tgZlzFFdIESdMRxPqYtTO5PEYm1ALunBcOp7lydUTSF58pC8dGnFMNpJ6HfH2jy1ORLlhpAwQ8zyKg==} engines: {node: '>=14.12'} cpu: [arm64] os: [win32] @@ -12631,8 +12649,8 @@ packages: dev: false optional: true - /@modern-js/swc-plugins-win32-x64-msvc@0.6.0: - resolution: {integrity: sha512-wPp537dlMPnWGSeO3nXLb3rcUNAa3VlSab1cpi3h+MIv1+0x0SSQ30/MBSu+VQOww6kr79DOh8iC6ODp6iyBog==} + /@modern-js/swc-plugins-win32-x64-msvc@0.6.3: + resolution: {integrity: sha512-7mKljY6MyidBHDlGmp0kEcSSvfhr9s+tOluVfSHn3U31QwIfVS4p9ByODnuJuAgffd3bZ02rblGzfQfY6mSWFw==} engines: {node: '>=14.12'} cpu: [x64] os: [win32] @@ -12640,22 +12658,22 @@ packages: dev: false optional: true - /@modern-js/swc-plugins@0.6.0(@swc/helpers@0.5.1): - resolution: {integrity: sha512-tMmrGcBDlPbemB4eJUXgdHBUMDozXaS3UMIP4UorQ+ImG7VwOKMEuLSN4FMd8YbH8WyGCEYtH8M3GOs5Rj/Cfg==} + /@modern-js/swc-plugins@0.6.3(@swc/helpers@0.5.1): + resolution: {integrity: sha512-BXUBfoGV+0Mzf7t1z2SsFuKcBUjHrp7Jn2C7Ax9pDVzC0+kcK55kHZJ20tgiIRoD3gTO5uHFbIaLK3snDc2NNg==} engines: {node: '>=14.17.6'} peerDependencies: '@swc/helpers': 0.5.1 dependencies: '@swc/helpers': 0.5.1 optionalDependencies: - '@modern-js/swc-plugins-darwin-arm64': 0.6.0 - '@modern-js/swc-plugins-darwin-x64': 0.6.0 - '@modern-js/swc-plugins-linux-arm64-gnu': 0.6.0 - '@modern-js/swc-plugins-linux-arm64-musl': 0.6.0 - '@modern-js/swc-plugins-linux-x64-gnu': 0.6.0 - '@modern-js/swc-plugins-linux-x64-musl': 0.6.0 - '@modern-js/swc-plugins-win32-arm64-msvc': 0.6.0 - '@modern-js/swc-plugins-win32-x64-msvc': 0.6.0 + '@modern-js/swc-plugins-darwin-arm64': 0.6.3 + '@modern-js/swc-plugins-darwin-x64': 0.6.3 + '@modern-js/swc-plugins-linux-arm64-gnu': 0.6.3 + '@modern-js/swc-plugins-linux-arm64-musl': 0.6.3 + '@modern-js/swc-plugins-linux-x64-gnu': 0.6.3 + '@modern-js/swc-plugins-linux-x64-musl': 0.6.3 + '@modern-js/swc-plugins-win32-arm64-msvc': 0.6.3 + '@modern-js/swc-plugins-win32-x64-msvc': 0.6.3 dev: false /@modern-js/utils@2.18.0(react-dom@18.2.0)(react@18.2.0): diff --git a/tests/integration/swc/fixtures/decorator/legacy/modern.config.ts b/tests/integration/swc/fixtures/decorator/legacy/modern.config.ts new file mode 100644 index 000000000000..f18051147e07 --- /dev/null +++ b/tests/integration/swc/fixtures/decorator/legacy/modern.config.ts @@ -0,0 +1,19 @@ +import appTools, { defineConfig } from '@modern-js/app-tools'; +import { swcPlugin } from '@modern-js/plugin-swc'; + +export default defineConfig({ + tools: { + swc: { + jsc: { + transform: { + /** ts cant use new decorator */ + legacyDecorator: false, + }, + }, + }, + }, + output: { + disableMinimize: true, + }, + plugins: [appTools(), swcPlugin()], +}); diff --git a/tests/integration/swc/fixtures/decorator/legacy/package.json b/tests/integration/swc/fixtures/decorator/legacy/package.json new file mode 100644 index 000000000000..c33ece311736 --- /dev/null +++ b/tests/integration/swc/fixtures/decorator/legacy/package.json @@ -0,0 +1,10 @@ +{ + "private": true, + "name": "swc-test-decorator-legacy", + "version": "2.9.0", + "dependencies": {}, + "devDependencies": { + "@modern-js/app-tools": "workspace:*", + "@modern-js/plugin-swc": "workspace:*" + } +} diff --git a/tests/integration/swc/fixtures/decorator/legacy/src/index.ts b/tests/integration/swc/fixtures/decorator/legacy/src/index.ts new file mode 100644 index 000000000000..7589b8ffefdb --- /dev/null +++ b/tests/integration/swc/fixtures/decorator/legacy/src/index.ts @@ -0,0 +1,12 @@ +function newDecorator() { + console.log('foo decorator'); +} + +class Foo { + @newDecorator() + foo() { + console.log('foo'); + } +} + +console.log(new Foo()); diff --git a/tests/integration/swc/fixtures/decorator/new/modern.config.ts b/tests/integration/swc/fixtures/decorator/new/modern.config.ts new file mode 100644 index 000000000000..3b4896c36640 --- /dev/null +++ b/tests/integration/swc/fixtures/decorator/new/modern.config.ts @@ -0,0 +1,19 @@ +import appTools, { defineConfig } from '@modern-js/app-tools'; +import { swcPlugin } from '@modern-js/plugin-swc'; + +export default defineConfig({ + tools: { + swc: { + jsc: { + transform: { + /** use new decorator */ + legacyDecorator: false, + }, + }, + }, + }, + output: { + disableMinimize: true, + }, + plugins: [appTools(), swcPlugin()], +}); diff --git a/tests/integration/swc/fixtures/decorator/new/package.json b/tests/integration/swc/fixtures/decorator/new/package.json new file mode 100644 index 000000000000..0ec11b6106c5 --- /dev/null +++ b/tests/integration/swc/fixtures/decorator/new/package.json @@ -0,0 +1,10 @@ +{ + "private": true, + "name": "swc-test-decorator", + "version": "2.9.0", + "dependencies": {}, + "devDependencies": { + "@modern-js/app-tools": "workspace:*", + "@modern-js/plugin-swc": "workspace:*" + } +} diff --git a/tests/integration/swc/fixtures/decorator/new/src/index.js b/tests/integration/swc/fixtures/decorator/new/src/index.js new file mode 100644 index 000000000000..7589b8ffefdb --- /dev/null +++ b/tests/integration/swc/fixtures/decorator/new/src/index.js @@ -0,0 +1,12 @@ +function newDecorator() { + console.log('foo decorator'); +} + +class Foo { + @newDecorator() + foo() { + console.log('foo'); + } +} + +console.log(new Foo()); diff --git a/tests/integration/swc/tests/decorator.test.ts b/tests/integration/swc/tests/decorator.test.ts new file mode 100644 index 000000000000..04560ed600cc --- /dev/null +++ b/tests/integration/swc/tests/decorator.test.ts @@ -0,0 +1,36 @@ +import path from 'path'; +import { readFileSync, readdirSync } from 'fs'; +import { modernBuild } from '../../../utils/modernTestUtils'; + +const fixtures = path.resolve(__dirname, '../fixtures'); + +const getJsFiles = (appDir: string) => + readdirSync(path.resolve(appDir, 'dist/static/js')) + .filter(filepath => /\.js$/.test(filepath)) + .map(filePath => + readFileSync(path.join(appDir, 'dist/static/js', filePath)), + ); + +describe('swc use new decorator', () => { + test('should use new decorator', async () => { + const appDir = path.resolve(fixtures, 'decorator/new'); + + await modernBuild(appDir); + + const jsFiles = getJsFiles(appDir); + expect( + jsFiles.some(item => item.includes('@swc/helpers/esm/_decorate.js')), + ).toBeTruthy(); + }); + + test('should use legacy decorator', async () => { + const appDir = path.resolve(fixtures, 'decorator/legacy'); + + await modernBuild(appDir); + + const jsFiles = getJsFiles(appDir); + expect( + jsFiles.every(item => !item.includes('@swc/helpers/esm/_decorate.js')), + ).toBeTruthy(); + }); +});