From c2908c5aa23f393d3f52508491bbef6b90590056 Mon Sep 17 00:00:00 2001 From: Adam Thompson <2414030+TheSonOfThomp@users.noreply.github.com> Date: Fri, 4 Aug 2023 18:02:24 -0400 Subject: [PATCH] Build > Rollup API [LG-3487, LG-3476] (#1902) * 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 --- .changeset/smooth-dolls-glow.md | 7 +++ .npmignore | 1 + packages/palette/src/Palette.story.tsx | 2 +- packages/tokens/src/Tokens.story.tsx | 2 +- tools/build/package.json | 2 +- tools/build/src/rollup/build-package.spec.ts | 32 ------------ tools/build/src/rollup/build-package.ts | 49 ++++++++++++++++--- tools/cli/src/index.ts | 3 ++ .../config/npmpackagejsonlintrc.config.js | 2 +- yarn.lock | 8 +-- 10 files changed, 62 insertions(+), 46 deletions(-) create mode 100644 .changeset/smooth-dolls-glow.md create mode 100644 .npmignore delete mode 100644 tools/build/src/rollup/build-package.spec.ts diff --git a/.changeset/smooth-dolls-glow.md b/.changeset/smooth-dolls-glow.md new file mode 100644 index 0000000000..8010f7109b --- /dev/null +++ b/.changeset/smooth-dolls-glow.md @@ -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 diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000000..c72a4fc775 --- /dev/null +++ b/.npmignore @@ -0,0 +1 @@ +.turbo diff --git a/packages/palette/src/Palette.story.tsx b/packages/palette/src/Palette.story.tsx index 928fec9ebd..88cf26839c 100644 --- a/packages/palette/src/Palette.story.tsx +++ b/packages/palette/src/Palette.story.tsx @@ -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'; diff --git a/packages/tokens/src/Tokens.story.tsx b/packages/tokens/src/Tokens.story.tsx index 70db748c21..c7e3ab88e9 100644 --- a/packages/tokens/src/Tokens.story.tsx +++ b/packages/tokens/src/Tokens.story.tsx @@ -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'; diff --git a/tools/build/package.json b/tools/build/package.json index 7c3f005bd5..b9e126db17 100644 --- a/tools/build/package.json +++ b/tools/build/package.json @@ -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" }, diff --git a/tools/build/src/rollup/build-package.spec.ts b/tools/build/src/rollup/build-package.spec.ts deleted file mode 100644 index ef92a0d799..0000000000 --- a/tools/build/src/rollup/build-package.spec.ts +++ /dev/null @@ -1,32 +0,0 @@ -import xSpawn from 'cross-spawn'; -import path from 'path'; - -import { buildPackage } from './build-package'; - -type SpawnType = ReturnType; - -const spawnSpy = jest.spyOn(xSpawn, 'spawn'); -spawnSpy.mockImplementation((...args) => ({} as SpawnType)); - -describe('tools/build/build-package', () => { - afterEach(() => { - spawnSpy.mockClear(); - }); - - test('runs with no options', () => { - const defaultRollupConfigPath = path.join( - __dirname, // __dirname is `dist` - '../config/rollup.config.mjs', - ); - - buildPackage({}); - - expect(spawnSpy).toHaveBeenCalledWith( - 'rollup', - expect.arrayContaining(['--config', defaultRollupConfigPath]), - expect.objectContaining({ - stdio: 'inherit', - }), - ); - }); -}); diff --git a/tools/build/src/rollup/build-package.ts b/tools/build/src/rollup/build-package.ts index ed20225421..448d647809 100644 --- a/tools/build/src/rollup/build-package.ts +++ b/tools/build/src/rollup/build-package.ts @@ -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 { /** @@ -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( @@ -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; + 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)); + } + }, + ); } diff --git a/tools/cli/src/index.ts b/tools/cli/src/index.ts index ca7d24ea8e..50f4720156 100644 --- a/tools/cli/src/index.ts +++ b/tools/cli/src/index.ts @@ -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); diff --git a/tools/lint/config/npmpackagejsonlintrc.config.js b/tools/lint/config/npmpackagejsonlintrc.config.js index 4eaccc73b5..bb9b9564c2 100644 --- a/tools/lint/config/npmpackagejsonlintrc.config.js +++ b/tools/lint/config/npmpackagejsonlintrc.config.js @@ -16,7 +16,7 @@ module.exports = { ], ], 'prefer-caret-version-dependencies': [ - 'warning', + 'off', { exceptions: ['highlight.js', 'prettier'] }, ], 'valid-values-license': ['error', ['Apache-2.0']], diff --git a/yarn.lock b/yarn.lock index 5924a420c3..1a4cfde6c5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -14276,10 +14276,10 @@ rollup-plugin-polyfill-node@0.12.0: dependencies: "@rollup/plugin-inject" "^5.0.1" -rollup@3.25.1: - version "3.25.1" - resolved "https://registry.yarnpkg.com/rollup/-/rollup-3.25.1.tgz#9fff79d22ff1a904b2b595a2fb9bc3793cb987d8" - integrity sha512-tywOR+rwIt5m2ZAWSe5AIJcTat8vGlnPFAv15ycCrw33t6iFsXZ6mzHVFh2psSjxQPmI+xgzMZZizUAukBI4aQ== +rollup@3.25.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"