Skip to content

Commit 38af6bd

Browse files
hannoeruegoistEGOIST
authored
feat: allow configure jsc options & use tsconfig build target (#23)
Co-authored-by: EGOIST <[email protected]> Co-authored-by: EGOIST <[email protected]>
1 parent aedabc4 commit 38af6bd

File tree

5 files changed

+63
-33
lines changed

5 files changed

+63
-33
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ Files to include in the transpilation process.
9494

9595
Files to exclude in the transpilation process.
9696

97+
### `options.jsc`
98+
99+
- Type: `object`
100+
101+
Custom [jsc](https://swc.rs/docs/configuration/compilation) options to merge with the default one.
102+
97103
## Sponsors
98104

99105
[![sponsors](https://sponsors-images.egoist.sh/sponsors.svg)](https://github.com/sponsors/egoist)

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
"@egoist/prettier-config": "1.0.0",
2424
"@rollup/pluginutils": "^4.1.1",
2525
"@swc/core": "^1.2.108",
26+
"@types/node": "^17.0.23",
27+
"defu": "^6.0.0",
2628
"esbuild": "0.14.9",
2729
"esbuild-register": "^3.1.2",
2830
"joycon": "^3.0.1",

pnpm-lock.yaml

+19-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import defu from 'defu'
12
import { createUnplugin } from 'unplugin'
23
import { createFilter, FilterPattern } from '@rollup/pluginutils'
34

@@ -36,7 +37,7 @@ export default createUnplugin(
3637

3738
const isTs = /\.tsx?$/.test(id)
3839

39-
const jsc: JscConfig = {
40+
let jsc: JscConfig = {
4041
parser: {
4142
syntax: isTs ? 'typescript' : 'ecmascript',
4243
},
@@ -68,6 +69,14 @@ export default createUnplugin(
6869
})
6970
}
7071

72+
if (compilerOptions.target) {
73+
jsc.target = compilerOptions.target
74+
}
75+
76+
if (options.jsc) {
77+
jsc = defu(options.jsc, jsc)
78+
}
79+
7180
const result = await transform(code, {
7281
filename: id,
7382
sourceMaps: true,

test/index.test.ts

+26-25
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import { test } from 'uvu'
2-
import assert from 'uvu/assert'
3-
import path from 'path'
4-
import swc from '../dist'
5-
import { rollup } from 'rollup'
1+
import { test } from "uvu"
2+
import assert from "uvu/assert"
3+
import path from "path"
4+
import swc from "../dist"
5+
import { rollup } from "rollup"
66

7-
const fixture = (...args: string[]) => path.join(__dirname, 'fixtures', ...args)
7+
const fixture = (...args: string[]) => path.join(__dirname, "fixtures", ...args)
88

9-
test('rollup', async () => {
9+
test("rollup", async () => {
1010
const bundle = await rollup({
11-
input: fixture('rollup/index.ts'),
11+
input: fixture("rollup/index.ts"),
1212
plugins: [
1313
swc.rollup({
1414
tsconfigFile: false,
@@ -17,8 +17,8 @@ test('rollup', async () => {
1717
})
1818

1919
const { output } = await bundle.generate({
20-
format: 'cjs',
21-
dir: fixture('rollup/dist'),
20+
format: "cjs",
21+
dir: fixture("rollup/dist"),
2222
})
2323

2424
assert.is(
@@ -34,24 +34,24 @@ exports.foo = foo;
3434
)
3535
})
3636

37-
test('read tsconfig', async () => {
37+
test("read tsconfig", async () => {
3838
const bundle = await rollup({
39-
input: fixture('read-tsconfig/index.tsx'),
39+
input: fixture("read-tsconfig/index.tsx"),
4040
plugins: [swc.rollup()],
4141
})
4242

4343
const { output } = await bundle.generate({
44-
format: 'cjs',
45-
dir: fixture('read-tsconfig/dist'),
44+
format: "cjs",
45+
dir: fixture("read-tsconfig/dist"),
4646
})
4747

4848
const code = output[0].code
49-
assert.match(code, 'customJsxFactory')
49+
assert.match(code, "customJsxFactory")
5050
})
5151

52-
test('custom swcrc', async () => {
52+
test("custom swcrc", async () => {
5353
const bundle = await rollup({
54-
input: fixture('custom-swcrc/index.tsx'),
54+
input: fixture("custom-swcrc/index.tsx"),
5555
plugins: [
5656
swc.rollup({
5757
tsconfigFile: false,
@@ -60,17 +60,17 @@ test('custom swcrc', async () => {
6060
})
6161

6262
const { output } = await bundle.generate({
63-
format: 'cjs',
64-
dir: fixture('custom-swcrc/dist'),
63+
format: "cjs",
64+
dir: fixture("custom-swcrc/dist"),
6565
})
6666

6767
const code = output[0].code
68-
assert.match(code, 'customPragma')
68+
assert.match(code, "customPragma")
6969
})
7070

71-
test('minify', async () => {
71+
test("minify", async () => {
7272
const bundle = await rollup({
73-
input: fixture('minify/index.ts'),
73+
input: fixture("minify/index.ts"),
7474
plugins: [
7575
swc.rollup({
7676
minify: true,
@@ -79,14 +79,15 @@ test('minify', async () => {
7979
})
8080

8181
const { output } = await bundle.generate({
82-
format: 'cjs',
83-
dir: fixture('minify/dist'),
82+
format: "cjs",
83+
dir: fixture("minify/dist"),
8484
})
8585

8686
const code = output[0].code
87+
console.log(code)
8788
assert.match(
8889
code,
89-
`var Foo1=function Foo(){_classCallCheck(this,Foo);this.a=1}`,
90+
`var Foo=function Foo(){_classCallCheck(this,Foo);this.a=1}`,
9091
)
9192
})
9293

0 commit comments

Comments
 (0)