From d49eb2456baa78d394d532e2f6e9887ab6d8bf8b Mon Sep 17 00:00:00 2001 From: Dominic Barnes Date: Wed, 18 Jun 2014 02:04:20 -0500 Subject: [PATCH] adding asset-path rewriting (similar to css url-rewriting) for scripts --- lib/builders/scripts.js | 20 +++++++++++++++- test/fixtures/js-asset-path/assets/foo.txt | 1 + test/fixtures/js-asset-path/component.json | 11 +++++++++ test/fixtures/js-asset-path/index.js | 1 + test/fixtures/js-asset-path/lib/index.js | 1 + test/fixtures/js-asset-path/test.txt | 1 + test/scripts.js | 28 ++++++++++++++++++++++ 7 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/js-asset-path/assets/foo.txt create mode 100644 test/fixtures/js-asset-path/component.json create mode 100644 test/fixtures/js-asset-path/index.js create mode 100644 test/fixtures/js-asset-path/lib/index.js create mode 100644 test/fixtures/js-asset-path/test.txt diff --git a/lib/builders/scripts.js b/lib/builders/scripts.js index 8adfc36..d3cf124 100644 --- a/lib/builders/scripts.js +++ b/lib/builders/scripts.js @@ -1,6 +1,7 @@ var debug = require('debug')('component-builder:scripts'); -var relative = require('path').relative; +var path = require('path'); +var relative = path.relative; var requires = require('requires'); var fs = require('graceful-fs'); var url = require('url'); @@ -201,6 +202,12 @@ Scripts.prototype.register = function (file) { + '")'; }); + // rewrite asset paths + js = assetPaths(js, function (asset) { + asset = relative(file.manifest.path, path.resolve(path.dirname(file.filename), asset)); + return path.join(utils.rewriteUrl(file.branch), asset); + }); + var name = file.name; if (this.sourceMap || this.sourceURL) { if (this.sourceMap && file.sourceMap) { @@ -419,3 +426,14 @@ Scripts.prototype.lookupDependency = function (file, target) { debug('could not resolve "%s" from "%s"', target, file.name) return target } + + + +// private helpers + +function assetPaths(source, replacer) { + return source.replace(/\/\* component:file \*\/\s+['"](\S+)['"]/g, function (match, p1) { + var replacement = replacer(p1); + return replacement ? JSON.stringify(replacement) : match; + }); +} diff --git a/test/fixtures/js-asset-path/assets/foo.txt b/test/fixtures/js-asset-path/assets/foo.txt new file mode 100644 index 0000000..980a0d5 --- /dev/null +++ b/test/fixtures/js-asset-path/assets/foo.txt @@ -0,0 +1 @@ +Hello World! diff --git a/test/fixtures/js-asset-path/component.json b/test/fixtures/js-asset-path/component.json new file mode 100644 index 0000000..417a333 --- /dev/null +++ b/test/fixtures/js-asset-path/component.json @@ -0,0 +1,11 @@ +{ + "name": "js-asset-path", + "scripts": [ + "index.js", + "lib/index.js" + ], + "files": [ + "test.txt", + "assets/foo.txt" + ] +} diff --git a/test/fixtures/js-asset-path/index.js b/test/fixtures/js-asset-path/index.js new file mode 100644 index 0000000..85f3f9f --- /dev/null +++ b/test/fixtures/js-asset-path/index.js @@ -0,0 +1 @@ +var path = /* component:file */ "test.txt"; diff --git a/test/fixtures/js-asset-path/lib/index.js b/test/fixtures/js-asset-path/lib/index.js new file mode 100644 index 0000000..40c689f --- /dev/null +++ b/test/fixtures/js-asset-path/lib/index.js @@ -0,0 +1 @@ +var path = /* component:file */ "../assets/foo.txt"; diff --git a/test/fixtures/js-asset-path/test.txt b/test/fixtures/js-asset-path/test.txt new file mode 100644 index 0000000..b0ee791 --- /dev/null +++ b/test/fixtures/js-asset-path/test.txt @@ -0,0 +1 @@ +Lorem ipsum solor dit amet... diff --git a/test/scripts.js b/test/scripts.js index bc9818a..787c8cd 100644 --- a/test/scripts.js +++ b/test/scripts.js @@ -425,3 +425,31 @@ describe('js-page.js', function () { }) }) +describe('js-asset-path', function () { + var tree; + var js = Builder.require; + + it('should install', co(function* () { + tree = yield* resolve(fixture('js-asset-path'), options); + })) + + it('should build', co(function* () { + js += yield build(tree).end(); + })) + + it('should execute', function () { + var ctx = vm.createContext(); + vm.runInContext(js, ctx); + vm.runInContext('require("js-asset-path")', ctx); + }) + + it('should rewrite asset path comments', function () { + js.should.not.include('/* component:file */ "test.txt"'); + js.should.include('"js-asset-path/test.txt"'); + }); + + it("should be able to handle relative paths", function () { + js.should.not.include('/* component:file */ "../assets/foo.txt"'); + js.should.include('"js-asset-path/assets/foo.txt"'); + }); +})