From 0e82e36be7f447827375f56a490b17a047bcbc18 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Sun, 21 Oct 2018 14:25:09 -0700 Subject: [PATCH] Allow bundle content retrieval to be customized --- src/parseUtils.js | 8 ++++++-- test/parseUtils.js | 12 ++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/parseUtils.js b/src/parseUtils.js index 5169c00f..48b7ceeb 100644 --- a/src/parseUtils.js +++ b/src/parseUtils.js @@ -7,8 +7,12 @@ module.exports = { parseBundle }; -function parseBundle(bundlePath) { - const content = fs.readFileSync(bundlePath, 'utf8'); +const defaultBundleHost = { + getFileContent: path => fs.readFileSync(path, 'utf8') +}; + +function parseBundle(bundlePath, bundleHost = defaultBundleHost) { + const content = bundleHost.getFileContent(bundlePath); const ast = acorn.parse(content, { sourceType: 'script', // I believe in a bright future of ECMAScript! diff --git a/test/parseUtils.js b/test/parseUtils.js index f863e689..970dbfbe 100644 --- a/test/parseUtils.js +++ b/test/parseUtils.js @@ -30,4 +30,16 @@ describe('parseBundle', function () { expect(bundle.src).to.equal(fs.readFileSync(bundleFile, 'utf8')); expect(bundle.modules).to.deep.equal({}); }); + + it('should allow a custom bundle host to override file system operations', function () { + let getBundleContentArg; + const bundle = parseBundle('arg', { + getFileContent: bundlePath => { + getBundleContentArg = bundlePath; + return 'webpackJsonp([0],[(t,e,r)=>{}])'; + } + }); + expect(getBundleContentArg).to.equal('arg'); + expect(bundle.modules).to.deep.equal({0: '(t,e,r)=>{}'}); + }); });