-
Notifications
You must be signed in to change notification settings - Fork 508
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(types/refactor): be more specific than 'any' in build code (#401)
- add types for all options and use those types throughout the build/watch code - fix some incorrect types in a few places that became more obvious or errors once types were added - fix default target from 'web' (not an option) to 'browser' - extract createBuildConfigs into a separate file as it's fairly long and has some relatively complex code - and refactor it a bit to make it a bit easier to read as well as easier to type (and type cast) Co-authored-by: Jared Palmer <jaredloganpalmer@gmail.com>
- Loading branch information
1 parent
a38041f
commit 158ee9a
Showing
3 changed files
with
145 additions
and
91 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { RollupOptions, OutputOptions } from 'rollup'; | ||
import * as fs from 'fs-extra'; | ||
import { concatAllArray } from 'jpjs'; | ||
|
||
import { paths } from './constants'; | ||
import { TsdxOptions, NormalizedOpts } from './types'; | ||
|
||
import { createRollupConfig } from './createRollupConfig'; | ||
|
||
// check for custom tsdx.config.js | ||
let tsdxConfig = { | ||
rollup(config: RollupOptions, _options: TsdxOptions): RollupOptions { | ||
return config; | ||
}, | ||
}; | ||
|
||
if (fs.existsSync(paths.appConfig)) { | ||
tsdxConfig = require(paths.appConfig); | ||
} | ||
|
||
export async function createBuildConfigs( | ||
opts: NormalizedOpts | ||
): Promise<Array<RollupOptions & { output: OutputOptions }>> { | ||
const allInputs = concatAllArray( | ||
opts.input.map((input: string) => | ||
createAllFormats(opts, input).map( | ||
(options: TsdxOptions, index: number) => ({ | ||
...options, | ||
// We want to know if this is the first run for each entryfile | ||
// for certain plugins (e.g. css) | ||
writeMeta: index === 0, | ||
}) | ||
) | ||
) | ||
); | ||
|
||
return await Promise.all( | ||
allInputs.map(async (options: TsdxOptions) => { | ||
// pass the full rollup config to tsdx.config.js override | ||
const config = await createRollupConfig(options); | ||
return tsdxConfig.rollup(config, options); | ||
}) | ||
); | ||
} | ||
|
||
function createAllFormats( | ||
opts: NormalizedOpts, | ||
input: string | ||
): [TsdxOptions, ...TsdxOptions[]] { | ||
return [ | ||
opts.format.includes('cjs') && { | ||
...opts, | ||
format: 'cjs', | ||
env: 'development', | ||
input, | ||
}, | ||
opts.format.includes('cjs') && { | ||
...opts, | ||
format: 'cjs', | ||
env: 'production', | ||
input, | ||
}, | ||
opts.format.includes('esm') && { ...opts, format: 'esm', input }, | ||
opts.format.includes('umd') && { | ||
...opts, | ||
format: 'umd', | ||
env: 'development', | ||
input, | ||
}, | ||
opts.format.includes('umd') && { | ||
...opts, | ||
format: 'umd', | ||
env: 'production', | ||
input, | ||
}, | ||
opts.format.includes('system') && { | ||
...opts, | ||
format: 'system', | ||
env: 'development', | ||
input, | ||
}, | ||
opts.format.includes('system') && { | ||
...opts, | ||
format: 'system', | ||
env: 'production', | ||
input, | ||
}, | ||
].filter(Boolean) as [TsdxOptions, ...TsdxOptions[]]; | ||
} |
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