-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: respect esbuild minify config (#8754)
Co-authored-by: 翠 / green <[email protected]>
- Loading branch information
1 parent
8108b1b
commit 8b77695
Showing
5 changed files
with
285 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
184 changes: 184 additions & 0 deletions
184
packages/vite/src/node/__tests__/plugins/esbuild.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
import { describe, expect, test } from 'vitest' | ||
import type { ResolvedConfig, UserConfig } from '../../config' | ||
import { resolveEsbuildTranspileOptions } from '../../plugins/esbuild' | ||
|
||
describe('resolveEsbuildTranspileOptions', () => { | ||
test('resolve default', () => { | ||
const options = resolveEsbuildTranspileOptions( | ||
defineResolvedConfig({ | ||
build: { | ||
target: 'es2020', | ||
minify: 'esbuild' | ||
}, | ||
esbuild: { | ||
keepNames: true | ||
} | ||
}), | ||
'es' | ||
) | ||
expect(options).toEqual({ | ||
target: 'es2020', | ||
format: 'esm', | ||
keepNames: true, | ||
minify: true, | ||
treeShaking: true | ||
}) | ||
}) | ||
|
||
test('resolve esnext no minify', () => { | ||
const options = resolveEsbuildTranspileOptions( | ||
defineResolvedConfig({ | ||
build: { | ||
target: 'esnext', | ||
minify: false | ||
}, | ||
esbuild: { | ||
keepNames: true | ||
} | ||
}), | ||
'es' | ||
) | ||
expect(options).toEqual(null) | ||
}) | ||
|
||
test('resolve no minify', () => { | ||
const options = resolveEsbuildTranspileOptions( | ||
defineResolvedConfig({ | ||
build: { | ||
target: 'es2020', | ||
minify: false | ||
}, | ||
esbuild: { | ||
keepNames: true | ||
} | ||
}), | ||
'es' | ||
) | ||
expect(options).toEqual({ | ||
target: 'es2020', | ||
format: 'esm', | ||
keepNames: true, | ||
minify: false, | ||
minifyIdentifiers: false, | ||
minifySyntax: false, | ||
minifyWhitespace: false, | ||
treeShaking: false | ||
}) | ||
}) | ||
|
||
test('resolve es lib', () => { | ||
const options = resolveEsbuildTranspileOptions( | ||
defineResolvedConfig({ | ||
build: { | ||
minify: 'esbuild', | ||
lib: { | ||
entry: './somewhere.js' | ||
} | ||
}, | ||
esbuild: { | ||
keepNames: true | ||
} | ||
}), | ||
'es' | ||
) | ||
expect(options).toEqual({ | ||
target: undefined, | ||
format: 'esm', | ||
keepNames: true, | ||
minify: false, | ||
minifyIdentifiers: true, | ||
minifySyntax: true, | ||
minifyWhitespace: false, | ||
treeShaking: true | ||
}) | ||
}) | ||
|
||
test('resolve cjs lib', () => { | ||
const options = resolveEsbuildTranspileOptions( | ||
defineResolvedConfig({ | ||
build: { | ||
minify: 'esbuild', | ||
lib: { | ||
entry: './somewhere.js' | ||
} | ||
}, | ||
esbuild: { | ||
keepNames: true | ||
} | ||
}), | ||
'cjs' | ||
) | ||
expect(options).toEqual({ | ||
target: undefined, | ||
format: 'cjs', | ||
keepNames: true, | ||
minify: true, | ||
treeShaking: true | ||
}) | ||
}) | ||
|
||
test('resolve es lib with specific minify options', () => { | ||
const options = resolveEsbuildTranspileOptions( | ||
defineResolvedConfig({ | ||
build: { | ||
minify: 'esbuild', | ||
lib: { | ||
entry: './somewhere.js' | ||
} | ||
}, | ||
esbuild: { | ||
keepNames: true, | ||
minifyIdentifiers: true, | ||
minifyWhitespace: true | ||
} | ||
}), | ||
'es' | ||
) | ||
expect(options).toEqual({ | ||
target: undefined, | ||
format: 'esm', | ||
keepNames: true, | ||
minify: false, | ||
minifyIdentifiers: true, | ||
minifyWhitespace: false, | ||
treeShaking: true | ||
}) | ||
}) | ||
|
||
test('resolve cjs lib with specific minify options', () => { | ||
const options = resolveEsbuildTranspileOptions( | ||
defineResolvedConfig({ | ||
build: { | ||
minify: 'esbuild', | ||
lib: { | ||
entry: './somewhere.js' | ||
} | ||
}, | ||
esbuild: { | ||
keepNames: true, | ||
minifyIdentifiers: true, | ||
minifyWhitespace: true, | ||
treeShaking: true | ||
} | ||
}), | ||
'cjs' | ||
) | ||
expect(options).toEqual({ | ||
target: undefined, | ||
format: 'cjs', | ||
keepNames: true, | ||
minify: false, | ||
minifyIdentifiers: true, | ||
minifyWhitespace: true, | ||
treeShaking: true | ||
}) | ||
}) | ||
}) | ||
|
||
/** | ||
* Helper for `resolveEsbuildTranspileOptions` to created resolved config with types. | ||
* Note: The function only uses `build.target`, `build.minify` and `esbuild` options. | ||
*/ | ||
function defineResolvedConfig(config: UserConfig): ResolvedConfig { | ||
return config as any | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters