Skip to content

Commit

Permalink
feat: allow configure jsc options & use tsconfig build target (#23)
Browse files Browse the repository at this point in the history
Co-authored-by: EGOIST <[email protected]>
Co-authored-by: EGOIST <[email protected]>
  • Loading branch information
3 people authored Apr 12, 2022
1 parent aedabc4 commit 38af6bd
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 33 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ Files to include in the transpilation process.

Files to exclude in the transpilation process.

### `options.jsc`

- Type: `object`

Custom [jsc](https://swc.rs/docs/configuration/compilation) options to merge with the default one.

## Sponsors

[![sponsors](https://sponsors-images.egoist.sh/sponsors.svg)](https://github.com/sponsors/egoist)
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
"@egoist/prettier-config": "1.0.0",
"@rollup/pluginutils": "^4.1.1",
"@swc/core": "^1.2.108",
"@types/node": "^17.0.23",
"defu": "^6.0.0",
"esbuild": "0.14.9",
"esbuild-register": "^3.1.2",
"joycon": "^3.0.1",
Expand Down
26 changes: 19 additions & 7 deletions pnpm-lock.yaml

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

11 changes: 10 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import defu from 'defu'
import { createUnplugin } from 'unplugin'
import { createFilter, FilterPattern } from '@rollup/pluginutils'

Expand Down Expand Up @@ -36,7 +37,7 @@ export default createUnplugin(

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

const jsc: JscConfig = {
let jsc: JscConfig = {
parser: {
syntax: isTs ? 'typescript' : 'ecmascript',
},
Expand Down Expand Up @@ -68,6 +69,14 @@ export default createUnplugin(
})
}

if (compilerOptions.target) {
jsc.target = compilerOptions.target
}

if (options.jsc) {
jsc = defu(options.jsc, jsc)
}

const result = await transform(code, {
filename: id,
sourceMaps: true,
Expand Down
51 changes: 26 additions & 25 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { test } from 'uvu'
import assert from 'uvu/assert'
import path from 'path'
import swc from '../dist'
import { rollup } from 'rollup'
import { test } from "uvu"
import assert from "uvu/assert"
import path from "path"
import swc from "../dist"
import { rollup } from "rollup"

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

test('rollup', async () => {
test("rollup", async () => {
const bundle = await rollup({
input: fixture('rollup/index.ts'),
input: fixture("rollup/index.ts"),
plugins: [
swc.rollup({
tsconfigFile: false,
Expand All @@ -17,8 +17,8 @@ test('rollup', async () => {
})

const { output } = await bundle.generate({
format: 'cjs',
dir: fixture('rollup/dist'),
format: "cjs",
dir: fixture("rollup/dist"),
})

assert.is(
Expand All @@ -34,24 +34,24 @@ exports.foo = foo;
)
})

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

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

const code = output[0].code
assert.match(code, 'customJsxFactory')
assert.match(code, "customJsxFactory")
})

test('custom swcrc', async () => {
test("custom swcrc", async () => {
const bundle = await rollup({
input: fixture('custom-swcrc/index.tsx'),
input: fixture("custom-swcrc/index.tsx"),
plugins: [
swc.rollup({
tsconfigFile: false,
Expand All @@ -60,17 +60,17 @@ test('custom swcrc', async () => {
})

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

const code = output[0].code
assert.match(code, 'customPragma')
assert.match(code, "customPragma")
})

test('minify', async () => {
test("minify", async () => {
const bundle = await rollup({
input: fixture('minify/index.ts'),
input: fixture("minify/index.ts"),
plugins: [
swc.rollup({
minify: true,
Expand All @@ -79,14 +79,15 @@ test('minify', async () => {
})

const { output } = await bundle.generate({
format: 'cjs',
dir: fixture('minify/dist'),
format: "cjs",
dir: fixture("minify/dist"),
})

const code = output[0].code
console.log(code)
assert.match(
code,
`var Foo1=function Foo(){_classCallCheck(this,Foo);this.a=1}`,
`var Foo=function Foo(){_classCallCheck(this,Foo);this.a=1}`,
)
})

Expand Down

0 comments on commit 38af6bd

Please sign in to comment.