Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Replace builds with artifacts. #2

Merged
merged 7 commits into from
Oct 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
"@babel/preset-typescript": "^7.10.4",
"@boost/cli": "^2.2.0",
"@boost/common": "^2.2.2",
"@boost/event": "^2.2.0",
"@boost/pipeline": "^2.1.3",
"@boost/terminal": "^2.1.0",
"@rollup/plugin-babel": "^5.2.1",
Expand Down
36 changes: 36 additions & 0 deletions src/Artifact.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* eslint-disable no-empty-function */

import Package from './Package';
import { ArtifactFlags, BuildResult, ArtifactState } from './types';

export default abstract class Artifact {
flags: ArtifactFlags = {};

package: Package;

result?: BuildResult;

state: ArtifactState = 'pending';

constructor(pkg: Package) {
this.package = pkg;
}

async boot(): Promise<void> {}

async build(): Promise<void> {}

async pack(): Promise<void> {}

isRunning(): boolean {
return this.state === 'booting' || this.state === 'building' || this.state === 'packing';
}

shouldSkip(): boolean {
return this.state === 'skipped' || this.state === 'failed';
}

abstract getLabel(): string;

abstract getBuilds(): string[];
}
109 changes: 0 additions & 109 deletions src/Build.ts

This file was deleted.

112 changes: 112 additions & 0 deletions src/BundleArtifact.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { Path, toArray } from '@boost/common';
import { rollup, RollupCache } from 'rollup';
import Artifact from './Artifact';
import { getRollupConfig } from './configs/rollup';
import { Format, Platform } from './types';

export default class BundleArtifact extends Artifact {
cache?: RollupCache;

formats: Format[] = [];

// Path to the input file relative to the package
inputPath: string = '';

// Namespace for UMD bundles
namespace: string = '';

// Name of the output file without extension
outputName: string = '';

async build() {
const rollupConfig = getRollupConfig(this, this.package.getFeatureFlags());

// Skip build because of invalid config
if (!rollupConfig) {
this.state = 'skipped';

return;
}

this.result = {
time: 0,
};

const { output = [], ...input } = rollupConfig;
const start = Date.now();
const bundle = await rollup(input);

if (bundle.cache) {
this.cache = bundle.cache;
}

await Promise.all(
toArray(output).map(async (out) => {
const { originalFormat, ...outOptions } = out;

try {
await bundle.write(outOptions);

this.state = 'passed';
} catch (error) {
this.state = 'failed';
}
}),
);

this.result.time = Date.now() - start;
}

getLabel(): string {
return this.outputName;
}

getBuilds(): string[] {
return this.formats;
}

getPlatform(format: Format): Platform {
if (format === 'cjs' || format === 'mjs') {
return 'node';
}

if (format === 'esm' || format === 'umd') {
return 'browser';
}

// "lib" is a shared format across all platforms,
// and when a package wants to support multiple platforms,
// we must down-level the "lib" format to the lowest platform.
if (this.flags.requiresSharedLib) {
const platforms = new Set(this.package.platforms);

if (platforms.has('browser')) {
return 'browser';
} else if (platforms.has('node')) {
return 'node';
}
}

return this.package.platforms[0];
}

getInputPath(): Path | null {
const inputPath = this.package.path.append(this.inputPath);

if (inputPath.exists()) {
return inputPath;
}

console.warn(
`Cannot find input "${this.inputPath}" for package "${this.package.contents.name}". Skipping package.`,
);

return null;
}

getOutputPath(format: Format): Path {
const ext = format === 'cjs' || format === 'mjs' ? format : 'js';

return this.package.path.append(`${format}/${this.outputName}.${ext}`);
}
}
96 changes: 96 additions & 0 deletions src/Package.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* eslint-disable @typescript-eslint/member-ordering */

import { Memoize, Path, toArray } from '@boost/common';
import Artifact from './Artifact';
import Project from './Project';
import resolveTsConfig from './helpers/resolveTsConfig';
import { FeatureFlags, PackemonPackage, Platform, Target } from './types';

export default class Package {
artifacts: Artifact[] = [];

contents: PackemonPackage;

path: Path;

platforms: Platform[] = [];

project: Project;

root: boolean = false;

target: Target = 'legacy';

constructor(project: Project, path: Path, contents: PackemonPackage) {
this.project = project;
this.path = path;
this.contents = contents;

// Workspace root `package.json`s may not have this
if (contents.packemon) {
this.platforms = toArray(contents.packemon.platform);
this.target = contents.packemon.target;
}
}

addArtifact(artifact: Artifact): Artifact {
this.artifacts.push(artifact);

return artifact;
}

getName(): string {
return this.contents.name;
}

@Memoize()
getFeatureFlags(): FeatureFlags {
const flags: FeatureFlags = this.root ? {} : this.project.rootPackage.getFeatureFlags();
flags.workspaces = this.project.workspaces;

// React
if (this.hasDependency('react')) {
flags.react = true;
}

// TypeScript
const tsconfigPath = this.project.root.append('tsconfig.json');

if (this.hasDependency('typescript') || tsconfigPath.exists()) {
flags.typescript = true;
flags.decorators = Boolean(
resolveTsConfig(tsconfigPath)?.compilerOptions?.experimentalDecorators,
);
}

// Flow
const flowconfigPath = this.project.root.append('.flowconfig');

if (this.hasDependency('flow-bin') || flowconfigPath.exists()) {
flags.flow = true;
}

return flags;
}

getJsonPath(): Path {
return this.path.append('package.json');
}

hasDependency(name: string): boolean {
const pkg = this.contents;

return Boolean(
pkg.dependencies?.[name] ||
pkg.devDependencies?.[name] ||
pkg.peerDependencies?.[name] ||
pkg.optionalDependencies?.[name],
);
}

isBuilt(): boolean {
return this.artifacts.every(
(artifact) => artifact.state !== 'pending' && !artifact.isRunning(),
);
}
}
Loading