diff --git a/lib/builder.js b/lib/builder.js index 1748c07..cb674eb 100644 --- a/lib/builder.js +++ b/lib/builder.js @@ -34,7 +34,7 @@ module.exports = Builder; * @api private */ -function Builder(dir, parent) { +function Builder(dir, parent, config) { var self = this; this._cache = {}; this._hooks = {}; @@ -57,7 +57,7 @@ function Builder(dir, parent) { }; // load config - this.config = this.json(); + this.config = config || this.json(); this.config.paths = this.config.paths || []; this.basename = this.root diff --git a/test/builder.js b/test/builder.js index 310af49..362f33c 100644 --- a/test/builder.js +++ b/test/builder.js @@ -103,6 +103,19 @@ describe('Builder', function(){ }) }) + it('should build js with config injected', function(done){ + var builder = new Builder('test/fixtures/hello-no-json', null, require('./fixtures/hello-no-json/no-component.json')); + builder.addLookup('test/fixtures'); + builder.build(function(err, res){ + if (err) return done(err); + var out = read('test/fixtures/hello.js', 'utf8'); + res.js.trim().should.equal(out.trim()); + done(); + }) + }) + + + it('should build css', function(done){ var builder = new Builder('test/fixtures/hello'); builder.addLookup('test/fixtures'); @@ -114,6 +127,18 @@ describe('Builder', function(){ }) }) + it('should build css with config injected', function(done){ + var builder = new Builder('test/fixtures/hello', null, require('./fixtures/hello-no-json/no-component.json')); + builder.addLookup('test/fixtures'); + builder.build(function(err, res){ + if (err) return done(err); + var out = read('test/fixtures/hello.css', 'utf8'); + res.css.should.equal(out); + done(); + }) + }) + + it('should load the require.js script', function(done){ var builder = new Builder('test/fixtures/hello'); builder.addLookup('test/fixtures'); @@ -167,6 +192,25 @@ describe('Builder', function(){ }); }) + it('should symlink .images with config injected', function(done){ + var builder = new Builder('test/fixtures/assets-no-json', null, require('./fixtures/assets-no-json/no-component.json')); + builder.addLookup('test/fixtures'); + builder.copyAssetsTo('/tmp/build'); + builder.build(function(err, res){ + if (err) return done(err); + assert(3 == res.images.length); + assert('/tmp/build/assets/images/logo.png' == res.images[0]); + assert('/tmp/build/assets/images/maru.jpeg' == res.images[1]); + assert('/tmp/build/assets/images/npm.png' == res.images[2]); + assert(exists('/tmp/build/assets/images/maru.jpeg')); + assert(exists('/tmp/build/assets/images/logo.png')); + assert(exists('/tmp/build/assets/images/npm.png')); + done(); + }); + }) + + + it('should symlink .files', function(done){ var builder = new Builder('test/fixtures/assets-parent'); builder.addLookup('test/fixtures'); @@ -205,6 +249,29 @@ describe('Builder', function(){ }); }) + it('should copy .images in copyFiles mode when config injected', function(done){ + var builder = new Builder('test/fixtures/assets', null, require('./fixtures/assets-no-json/no-component.json')); + builder.copyFiles(); + builder.addLookup('test/fixtures'); + builder.copyAssetsTo('/tmp/build'); + builder.build(function(err, res){ + if (err) return done(err); + assert(3 == res.images.length); + assert('/tmp/build/assets/images/logo.png' == res.images[0]); + assert('/tmp/build/assets/images/maru.jpeg' == res.images[1]); + assert('/tmp/build/assets/images/npm.png' == res.images[2]); + assert(exists('/tmp/build/assets/images/maru.jpeg')); + assert(exists('/tmp/build/assets/images/logo.png')); + assert(exists('/tmp/build/assets/images/npm.png')); + // images aren't symlinks + fs.lstat('/tmp/build/assets/images/npm.png', function(err, stats) { + assert(stats.isSymbolicLink() == false); + done(); + }); + }); + }) + + it('should copy .files in copyFiles mode', function(done){ var builder = new Builder('test/fixtures/assets-parent'); builder.copyFiles(); diff --git a/test/fixtures/assets-no-json/css/sub.css b/test/fixtures/assets-no-json/css/sub.css new file mode 100644 index 0000000..5cf3810 --- /dev/null +++ b/test/fixtures/assets-no-json/css/sub.css @@ -0,0 +1,3 @@ +body { + background-image: url(../images/npm.png); +} \ No newline at end of file diff --git a/test/fixtures/assets-no-json/images/logo.png b/test/fixtures/assets-no-json/images/logo.png new file mode 100644 index 0000000..d9f4373 Binary files /dev/null and b/test/fixtures/assets-no-json/images/logo.png differ diff --git a/test/fixtures/assets-no-json/images/maru.jpeg b/test/fixtures/assets-no-json/images/maru.jpeg new file mode 100644 index 0000000..af77a62 Binary files /dev/null and b/test/fixtures/assets-no-json/images/maru.jpeg differ diff --git a/test/fixtures/assets-no-json/images/npm.png b/test/fixtures/assets-no-json/images/npm.png new file mode 100644 index 0000000..57fca76 Binary files /dev/null and b/test/fixtures/assets-no-json/images/npm.png differ diff --git a/test/fixtures/assets-no-json/no-component.json b/test/fixtures/assets-no-json/no-component.json new file mode 100644 index 0000000..d17902b --- /dev/null +++ b/test/fixtures/assets-no-json/no-component.json @@ -0,0 +1,16 @@ +{ + "name": "assets", + "description": "a bunch of assets", + "images": [ + "images/logo.png", + "images/maru.jpeg", + "images/npm.png" + ], + "files": [ + "some.txt" + ], + "styles": [ + "style.css", + "css/sub.css" + ] +} \ No newline at end of file diff --git a/test/fixtures/assets-no-json/some.txt b/test/fixtures/assets-no-json/some.txt new file mode 100644 index 0000000..9dbfeee --- /dev/null +++ b/test/fixtures/assets-no-json/some.txt @@ -0,0 +1 @@ +Some text \ No newline at end of file diff --git a/test/fixtures/assets-no-json/style.css b/test/fixtures/assets-no-json/style.css new file mode 100644 index 0000000..58e86dc --- /dev/null +++ b/test/fixtures/assets-no-json/style.css @@ -0,0 +1,21 @@ +.logo { + background: url(images/logo.png); +} + +#maru { + width: 500px; + height: 500px; + background: url("images/maru.jpeg") no-repeat; +} + +#manny { + background: url(http://example.com/images/manny.png); +} + +#bob { + background-image: url(data:image/png;base64,PNG DATA HERE) +} + +#bar { + background-image: url(/public/images/foo.png) +} diff --git a/test/fixtures/hello-no-json/bar.css b/test/fixtures/hello-no-json/bar.css new file mode 100644 index 0000000..ec38515 --- /dev/null +++ b/test/fixtures/hello-no-json/bar.css @@ -0,0 +1,3 @@ +bar { + baz: 'raz'; +} \ No newline at end of file diff --git a/test/fixtures/hello-no-json/bar.js b/test/fixtures/hello-no-json/bar.js new file mode 100644 index 0000000..770188a --- /dev/null +++ b/test/fixtures/hello-no-json/bar.js @@ -0,0 +1 @@ +module.exports = 'bar'; \ No newline at end of file diff --git a/test/fixtures/hello-no-json/foo.css b/test/fixtures/hello-no-json/foo.css new file mode 100644 index 0000000..84f5ffd --- /dev/null +++ b/test/fixtures/hello-no-json/foo.css @@ -0,0 +1,3 @@ +foo { + bar: 'baz'; +} \ No newline at end of file diff --git a/test/fixtures/hello-no-json/foo.js b/test/fixtures/hello-no-json/foo.js new file mode 100644 index 0000000..d00e0a2 --- /dev/null +++ b/test/fixtures/hello-no-json/foo.js @@ -0,0 +1 @@ +module.exports = 'foo'; \ No newline at end of file diff --git a/test/fixtures/hello-no-json/no-component.json b/test/fixtures/hello-no-json/no-component.json new file mode 100644 index 0000000..455c874 --- /dev/null +++ b/test/fixtures/hello-no-json/no-component.json @@ -0,0 +1,10 @@ +{ + "name": "hello", + "description": "hello world component", + "version": "0.0.1", + "dependencies": { + "component/emitter": "*" + }, + "scripts": ["foo.js", "bar.js"], + "styles": ["foo.css", "bar.css"] +} \ No newline at end of file