Skip to content

Commit

Permalink
Merge pull request #2789 from parcel-bundler/wbinnssmith/flow-cleanup
Browse files Browse the repository at this point in the history
Miscellaneous flow cleanup
  • Loading branch information
padmaia committed Mar 13, 2019
2 parents ab9366d + ece23b4 commit 7d9fdbd
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 48 deletions.
43 changes: 24 additions & 19 deletions packages/core/cache/src/Cache.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// @flow

import * as fs from '@parcel/fs';
import pkg from '../package.json';
import Path from 'path';
import path from 'path';
import {md5FromString} from '@parcel/utils/src/md5';
import objectHash from '@parcel/utils/src/objectHash';
import logger from '@parcel/logger';
Expand Down Expand Up @@ -30,8 +31,8 @@ export class Cache {
invalidated: Set<FilePath>;
optionsHash: string;

init(options: CLIOptions) {
this.dir = Path.resolve(options.cacheDir || DEFAULT_CACHE_DIR);
init(options: CLIOptions): void {
this.dir = path.resolve(options.cacheDir || DEFAULT_CACHE_DIR);
this.invalidated = new Set();
this.optionsHash = objectHash(
OPTION_KEYS.reduce((p: JSONObject, k) => ((p[k] = options[k]), p), {
Expand All @@ -40,34 +41,38 @@ export class Cache {
);
}

async createCacheDir(dir: FilePath = DEFAULT_CACHE_DIR) {
dir = Path.resolve(dir);
async createCacheDir(dir: FilePath = DEFAULT_CACHE_DIR): Promise<void> {
dir = path.resolve(dir);
if (existsCache.has(dir)) {
return;
}

// Create sub-directories for every possible hex value
// This speeds up large caches on many file systems since there are fewer files in a single directory.
for (let i = 0; i < 256; i++) {
await fs.mkdirp(Path.join(dir, ('00' + i.toString(16)).slice(-2)));
await fs.mkdirp(path.join(dir, ('00' + i.toString(16)).slice(-2)));
}

existsCache.add(dir);
}

getCacheId(appendedData: string, env: Environment) {
getCacheId(appendedData: string, env: Environment): string {
return md5FromString(this.optionsHash + appendedData + JSON.stringify(env));
}

getCachePath(cacheId: string, extension: string = '.json'): FilePath {
return Path.join(
return path.join(
this.dir,
cacheId.slice(0, 2),
cacheId.slice(2) + extension
);
}

async writeBlob(type: string, cacheId: string, data: any) {
async writeBlob(
type: string,
cacheId: string,
data: any
): Promise<CacheReference> {
let blobPath = this.getCachePath(cacheId, '.' + type);
if (typeof data === 'object') {
if (Buffer.isBuffer(data)) {
Expand All @@ -81,10 +86,10 @@ export class Cache {
}

await fs.writeFile(blobPath, data);
return new CacheReference(Path.relative(this.dir, blobPath));
return new CacheReference(path.relative(this.dir, blobPath));
}

async _writeBlobs(assets: Array<Asset>) {
async _writeBlobs(assets: Array<Asset>): Promise<Array<Asset>> {
return await Promise.all(
assets.map(async asset => {
let assetCacheId = this.getCacheId(asset.id, asset.env);
Expand All @@ -101,7 +106,7 @@ export class Cache {
);
}

async writeBlobs(cacheEntry: CacheEntry) {
async writeBlobs(cacheEntry: CacheEntry): Promise<CacheEntry> {
cacheEntry.assets = await this._writeBlobs(cacheEntry.assets);
if (cacheEntry.initialAssets) {
cacheEntry.initialAssets = await this._writeBlobs(
Expand All @@ -112,7 +117,7 @@ export class Cache {
return cacheEntry;
}

async write(cacheEntry: CacheEntry) {
async write(cacheEntry: CacheEntry): Promise<void> {
try {
let cacheId = this.getCacheId(cacheEntry.filePath, cacheEntry.env);
await this.writeBlobs(cacheEntry);
Expand All @@ -123,9 +128,9 @@ export class Cache {
}
}

async readBlob(blobKey: FilePath) {
let extension = Path.extname(blobKey);
let data = await fs.readFile(Path.resolve(this.dir, blobKey), {
async readBlob(blobKey: FilePath): Promise<any> {
let extension = path.extname(blobKey);
let data = await fs.readFile(path.resolve(this.dir, blobKey), {
encoding: extension === '.bin' ? undefined : 'utf8'
});

Expand All @@ -136,7 +141,7 @@ export class Cache {
return data;
}

async readBlobs(asset: Asset) {
async readBlobs(asset: Asset): Promise<void> {
await Promise.all(
Object.keys(asset.output).map(async blobKey => {
if (asset.output[blobKey] instanceof CacheReference) {
Expand Down Expand Up @@ -165,7 +170,7 @@ export class Cache {
this.invalidated.add(filePath);
}

async delete(filePath: FilePath, env: Environment) {
async delete(filePath: FilePath, env: Environment): Promise<void> {
try {
let cacheId = this.getCacheId(filePath, env);
// TODO: delete blobs
Expand All @@ -183,7 +188,7 @@ export class CacheReference {
this.filePath = filePath;
}

static deserialize(value: {filePath: FilePath}) {
static deserialize(value: {filePath: FilePath}): Promise<CacheReference> {
return new CacheReference(value.filePath);
}
}
Expand Down
26 changes: 15 additions & 11 deletions packages/core/core/src/BundlerRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import type AssetGraph from './AssetGraph';
import type {
Namer,
Asset,
Bundle,
FilePath,
CLIOptions,
FilePath,
Namer,
TransformerRequest
} from '@parcel/types';
import type Config from './Config';

import nullthrows from 'nullthrows';
import BundleGraph from './BundleGraph';
import AssetGraphBuilder from './AssetGraphBuilder';

Expand All @@ -29,7 +32,7 @@ export default class BundlerRunner {
this.rootDir = opts.rootDir;
}

async bundle(graph: AssetGraph) {
async bundle(graph: AssetGraph): Promise<BundleGraph> {
let bundler = await this.config.getBundler();

let bundleGraph = new BundleGraph();
Expand All @@ -40,7 +43,7 @@ export default class BundlerRunner {
return bundleGraph;
}

async nameBundles(bundleGraph: BundleGraph) {
async nameBundles(bundleGraph: BundleGraph): Promise<void> {
let namers = await this.config.getNamers();
let promises = [];
bundleGraph.traverseBundles(bundle => {
Expand All @@ -50,7 +53,7 @@ export default class BundlerRunner {
await Promise.all(promises);
}

async nameBundle(namers: Array<Namer>, bundle: Bundle) {
async nameBundle(namers: Array<Namer>, bundle: Bundle): Promise<void> {
for (let namer of namers) {
let filePath = await namer.name(bundle, {
rootDir: this.rootDir
Expand All @@ -65,7 +68,7 @@ export default class BundlerRunner {
throw new Error('Unable to name bundle');
}

async applyRuntimes(bundleGraph: BundleGraph) {
async applyRuntimes(bundleGraph: BundleGraph): Promise<void> {
let bundles = [];
bundleGraph.traverseBundles(bundle => {
bundles.push(bundle);
Expand All @@ -76,7 +79,10 @@ export default class BundlerRunner {
}
}

async applyRuntimesToBundle(bundleGraph: BundleGraph, bundle: Bundle) {
async applyRuntimesToBundle(
bundleGraph: BundleGraph,
bundle: Bundle
): Promise<void> {
// HACK. TODO: move this into some sort of asset graph proxy
// $FlowFixMe
bundle.assetGraph.addRuntimeAsset = this.addRuntimeAsset.bind(
Expand All @@ -96,7 +102,7 @@ export default class BundlerRunner {
bundle: Bundle,
node: {id: string},
transformerRequest: TransformerRequest
) {
): Promise<Asset> {
let builder = new AssetGraphBuilder({
cliOpts: this.cliOpts,
config: this.config,
Expand All @@ -106,8 +112,7 @@ export default class BundlerRunner {

let graph: AssetGraph = await builder.build();
let entry = graph.getEntryAssets()[0];
// $FlowFixMe - node will always exist
let subGraph = graph.getSubGraph(graph.getNode(entry.id));
let subGraph = graph.getSubGraph(nullthrows(graph.getNode(entry.id)));

// Exclude modules that are already included in an ancestor bundle
subGraph.traverseAssets(asset => {
Expand All @@ -117,7 +122,6 @@ export default class BundlerRunner {
});

bundle.assetGraph.merge(subGraph);
// $FlowFixMe
bundle.assetGraph.addEdge({from: node.id, to: entry.id});
return entry;
}
Expand Down
5 changes: 1 addition & 4 deletions packages/core/core/src/Graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ export default class Graph<TNode: Node> implements IGraph<TNode> {

getNodesConnectedTo(node: TNode): Array<TNode> {
let edges = Array.from(this.edges).filter(edge => edge.to === node.id);
return edges.map(edge => {
// $FlowFixMe
return this.nodes.get(edge.from);
});
return edges.map(edge => nullthrows(this.nodes.get(edge.from)));
}

getNodesConnectedFrom(node: TNode): Array<TNode> {
Expand Down
8 changes: 4 additions & 4 deletions packages/core/core/src/PackagerRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ export default class PackagerRunner {
}

async package(bundle: Bundle): Promise<Blob> {
// $FlowFixMe - filePath should already be filled in at this point
let packager = await this.config.getPackager(bundle.filePath);
let packager = await this.config.getPackager(nullthrows(bundle.filePath));
return await packager.package(bundle, this.cliOpts);
}

async optimize(bundle: Bundle, contents: Blob): Promise<Blob> {
// $FlowFixMe - filePath should already be filled in at this point
let optimizers = await this.config.getOptimizers(bundle.filePath);
let optimizers = await this.config.getOptimizers(
nullthrows(bundle.filePath)
);

for (let optimizer of optimizers) {
contents = await optimizer.optimize(bundle, contents, this.cliOpts);
Expand Down
14 changes: 7 additions & 7 deletions packages/core/core/src/Parcel.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default class Parcel {
assetGraphBuilder: AssetGraphBuilder;
bundlerRunner: BundlerRunner;
farm: WorkerFarm;
runPackage: (bundle: Bundle) => Promise<any>;
runPackage: (bundle: Bundle) => Promise<mixed>;

constructor(options: ParcelOpts) {
let {entries} = options;
Expand All @@ -45,7 +45,7 @@ export default class Parcel {
this.rootDir = getRootDir(this.entries);
}

async init() {
async init(): Promise<void> {
await Cache.createCacheDir(this.options.cliOpts.cacheDir);

if (!this.options.env) {
Expand Down Expand Up @@ -106,7 +106,7 @@ export default class Parcel {
this.runPackage = this.farm.mkhandle('runPackage');
}

async run() {
async run(): Promise<BundleGraph> {
await this.init();

this.assetGraphBuilder.on('invalidate', () => {
Expand All @@ -116,10 +116,9 @@ export default class Parcel {
return await this.build();
}

async build() {
async build(): Promise<BundleGraph> {
try {
// console.log('Starting build'); // eslint-disable-line no-console
// $FlowFixMe This reliably fails on Windows. Not sure why.
let assetGraph = await this.assetGraphBuilder.build();

if (process.env.PARCEL_DUMP_GRAPH != null) {
Expand All @@ -141,14 +140,15 @@ export default class Parcel {
if (e !== abortError) {
console.error(e); // eslint-disable-line no-console
}
throw e;
}
}

bundle(assetGraph: AssetGraph) {
bundle(assetGraph: AssetGraph): Promise<BundleGraph> {
return this.bundlerRunner.bundle(assetGraph);
}

package(bundleGraph: BundleGraph) {
package(bundleGraph: BundleGraph): Promise<mixed> {
let promises = [];
bundleGraph.traverseBundles(bundle => {
promises.push(this.runPackage(bundle));
Expand Down
6 changes: 3 additions & 3 deletions packages/core/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
},
"dependencies": {
"@parcel/fs": "^1.10.3",
"@parcel/workers": "^1.10.3"
"@parcel/workers": "^1.10.3",
"nullthrows": "^1.1.1"
},
"devDependencies": {
"@babel/plugin-transform-flow-strip-types": "^7.2.0",
"nullthrows": "^1.1.1"
"@babel/plugin-transform-flow-strip-types": "^7.2.0"
}
}

0 comments on commit 7d9fdbd

Please sign in to comment.