diff --git a/lib/build/bundler.js b/lib/build/bundler.js index 8969a5d9c..b9e3a7327 100644 --- a/lib/build/bundler.js +++ b/lib/build/bundler.js @@ -133,10 +133,13 @@ exports.Bundler = class { } build(opts) { - let onRequiringModule; + let onRequiringModule, onNotBundled; if (opts && typeof opts.onRequiringModule === 'function') { onRequiringModule = opts.onRequiringModule; } + if (opts && typeof opts.onNotBundled === 'function') { + onNotBundled = opts.onNotBundled; + } const doTranform = (initSet) => { let deps = new Set(initSet); @@ -224,6 +227,11 @@ exports.Bundler = class { return Promise.resolve() .then(() => doTranform()) + .then(() => { + if (!onNotBundled) return; + const notBundled = this.items.filter(t => !t.includedIn); + if (notBundled.length) onNotBundled(notBundled); + }) .catch(e => { logger.error('Failed to do transforms'); logger.info(e); diff --git a/spec/lib/build/bundler.spec.js b/spec/lib/build/bundler.spec.js index bcf8b24e5..b72a3bf14 100644 --- a/spec/lib/build/bundler.spec.js +++ b/spec/lib/build/bundler.spec.js @@ -883,7 +883,104 @@ describe('the Bundler module', () => { .catch(e => done.fail(e)); }); + it('build supports onNotBundled to report not bundled items', done => { + let project = { + paths: { + root: 'src', + foo: 'bar' + }, + build: { loader: {} } + }; + + let bundler = new Bundler(project, analyzer); + + bundler.items = [ + { + includedIn: 1, + moduleId: 'one', + transform: jasmine.createSpy('transform1') + .and.returnValues([], undefined) + }, + { + moduleId: 'two', + transform: jasmine.createSpy('transform2') + .and.returnValues([], undefined) + } + ]; + + let bundle = { + getRawBundledModuleIds: () => ['one'], + addAlias: jasmine.createSpy('addAlias') + }; + + bundler.bundles = [bundle]; + + bundler.addNpmResource = jasmine.createSpy('addNpmResource') + .and.returnValue(Promise.resolve()); + + let notBundled; + bundler.build({ + onNotBundled: function(items) { + notBundled = items; + } + }) + .then(() => { + expect(bundler.addNpmResource).not.toHaveBeenCalled(); + expect(bundle.addAlias).not.toHaveBeenCalled(); + expect(notBundled.length).toBe(1); + expect(notBundled[0].moduleId).toBe('two'); + done(); + }) + .catch(e => done.fail(e)); + }); + + it('build\'s onNotBundled is not called if there is no not-bundled', done => { + let project = { + paths: { + root: 'src', + foo: 'bar' + }, + build: { loader: {} } + }; + let bundler = new Bundler(project, analyzer); + + bundler.items = [ + { + includedIn: 1, + moduleId: 'one', + transform: jasmine.createSpy('transform1') + .and.returnValues([], undefined) + }, + { + includedIn: 1, + moduleId: 'two', + transform: jasmine.createSpy('transform2') + .and.returnValues([], undefined) + } + ]; + + let bundle = { + getRawBundledModuleIds: () => ['one'], + addAlias: jasmine.createSpy('addAlias') + }; + + bundler.bundles = [bundle]; + + bundler.addNpmResource = jasmine.createSpy('addNpmResource') + .and.returnValue(Promise.resolve()); + + const onNotBundled = jasmine.createSpy('onNotBundled'); + + bundler.build({onNotBundled}) + .then(() => { + expect(bundler.addNpmResource).not.toHaveBeenCalled(); + expect(bundle.addAlias).not.toHaveBeenCalled(); + expect(onNotBundled).not.toHaveBeenCalled(); + done(); + }) + .catch(e => done.fail(e)); + }); afterEach(() => { cliOptionsMock.detach(); });