feat(config): allow conditional config (#452)

Co-authored-by: EGOIST <0x142857@gmail.com>
This commit is contained in:
Tmk 2021-11-12 16:04:12 +08:00 committed by GitHub
parent 112fbf6158
commit 449d9e0ced
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 41 additions and 30 deletions

View File

@ -68,34 +68,35 @@ You can use any of these files:
[Check out all available options](https://github.com/egoist/tsup/blob/master/src/options.ts).
#### TypeScript
#### TypeScript / JavaScript
```ts
// tsup.config.ts
import type { Options } from 'tsup'
export const tsup: Options = {
import { defineConfig } from 'tsup'
export default defineConfig({
splitting: false,
sourcemap: true,
clean: true,
entryPoints: ['src/index.ts'],
}
})
```
#### JavaScript
#### Conditional config
```js
// tsup.config.cjs
/**
* @type {import("tsup").Options}
*/
module.exports = {
splitting: false,
sourcemap: true,
clean: true,
entryPoints: ['src/index.ts'],
}
If the config needs to be conditionally determined based on CLI flags, it can export a function instead:
```ts
import { defineConfig } from 'tsup'
export default defineConfig((options) => {
return {
minify: !options.watch,
}
})
```
The `options` here is derived from CLI flags.
#### package.json
```json
@ -290,14 +291,14 @@ The file outputs as `metafile-{format}.json`, e.g. `tsup --format cjs,esm` will
Use `esbuildPlugins` and `esbuildOptions` respectively in `tsup.config.ts`:
```ts
import { Options } from 'tsup'
import { defineConfig } from 'tsup'
export const tsup: Options = {
export default defineConfig({
esbuildPlugins: [YourPlugin],
esbuildOptions(options, context) {
options.define.foo = '"bar"'
},
}
})
```
The `context` argument for `esbuildOptions`:

View File

@ -73,7 +73,7 @@ export async function main(options: Options = {}) {
.option('--clean', 'Clean output directory')
.option(
'--silent',
'Supress non-error logs (excluding "onSuccess" process output)'
'Suppress non-error logs (excluding "onSuccess" process output)'
)
.option('--pure <express>', 'Mark specific expressions as pure')
.option('--metafile', 'Emit esbuild metafile (a JSON file)')

View File

@ -66,7 +66,7 @@ export const postcssPlugin = ({
}
}
// Tranform CSS
// Transform CSS
const result = await postcss
?.default(plugins)
.process(contents, { ...options, from: args.path })

View File

@ -54,7 +54,14 @@ const getOutputExtensionMap = (
return map
}
export const defineConfig = (options: Options) => options
export const defineConfig = (
options:
| Options
| ((
/** The options derived from CLI flags */
overrideOptions: Options
) => Options)
) => options
export async function runEsbuild(
options: NormalizedOptions,
@ -239,7 +246,7 @@ export async function runEsbuild(
)
}
}
// Workaound to enable code splitting for cjs format
// Workaround to enable code splitting for cjs format
// Manually transform esm to cjs
// TODO: remove this once esbuild supports code splitting for cjs natively
if (splitting && format === 'cjs') {
@ -282,7 +289,7 @@ const killProcess = ({
})
const normalizeOptions = async (
optionsFromConfigFile: Options,
optionsFromConfigFile: Options | undefined,
optionsOverride: Options
) => {
const options: Buildable<NormalizedOptions> = {
@ -344,7 +351,10 @@ const normalizeOptions = async (
export async function build(_options: Options) {
const config = await loadTsupConfig(process.cwd())
const options = await normalizeOptions(config.data, _options)
const configData =
typeof config.data === 'function' ? config.data(_options) : config.data
const options = await normalizeOptions(configData, _options)
log('CLI', 'info', `tsup v${version}`)

View File

@ -69,7 +69,7 @@ export type Options = {
esbuildPlugins?: EsbuildPlugin[]
esbuildOptions?: (options: BuildOptions, context: { format: Format }) => void
/**
* Supress non-error logs (excluding "onSuccess" process output)
* Suppress non-error logs (excluding "onSuccess" process output)
*/
silent?: boolean
/**

View File

@ -49,10 +49,10 @@ const findLowestCommonAncestor = (filepaths: string[]) => {
// See #316
const toObjectEntry = (entry: string[]) => {
entry = entry.map((e) => e.replace(/\\/g, '/'))
const ancester = findLowestCommonAncestor(entry)
const ancestor = findLowestCommonAncestor(entry)
return entry.reduce((result, item) => {
const key = item
.replace(ancester, '')
.replace(ancestor, '')
.replace(/^\//, '')
.replace(/\.[a-z]+$/, '')
return {

View File

@ -52,7 +52,7 @@ export const tsResolvePlugin: PluginImpl<TsResolveOptions> = ({
if (!shouldResolve) return null
}
// Skip absolut path
// Skip absolute path
if (path.isAbsolute(source)) return null
const basedir = importer ? path.dirname(importer) : process.cwd()