-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(swc): transform core-js-pure incorrectly, allow using new decorat…
…or for js (#4680)
- Loading branch information
Showing
14 changed files
with
194 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
packages/builder/plugin-swc/tests/fixtures/extensions/lock-corejs-versions/actual.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
4 changes: 4 additions & 0 deletions
4
packages/builder/plugin-swc/tests/fixtures/extensions/lock-corejs-versions/expected.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
8 changes: 8 additions & 0 deletions
8
packages/builder/plugin-swc/tests/fixtures/extensions/lock-corejs-versions/option.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extensions": { | ||
"lockCorejsVersion": { | ||
"corejs": "/path/to/corejs", | ||
"swcHelpers": "/path/to/helper" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
19
tests/integration/swc/fixtures/decorator/legacy/modern.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
tests/integration/swc/fixtures/decorator/legacy/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
12
tests/integration/swc/fixtures/decorator/legacy/src/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
tests/integration/swc/fixtures/decorator/new/modern.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:*" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |