diff --git a/scripts/build.ts b/scripts/build.ts index 3fd596c8a..1e5630d46 100644 --- a/scripts/build.ts +++ b/scripts/build.ts @@ -20,6 +20,7 @@ import { logError, cleanDistFolder, parseArgs, + flatten, } from "./utils"; import { ScriptOpts, NormalizedOpts } from "./types"; import * as fs from "fs-extra"; @@ -267,10 +268,10 @@ function createConfigItems(type: any, items: any[]) { function mergeConfigItems(type: any, ...configItemsToMerge: any[]) { const mergedItems: any[] = []; - configItemsToMerge.forEach(configItemToMerge => { + configItemsToMerge.forEach((configItemToMerge) => { configItemToMerge.forEach((item: any) => { const itemToMergeWithIndex = mergedItems.findIndex( - mergedItem => mergedItem.file.resolved === item.file.resolved + (mergedItem) => mergedItem.file.resolved === item.file.resolved ); if (itemToMergeWithIndex === -1) { @@ -307,13 +308,17 @@ if (process.env.NODE_ENV === 'production') { export async function createBuildConfigs( opts: NormalizedOpts ): Promise { - const allInputs = opts.input.flatMap((input: string) => - createAllFormats(opts, input).map((options: ScriptOpts, 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, - })) + const allInputs = flatten( + flatten(opts.input as any).map((input: string) => + createAllFormats(opts, input).map( + (options: ScriptOpts, 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( diff --git a/scripts/utils.ts b/scripts/utils.ts index 456b2e1fa..3c9eae8e7 100644 --- a/scripts/utils.ts +++ b/scripts/utils.ts @@ -61,13 +61,12 @@ export async function isDir(name: string) { export async function getInputs( entries?: string | string[] ): Promise { - return ([] as any[]) - .concat( - entries && entries.length - ? entries - : (await isDir(resolveApp("src"))) && (await jsOrTs("src/index")) - ) - .flatMap(file => glob(file)); + const inputs = ([] as any[]).concat( + entries && entries.length + ? entries + : (await isDir(resolveApp("src"))) && (await jsOrTs("src/index")) + ); + return flatten(inputs).map((file) => glob(file)); } export function getPackageName(opts: any) { @@ -93,8 +92,9 @@ export async function createProgressEstimator() { export function logError(err: any) { const error = err.error || err; - const description = `${error.name ? error.name + ": " : ""}${error.message || - error}`; + const description = `${error.name ? error.name + ": " : ""}${ + error.message || error + }`; const message = error.plugin ? error.plugin === "rpt2" ? `(typescript) ${description}` @@ -127,3 +127,7 @@ export function parseArgs() { let { _, ...args } = mri(process.argv.slice(2)); return args; } + +export function flatten(arr: any[][]): any[] { + return arr.reduce((flat, next) => flat.concat(next), []); +}