Skip to content

Commit

Permalink
setup tracking, and failing of build based on file size (apollographq…
Browse files Browse the repository at this point in the history
  • Loading branch information
James Baxley committed May 12, 2016
1 parent 8623c73 commit 2da1037
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ node_modules
# don't commit compiled files
lib
test-lib
dist
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ script:
- npm test
- npm run coverage
- coveralls < ./coverage/lcov.info || true # ignore coveralls error

- npm run filesize

# Allow Travis tests to run in containers.
sudo: false

Expand Down
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
"pretest": "npm run compile",
"test": "mocha --require ./test/fixtures/setup.js --reporter spec --full-trace --recursive ./lib/test",
"posttest": "npm run lint",
"filesize": "npm run compile:browser && ./scripts/filesize.js --file=./dist/index.min.js --maxGzip=13",
"compile": "tsc",
"compile:browser": "rm -rf ./dist && mkdir ./dist && browserify ./lib/src/index.js --i react --i apollo-client -o=./dist/index.js && npm run minify:browser",
"minify:browser": "uglifyjs --compress --mangle --screw-ie8 -o=./dist/index.min.js -- ./dist/index.js",
"watch": "tsc -w",
"lint": "tslint src/*.ts* && tslint test/*.ts*",
"coverage": "istanbul cover ./node_modules/mocha/bin/_mocha -- --require ./test/fixtures/setup.js --reporter spec --full-trace --recursive ./lib/test",
Expand Down Expand Up @@ -35,15 +38,20 @@
},
"devDependencies": {
"apollo-client": "^0.3.0",
"browserify": "^13.0.0",
"chai": "^3.5.0",
"chai-as-promised": "^5.2.0",
"chai-enzyme": "^0.4.2",
"cheerio": "^0.20.0",
"colors": "^1.1.2",
"enzyme": "^2.2.0",
"graphql": "^0.5.0",
"gzip-size": "^3.0.0",
"istanbul": "^0.4.2",
"jsdom": "^8.3.1",
"minimist": "^1.2.0",
"mocha": "^2.3.3",
"pretty-bytes": "^3.0.1",
"react": "^15.0.1",
"react-addons-test-utils": "^15.0.1",
"react-dom": "^15.0.0",
Expand All @@ -54,7 +62,8 @@
"tslint": "^3.6.0",
"typescript": "^1.8.9",
"typescript-require": "^0.2.9-1",
"typings": "^0.7.9"
"typings": "^0.7.9",
"uglify-js": "^2.6.2"
},
"dependencies": {
"invariant": "^2.2.1",
Expand Down
45 changes: 45 additions & 0 deletions scripts/filesize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env node
'use strict';

const Flags = require('minimist')(process.argv.slice(2)),
Fs = require('fs'),
Path = require('path'),
GzipSize = require('gzip-size'),
PrettyBytes = require('pretty-bytes'),
Colors = require('colors');

if (!Flags.file) {
process.exit(1);
}

let totalSize = 0,
totalGzippedSize = 0,
filePath = Path.resolve(process.cwd(), Flags.file);

const rawSize = Fs.statSync(filePath).size;
totalSize = PrettyBytes(rawSize);
const rawGzippedSize = GzipSize.sync(Fs.readFileSync(filePath, 'utf8'));
totalGzippedSize = PrettyBytes(rawGzippedSize);

console.log('\n');
console.log('=============================== FileSize summary ===============================');
console.log(`The total size of ${Path.basename(filePath)} is ${Colors.green(totalSize)}.`);
console.log(`The total gzipped size of ${Path.basename(filePath)} is ${Colors.green(totalGzippedSize)}.`);
console.log('================================================================================');
console.log('\n');

if (Flags.max) {
const max = Number(Flags.max) * 1000; // kb to bytes
if (max > totalSize) {
process.exitCode = 1;
console.log(Colors.red(`The total size of ${Path.basename(filePath)} exceeds ${PrettyBytes(max)}.`));
}
}

if (Flags.maxGzip) {
const maxGzip = Number(Flags.maxGzip) * 1000; // kb to bytes
if (rawGzippedSize > maxGzip) {
process.exitCode = 1;
console.log(Colors.red(`The total gzipped size of ${Path.basename(filePath)} exceeds ${PrettyBytes(maxGzip)}.`));
}
}

0 comments on commit 2da1037

Please sign in to comment.