From fdeede420228dfeaae5600c0bde812f40863e64c Mon Sep 17 00:00:00 2001 From: jerrywu Date: Fri, 28 Apr 2023 11:23:18 +0800 Subject: [PATCH 1/2] fix: #893 --- src/index.ts | 9 ++++++++ src/plugins/es5.ts | 2 +- test/index.test.ts | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 64e062d2..fbbafdc3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -98,6 +98,15 @@ const normalizeOptions = async ( logger.info('CLI', `Building entry: ${JSON.stringify(entry)}`) } + // set tsconfig path from esbuildOptions function raw + if (!options.tsconfig && options.esbuildOptions) { + const regex = /(\w+)\.tsconfig\s*=\s*['"]([^'"]*)['"]/; + const match = options.esbuildOptions.toString().match(regex); + if (match) { + options.tsconfig = match[2]; + } + } + const tsconfig = loadTsConfig(process.cwd(), options.tsconfig) if (tsconfig) { logger.info( diff --git a/src/plugins/es5.ts b/src/plugins/es5.ts index 5d1030c7..284fc939 100644 --- a/src/plugins/es5.ts +++ b/src/plugins/es5.ts @@ -9,7 +9,7 @@ export const es5 = (): Plugin => { esbuildOptions(options) { if (options.target === 'es5') { - options.target = 'es2020' + // options.target = 'es2020' enabled = true } }, diff --git a/test/index.test.ts b/test/index.test.ts index 9e7c71e8..b5d9be38 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -1266,4 +1266,58 @@ test('custom inject style function', async () => { expect(outFiles).toEqual(['input.js', 'input.mjs']) expect(await getFileContent('dist/input.mjs')).toContain('__custom_inject_style__(`.hello{color:red}\n`)') expect(await getFileContent('dist/input.js')).toContain('__custom_inject_style__(`.hello{color:red}\n`)') +}) + +test('set target by esbuildOptions', async () => { + await expect( + run( + getTestName(), + { + 'input.ts': `const foo = 'foo';export default foo;`, + 'tsup.config.ts': `export default { + esbuildOptions(options) { + options.tsconfig = 'custom.tsconfig.json'; + }, + }`, + 'custom.tsconfig.json': `{ + "compilerOptions": { + "target": "es5", + "module": "esnext" + } + }` + }, + { + flags: [ + '--format', + 'cjs', + // '--tsconfig', + // 'custom.tsconfig.json' + ], + } + ) + ).rejects.toThrowError( + `Transforming const to the configured target environment ("es5") is not supported yet` + ) +}) + +test('set target by default tsconfig.json', async () => { + await expect( + run( + getTestName(), + { + 'input.ts': `const foo = 'foo';export default foo;`, + 'tsconfig.json': `{ + "compilerOptions": { + "target": "es5", + "module": "esnext" + } + }` + }, + { + flags: [ '--format', 'cjs' ], + } + ) + ).rejects.toThrowError( + `Transforming const to the configured target environment ("es5") is not supported yet` + ) }) \ No newline at end of file From 003b4962a83a88d7adb866c8a00000d7efd9ade7 Mon Sep 17 00:00:00 2001 From: jerrywu Date: Fri, 28 Apr 2023 18:13:10 +0800 Subject: [PATCH 2/2] fix: update #893 --- src/esbuild/index.ts | 9 +++++++-- src/index.ts | 2 ++ src/load.ts | 2 +- src/options.ts | 2 ++ src/plugins/es5.ts | 5 ++--- 5 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/esbuild/index.ts b/src/esbuild/index.ts index a048d620..5955ef09 100644 --- a/src/esbuild/index.ts +++ b/src/esbuild/index.ts @@ -7,7 +7,7 @@ import { Plugin as EsbuildPlugin, } from 'esbuild' import { NormalizedOptions, Format } from '..' -import { getProductionDeps, loadPkg } from '../load' +import { getProductionDeps, loadJson, loadPkg } from '../load' import { Logger, getSilent } from '../log' import { nodeProtocolPlugin } from './node-protocol' import { externalPlugin } from './external' @@ -182,6 +182,11 @@ export async function runEsbuild( : options.footer try { + const tsconfigData = await loadJson(options.tsconfig as string); + const targetFromJson = tsconfigData?.compilerOptions.target; + // If --target=es5/tsup.target=es5 take effect, read target from tsconfig + const target = options.es5TargetOutOfJson ? targetFromJson : options.target + result = await esbuild({ entryPoints: options.entry, format: @@ -192,7 +197,7 @@ export async function runEsbuild( jsxFactory: options.jsxFactory, jsxFragment: options.jsxFragment, sourcemap: options.sourcemap ? 'external' : false, - target: options.target, + target, banner, footer, tsconfig: options.tsconfig, diff --git a/src/index.ts b/src/index.ts index fbbafdc3..b54a1cb6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -107,6 +107,8 @@ const normalizeOptions = async ( } } + options.es5TargetOutOfJson = options.target && options.target === 'es5'; + const tsconfig = loadTsConfig(process.cwd(), options.tsconfig) if (tsconfig) { logger.info( diff --git a/src/load.ts b/src/load.ts index bda258ec..d9d16374 100644 --- a/src/load.ts +++ b/src/load.ts @@ -7,7 +7,7 @@ import { jsoncParse } from './utils' const joycon = new JoyCon() -const loadJson = async (filepath: string) => { +export const loadJson = async (filepath: string) => { try { return jsoncParse(await fs.promises.readFile(filepath, 'utf8')) } catch (error) { diff --git a/src/options.ts b/src/options.ts index fc3c2afa..45b96719 100644 --- a/src/options.ts +++ b/src/options.ts @@ -100,6 +100,8 @@ export type Options = { * default to `node14` */ target?: Target | Target[] + /** set es5 target from command line/tsup target property? */ + es5TargetOutOfJson?: boolean; minify?: boolean | 'terser' terserOptions?: MinifyOptions minifyWhitespace?: boolean diff --git a/src/plugins/es5.ts b/src/plugins/es5.ts index 284fc939..10fb0f77 100644 --- a/src/plugins/es5.ts +++ b/src/plugins/es5.ts @@ -7,9 +7,8 @@ export const es5 = (): Plugin => { return { name: 'es5-target', - esbuildOptions(options) { - if (options.target === 'es5') { - // options.target = 'es2020' + esbuildOptions() { + if (this.options.es5TargetOutOfJson) { enabled = true } },