Skip to content

Commit

Permalink
made a lot of progress on types
Browse files Browse the repository at this point in the history
  • Loading branch information
jchitel committed Aug 26, 2018
1 parent 5dd1426 commit d314175
Show file tree
Hide file tree
Showing 63 changed files with 3,728 additions and 3,328 deletions.
1 change: 1 addition & 0 deletions ansi-to-html.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'ansi-to-html';
18 changes: 18 additions & 0 deletions fswatcher-child.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
declare module 'fswatcher-child' {
import { EventEmitter } from 'events';

export interface FSWatcherOptions {
useFsEvents?: boolean;
ignoreInitial?: boolean;
ignorePermissionErrors?: boolean;
ignored?: RegExp;
}

export default class FSWatcher extends EventEmitter {
constructor(options?: FSWatcherOptions);
_closePath(dir: string): void;
add(dir: string): void;
unwatch(dir: string): void;
close(): void;
}
}
40 changes: 40 additions & 0 deletions node-libs-browser.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
declare module 'node-libs-browser' {
export const assert: string;
export const buffer: string;
export const child_process: null;
export const cluster: null;
export const console: string;
export const constants: string;
export const crypto: string;
export const dgram: null;
export const dns: null;
export const domain: string;
export const events: string;
export const fs: null;
export const http: string;
export const https: string;
export const module: null;
export const net: null;
export const os: string;
export const path: string;
export const punycode: string;
export const process: string;
export const querystring: string;
export const readline: null;
export const repl: null;
export const stream: string;
export const _stream_duplex: string;
export const _stream_passthrough: string;
export const _stream_readable: string;
export const _stream_transform: string;
export const _stream_writable: string;
export const string_decoder: string;
export const sys: string;
export const timers: string;
export const tls: null;
export const tty: string;
export const url: string;
export const util: string;
export const vm: string;
export const zlib: string;
}
11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,25 @@
"ws": "^5.1.1"
},
"devDependencies": {
"@types/babel-generator": "^6.25.2",
"@types/babel-template": "^6.25.1",
"@types/babel-traverse": "^6.25.4",
"@types/babylon-walk": "^3.10.1",
"@types/clone": "^0.1.30",
"@types/deasync": "^0.1.0",
"@types/get-port": "^4.0.0",
"@types/is-glob": "^4.0.0",
"@types/is-url": "^1.2.28",
"@types/json5": "^0.0.29",
"@types/micromatch": "^3.1.0",
"@types/mkdirp": "^0.5.2",
"@types/node": "^10.7.1",
"@types/node-forge": "^0.7.4",
"@types/ora": "^1.3.4",
"@types/resolve": "^0.0.8",
"@types/serve-static": "^1.13.2",
"@types/strip-ansi": "^3.0.0",
"@types/ws": "^6.0.0",
"@vue/component-compiler-utils": "^2.0.0",
"babel-cli": "^6.26.0",
"babel-plugin-transform-async-super": "^1.0.0",
Expand Down
1 change: 1 addition & 0 deletions posthtml.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'posthtml';
84 changes: 56 additions & 28 deletions src/Asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,52 @@ import md5 from './utils/md5';
import isURL from './utils/is-url';
import * as config from './utils/config';
import syncPromise from './utils/syncPromise';
import * as logger from './Logger';
import Resolver from './Resolver';
import logger from './Logger';
import Resolver, { PackageJson } from './Resolver';
import Parser from './Parser';


export interface AssetOptions {
rootDir: string;
rendition?: none;
parser: Parser;
production?: boolean;
scopeHoist?: boolean;
bundleLoaders: { [type: string]: none };
publicURL: string;
outDir: string;
entryFiles: string[];
outFile: string;
}

export type AssetGeneration = { type?: string, value?: string, final?: boolean };

/**
* Most assets should return this from generate().
*/
export type StandardAssetGeneration = AssetGeneration | AssetGeneration[];

export type AssetClass<Contents, Generated> = { new (name: string, options: AssetOptions): Asset<Contents, Generated> };

/**
* An Asset represents a file in the dependency tree. Assets can have multiple
* parents that depend on it, and can be added to multiple output bundles.
* The base Asset class doesn't do much by itself, but sets up an interface
* for subclasses to implement.
*/
export default class Asset {
export default class Asset<Contents, Generated> {
id: string | null;
name: string;
basename: string;
relativeName: string;
options: none;
options: AssetOptions;
encoding: string;
type: string;
type: string | null;
processed: boolean;
contents: none | null;
contents: Contents | null;
ast: none | null;
generated: none | null;
hash: none | null;
generated: Generated | null;
hash: string | Buffer | null;
parentDeps: Set<none>;
dependencies: Map<string, none>;
depAssets: Map<none, none>;
Expand All @@ -41,7 +65,11 @@ export default class Asset {
bundledSize: number;
resolver: Resolver;

constructor(name: string, options: none) {
usedExports?: Set<none>;

private _package?: PackageJson;

constructor(name: string, options: AssetOptions) {
this.id = null;
this.name = name;
this.basename = path.basename(this.name);
Expand Down Expand Up @@ -81,7 +109,7 @@ export default class Asset {
async parseIfNeeded(): Promise<void> {
await this.loadIfNeeded();
if (!this.ast) {
this.ast = await this.parse(this.contents);
this.ast = await this.parse(this.contents!);
}
}

Expand All @@ -101,7 +129,7 @@ export default class Asset {
}
}

addDependency(name: string, opts: none) {
addDependency(name: string, opts?: none): void {
this.dependencies.set(name, Object.assign({ name }, opts));
}

Expand Down Expand Up @@ -140,22 +168,22 @@ export default class Asset {
return URL.format(parsed);
}

get package() {
get package(): PackageJson | undefined {
logger.warn(
'`asset.package` is deprecated. Please use `await asset.getPackage()` instead.'
);
return syncPromise(this.getPackage());
}

async getPackage() {
async getPackage(): Promise<PackageJson | undefined> {
if (!this._package) {
this._package = await this.resolver.findPackage(path.dirname(this.name));
}

return this._package;
}

async getConfig(filenames, opts = {}) {
async getConfig(filenames: string[], opts: { packageKey?: keyof PackageJson, path?: string, load?: boolean } = {}): Promise<unknown> {
if (opts.packageKey) {
let pkg = await this.getPackage();
if (pkg && pkg[opts.packageKey]) {
Expand Down Expand Up @@ -183,11 +211,11 @@ export default class Asset {
return true;
}

async load() {
return await fs.readFile(this.name, this.encoding);
async load(): Promise<Contents> {
return await fs.readFile(this.name, this.encoding) as any; // this is the base, an asset loads the file as a string
}

parse() {
parse(_contents?: Contents) {
// do nothing by default
}

Expand All @@ -203,10 +231,10 @@ export default class Asset {
// do nothing by default
}

async generate() {
async generate(): Promise<Generated> {
return {
[this.type]: this.contents
};
[this.type as string]: this.contents
} as any; // default implementation
}

async process() {
Expand All @@ -233,11 +261,11 @@ export default class Asset {
return this.generated;
}

async postProcess(generated) {
async postProcess(generated: Generated): Promise<Generated> {
return generated;
}

generateHash() {
async generateHash(): Promise<string | Buffer> {
return objectHash(this.generated);
}

Expand All @@ -263,13 +291,13 @@ export default class Asset {
return md5(this.name) + '.' + this.type;
}

replaceBundleNames(bundleNameMap) {
replaceBundleNames(bundleNameMap: Map<string, none>) {
let copied = false;
for (let key in this.generated) {
let value = this.generated[key];
for (let key in this.generated!) {
let value = this.generated![key];
if (typeof value === 'string') {
// Replace temporary bundle names in the output with the final content-hashed names.
let newValue = value;
let newValue: string = value;
for (let [name, map] of bundleNameMap) {
newValue = newValue.split(name).join(map);
}
Expand All @@ -280,12 +308,12 @@ export default class Asset {
copied = true;
}

this.generated[key] = newValue;
this.generated![key] = newValue;
}
}
}

generateErrorMessage(err) {
generateErrorMessage(err: none): none {
return err;
}
}
Loading

0 comments on commit d314175

Please sign in to comment.