Skip to content

Commit

Permalink
fix(bundler): don't trace dependencies which have no main file
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenVinke committed Aug 12, 2017
1 parent 7f5e84c commit a2cf32b
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 15 deletions.
5 changes: 4 additions & 1 deletion lib/build/dependency-inclusion.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ exports.DependencyInclusion = class {
this.description = description;
this.items = [];
this.traceDependencies = true;
bundle.bundler.addFile(new DependencyFile(bundle, description), this);

if (description.loaderConfig.main !== false) {
bundle.bundler.addFile(new DependencyFile(bundle, description), this);
}
}

traceResources() {
Expand Down
37 changes: 23 additions & 14 deletions spec/lib/build/bundle.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const BundlerMock = require('../../mocks/bundler');
const Bundle = require('../../../lib/build/bundle').Bundle;
const CLIOptionsMock = require('../../mocks/cli-options');
const DependencyDescription = require('../../../lib/build/dependency-description').DependencyDescription;

describe('the Bundle module', () => {
let sut;
Expand Down Expand Up @@ -118,11 +119,16 @@ describe('the Bundle module', () => {
return true;
});
bundler.configureDependency.and.callFake(dep => {
return Promise.resolve({
loaderConfig: {
name: dep.name || dep
}
});
let depName = dep.name || dep;
let description = new DependencyDescription(depName);

description.loaderConfig = {
name: depName,
path: '../node_modules/' + depName,
main: 'index.js'
};

return Promise.resolve(description);
});

let config = {
Expand Down Expand Up @@ -211,10 +217,12 @@ describe('the Bundle module', () => {
let configuredDependencies = [];
bundler.configureDependency.and.callFake(dep => {
let depName = dep.name || dep;
let description = {
loaderConfig: {
name: depName
}

let description = new DependencyDescription(depName);
description.loaderConfig = {
name: depName,
path: '../node_modules/' + depName,
main: 'index.js'
};

return new Promise(resolve => {
Expand Down Expand Up @@ -262,11 +270,12 @@ describe('the Bundle module', () => {
it('add dependencies in the same order as they were entered to prevent a wrong module load order', done => {
let bundler = new BundlerMock();
bundler.configureDependency.and.callFake(dep => {
return Promise.resolve({
loaderConfig: {
name: dep.name || dep
}
});
let description = new DependencyDescription(dep.name || dep);
description.loaderConfig = {
name: dep.name || dep
};

return Promise.resolve(description);
});
bundler.itemIncludedInBuild.and.callFake((dep) => true);

Expand Down
100 changes: 100 additions & 0 deletions spec/lib/build/dependency-inclusion.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
'use strict';

const BundlerMock = require('../../mocks/bundler');
const DependencyInclusion = require('../../../lib/build/dependency-inclusion').DependencyInclusion;
const DependencyDescription = require('../../../lib/build/dependency-description').DependencyDescription;
const mockfs = require('mock-fs');

describe('the DependencyInclusion module', () => {
let bundler;

beforeEach(() => {
bundler = new BundlerMock();
});

afterEach(() => {
mockfs.restore();
});

it('adds a dependency file to the bundle when there is a main file', () => {
let bundle = {
bundler: bundler
};

let description = new DependencyDescription();
description.loaderConfig = {
path: '../node_modules/my-package',
name: 'my-package',
main: 'index.js'
};
// eslint-disable-next-line no-unused-vars
let sut = new DependencyInclusion(bundle, description);

expect(bundler.addFile).toHaveBeenCalled();
});

it('adds a dependency file to the bundle when the path is a file', () => {
mockfs({
'../node_modules': {
'my-package': {
'index.js': 'some-content'
}
}
});

let bundle = {
bundler: bundler
};

let description = new DependencyDescription();
description.loaderConfig = {
path: '../node_modules/my-package/index.js',
name: 'my-package'
};

// eslint-disable-next-line no-unused-vars
let sut = new DependencyInclusion(bundle, description);

expect(bundler.addFile).toHaveBeenCalled();
});

it('does not add dependency file to the bundle when main is set to false', () => {
mockfs();

let bundle = {
bundler: bundler
};

let description = new DependencyDescription();
description.loaderConfig = {
path: '../node_modules/my-package',
name: 'my-package',
main: false
};

// eslint-disable-next-line no-unused-vars
let sut = new DependencyInclusion(bundle, description);

expect(bundler.addFile).not.toHaveBeenCalled();
});

it('adds dependency file to the bundle when main is set to something other than false', () => {
mockfs();

let bundle = {
bundler: bundler
};

let description = new DependencyDescription();
description.loaderConfig = {
path: '../node_modules/my-package',
name: 'my-package',
main: 'my-main-file.js'
};

// eslint-disable-next-line no-unused-vars
let sut = new DependencyInclusion(bundle, description);

expect(bundler.addFile).toHaveBeenCalled();
});
});
4 changes: 4 additions & 0 deletions spec/mocks/bundler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

const Configuration = require('../../lib/configuration').Configuration;
const CLIOptions = require('../../lib/cli-options').CLIOptions;
const ProjectMock = require('./project-mock');

module.exports = class Bundler {
constructor() {
Expand All @@ -9,6 +11,8 @@ module.exports = class Bundler {
this.configureDependency = jasmine.createSpy('configureDependency');
this.addFile = jasmine.createSpy('addFile');

CLIOptions.instance = new CLIOptions();
this.buildOptions = new Configuration({}, {});
this.project = new ProjectMock();
}
};
9 changes: 9 additions & 0 deletions spec/mocks/project-mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = class ProjectMock {
constructor() {
this.paths = {
root: ''
};
}
};

0 comments on commit a2cf32b

Please sign in to comment.