diff --git a/package.json b/package.json index 08ad538a..1d32b04c 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/files.js b/src/files.js index 586231fc..dd30267e 100644 --- a/src/files.js +++ b/src/files.js @@ -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 diff --git a/tests/fixtures/12/build/asset-manifest.json b/tests/fixtures/12/build/asset-manifest.json new file mode 100644 index 00000000..f4f46965 --- /dev/null +++ b/tests/fixtures/12/build/asset-manifest.json @@ -0,0 +1,3 @@ +{ + "js/file-1.js": "/js/file-1.js" +} diff --git a/tests/fixtures/12/build/js/file-1.js b/tests/fixtures/12/build/js/file-1.js new file mode 100644 index 00000000..b00c696e --- /dev/null +++ b/tests/fixtures/12/build/js/file-1.js @@ -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() +}) diff --git a/tests/fixtures/12/bundlesize.config.json b/tests/fixtures/12/bundlesize.config.json new file mode 100644 index 00000000..f5b5895d --- /dev/null +++ b/tests/fixtures/12/bundlesize.config.json @@ -0,0 +1,8 @@ +{ + "files": [ + { + "assetManifest": "build/asset-manifest.json", + "maxSize": "300B" + } + ] +} diff --git a/tests/fixtures/13/build/asset-manifest.json b/tests/fixtures/13/build/asset-manifest.json new file mode 100644 index 00000000..c6668702 --- /dev/null +++ b/tests/fixtures/13/build/asset-manifest.json @@ -0,0 +1,4 @@ +{ + "js/file-1.js": "/js/file-1.js", + "other/file-1.js": "/other/file-2.js" +} diff --git a/tests/fixtures/13/build/js/file-1.js b/tests/fixtures/13/build/js/file-1.js new file mode 100644 index 00000000..b00c696e --- /dev/null +++ b/tests/fixtures/13/build/js/file-1.js @@ -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() +}) diff --git a/tests/fixtures/13/build/other/file-2.js b/tests/fixtures/13/build/other/file-2.js new file mode 100644 index 00000000..b00c696e --- /dev/null +++ b/tests/fixtures/13/build/other/file-2.js @@ -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() +}) diff --git a/tests/fixtures/13/bundlesize.config.json b/tests/fixtures/13/bundlesize.config.json new file mode 100644 index 00000000..c7746c12 --- /dev/null +++ b/tests/fixtures/13/bundlesize.config.json @@ -0,0 +1,9 @@ +{ + "files": [ + { + "assetManifest": "build/asset-manifest.json", + "path": "/js/*", + "maxSize": "300B" + } + ] +} diff --git a/tests/index.js b/tests/index.js index bc298d5f..bf300502 100644 --- a/tests/index.js +++ b/tests/index.js @@ -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) +}) diff --git a/tests/snapshots/index.js.md b/tests/snapshots/index.js.md index a1cd5a60..d171991f 100644 --- a/tests/snapshots/index.js.md +++ b/tests/snapshots/index.js.md @@ -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 diff --git a/tests/snapshots/index.js.snap b/tests/snapshots/index.js.snap index ed124eb2..00a3a80b 100644 Binary files a/tests/snapshots/index.js.snap and b/tests/snapshots/index.js.snap differ