From 1dbd86b73481520352bbc9bd24f365f052b3f67e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90=20Kevin=20Deng?= Date: Mon, 16 Oct 2023 20:47:47 +0800 Subject: [PATCH] feat: support es3 target (#965) --- src/index.ts | 4 +-- src/plugins/es5.ts | 52 ------------------------------- src/plugins/swc-target.ts | 65 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 54 deletions(-) delete mode 100644 src/plugins/es5.ts create mode 100644 src/plugins/swc-target.ts diff --git a/src/index.ts b/src/index.ts index 0eecc990..f6d4ce70 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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' @@ -256,7 +256,7 @@ export async function build(_options: Options) { }), cjsSplitting(), cjsInterop(), - es5(), + swcTarget(), sizeReporter(), terserPlugin({ minifyOptions: options.minify, diff --git a/src/plugins/es5.ts b/src/plugins/es5.ts deleted file mode 100644 index 99bd9d11..00000000 --- a/src/plugins/es5.ts +++ /dev/null @@ -1,52 +0,0 @@ -import { PrettyError } from '../errors' -import { Plugin } from '../plugin' -import { localRequire } from '../utils' - -export const es5 = (): Plugin => { - let enabled = false - return { - name: 'es5-target', - - esbuildOptions(options) { - if (options.target === 'es5') { - options.target = 'es2020' - enabled = true - } - }, - - async renderChunk(code, info) { - if (!enabled || !/\.(cjs|js)$/.test(info.path)) { - return - } - const swc: typeof import('@swc/core') = localRequire('@swc/core') - - if (!swc) { - throw new PrettyError( - '@swc/core is required for es5 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: 'es5', - 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, - } - }, - } -} diff --git a/src/plugins/swc-target.ts b/src/plugins/swc-target.ts new file mode 100644 index 00000000..25aa6d5a --- /dev/null +++ b/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, + } + }, + } +}