Skip to content

Commit

Permalink
new: Add new build analyze command.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Oct 26, 2020
1 parent c6fe0bc commit a40715a
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 21 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,10 @@
"rimraf": "^3.0.2",
"rollup": "^2.32.1",
"rollup-plugin-node-externals": "^2.2.0",
"rollup-plugin-visualizer": "^4.1.2",
"semver": "^7.3.2",
"spdx-license-list": "^6.3.0",
"string-hash": "^1.1.3",
"typescript": "^4.0.3"
}
}
7 changes: 6 additions & 1 deletion src/Artifact.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fs from 'fs-extra';
import { applyStyle } from '@boost/cli';
import { Path } from '@boost/common';
import Package from './Package';
Expand All @@ -8,6 +9,8 @@ export default abstract class Artifact<T extends object = {}> {

readonly buildResult: BuildResult = { time: 0 };

readonly filesToCleanup: string[] = [];

readonly package: Package;

state: ArtifactState = 'pending';
Expand All @@ -17,7 +20,9 @@ export default abstract class Artifact<T extends object = {}> {
this.builds = builds;
}

cleanup(): Awaitable {}
async cleanup(): Promise<void> {
await Promise.all(this.filesToCleanup.map((file) => fs.remove(file)));
}

build(options: BuildOptions): Awaitable {}

Expand Down
7 changes: 5 additions & 2 deletions src/BundleArtifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,13 @@ export default class BundleArtifact extends Artifact<BundleBuild> {
};
}

async build(): Promise<void> {
async build(options: BuildOptions): Promise<void> {
debug('Building %s bundle artifact with Rollup', this.outputName);

const { output = [], ...input } = getRollupConfig(this, this.package.getFeatureFlags());
const features = this.package.getFeatureFlags();
features.analyze = options.analyzeBundle;

const { output = [], ...input } = getRollupConfig(this, features);
const bundle = await rollup({
...input,
onwarn: ({ id, loc = {}, message }) => {
Expand Down
2 changes: 2 additions & 0 deletions src/Packemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
PackemonPackageConfig,
Platform,
TypesBuild,
AnalyzeType,
} from './types';

const debug = createDebugger('packemon:core');
Expand Down Expand Up @@ -78,6 +79,7 @@ export default class Packemon {
const options = optimal(baseOptions, {
addEngines: bool(),
addExports: bool(),
analyzeBundle: string('treemap').oneOf<AnalyzeType>(['sunburst', 'treemap', 'network']),
checkLicenses: bool(),
concurrency: number(1).gte(1),
generateDeclaration: bool(),
Expand Down
11 changes: 1 addition & 10 deletions src/TypesArtifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,6 @@ const extractorConfig = require(path.join(__dirname, '../api-extractor.json')) a
};

export default class TypesArtifact extends Artifact<TypesBuild> {
private apiExtractorConfigPaths: string[] = [];

async cleanup(): Promise<void> {
debug('Cleaning up temporary API extractor config files');

// Absolute paths to each temporary config file
await Promise.all(this.apiExtractorConfigPaths.map((cfgPath) => fs.remove(cfgPath)));
}

async build(): Promise<void> {
debug('Building types artifact with TypeScript');

Expand Down Expand Up @@ -143,7 +134,7 @@ export default class TypesArtifact extends Artifact<TypesBuild> {
}

// Enqueue to remove the config file
this.apiExtractorConfigPaths.push(configPath);
this.filesToCleanup.push(configPath);

return result;
}
Expand Down
8 changes: 7 additions & 1 deletion src/commands/Build.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import os from 'os';
import { Arg, Command, Config, GlobalOptions } from '@boost/cli';
import Build from '../components/Build';
import Packemon from '../Packemon';
import { BuildOptions } from '../types';
import { AnalyzeType, BuildOptions } from '../types';

export type BuildParams = [string];

Expand All @@ -16,6 +16,11 @@ export class BuildCommand extends Command<GlobalOptions & BuildOptions, BuildPar
@Arg.Flag('Add `exports` fields to each `package.json`')
addExports: boolean = false;

@Arg.String('Visualize and analyze your generated builds', {
choices: ['sunburst', 'treemap', 'network'],
})
analyze: AnalyzeType = 'treemap';

@Arg.Flag('Check that packages have a valid `license` field')
checkLicenses: boolean = false;

Expand All @@ -42,6 +47,7 @@ export class BuildCommand extends Command<GlobalOptions & BuildOptions, BuildPar
packemon={new Packemon(cwd)}
addEngines={this.addEngines}
addExports={this.addExports}
analyzeBundle={this.analyze}
checkLicenses={this.checkLicenses}
concurrency={this.concurrency}
generateDeclaration={this.generateDeclaration}
Expand Down
23 changes: 22 additions & 1 deletion src/rollup/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import path from 'path';
import hash from 'string-hash';
import { RollupOptions, OutputOptions, ModuleFormat } from 'rollup';
import externals from 'rollup-plugin-node-externals';
import visualizer from 'rollup-plugin-visualizer';
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import { getBabelInputPlugin, getBabelOutputPlugin } from '@rollup/plugin-babel';
Expand Down Expand Up @@ -79,6 +81,25 @@ export function getRollupConfig(artifact: BundleArtifact, features: FeatureFlags
treeshake: true,
};

// Analyze the bundle for debugging purposes
if (features.analyze) {
const title = `${artifact.package.getName()} / ${artifact.getLabel()}`;
const filename = `stats-${hash(title)}.html`;

artifact.filesToCleanup.push(artifact.package.project.root.append(filename).path());

config.plugins!.push(
visualizer({
filename,
gzipSize: true,
open: true,
sourcemap: true,
template: features.analyze,
title,
}),
);
}

// Add an output for each format
config.output = artifact.builds.map((build) => {
const { format, platform, support } = build;
Expand Down Expand Up @@ -108,7 +129,7 @@ export function getRollupConfig(artifact: BundleArtifact, features: FeatureFlags
}),
],
// Only enable source maps for browsers
sourcemap: platform === 'browser',
sourcemap: Boolean(features.analyze) || platform === 'browser',
sourcemapExcludeSources: true,
};

Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export type NodeFormat =

export type Format = NodeFormat | BrowserFormat;

export type AnalyzeType = 'sunburst' | 'treemap' | 'network';

// PACKAGES

export interface PackemonPackageConfig {
Expand Down Expand Up @@ -62,6 +64,7 @@ export type ArtifactState = 'pending' | 'building' | 'passed' | 'failed';
export interface BuildOptions {
addEngines: boolean;
addExports: boolean;
analyzeBundle: AnalyzeType;
checkLicenses: boolean;
concurrency: number;
generateDeclaration: boolean;
Expand Down Expand Up @@ -89,6 +92,7 @@ export interface TypesBuild {
// CONFIG

export interface FeatureFlags {
analyze?: AnalyzeType;
decorators?: boolean;
flow?: boolean;
react?: boolean;
Expand Down
16 changes: 16 additions & 0 deletions types/rollup-plugin-visualizer.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
declare module 'rollup-plugin-visualizer' {
import { Plugin } from 'rollup';

interface PluginVisualizerOptions {
filename?: string;
gzipSize?: boolean;
open?: boolean;
sourcemap?: boolean;
template?: string;
title?: string;
}

function visualizer(options?: PluginVisualizerOptions): Plugin;

export = visualizer;
}
3 changes: 3 additions & 0 deletions types/string-hash.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module 'string-hash' {
export default function hash(value: string): string;
}
72 changes: 66 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2729,6 +2729,15 @@ cliui@^6.0.0:
strip-ansi "^6.0.0"
wrap-ansi "^6.2.0"

cliui@^7.0.2:
version "7.0.3"
resolved "https://registry.yarnpkg.com/cliui/-/cliui-7.0.3.tgz#ef180f26c8d9bff3927ee52428bfec2090427981"
integrity sha512-Gj3QHTkVMPKqwP3f7B4KPkBZRMR9r4rfi5bXFpg1a+Svvj8l7q5CnkBkVQzfxT5DFSsGk2+PascOgL0JYkL2kw==
dependencies:
string-width "^4.2.0"
strip-ansi "^6.0.0"
wrap-ansi "^7.0.0"

co@^4.6.0:
version "4.6.0"
resolved "https://registry.npmjs.org/co/-/co-4.6.0.tgz#6ea6bdf3d853ae54ccb8e47bfa0bf3f9031fb184"
Expand Down Expand Up @@ -3210,10 +3219,10 @@ es-to-primitive@^1.2.1:
is-date-object "^1.0.1"
is-symbol "^1.0.2"

escalade@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/escalade/-/escalade-3.0.2.tgz#6a580d70edb87880f22b4c91d0d56078df6962c4"
integrity sha512-gPYAU37hYCUhW5euPeR+Y74F7BL+IBsV93j5cvGriSaD1aG6MGsqsV1yamRdrWrb2j3aiZvb0X+UBOWpx3JWtQ==
escalade@^3.0.2, escalade@^3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/escalade/-/escalade-3.1.1.tgz#d8cfdc7000965c5a0174b4a82eaa5c0552742e40"
integrity sha512-k0er2gUkLf8O0zKJiAhmkTnJlTvINGv7ygDNPbeIsX/TJjGJZHuh9B2UxbsaEkmlEo9MfhrSzmhIlhRlI2GXnw==

escape-string-regexp@^1.0.5:
version "1.0.5"
Expand Down Expand Up @@ -3908,7 +3917,7 @@ gensync@^1.0.0-beta.1:
resolved "https://registry.yarnpkg.com/gensync/-/gensync-1.0.0-beta.1.tgz#58f4361ff987e5ff6e1e7a210827aa371eaac269"
integrity sha512-r8EC6NO1sngH/zdD9fiRDLdcgnbayXah+mLgManTaIZJqEC1MZstmnox8KpnI2/fxQwrp5OpCOYWLp4rBl4Jcg==

get-caller-file@^2.0.1:
get-caller-file@^2.0.1, get-caller-file@^2.0.5:
version "2.0.5"
resolved "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz#4f94412a82db32f36e3b0b9741f8a97feb031f7e"
integrity sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==
Expand Down Expand Up @@ -4529,7 +4538,7 @@ is-windows@^1.0.2:
resolved "https://registry.npmjs.org/is-windows/-/is-windows-1.0.2.tgz#d1850eb9791ecd18e6182ce12a30f396634bb19d"
integrity sha512-eXK1UInq2bPmjyX6e3VHIzMLobc4J94i4AWn+Hpq3OU5KkrRC96OAcR3PRJ/pGu6m8TRnBHP9dkXQVsT/COVIA==

is-wsl@^2.2.0:
is-wsl@^2.1.1, is-wsl@^2.2.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/is-wsl/-/is-wsl-2.2.0.tgz#74a4c76e77ca9fd3f932f290c17ea326cd157271"
integrity sha512-fKzAra0rGJUUBwGBgNkHZuToZcn+TtXHpeCgmkMJMMYx1sQDYaCSyjJBSCa2nH1DGm7s3n1oBnohoVTBaN7Lww==
Expand Down Expand Up @@ -5481,6 +5490,11 @@ nan@^2.12.1:
resolved "https://registry.npmjs.org/nan/-/nan-2.14.0.tgz#7818f722027b2459a86f0295d434d1fc2336c52c"
integrity sha512-INOFj37C7k3AfaNTtX8RhsTw7qRy7eLET14cROi9+5HAVbbHuIWUHEauBv5qT4Av2tWasiTY1Jw6puUNqRJXQg==

nanoid@^3.0.1:
version "3.1.16"
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.1.16.tgz#b21f0a7d031196faf75314d7c65d36352beeef64"
integrity sha512-+AK8MN0WHji40lj8AEuwLOvLSbWYApQpre/aFJZD71r43wVRLrOYS4FmJOPQYon1TqB462RzrrxlfA74XRES8w==

nanomatch@^1.2.9:
version "1.2.13"
resolved "https://registry.npmjs.org/nanomatch/-/nanomatch-1.2.13.tgz#b87a8aa4fc0de8fe6be88895b38983ff265bd119"
Expand Down Expand Up @@ -5740,6 +5754,14 @@ onetime@^5.1.0:
dependencies:
mimic-fn "^2.1.0"

open@^7.0.3:
version "7.3.0"
resolved "https://registry.yarnpkg.com/open/-/open-7.3.0.tgz#45461fdee46444f3645b6e14eb3ca94b82e1be69"
integrity sha512-mgLwQIx2F/ye9SmbrUkurZCnkoXyXyu9EbHtJZrICjVAJfyMArdHp3KkixGdZx1ZHFPNIwl0DDM1dFFqXbTLZw==
dependencies:
is-docker "^2.0.0"
is-wsl "^2.1.1"

optimal@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/optimal/-/optimal-4.2.0.tgz#a97d9a12cb543988f2e54b88416b77d9a811ed69"
Expand Down Expand Up @@ -6452,6 +6474,16 @@ rollup-plugin-node-externals@^2.2.0:
dependencies:
find-up "^4.1.0"

rollup-plugin-visualizer@^4.1.2:
version "4.1.2"
resolved "https://registry.yarnpkg.com/rollup-plugin-visualizer/-/rollup-plugin-visualizer-4.1.2.tgz#d94202e3aa06e96007eecde84cca4864273fc14f"
integrity sha512-GdUYsbMSsIC7aXKMObNHHxu2CWyIem3uVGZJPx78e3W+TX7T7+dTj7kVTy4TMbBd2vFtVQ2E0PnwQfqYoY0sMw==
dependencies:
nanoid "^3.0.1"
open "^7.0.3"
source-map "^0.7.3"
yargs "^16.0.3"

rollup@^2.32.1:
version "2.32.1"
resolved "https://registry.yarnpkg.com/rollup/-/rollup-2.32.1.tgz#625a92c54f5b4d28ada12d618641491d4dbb548c"
Expand Down Expand Up @@ -6841,6 +6873,11 @@ string-argv@~0.3.1:
resolved "https://registry.yarnpkg.com/string-argv/-/string-argv-0.3.1.tgz#95e2fbec0427ae19184935f816d74aaa4c5c19da"
integrity sha512-a1uQGz7IyVy9YwhqjZIZu1c8JO8dNIe20xBmSS6qu9kv++k3JGzCVmprbNN5Kn+BgzD5E7YYwg1CcjuJMRNsvg==

string-hash@^1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/string-hash/-/string-hash-1.1.3.tgz#e8aafc0ac1855b4666929ed7dd1275df5d6c811b"
integrity sha1-6Kr8CsGFW0Zmkp7X3RJ1311sgRs=

string-length@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/string-length/-/string-length-3.1.0.tgz#107ef8c23456e187a8abd4a61162ff4ac6e25837"
Expand Down Expand Up @@ -7556,6 +7593,11 @@ y18n@^4.0.0:
resolved "https://registry.npmjs.org/y18n/-/y18n-4.0.0.tgz#95ef94f85ecc81d007c264e190a120f0a3c8566b"
integrity sha512-r9S/ZyXu/Xu9q1tYlpsLIsa3EeLXXk0VwlxqTcFRfg9EhMW+17kbt9G0NrgCmhGb5vT2hyhJZLfDGx+7+5Uj/w==

y18n@^5.0.2:
version "5.0.5"
resolved "https://registry.yarnpkg.com/y18n/-/y18n-5.0.5.tgz#8769ec08d03b1ea2df2500acef561743bbb9ab18"
integrity sha512-hsRUr4FFrvhhRH12wOdfs38Gy7k2FFzB9qgN9v3aLykRq0dRcdcpz5C9FxdS2NuhOrI/628b/KSTJ3rwHysYSg==

yallist@^3.0.0, yallist@^3.0.2:
version "3.0.3"
resolved "https://registry.npmjs.org/yallist/-/yallist-3.0.3.tgz#b4b049e314be545e3ce802236d6cd22cd91c3de9"
Expand Down Expand Up @@ -7589,6 +7631,11 @@ yargs-parser@^18.1.2, yargs-parser@^18.1.3:
camelcase "^5.0.0"
decamelize "^1.2.0"

yargs-parser@^20.2.2:
version "20.2.3"
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-20.2.3.tgz#92419ba867b858c868acf8bae9bf74af0dd0ce26"
integrity sha512-emOFRT9WVHw03QSvN5qor9QQT9+sw5vwxfYweivSMHTcAXPefwVae2FjO7JJjj8hCE4CzPOPeFM83VwT29HCww==

yargs@^15.3.1, yargs@^15.4.1:
version "15.4.1"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-15.4.1.tgz#0d87a16de01aee9d8bec2bfbf74f67851730f4f8"
Expand All @@ -7606,6 +7653,19 @@ yargs@^15.3.1, yargs@^15.4.1:
y18n "^4.0.0"
yargs-parser "^18.1.2"

yargs@^16.0.3:
version "16.1.0"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-16.1.0.tgz#fc333fe4791660eace5a894b39d42f851cd48f2a"
integrity sha512-upWFJOmDdHN0syLuESuvXDmrRcWd1QafJolHskzaw79uZa7/x53gxQKiR07W59GWY1tFhhU/Th9DrtSfpS782g==
dependencies:
cliui "^7.0.2"
escalade "^3.1.1"
get-caller-file "^2.0.5"
require-directory "^2.1.1"
string-width "^4.2.0"
y18n "^5.0.2"
yargs-parser "^20.2.2"

yoga-layout-prebuilt@^1.9.6:
version "1.9.6"
resolved "https://registry.yarnpkg.com/yoga-layout-prebuilt/-/yoga-layout-prebuilt-1.9.6.tgz#98dde95bbf8e6e12835876e9305f1e995c4bb801"
Expand Down

0 comments on commit a40715a

Please sign in to comment.