Skip to content

Commit

Permalink
fix(swc): transform core-js-pure incorrectly, allow using new decorat…
Browse files Browse the repository at this point in the history
…or for js (#4680)
  • Loading branch information
JSerFeng authored Sep 20, 2023
1 parent 24482a5 commit 14b0906
Show file tree
Hide file tree
Showing 14 changed files with 194 additions and 32 deletions.
7 changes: 7 additions & 0 deletions .changeset/shiny-deers-live.md
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion packages/builder/plugin-swc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** should be transformed */ import "/path/to/corejs/foo/bar";
import "/path/to/helper/foo/bar";
/** should stay as is */ import "<CORE_JS>-pure/foo/bar";
import "<SWC_HELPER>-custom/foo/bar";
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extensions": {
"lockCorejsVersion": {
"corejs": "/path/to/corejs",
"swcHelpers": "/path/to/helper"
}
}
}
2 changes: 1 addition & 1 deletion packages/libuild/libuild-plugin-swc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
78 changes: 48 additions & 30 deletions pnpm-lock.yaml

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

19 changes: 19 additions & 0 deletions tests/integration/swc/fixtures/decorator/legacy/modern.config.ts
Original file line number Diff line number Diff line change
@@ -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()],
});
10 changes: 10 additions & 0 deletions tests/integration/swc/fixtures/decorator/legacy/package.json
Original file line number Diff line number Diff line change
@@ -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:*"
}
}
12 changes: 12 additions & 0 deletions tests/integration/swc/fixtures/decorator/legacy/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function newDecorator() {
console.log('foo decorator');
}

class Foo {
@newDecorator()
foo() {
console.log('foo');
}
}

console.log(new Foo());
19 changes: 19 additions & 0 deletions tests/integration/swc/fixtures/decorator/new/modern.config.ts
Original file line number Diff line number Diff line change
@@ -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()],
});
10 changes: 10 additions & 0 deletions tests/integration/swc/fixtures/decorator/new/package.json
Original file line number Diff line number Diff line change
@@ -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:*"
}
}
12 changes: 12 additions & 0 deletions tests/integration/swc/fixtures/decorator/new/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function newDecorator() {
console.log('foo decorator');
}

class Foo {
@newDecorator()
foo() {
console.log('foo');
}
}

console.log(new Foo());
36 changes: 36 additions & 0 deletions tests/integration/swc/tests/decorator.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit 14b0906

Please sign in to comment.