Skip to content

Commit

Permalink
fix(bundler): use paths from aurelia.json
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenVinke committed Apr 1, 2017
1 parent 84952de commit 625929c
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 1 deletion.
25 changes: 24 additions & 1 deletion lib/build/bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ exports.Bundler = class {

this.loaderConfig = {
baseUrl: project.paths.root,
paths: {},
paths: ensurePathsRelativelyFromRoot(project.paths || {}),
packages: [],
stubModules: [],
shim: {}
Expand Down Expand Up @@ -234,3 +234,26 @@ function subsume(bundles, item) {
function normalizeKey(p) {
return path.normalize(p);
}

function ensurePathsRelativelyFromRoot(p) {
let keys = Object.keys(p);
let original = JSON.stringify(p, null, 2);
let warn = false;

for (let i = 0; i < keys.length; i++) {
let key = keys[i];
if (key !== 'root' && p[key].indexOf(p.root + '/') === 0) {
warn = true;
p[key] = p[key].slice(p.root.length + 1);
}
}

if (warn) {
console.log('Warning: paths in the "paths" object in aurelia.json must be relative from the root path. Change ');
console.log(original);
console.log('to: ');
console.log(JSON.stringify(p, null, 2));
}

return p;
}
56 changes: 56 additions & 0 deletions spec/lib/build/bundler.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
'use strict';

const Bundler = require('../../../lib/build/bundler').Bundler;
const PackageAnalyzer = require('../../mocks/package-analyzer');
const CLIOptionsMock = require('../../mocks/cli-options');

describe('the Bundler module', () => {
let analyzer;
let cliOptionsMock;

beforeEach(() => {
analyzer = new PackageAnalyzer();
cliOptionsMock = new CLIOptionsMock();
cliOptionsMock.attach();
});

it('uses paths.root from aurelia.json in the loaderConfig as baseUrl', () => {
let project = {
paths: {
root: 'src'
},
build: { loader: {} }
};
let bundler = new Bundler(project, analyzer);
expect(bundler.loaderConfig.baseUrl).toBe('src');
});

it('takes paths from aurelia.json and uses it in the loaderConfig', () => {
let project = {
paths: {
root: 'src',
foo: 'bar'
},
build: { loader: {} }
};
let bundler = new Bundler(project, analyzer);
expect(bundler.loaderConfig.paths.root).toBe('src');
expect(bundler.loaderConfig.paths.foo).toBe('bar');
});

it('ensures that paths in aurelia.json are relative from the root path', () => {
let project = {
paths: {
root: 'src',
foo: 'src/bar'
},
build: { loader: {} }
};
let bundler = new Bundler(project, analyzer);
expect(bundler.loaderConfig.paths.foo).toBe('bar');
});

afterEach(() => {
cliOptionsMock.detach();
});
});
2 changes: 2 additions & 0 deletions spec/mocks/bundler.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

module.exports = class Bundler {
constructor() {
this.itemIncludedInBuild = jasmine.createSpy('itemIncludedInBuild');
Expand Down
19 changes: 19 additions & 0 deletions spec/mocks/cli-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

let OriginalCLIOptions = require('../../lib/cli-options').CLIOptions;

module.exports = class CLIOptionsMock {

constructor() {
this.originalFns = {};
}

attach() {
this.originalFns.getEnvironment = OriginalCLIOptions.prototype.getEnvironment;
OriginalCLIOptions.getEnvironment = jasmine.createSpy('getEnvironment');
}

detach() {
OriginalCLIOptions.getEnvironment = this.originalFns.getEnvironment;
}
};
6 changes: 6 additions & 0 deletions spec/mocks/package-analyzer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

module.exports = class PackageAnalyzer {
constructor() {
}
};

0 comments on commit 625929c

Please sign in to comment.