From 88df1df45cd8774c3656f5f4a27380453b6d882c Mon Sep 17 00:00:00 2001 From: mseiler Date: Mon, 5 Mar 2018 13:06:46 +0100 Subject: [PATCH 1/2] Fix: invalid display of source files in Chrome DevTool and Firefox debugger because of sourceURL format --- spec/browser/sourceURL.spec.js | 64 ++++++++++++++++++++++++++++++++ src/browser-modules/eval.js | 4 +- src/browser-modules/sourceURL.js | 33 ++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 spec/browser/sourceURL.spec.js create mode 100644 src/browser-modules/sourceURL.js diff --git a/spec/browser/sourceURL.spec.js b/spec/browser/sourceURL.spec.js new file mode 100644 index 0000000..a498112 --- /dev/null +++ b/spec/browser/sourceURL.spec.js @@ -0,0 +1,64 @@ +var SourceURL = require('../../src/browser-modules/sourceURL.js'); +var expect = global.expect ? global.expect : require("expect.js"); + +describe("SourceURL", function() { + 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; + } +}; From d612ca3c963091c1e56cbb4484a2a9d07caf7680 Mon Sep 17 00:00:00 2001 From: Mathieu Seiler Date: Tue, 6 Mar 2018 20:25:18 +0100 Subject: [PATCH 2/2] Update sourceURL.spec.js --- Gruntfile.js | 3 +++ spec/browser/sourceURL.spec.js | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) 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 index a498112..02725f9 100644 --- a/spec/browser/sourceURL.spec.js +++ b/spec/browser/sourceURL.spec.js @@ -1,7 +1,22 @@ -var SourceURL = require('../../src/browser-modules/sourceURL.js'); 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 = {