Skip to content

Commit

Permalink
feat: onNotBundled callback to report not bundled items
Browse files Browse the repository at this point in the history
  • Loading branch information
3cp committed May 15, 2020
1 parent 814e8b4 commit 62305d0
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/build/bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
97 changes: 97 additions & 0 deletions spec/lib/build/bundler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down

0 comments on commit 62305d0

Please sign in to comment.