diff --git a/Gruntfile.js b/Gruntfile.js index cfbe72d..1752db2 100755 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -178,6 +178,9 @@ module.exports = function(grunt) { 'spec/browser/json2.js', 'spec/browser/injectNoder.js', 'spec/browser/**/*.spec.js', { + pattern: 'src/browser-modules/**', + included: false + }, { pattern: 'dist/browser/**', included: false }, { diff --git a/spec/browser/sourceURL.spec.js b/spec/browser/sourceURL.spec.js new file mode 100644 index 0000000..02725f9 --- /dev/null +++ b/spec/browser/sourceURL.spec.js @@ -0,0 +1,79 @@ +var expect = global.expect ? global.expect : require("expect.js"); +var noder = global.noder || require('../../dist/node/noder.js'); + +describe("SourceURL", function() { + var SourceURL; + if (global.noder) { + var newRootModule = noder.createContext({ + packaging: { + requestConfig: { + sync: true + }, + baseUrl: "/base/src/browser-modules/" + } + }); + SourceURL = newRootModule.require('sourceURL.js'); + } else { + SourceURL = require('../../src/browser-modules/sourceURL.js'); + } + + it('should generate sourceURL Basic', function() { + var src = "util/json"; + var location = { + protocol: "http:", + hostname: "test.aria.com" + }; + var sourceURL = SourceURL.generateSourceURL(location, src); + expect(sourceURL).to.equal('http://test.aria.com/util/json'); + }); + + it('should generate sourceURL Relative', function() { + var src = "./util/json"; + var location = { + protocol: "http:", + pathname: "/dist", + hostname: "test.aria.com" + }; + var sourceURL = SourceURL.generateSourceURL(location, src); + expect(sourceURL).to.equal('http://test.aria.com/dist/util/json'); + }); + + it('should generate sourceURL Absolute', function() { + var src = "/util/json"; + var location = { + protocol: "http:", + hostname: "test.aria.com" + }; + var sourceURL = SourceURL.generateSourceURL(location, src); + expect(sourceURL).to.equal('http://test.aria.com/util/json'); + }); + + it('should generate sourceURL With URL', function() { + var src = "http://test.aria.com/util/json"; + var location = { + protocol: "http:", + pathname: "/dist", + hostname: "test.aria.com" + }; + var sourceURL = SourceURL.generateSourceURL(location, src); + expect(sourceURL).to.equal('http://test.aria.com/util/json'); + }); + + it('should generate sourceURL With Scheme', function() { + var src = "aria://util/json"; + var location = { + protocol: "http:", + pathname: "/dist", + hostname: "test.aria.com" + }; + var sourceURL = SourceURL.generateSourceURL(location, src); + expect(sourceURL).to.equal('aria://util/json'); + }); + + it('should generate sourceURL NoLocation', function() { + var src = "util/json"; + var sourceURL = SourceURL.generateSourceURL(undefined, src); + expect(sourceURL).to.equal('util/json'); + }); + +}); diff --git a/src/browser-modules/eval.js b/src/browser-modules/eval.js index 4d278d4..050b315 100755 --- a/src/browser-modules/eval.js +++ b/src/browser-modules/eval.js @@ -12,12 +12,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +var location = global.location; +var SourceURL = require("../sourceURL.js"); module.exports = function(code, fileName) { var res = {}; // Using the 'arguments[1].res = ...' trick because IE does not let eval return a function if (fileName) { - code = ['/*\n * File: ', fileName, '\n */\narguments[1].res=', code, '\n//# sourceURL=', fileName].join(''); + code = ['/*\n * File: ', fileName, '\n */\narguments[1].res=', code, '\n//# sourceURL=', SourceURL.generateSourceURL(location, fileName)].join(''); } else { code = 'arguments[1].res=' + code; } diff --git a/src/browser-modules/sourceURL.js b/src/browser-modules/sourceURL.js new file mode 100644 index 0000000..f539b52 --- /dev/null +++ b/src/browser-modules/sourceURL.js @@ -0,0 +1,33 @@ +/* + * Copyright 2012 Amadeus s.a.s. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +module.exports = { + generateSourceURL: function(location, src) { + var sourceURL = src; + // Check if src match a an URL + if (typeof src === "string" && location && !(/[a-z]*:\/\//.test(src))) { + sourceURL = location.protocol + "//" + location.hostname; + + if (src.length > 0 && src[0] === "/") { + sourceURL += src; + } else if (src.length > 1 && src[0] === "." && src[1] === "/") { + sourceURL += location.pathname + src.substring(1); + } else { + sourceURL += "/" + src; + } + } + + return sourceURL; + } +};