Skip to content

Commit

Permalink
refactor!: don't resolve tsconfig (#370)
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
- `target` now defaults to `es2020`
- Required esbuild version should be 0.18 and above
- For tsconfig, switch to use the built-in strategy of esbuild. (For example, esbuild v0.18 and later will no longer follow `target` in `tsconfig.json`). Read more in [esbuild changelog](https://github.com/evanw/esbuild/releases/tag/v0.18.0)
  • Loading branch information
sxzz committed Sep 20, 2023
1 parent c8caef9 commit cfdeb28
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 76 deletions.
7 changes: 7 additions & 0 deletions example/class.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class A {
foo = 'bar'
num
constructor() {
this.num ||= 2
}
}
3 changes: 2 additions & 1 deletion example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ import * as Vue from 'vue'
import React from 'react'
import * as Three from 'three'
import * as _ from 'lodash'
import * as clz from './class'

export { Foo, Vue, React, Three, _ }
export { Foo, Vue, React, Three, _, clz }
5 changes: 5 additions & 0 deletions example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"compilerOptions": {
"useDefineForClassFields": false
}
}
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@
"@rollup/pluginutils": "^5.0.4",
"debug": "^4.3.4",
"es-module-lexer": "^1.3.1",
"joycon": "^3.1.1",
"jsonc-parser": "^3.2.0"
"joycon": "^3.1.1"
},
"peerDependencies": {
"esbuild": ">=0.10.1",
"esbuild": ">=0.18.0",
"rollup": "^1.20.0 || ^2.0.0 || ^3.0.0"
},
"engines": {
Expand Down
4 changes: 1 addition & 3 deletions pnpm-lock.yaml

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

22 changes: 9 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { existsSync, statSync } from 'fs'
import { extname, resolve, dirname, join } from 'path'
import { Plugin as RollupPlugin } from 'rollup'
import { readFile } from 'fs/promises'
import { transform, Loader, TransformOptions } from 'esbuild'
import { MarkOptional } from 'ts-essentials'
import { createFilter, FilterPattern } from '@rollup/pluginutils'
import createDebug from 'debug'
import { getOptions } from './options'
import { minify, getRenderChunk } from './minify'
import { warn } from './warn'
import {
optimizeDeps as doOptimizeDeps,
OptimizeDepsOptions,
OptimizeDepsResult,
} from './optimizer/optmize-deps'
import { getTsconfig } from './tsconfig'

export { minify }

Expand All @@ -25,10 +26,7 @@ const defaultLoaders: { [ext: string]: Loader } = {
'.tsx': 'tsx',
}

export type Options = Omit<
TransformOptions,
'sourcemap' | 'loader' | 'tsconfigRaw'
> & {
export type Options = Omit<TransformOptions, 'sourcemap' | 'loader'> & {
include?: FilterPattern
exclude?: FilterPattern
sourceMap?: boolean
Expand Down Expand Up @@ -152,19 +150,17 @@ export default ({
return null
}

const defaultOptions =
tsconfig === false ? {} : await getOptions(dirname(id), tsconfig)
const tsconfigRaw =
tsconfig === false
? undefined
: await getTsconfig(dirname(id), tsconfig)

const result = await transform(code, {
loader,
target: defaultOptions.target || 'es2017',
jsxFactory: defaultOptions.jsxFactory,
jsxFragment: defaultOptions.jsxFragment,
jsx: defaultOptions.jsx,
// Compat: passing this option in older esbuild version will result in error
...(defaultOptions.jsxDev ? { jsxDev: true } : {}),
sourcemap: sourceMap,
sourcefile: id,
tsconfigRaw,
target: 'es2020',
...esbuildOptions,
})

Expand Down
56 changes: 0 additions & 56 deletions src/options.ts

This file was deleted.

18 changes: 18 additions & 0 deletions src/tsconfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { readFile } from 'fs/promises'
import JoyCon from 'joycon'

const joycon = new JoyCon()

joycon.addLoader({
test: /\.json$/,
load: (file) => readFile(file, 'utf8'),
})

export const getTsconfig = async (
cwd: string,
tsconfig?: string,
): Promise<string> => {
// This call is cached
const { data } = await joycon.load([tsconfig || 'tsconfig.json'], cwd)
return data
}

0 comments on commit cfdeb28

Please sign in to comment.