Skip to content

Commit

Permalink
perf(core): reduce code execution in build
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Jun 11, 2020
1 parent 5017805 commit be34f6c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
20 changes: 9 additions & 11 deletions packages/core/src/build/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ export function getRollupConfig(
}: Package = new Package()
): RollupOptions[] {
const resolvePath = (...path: string[]) => resolve(rootDir, ...path)
input = input ? resolvePath(input) : entrypoint
if (!input && !binaries.length) return []

const name = basename(pkg.name.replace(suffix, ''))
const getFilenames = getNameFunction(rootDir, name)

Expand Down Expand Up @@ -89,20 +92,16 @@ export function getRollupConfig(
jsonPlugin(),
].concat(plugins)

input = input ? resolvePath(input) : entrypoint

if (!input && !binaries.length) return []

const defaultOutputs = [
{
...getFilenames(pkg.main),
format: 'cjs',
preferConst: true,
},
...includeIf(pkg.module && !dev, {
...getFilenames(pkg.module, '-es'),
...includeIf(!dev && pkg.module, pkgModule => ({
...getFilenames(pkgModule, '-es'),
format: 'es',
}),
})),
]

return [
Expand All @@ -119,16 +118,15 @@ export function getRollupConfig(
plugins: getPlugins(),
})
}),
...includeIf(
input,
...includeIf(input, input =>
defu({}, options, {
input,
output: defaultOutputs,
external,
plugins: getPlugins(),
})
),
...includeIf(pkg.types && input, {
...includeIf(pkg.types && input, input => ({
input,
output: {
file: resolvePath(pkg.types || ''),
Expand All @@ -143,6 +141,6 @@ export function getRollupConfig(
},
}),
],
}),
})),
]
}
7 changes: 6 additions & 1 deletion packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ export const includeDefinedProperties = <T extends Record<string, any>>(
Object.entries(options).filter(([_, value]) => value !== undefined)
) as ExcludeNullable<T>

export const includeIf = <T>(test: any, item: T) => (test ? [item] : [])
type NonFalsy<T> = T extends null | undefined | false ? never : T

export const includeIf = <T, I>(
test: T,
itemFactory: (outcome: NonFalsy<T>) => I
) => (test ? [itemFactory(test as any)] : [])

export const runInParallel = async <T, R extends any>(
items: T[],
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/utils/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ describe('includeDefinedProperties', definedPropertiesTest)
describe('includeIf', () => {
it('should return an empty array when falsy', () => {
const results = [
includeIf(false, 'test'),
includeIf(null, 'test'),
includeIf(undefined, 'test'),
includeIf(false, () => 'test'),
includeIf(null, () => 'test'),
includeIf(undefined, () => 'test'),
]
expect(results).toEqual([[], [], []])
})
it('should return the item in an array when true', () => {
const result = includeIf(true, 'test')
const result = includeIf(true, () => 'test')
expect(result).toEqual(['test'])
})
})
Expand Down

0 comments on commit be34f6c

Please sign in to comment.