Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use asset manifest to read bundle sizes #355

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"github-build": "^1.2.0",
"glob": "^7.1.4",
"gzip-size": "^4.0.0",
"minimatch": "^3.0.4",
"prettycli": "^1.4.3"
},
"lint-staged": {
Expand Down
13 changes: 12 additions & 1 deletion src/files.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
const fs = require('fs')
const path = require('path')
const bytes = require('bytes')
const glob = require('glob')
const { error } = require('prettycli')
const config = require('./config')
const debug = require('./debug')
const compressedSize = require('./compressed-size')
const minimatch = require("minimatch")
const files = []

config.map(file => {
const paths = glob.sync(file.path)
let paths = [];
if (file.assetManifest) {
const buildPath = path.dirname(file.assetManifest);
const rawData = fs.readFileSync(file.assetManifest, 'utf8');
const assets = JSON.parse(rawData);
paths = Object.values(assets).map(asset => `${buildPath}${asset}`)
.filter(filename => !file.path || minimatch(filename, file.path));
} else {
paths = glob.sync(file.path)
}
if (!paths.length) {
error(`There is no matching file for ${file.path} in ${process.cwd()}`, {
silent: true
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/12/build/asset-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"js/file-1.js": "/js/file-1.js"
}
22 changes: 22 additions & 0 deletions tests/fixtures/12/build/js/file-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
The contents of this file aren't important,
what's important is the size

269B gzip
240B brotli
437B without compression

*/

const { inspect } = require('util')
const files = require('./src/files')
const reporter = require('./src/reporter')
const build = require('./src/build')

reporter(files)

process.on('unhandledRejection', function(reason) {
console.log('Unhandled Promise')
console.log(inspect(reason))
build.error()
})
8 changes: 8 additions & 0 deletions tests/fixtures/12/bundlesize.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"files": [
{
"assetManifest": "build/asset-manifest.json",
"maxSize": "300B"
}
]
}
4 changes: 4 additions & 0 deletions tests/fixtures/13/build/asset-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"js/file-1.js": "/js/file-1.js",
"other/file-1.js": "/other/file-2.js"
}
22 changes: 22 additions & 0 deletions tests/fixtures/13/build/js/file-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
The contents of this file aren't important,
what's important is the size

269B gzip
240B brotli
437B without compression

*/

const { inspect } = require('util')
const files = require('./src/files')
const reporter = require('./src/reporter')
const build = require('./src/build')

reporter(files)

process.on('unhandledRejection', function(reason) {
console.log('Unhandled Promise')
console.log(inspect(reason))
build.error()
})
22 changes: 22 additions & 0 deletions tests/fixtures/13/build/other/file-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
The contents of this file aren't important,
what's important is the size

269B gzip
240B brotli
437B without compression

*/

const { inspect } = require('util')
const files = require('./src/files')
const reporter = require('./src/reporter')
const build = require('./src/build')

reporter(files)

process.on('unhandledRejection', function(reason) {
console.log('Unhandled Promise')
console.log(inspect(reason))
build.error()
})
9 changes: 9 additions & 0 deletions tests/fixtures/13/bundlesize.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"files": [
{
"assetManifest": "build/asset-manifest.json",
"path": "/js/*",
"maxSize": "300B"
}
]
}
12 changes: 12 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,15 @@ test.skip('11. bug repro: bundlesize should dedup files', t => {
t.is(exitCode, 0) // this is failing
t.snapshot(stdout)
})

test.serial('12. bug repro: bundlesize should read and process files from an asset manifest', t => {
const { stdout, exitCode } = run(12)
t.is(exitCode, 0) // this is failing
t.snapshot(stdout)
})

test.serial('13. bug repro: bundlesize should read and process files from an asset manifest and filter by path', t => {
const { stdout, exitCode } = run(12)
t.is(exitCode, 0) // this is failing
t.snapshot(stdout)
})
12 changes: 12 additions & 0 deletions tests/snapshots/index.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ Generated by [AVA](https://ava.li).
PASS build/chunks/chunk-ch0nk.js: 270B < maxSize 300B (gzip)`

## 12. bug repro: bundlesize should read and process files from an asset manifest

> Snapshot 1

'PASS build/js/file-1.js: 270B < maxSize 300B (gzip)'

## 13. bug repro: bundlesize should read and process files from an asset manifest and filter by path

> Snapshot 1

'PASS build/js/file-1.js: 270B < maxSize 300B (gzip)'

## 2. fail: single file larger than limit

> Snapshot 1
Expand Down
Binary file modified tests/snapshots/index.js.snap
Binary file not shown.