Skip to content

Commit

Permalink
Merge pull request #578 from JeroenVinke/fix/paths-aureliajson
Browse files Browse the repository at this point in the history
Fix/paths aureliajson
  • Loading branch information
EisenbergEffect authored Apr 4, 2017
2 parents ad5314f + b23c8dd commit 5f8a6ee
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 6 deletions.
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;
}
10 changes: 5 additions & 5 deletions lib/commands/new/project-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,11 @@ exports.ProjectTemplate = class {

this.model.paths = Object.assign(this.model.paths, {
root: appRoot,
resources: this.resources.calculateRelativePath(this.root),
elements: this.elements.calculateRelativePath(this.root),
attributes: this.attributes.calculateRelativePath(this.root),
valueConverters: this.valueConverters.calculateRelativePath(this.root),
bindingBehaviors: this.bindingBehaviors.calculateRelativePath(this.root)
resources: this.resources.calculateRelativePath(this.src),
elements: this.elements.calculateRelativePath(this.src),
attributes: this.attributes.calculateRelativePath(this.src),
valueConverters: this.valueConverters.calculateRelativePath(this.src),
bindingBehaviors: this.bindingBehaviors.calculateRelativePath(this.src)
});

this.model.transpiler.source = path.posix.join(appRoot, '**/*' + this.model.transpiler.fileExtension);
Expand Down
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 5f8a6ee

Please sign in to comment.