Skip to content

Commit

Permalink
Build > Rollup API [LG-3487, LG-3476] (#1902)
Browse files Browse the repository at this point in the history
* Uses rollup api

Removes spawn

* Adds NPM ignore

* Update .npmignore

* Create smooth-dolls-glow.md

* Delete build-package.spec.ts

* re-add terser

* Update Palette.story.tsx

* Update npmpackagejsonlintrc.config.js
  • Loading branch information
TheSonOfThomp authored Aug 4, 2023
1 parent 6a1495f commit c2908c5
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 46 deletions.
7 changes: 7 additions & 0 deletions .changeset/smooth-dolls-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@lg-tools/build': minor
'@lg-tools/cli': minor
---

Updates `build` package to use Rollup's JS API rather than spawning rollup using the command line. This gives us more control over the build, and we don't need to worry about the path of the `rollup` binary in consuming ap
plications
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.turbo
2 changes: 1 addition & 1 deletion packages/palette/src/Palette.story.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { isUndefined } from 'lodash';
import isUndefined from 'lodash/isUndefined';
import { darken, lighten, readableColor, transparentize } from 'polished';

import { css, cx } from '@leafygreen-ui/emotion';
Expand Down
2 changes: 1 addition & 1 deletion packages/tokens/src/Tokens.story.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable jsx-a11y/no-noninteractive-tabindex */
import React from 'react';
import { startCase } from 'lodash';
import startCase from 'lodash/startCase';

import { css } from '@leafygreen-ui/emotion';
import { StoryMetaType } from '@leafygreen-ui/lib';
Expand Down
2 changes: 1 addition & 1 deletion tools/build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"glob": "10.3.3",
"lodash": "4.17.21",
"react-docgen-typescript": "2.2.2",
"rollup": "3.25.1",
"rollup": "3.25.3",
"rollup-plugin-node-externals": "6.1.1",
"rollup-plugin-polyfill-node": "0.12.0"
},
Expand Down
32 changes: 0 additions & 32 deletions tools/build/src/rollup/build-package.spec.ts

This file was deleted.

49 changes: 43 additions & 6 deletions tools/build/src/rollup/build-package.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
/* eslint-disable no-console */
import chalk from 'chalk';
import { spawn } from 'cross-spawn';
import fse from 'fs-extra';
import path from 'path';
import rollup, { type MergedRollupOptions } from 'rollup';
import {
type BatchWarnings,
type LoadConfigFile,
} from 'rollup/dist/loadConfigFile';
// @ts-expect-error - type declaration incorrectly defined
import { loadConfigFile } from 'rollup/loadConfigFile';

interface BuildPackageOptions {
/**
Expand All @@ -17,7 +23,7 @@ interface BuildPackageOptions {
/**
* Builds packages using rollup for the current directory
*/
export function buildPackage({ direct }: BuildPackageOptions) {
export function buildPackage({ direct, verbose }: BuildPackageOptions) {
const packageDir = process.cwd();
const localRollupConfigPath = path.join(packageDir, 'rollup.config.mjs');
const defaultRollupConfigPath = path.join(
Expand Down Expand Up @@ -51,8 +57,39 @@ export function buildPackage({ direct }: BuildPackageOptions) {
);
}

spawn('rollup', ['--config', rollupConfigPath], {
cwd: packageDir,
stdio: 'inherit',
});
// load the rollup config file, and run rollup
(loadConfigFile as LoadConfigFile)(rollupConfigPath, {}).then(
async ({
options,
warnings,
}: {
options: Array<MergedRollupOptions>;
warnings: BatchWarnings;
}) => {
if (warnings.count > 0) {
if (verbose) {
// This prints all deferred warnings
warnings.flush();
} else {
console.log(
warnings.count + ' build warnings. Run with `--verbose` for more',
);
}
}

for (const optionsObj of options) {
const bundle = await rollup.rollup({
...optionsObj,
logLevel: verbose ? 'debug' : 'warn',
});
verbose &&
console.log(
`${chalk.bold(optionsObj.input)} > ${chalk.bold(
optionsObj.output.map(obj => obj.dir || obj.file),
)}`,
);
await Promise.all(optionsObj.output.map(bundle.write));
}
},
);
}
3 changes: 3 additions & 0 deletions tools/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,17 @@ cli
cli
.command('build-package')
.description('Builds a package')
.option('-v --verbose', 'Prints additional information to the console', false)
.action(buildPackage);
cli
.command('build-ts')
.description("Builds a package's TypeScript definitions")
.option('-v --verbose', 'Prints additional information to the console', false)
.action(buildTypescript);
cli
.command('build-tsdoc')
.description("Builds a package's TSDoc file")
.option('-v --verbose', 'Prints additional information to the console', false)
.action(buildTSDoc);

cli.parse(process.argv);
2 changes: 1 addition & 1 deletion tools/lint/config/npmpackagejsonlintrc.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = {
],
],
'prefer-caret-version-dependencies': [
'warning',
'off',
{ exceptions: ['highlight.js', 'prettier'] },
],
'valid-values-license': ['error', ['Apache-2.0']],
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14276,10 +14276,10 @@ [email protected]:
dependencies:
"@rollup/plugin-inject" "^5.0.1"

[email protected].1:
version "3.25.1"
resolved "https://registry.yarnpkg.com/rollup/-/rollup-3.25.1.tgz#9fff79d22ff1a904b2b595a2fb9bc3793cb987d8"
integrity sha512-tywOR+rwIt5m2ZAWSe5AIJcTat8vGlnPFAv15ycCrw33t6iFsXZ6mzHVFh2psSjxQPmI+xgzMZZizUAukBI4aQ==
[email protected].3:
version "3.25.3"
resolved "https://registry.yarnpkg.com/rollup/-/rollup-3.25.3.tgz#f9a8986f0f244bcfde2208da91ba46b8fd252551"
integrity sha512-ZT279hx8gszBj9uy5FfhoG4bZx8c+0A1sbqtr7Q3KNWIizpTdDEPZbV2xcbvHsnFp4MavCQYZyzApJ+virB8Yw==
optionalDependencies:
fsevents "~2.3.2"

Expand Down

0 comments on commit c2908c5

Please sign in to comment.