From 759fde80c4d3f3e2de2c3b72dbdf524d038ca0f4 Mon Sep 17 00:00:00 2001 From: EugeneZ Date: Wed, 23 Nov 2016 20:35:27 -0800 Subject: [PATCH 1/6] 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/6] 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/6] 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; From 2000deb99622d5f1f6dc1f1f8615df4b6a74bdbb Mon Sep 17 00:00:00 2001 From: Matti Lankinen Date: Fri, 25 Nov 2016 15:02:40 +0200 Subject: [PATCH 4/6] Re-arrange module compiling and loading codes --- src/browserify-plugin/main.js | 12 +----------- src/reloading.js | 12 ++++++++++-- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/browserify-plugin/main.js b/src/browserify-plugin/main.js index a78a931..f986a39 100644 --- a/src/browserify-plugin/main.js +++ b/src/browserify-plugin/main.js @@ -153,8 +153,7 @@ function LiveReactloadPlugin(b, opts = {}) { clientOpts ] let bundleSrc = - `(${loader.toString()})(${args.map(a => JSON.stringify(a, null, 2)).join(", ")}); - ${__livereactload_loadAsModule.toString()};` + `(${loader.toString()})(${args.map(a => JSON.stringify(a, null, 2)).join(", ")});` if (standalone) { bundleSrc = umd(standalone, `return ${bundleSrc}`) } @@ -173,13 +172,4 @@ 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 b501c91..b2380d3 100644 --- a/src/reloading.js +++ b/src/reloading.js @@ -84,12 +84,21 @@ function loader(mappings, entryPoints, options) { var body = mapping[0]; if (typeof body !== "function") { debug("Compiling module", mapping[2]) - var compiled = __livereactload_loadAsModule(body, mapping[2].sourcemap); + var compiled = compileModule(body, mapping[2].sourcemap); mapping[0] = compiled; mapping[2].source = body; } } + function compileModule(source, sourcemap) { + return eval( + "function __livereactload_module(require, module, exports){\n" + + source + + "\n}; __livereactload_module;" + + (sourcemap || "") + ); + } + function unknownUseCase() { throw new Error( "Unknown use-case encountered! Please raise an issue: " + @@ -428,7 +437,6 @@ function loader(mappings, entryPoints, options) { function error(msg) { console.error("LiveReactload ::", msg); } - } module.exports = loader; From 6d41e1e24791aa9719e0e6fe082be596924c7ad6 Mon Sep 17 00:00:00 2001 From: Matti Lankinen Date: Fri, 25 Nov 2016 15:02:49 +0200 Subject: [PATCH 5/6] Fix hash --- src/browserify-plugin/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/browserify-plugin/main.js b/src/browserify-plugin/main.js index f986a39..c9566b7 100644 --- a/src/browserify-plugin/main.js +++ b/src/browserify-plugin/main.js @@ -116,7 +116,7 @@ function LiveReactloadPlugin(b, opts = {}) { if (entry) { entries.push(file) } - mappings[file] = [sourceWithoutMaps, deps, {id: file, hash: md5(sourceWithoutMaps), browserifyId: id, sourcemap: adjustedSourcemap}] + mappings[file] = [sourceWithoutMaps, deps, {id: file, hash: hash, browserifyId: id, sourcemap: adjustedSourcemap}] next(null, row) }, function flush(next) { From e3defcbe901a5d0937850ca2bc1b4f98005526e9 Mon Sep 17 00:00:00 2001 From: Matti Lankinen Date: Fri, 25 Nov 2016 15:03:36 +0200 Subject: [PATCH 6/6] Version 3.1.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a877d43..078973e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "livereactload", - "version": "3.1.0", + "version": "3.1.1", "description": "Live code editing with Browserify and React", "author": "Matti Lankinen (https://github.com/milankinen)", "license": "MIT",