From 759fde80c4d3f3e2de2c3b72dbdf524d038ca0f4 Mon Sep 17 00:00:00 2001 From: EugeneZ Date: Wed, 23 Nov 2016 20:35:27 -0800 Subject: [PATCH] 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;