Skip to content

Commit

Permalink
feat: support custom config file with --config flag
Browse files Browse the repository at this point in the history
  • Loading branch information
EGOIST committed Jun 5, 2022
1 parent ce599ad commit 90f2d1a
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 12 deletions.
2 changes: 2 additions & 0 deletions docs/README.md
Expand Up @@ -84,6 +84,8 @@ You can use any of these files:

> INFO: In all the custom files you can export the options either as `tsup`, `default` or `module.exports =`
You can also specify a custom filename using the `--config` flag, or passing `--no-config` to disable config files.

[Check out all available options](https://paka.dev/npm/tsup#module-index-export-Options).

#### TypeScript / JavaScript
Expand Down
1 change: 1 addition & 0 deletions src/cli-main.ts
Expand Up @@ -78,6 +78,7 @@ export async function main(options: Options = {}) {
default: 'node',
})
.option('--loader <ext=loader>', 'Specify the loader for a file extension')
.option('--config <filename>', 'Use a custom config file')
.option('--no-config', 'Disable config file')
.option('--shims', 'Enable cjs and esm shims')
.option('--inject-style', 'Inject style tag to document head')
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Expand Up @@ -115,7 +115,12 @@ const normalizeOptions = async (

export async function build(_options: Options) {
const config =
_options.config === false ? {} : await loadTsupConfig(process.cwd())
_options.config === false
? {}
: await loadTsupConfig(
process.cwd(),
_options.config === true ? undefined : _options.config
)

const configData =
typeof config.data === 'function'
Expand Down
21 changes: 12 additions & 9 deletions src/load.ts
Expand Up @@ -33,18 +33,21 @@ const jsonLoader = {
joycon.addLoader(jsonLoader)

export async function loadTsupConfig(
cwd: string
cwd: string,
configFile?: string
): Promise<{ path?: string; data?: ReturnType<typeof defineConfig> }> {
const configJoycon = new JoyCon()
const configPath = await configJoycon.resolve({
files: [
'tsup.config.ts',
'tsup.config.js',
'tsup.config.cjs',
'tsup.config.mjs',
'tsup.config.json',
'package.json',
],
files: configFile
? [configFile]
: [
'tsup.config.ts',
'tsup.config.js',
'tsup.config.cjs',
'tsup.config.mjs',
'tsup.config.json',
'package.json',
],
cwd,
stopDir: path.parse(cwd).root,
packageKey: 'tsup',
Expand Down
3 changes: 2 additions & 1 deletion src/options.ts
Expand Up @@ -142,8 +142,9 @@ export type Options = {
loader?: Record<string, Loader>
/**
* Disable config file with `false`
* Or pass a custom config filename
*/
config?: boolean
config?: boolean | string
/**
* Use a custom tsconfig
*/
Expand Down
21 changes: 21 additions & 0 deletions test/index.test.ts
Expand Up @@ -886,3 +886,24 @@ test('custom output extension', async () => {
]
`)
})

test('custom config file', async () => {
const { outFiles } = await run(
getTestName(),
{
'input.ts': `export const foo = [1,2,3]`,
'custom.config.ts': `export default {
format: ['esm']
}`,
},
{
entry: ['input.ts'],
flags: ['--config', 'custom.config.ts'],
}
)
expect(outFiles).toMatchInlineSnapshot(`
[
"input.mjs",
]
`)
})
3 changes: 2 additions & 1 deletion tsconfig.json
Expand Up @@ -8,7 +8,8 @@
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
"declaration": true /* Generates corresponding '.d.ts' file. */,
"declarationDir": "dist",
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
Expand Down

0 comments on commit 90f2d1a

Please sign in to comment.