Skip to content

Commit

Permalink
Merge pull request #138 from EugeneZ/master
Browse files Browse the repository at this point in the history
Fixes #127 #123
  • Loading branch information
milankinen committed Nov 25, 2016
2 parents c5d6935 + fb7ea01 commit 158f8cd
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
31 changes: 29 additions & 2 deletions src/browserify-plugin/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -16,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
Expand Down Expand Up @@ -96,10 +99,24 @@ 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 = ''
let hash;

if (converter) {
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) {
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) {
Expand Down Expand Up @@ -136,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}`)
}
Expand All @@ -155,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
4 changes: 2 additions & 2 deletions src/reloading.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = __livereactload_loadAsModule(body, mapping[2].sourcemap);
mapping[0] = compiled;
mapping[2].source = body;
}
Expand Down Expand Up @@ -428,8 +428,8 @@ function loader(mappings, entryPoints, options) {
function error(msg) {
console.error("LiveReactload ::", msg);
}
}

}

module.exports = loader;
module.exports["default"] = loader;
Expand Down

0 comments on commit 158f8cd

Please sign in to comment.