Skip to content

Commit

Permalink
Merge pull request #588 from raunofreiberg/fix/codesandbox-ci-build
Browse files Browse the repository at this point in the history
fix(build): Avoid `Array.flatMap` to work with Node v10
  • Loading branch information
Chance Strickland authored May 20, 2020
2 parents 667b89b + 3805ad4 commit ee2aad8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
23 changes: 14 additions & 9 deletions scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
logError,
cleanDistFolder,
parseArgs,
flatten,
} from "./utils";
import { ScriptOpts, NormalizedOpts } from "./types";
import * as fs from "fs-extra";
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -307,13 +308,17 @@ if (process.env.NODE_ENV === 'production') {
export async function createBuildConfigs(
opts: NormalizedOpts
): Promise<RollupOptions[]> {
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(
Expand Down
22 changes: 13 additions & 9 deletions scripts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,12 @@ export async function isDir(name: string) {
export async function getInputs(
entries?: string | string[]
): Promise<string[]> {
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) {
Expand All @@ -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}`
Expand Down Expand Up @@ -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), []);
}

0 comments on commit ee2aad8

Please sign in to comment.