Skip to content
This repository has been archived by the owner on Jan 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #45 from productboard/feat/bundle-size
Browse files Browse the repository at this point in the history
Ability to upload Info (along with Index) files
  • Loading branch information
jukben authored Apr 18, 2019
2 parents e082fca + f8cae66 commit ef51b23
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 18 deletions.
68 changes: 68 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Copyright (c) 2019-present, ProductBoard, Inc.
* All rights reserved.
*/

const fs = require('fs');
const child = require('child_process');
const gzipSize = require('gzip-size');

/**
* Webpack Plugin for generating info JSON (hash, sha, assetSize), it's consumable by webpack-deploy later on when deploying
*/
class BuildInfo {
constructor({ filename = 'build.log' }) {
this.filename = filename;
}

getSha() {
return child
.execSync('git rev-parse HEAD')
.toString()
.trim();
}

async createBuildInfoReport(compilation) {
const hash = compilation.getStats().toJson().hash;

const assetsSize = await Promise.all(
Object.entries(compilation.assets)
.filter(([asset]) => /.js$/.test(asset))
.map(async ([asset, content]) => {
const gzipedSize = await gzipSize(content.source());
return {
asset,
size: content.size(),
gzipedSize,
};
}),
);

// get full SHA from git
const sha = this.getSha();

const info = JSON.stringify({
sha,
hash,
assetsSize,
});

fs.writeFileSync(this.filename, info);
}

apply(compiler) {
compiler.plugin('emit', (compilation, callback) => {
this.createBuildInfoReport(compilation)
.catch(e => {
compilation.errors.push(
new Error(
`Webpack BuildInfo plugin has thrown an error in 'createBuildInfoReport': ${e}`,
),
);
})
.then(callback);
});
}
}

module.exports = { BuildInfo };
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"lint": "./node_modules/.bin/eslint --config .eslintrc.json tasks"
},
"engines": {
"npm": ">= 5.2.0"
"npm": ">= 8"
},
"dependencies": {
"async": "^2.6.1",
Expand All @@ -59,6 +59,7 @@
"gulp-svgstore": "^7.0.1",
"gulp-util": "^3.0.6",
"gulp-webpack": "^1.5.0",
"gzip-size": "^5.0.0",
"html-webpack-plugin": "^3.2.0",
"object-assign": "^4.0.1",
"promise-redis": "^0.0.5",
Expand Down
63 changes: 46 additions & 17 deletions tasks/deploy-redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,21 @@ const os = require('os');
const util = require('util');
const gutil = require('gulp-util');

const {
env,
getRevision,
getConfigFor,
getRedisClient,
} = require('./utils');
const { env, getRevision, getConfigFor, getRedisClient } = require('./utils');

//
// Deployment process tasks
//

async function uploadFile(config, file, rev) {
async function uploadFiles(config, files, rev) {
const client = await getRedisClient(config);

const timestamp = new Date();

// Store index file under indexKey, e.g. 'app:<rev-number>'
await client.set(util.format(config.indexKey, rev), file);
// Store file under appropriate keys, e.g. 'app:<rev-number>'
await Promise.all(
files.map(fileKey => client.set(util.format(fileKey, rev), files[fileKey])),
);

// Store timestamp information under 'app-timestamp:<rev-number>'
// so we can compare if we have newer version than latest major rev
Expand Down Expand Up @@ -52,15 +49,47 @@ async function deployRedis(config, rev) {

await Promise.all(
(config.files || [config]).map(async fileConfig => {
const fileName = fileConfig.indexPath;
gutil.log(
gutil.colors.yellow(env()),
'Deploying',
gutil.colors.magenta(fileName),
'...',
/**
* Define "files" keys to process. We currently support two of them: index and info.
* They has to came in pairs like indexKey and indexPath.
*
* E.g:
*
* {
* indexPath: 'dist/index.html',
* indexKey: 'app:%s',
* infoPath: 'build.log',
* infoKey: 'info:%s',
* },
*/
const FILES = ['index', 'info'];

const filesToUpload = FILES.reduce((acc, v) => {
const path = fileConfig[`${v}Path`];
const key = fileConfig[`${v}Key`];

if (!path || !key) {
return acc;
}

gutil.log(
gutil.colors.yellow(env()),
'Deploying',
gutil.colors.magenta(path),
'...',
);
const fileContent = fs.readFileSync(path, 'utf8');

acc[key] = fileContent;

return acc;
}, {});

return uploadFiles(
Object.assign({}, config, fileConfig),
filesToUpload,
rev,
);
const file = fs.readFileSync(fileName, 'utf8');
return uploadFile(Object.assign({}, config, fileConfig), file, rev);
}),
);
}
Expand Down
18 changes: 18 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,11 @@ [email protected]:
dependencies:
readable-stream "~1.1.9"

duplexer@^0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/duplexer/-/duplexer-0.1.1.tgz#ace6ff808c1ce66b57d1ebf97977acb02334cfc1"
integrity sha1-rOb/gIwc5mtX0ev5eXessCM0z8E=

duplexify@^3.5.0, duplexify@^3.6.0:
version "3.7.1"
resolved "https://registry.yarnpkg.com/duplexify/-/duplexify-3.7.1.tgz#2a4df5317f6ccfd91f86d6fd25d8d8a103b88309"
Expand Down Expand Up @@ -2327,6 +2332,14 @@ gulplog@^1.0.0:
dependencies:
glogg "^1.0.0"

gzip-size@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/gzip-size/-/gzip-size-5.0.0.tgz#a55ecd99222f4c48fd8c01c625ce3b349d0a0e80"
integrity sha512-5iI7omclyqrnWw4XbXAmGhPsABkSIDQonv2K0h61lybgofWa6iZyvrI3r2zsJH4P8Nb64fFVzlvfhs0g7BBxAA==
dependencies:
duplexer "^0.1.1"
pify "^3.0.0"

har-schema@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/har-schema/-/har-schema-2.0.0.tgz#a94c2224ebcac04782a0d9035521f24735b7ec92"
Expand Down Expand Up @@ -4121,6 +4134,11 @@ pify@^2.0.0, pify@^2.3.0:
resolved "https://registry.yarnpkg.com/pify/-/pify-2.3.0.tgz#ed141a6ac043a849ea588498e7dca8b15330e90c"
integrity sha1-7RQaasBDqEnqWISY59yosVMw6Qw=

pify@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/pify/-/pify-3.0.0.tgz#e5a4acd2c101fdf3d9a4d07f0dbc4db49dd28176"
integrity sha1-5aSs0sEB/fPZpNB/DbxNtJ3SgXY=

pinkie-promise@^2.0.0:
version "2.0.1"
resolved "https://registry.yarnpkg.com/pinkie-promise/-/pinkie-promise-2.0.1.tgz#2135d6dfa7a358c069ac9b178776288228450ffa"
Expand Down

0 comments on commit ef51b23

Please sign in to comment.