From 759fde80c4d3f3e2de2c3b72dbdf524d038ca0f4 Mon Sep 17 00:00:00 2001 From: EugeneZ Date: Wed, 23 Nov 2016 20:35:27 -0800 Subject: [PATCH 1/3] Fixes #127 Sourcemap issues fixed --- package.json | 2 ++ src/browserify-plugin/main.js | 14 +++++++++++++- src/reloading.js | 13 +++++++++++-- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 585c8cc..a877d43 100644 --- a/package.json +++ b/package.json @@ -28,8 +28,10 @@ }, "dependencies": { "cli-color": "1.1.0", + "convert-source-map": "1.3.0", "lodash": "4.13.1", "md5": "2.1.0", + "offset-sourcemap-lines": "1.0.0", "through2": "2.0.1", "umd": "3.0.1", "ws": "1.1.1" diff --git a/src/browserify-plugin/main.js b/src/browserify-plugin/main.js index 29d1be2..3a25185 100644 --- a/src/browserify-plugin/main.js +++ b/src/browserify-plugin/main.js @@ -4,6 +4,8 @@ import through from "through2" import md5 from "md5" import {readFileSync} from "fs" import {resolve} from "path" +import convertSourceMaps from 'convert-source-map' +import offsetSourceMaps from 'offset-sourcemap-lines' import {startServer} from "./server" import {log} from "./console" import loader from "../reloading" @@ -96,10 +98,20 @@ function LiveReactloadPlugin(b, opts = {}) { b.pipeline.get("label").push(through.obj( function transform(row, enc, next) { const {id, file, source, deps, entry} = row + const converter = convertSourceMaps.fromSource(source) + let sourceWithoutMaps = source + let adjustedSourcemap = '' + + if (converter) { + converter.setProperty('sources', [file]) + sourceWithoutMaps = convertSourceMaps.removeComments(source) + adjustedSourcemap = convertSourceMaps.fromObject(offsetSourceMaps(converter.toObject(), 1)).toComment() + } + if (entry) { entries.push(file) } - mappings[file] = [source, deps, {id: file, hash: md5(source), browserifyId: id}] + mappings[file] = [sourceWithoutMaps, deps, {id: file, hash: md5(sourceWithoutMaps), browserifyId: id, sourcemap: adjustedSourcemap}] next(null, row) }, function flush(next) { diff --git a/src/reloading.js b/src/reloading.js index cfe2ec5..aa262ae 100644 --- a/src/reloading.js +++ b/src/reloading.js @@ -84,7 +84,7 @@ function loader(mappings, entryPoints, options) { var body = mapping[0]; if (typeof body !== "function") { debug("Compiling module", mapping[2]) - var compiled = new Function("require", "module", "exports", body); + var compiled = loadAsModule(body, mapping[2].sourcemap); mapping[0] = compiled; mapping[2].source = body; } @@ -428,8 +428,17 @@ function loader(mappings, entryPoints, options) { function error(msg) { console.error("LiveReactload ::", msg); } -} + function loadAsModule(__livereactload_source, __livereactload_sourcemap) { + return eval( + 'function __livereactload_module(require, module, exports){\n' + + __livereactload_source + + '\n}; __livereactload_module;' + + (__livereactload_sourcemap || '') + ); + } + +} module.exports = loader; module.exports["default"] = loader; From f51d5bb6d3956385a3bbb03895cba02d3496cbd2 Mon Sep 17 00:00:00 2001 From: EugeneZ Date: Wed, 23 Nov 2016 21:18:32 -0800 Subject: [PATCH 2/3] New sourcemaps when the file changes, basepath replaced with hash --- src/browserify-plugin/main.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/browserify-plugin/main.js b/src/browserify-plugin/main.js index 3a25185..f986a39 100644 --- a/src/browserify-plugin/main.js +++ b/src/browserify-plugin/main.js @@ -18,6 +18,7 @@ function LiveReactloadPlugin(b, opts = {}) { client = true, dedupe = true, debug = false, + basedir = process.cwd(), 'ssl-cert': sslCert = null, 'ssl-key': sslKey = null, } = opts @@ -101,11 +102,15 @@ function LiveReactloadPlugin(b, opts = {}) { const converter = convertSourceMaps.fromSource(source) let sourceWithoutMaps = source let adjustedSourcemap = '' + let hash; if (converter) { - converter.setProperty('sources', [file]) sourceWithoutMaps = convertSourceMaps.removeComments(source) + hash = md5(sourceWithoutMaps) + converter.setProperty('sources', [file.replace(basedir, hash)]) adjustedSourcemap = convertSourceMaps.fromObject(offsetSourceMaps(converter.toObject(), 1)).toComment() + } else { + hash = md5(source) } if (entry) { From fb7ea0170e0dd808a0268e8d194183445b595a8b Mon Sep 17 00:00:00 2001 From: EugeneZ Date: Wed, 23 Nov 2016 21:31:55 -0800 Subject: [PATCH 3/3] Sideloading module loader to avoid scoping issues --- src/browserify-plugin/main.js | 12 +++++++++++- src/reloading.js | 11 +---------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/browserify-plugin/main.js b/src/browserify-plugin/main.js index f986a39..a78a931 100644 --- a/src/browserify-plugin/main.js +++ b/src/browserify-plugin/main.js @@ -153,7 +153,8 @@ function LiveReactloadPlugin(b, opts = {}) { clientOpts ] let bundleSrc = - `(${loader.toString()})(${args.map(a => JSON.stringify(a, null, 2)).join(", ")});` + `(${loader.toString()})(${args.map(a => JSON.stringify(a, null, 2)).join(", ")}); + ${__livereactload_loadAsModule.toString()};` if (standalone) { bundleSrc = umd(standalone, `return ${bundleSrc}`) } @@ -172,4 +173,13 @@ function LiveReactloadPlugin(b, opts = {}) { } } +function __livereactload_loadAsModule(__livereactload_source, __livereactload_sourcemap) { + return eval( + 'function __livereactload_module(require, module, exports){\n' + + __livereactload_source + + '\n}; __livereactload_module;' + + (__livereactload_sourcemap || '') + ); +} + module.exports = LiveReactloadPlugin diff --git a/src/reloading.js b/src/reloading.js index aa262ae..b501c91 100644 --- a/src/reloading.js +++ b/src/reloading.js @@ -84,7 +84,7 @@ function loader(mappings, entryPoints, options) { var body = mapping[0]; if (typeof body !== "function") { debug("Compiling module", mapping[2]) - var compiled = loadAsModule(body, mapping[2].sourcemap); + var compiled = __livereactload_loadAsModule(body, mapping[2].sourcemap); mapping[0] = compiled; mapping[2].source = body; } @@ -429,15 +429,6 @@ function loader(mappings, entryPoints, options) { console.error("LiveReactload ::", msg); } - function loadAsModule(__livereactload_source, __livereactload_sourcemap) { - return eval( - 'function __livereactload_module(require, module, exports){\n' + - __livereactload_source + - '\n}; __livereactload_module;' + - (__livereactload_sourcemap || '') - ); - } - } module.exports = loader;