这是indexloc提供的服务,不要输入任何密码
Skip to content

fix: #893 #896

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/esbuild/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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:
Expand All @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,17 @@ 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];
}
}

options.es5TargetOutOfJson = options.target && options.target === 'es5';

const tsconfig = loadTsConfig(process.cwd(), options.tsconfig)
if (tsconfig) {
logger.info(
Expand Down
2 changes: 1 addition & 1 deletion src/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions src/plugins/es5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ export const es5 = (): Plugin => {
return {
name: 'es5-target',

esbuildOptions(options) {
if (options.target === 'es5') {
options.target = 'es2020'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still worries me. I think I will get an error from esbuild about it not supporting es5, rather than esbuild working with es2020 and then swc transpiling it back to es5?

Copy link
Author

@jerrywu001 jerrywu001 Apr 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know what you worry about. See here, the usage:

  • --target es5/tsup.target=es5 and tsconfig.compilerOptions.target: "es5" can not use at the same time.
  • can only use --target es5/tsup.target=es5 to compile the code down to es5.

@ComLock

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just tested your branch and it breaks ES5 Support as I expected

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was a pleasure to work with you. @ComLock

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neither target on cmdline, in tsup.config.json nor tsconfig.custom.json works. All gives es5 not supported by esbuild errors.

Copy link
Author

@jerrywu001 jerrywu001 Apr 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all the best, after git pull, you should run npm run build, then run npm run test-only . @ComLock

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to avoid the error I have to set esbuildOptions target: 'es2020' in addition to 'es5' (cmdline or tsup.config or tsconfig.custom)

Copy link
Author

@jerrywu001 jerrywu001 Apr 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to do this change @ComLock

it breaks the case
image

just this, it can pass all the cases
image

esbuildOptions() {
if (this.options.es5TargetOutOfJson) {
enabled = true
}
},
Expand Down
54 changes: 54 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
)
})