Skip to content

Commit

Permalink
Rewritten concat pipe with a blazing fast handmade async pipe. 🔥
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanelian committed Jul 14, 2017
1 parent f7c04da commit 09fb668
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 190 deletions.
3 changes: 2 additions & 1 deletion cli.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ let packageInfo = {
let outdated = false;
let masterVersion = packageInfo.version;

https.get('https://raw.githubusercontent.com/ryanelian/instapack/master/package.json', response => {
let updater = https.get('https://raw.githubusercontent.com/ryanelian/instapack/master/package.json', response => {

let body = '';
response.setEncoding('utf8');
Expand Down Expand Up @@ -122,6 +122,8 @@ let parse = CLI.strict().help().argv;
//console.log(parse);

function updateNag() {
updater.abort();

if (outdated) {
console.log();
console.log(chalk.yellow('instapack') + ' is outdated. New version: ' + chalk.green(masterVersion));
Expand Down
5 changes: 0 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
"browserify": "14.4.0",
"chalk": "2.0.1",
"cssnano": "3.10.0",
"event-stream": "3.3.4",
"fs-extra": "3.0.1",
"gulp": "3.9.1",
"gulp-concat": "2.6.1",
"gulp-plumber": "1.1.0",
"gulp-postcss": "7.0.0",
"gulp-sourcemaps": "2.6.0",
Expand All @@ -52,17 +50,14 @@
"@types/autoprefixer": "^6.7.2",
"@types/browserify": "^12.0.32",
"@types/chalk": "^0.4.31",
"@types/event-stream": "^3.3.31",
"@types/fs-extra": "^3.0.3",
"@types/gulp-concat": "^0.0.30",
"@types/gulp-util": "^3.0.31",
"@types/gulp-watch": "^4.1.31",
"@types/html-minifier": "^1.1.30",
"@types/node": "^8.0.8",
"@types/node-sass": "^3.10.32",
"@types/prettyjson": "^0.0.28",
"@types/resolve": "^0.0.4",
"@types/source-map": "^0.5.0",
"@types/through2": "^2.0.33",
"@types/vinyl": "^2.0.0",
"@types/watchify": "^3.7.4",
Expand Down
96 changes: 47 additions & 49 deletions src/Compiler.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

119 changes: 56 additions & 63 deletions src/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import * as gutil from 'gulp-util';
import * as sourcemaps from 'gulp-sourcemaps';

// These are used by concat task
import * as concat from 'gulp-concat';
import * as es from 'event-stream';
import * as through2 from 'through2';
import * as vinyl from 'vinyl';
import * as resolve from 'resolve';
import * as fs from 'fs-extra';

Expand Down Expand Up @@ -118,7 +118,7 @@ export class Compiler {
build(taskName) {
gulp.start(taskName);
}

/**
* Flattens abyssmal sourcemap paths resulting from Browserify compilation.
*/
Expand Down Expand Up @@ -240,7 +240,7 @@ export class Compiler {
/**
* Returns true when package.json exists in project root folder but node_modules folder is missing.
*/
needPackageRestore(): boolean {
needPackageRestore() {
let hasNodeModules = fs.existsSync(this.settings.npmFolder);
let hasPackageJson = fs.existsSync(this.settings.packageJson);

Expand All @@ -254,45 +254,36 @@ export class Compiler {
}

/**
* Attempts to resolve modules using concat list and project folder in setting.
* Attempts to resolve a module using node resolution logic, relative to project folder path, asynchronously.
* @param path
*/
async resolveConcatModules(): Promise<ConcatenationLookup> {
let resolveOption = { basedir: this.settings.root };

let resolver: { [target: string]: Promise<string>[] } = {};
let promises: Promise<string>[] = [];

for (let target in this.settings.concat) {
let resolveList = [];

this.settings.concat[target].forEach(s => {
let p = new Promise<string>((ok, reject) => {
resolve(s, resolveOption, (error, result) => {
if (error) {
reject(error)
} else {
ok(result);
}
});
});

resolveList.push(p);
promises.push(p);
resolveAsPromise(path: string) {
return new Promise<string>((ok, reject) => {
resolve(path, {
basedir: this.settings.root
}, (error, result) => {
if (error) {
reject(error);
} else {
ok(result);
}
});
});
}

resolver[target] = resolveList;
}

// TODO: Use Object.values when using Node 8
await Promise.all(promises);

let resolution: ConcatenationLookup = {};
/**
* Returns a promise for a concatenated file content as string, resulting from a list of node modules.
* @param paths
*/
async resolveThenConcatenate(paths: string[]) {
let concat = '';

for (let target in resolver) {
resolution[target + '.js'] = await Promise.all(resolver[target]);
for (let path of paths) {
let absolute = await this.resolveAsPromise(path);
concat += await fs.readFile(absolute, 'utf8') + '\n';
}

return resolution;
return concat;
}

/**
Expand All @@ -302,34 +293,36 @@ export class Compiler {
let concatCount = this.settings.concatCount;
gutil.log('Resolving', gutil.colors.cyan(concatCount.toString()), 'concatenation targets...');

let concatTask = undefined;

if (concatCount) {
concatTask = async () => {
if (this.watchMode) {
gutil.log("Concatenation task will be run once and", gutil.colors.red("NOT watched!"));
}

let resolution = await this.resolveConcatModules();
//console.log(resolution);

let concatStreams = [];

for (let target in resolution) {
let targetFiles = resolution[target];
let targetStream = gulp.src(targetFiles)
.pipe(concat(target))
.pipe(To.MinifyProductionJs(this.productionMode))

concatStreams.push(targetStream);
}
if (concatCount === 0) {
gulp.task('concat', undefined);
return;
}

return es.merge(concatStreams)
.pipe(To.BuildLog('JS concatenation'))
.pipe(this.server ? this.server.Update() : gulp.dest(this.settings.outputJsFolder));
};
if (this.watchMode) {
gutil.log("Concatenation task will be run once and", gutil.colors.red("NOT watched!"));
}

gulp.task('concat', concatTask);
gulp.task('concat', () => {
let g = through2.obj();
let resolution = this.settings.concat;

for (let target in resolution) {
this.resolveThenConcatenate(resolution[target]).then(result => {
g.push(new vinyl({
path: target,
contents: Buffer.from(result)
}));

concatCount--;
if (concatCount === 0){
g.push(null);
}
});
}

return g.pipe(To.MinifyProductionJs(this.productionMode))
.pipe(To.BuildLog('JS concatenation'))
.pipe(this.server ? this.server.Update() : gulp.dest(this.settings.outputJsFolder));
});
}
}

0 comments on commit 09fb668

Please sign in to comment.