Skip to content

Commit

Permalink
Multiple package builds (#2201)
Browse files Browse the repository at this point in the history
* factor packages logic into build command

* Change comment

* Change docs

* Create itchy-geese-trade.md
  • Loading branch information
cristiano-belloni authored Nov 9, 2022
1 parent d9bed41 commit f79847a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .changeset/itchy-geese-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"modular-scripts": patch
---

Mention multiple package builds in the docs
8 changes: 3 additions & 5 deletions docs/commands/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ parent: Commands
title: modular build
---

# `modular build <packageName>`
# `modular build <packages...>`

Searches by the name of the package indicated in the `package.json` file in the
workspace and runs
[`react-scripts build`](https://create-react-app.dev/docs/production-build)
against that package.
Search workspaces based on their `name` field in the `package.json` and build
them according to their respective `modular.type`.

The output directory for built artifacts is `dist/`, which has a flat structure
of modular package names. Each built app/view/package is added to the `dist/` as
Expand Down
29 changes: 18 additions & 11 deletions packages/modular-scripts/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,23 +268,30 @@ async function buildStandalone(
}

async function build(
target: string,
targets: string[],
preserveModules = true,
includePrivate = false,
): Promise<void> {
const targetDirectory = await getLocation(target);
for (const target of targets) {
try {
const targetDirectory = await getLocation(target);

await setupEnvForDirectory(targetDirectory);
await setupEnvForDirectory(targetDirectory);

const targetType = getModularType(targetDirectory);
if (targetType === 'app' || targetType === 'esm-view') {
await buildStandalone(target, targetType);
} else {
const { buildPackage } = await import('./buildPackage');
// ^ we do a dynamic import here to defer the module's initial side effects
// till when it's actually needed (i.e. now)
const targetType = getModularType(targetDirectory);
if (targetType === 'app' || targetType === 'esm-view') {
await buildStandalone(target, targetType);
} else {
const { buildPackage } = await import('./buildPackage');
// ^ we do a dynamic import here to defer the module's loading
// till when it's actually needed

await buildPackage(target, preserveModules, includePrivate);
await buildPackage(target, preserveModules, includePrivate);
}
} catch (err) {
logger.error(`building ${target} failed`);
throw err;
}
}
}

Expand Down
17 changes: 5 additions & 12 deletions packages/modular-scripts/src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,11 @@ program
const { default: build } = await import('./build');
logger.log('building packages at:', packagePaths.join(', '));

for (let i = 0; i < packagePaths.length; i++) {
try {
await build(
packagePaths[i],
JSON.parse(options.preserveModules) as boolean,
options.private,
);
} catch (err) {
logger.error(`building ${packagePaths[i]} failed`);
throw err;
}
}
await build(
packagePaths,
JSON.parse(options.preserveModules) as boolean,
options.private,
);
},
);

Expand Down

0 comments on commit f79847a

Please sign in to comment.