diff --git a/lib/build/bundler.js b/lib/build/bundler.js index 11f7070c6..89b918bee 100644 --- a/lib/build/bundler.js +++ b/lib/build/bundler.js @@ -25,7 +25,7 @@ exports.Bundler = class { this.loaderConfig = { baseUrl: project.paths.root, - paths: {}, + paths: ensurePathsRelativelyFromRoot(project.paths || {}), packages: [], stubModules: [], shim: {} @@ -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; +} diff --git a/spec/lib/build/bundler.spec.js b/spec/lib/build/bundler.spec.js new file mode 100644 index 000000000..8e482fbe1 --- /dev/null +++ b/spec/lib/build/bundler.spec.js @@ -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(); + }); +}); diff --git a/spec/mocks/bundler.js b/spec/mocks/bundler.js index fbc3741fb..ef5127fe1 100644 --- a/spec/mocks/bundler.js +++ b/spec/mocks/bundler.js @@ -1,3 +1,5 @@ +'use strict'; + module.exports = class Bundler { constructor() { this.itemIncludedInBuild = jasmine.createSpy('itemIncludedInBuild'); diff --git a/spec/mocks/cli-options.js b/spec/mocks/cli-options.js new file mode 100644 index 000000000..5eb3b2d33 --- /dev/null +++ b/spec/mocks/cli-options.js @@ -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; + } +}; diff --git a/spec/mocks/package-analyzer.js b/spec/mocks/package-analyzer.js new file mode 100644 index 000000000..020b2719e --- /dev/null +++ b/spec/mocks/package-analyzer.js @@ -0,0 +1,6 @@ +'use strict'; + +module.exports = class PackageAnalyzer { + constructor() { + } +};