Skip to content

Commit

Permalink
feat: support es3 target (#965)
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Oct 16, 2023
1 parent 438ec47 commit 1dbd86b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 54 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Expand Up @@ -16,7 +16,7 @@ import { runEsbuild } from './esbuild'
import { shebang } from './plugins/shebang'
import { cjsSplitting } from './plugins/cjs-splitting'
import { PluginContainer } from './plugin'
import { es5 } from './plugins/es5'
import { swcTarget } from './plugins/swc-target'
import { sizeReporter } from './plugins/size-reporter'
import { treeShakingPlugin } from './plugins/tree-shaking'
import { copyPublicDir, isInPublicDir } from './lib/public-dir'
Expand Down Expand Up @@ -256,7 +256,7 @@ export async function build(_options: Options) {
}),
cjsSplitting(),
cjsInterop(),
es5(),
swcTarget(),
sizeReporter(),
terserPlugin({
minifyOptions: options.minify,
Expand Down
52 changes: 0 additions & 52 deletions src/plugins/es5.ts

This file was deleted.

65 changes: 65 additions & 0 deletions src/plugins/swc-target.ts
@@ -0,0 +1,65 @@
import { PrettyError } from '../errors'
import { Plugin } from '../plugin'
import { localRequire } from '../utils'

const TARGETS = ['es5', 'es3'] as const

export const swcTarget = (): Plugin => {
let enabled = false
let target: typeof TARGETS[number]

return {
name: 'swc-target',

esbuildOptions(options) {
if (
typeof options.target === 'string' &&
TARGETS.includes(options.target as any)
) {
target = options.target as any
options.target = 'es2020'
enabled = true
}
},

async renderChunk(code, info) {
if (!enabled || !/\.(cjs|js)$/.test(info.path) || this.format !== 'cjs') {
return
}
const swc: typeof import('@swc/core') = localRequire('@swc/core')

if (!swc) {
throw new PrettyError(
`@swc/core is required for ${target} target. Please install it with \`npm install @swc/core -D\``
)
}

const result = await swc.transform(code, {
filename: info.path,
sourceMaps: this.options.sourcemap,
minify: Boolean(this.options.minify),
jsc: {
target,
parser: {
syntax: 'ecmascript',
},
minify:
this.options.minify === true
? {
compress: false,
mangle: {
reserved: this.options.globalName
? [this.options.globalName]
: [],
},
}
: undefined,
},
})
return {
code: result.code,
map: result.map,
}
},
}
}

0 comments on commit 1dbd86b

Please sign in to comment.