Skip to content

Commit

Permalink
Merge pull request #152 from EugeneZ/master
Browse files Browse the repository at this point in the history
Fixes #147 (rev replaced with hash)
  • Loading branch information
milankinen authored Mar 12, 2017
2 parents 19ed5ff + 00edc09 commit b6fbe69
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 31 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
"dependencies": {
"cli-color": "1.1.0",
"convert-source-map": "1.3.0",
"crc": "3.4.4",
"left-pad": "1.1.3",
"lodash": "4.17.4",
"md5": "2.2.1",
"offset-sourcemap-lines": "1.0.0",
"through2": "2.0.3",
"umd": "3.0.1",
Expand Down
16 changes: 13 additions & 3 deletions src/browserify-plugin/main.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import _ from "lodash"
import umd from "umd"
import through from "through2"
import md5 from "md5"
import crc from "crc"
import {readFileSync} from "fs"
import {resolve} from "path"
import convertSourceMaps from 'convert-source-map'
import offsetSourceMaps from 'offset-sourcemap-lines'
import leftPad from 'left-pad'
import {startServer} from "./server"
import {log} from "./console"
import loader from "../reloading"
Expand Down Expand Up @@ -107,11 +108,13 @@ function LiveReactloadPlugin(b, opts = {}) {
let hash;

if (converter) {
const sources = converter.getProperty("sources") || [];
sourceWithoutMaps = convertSourceMaps.removeComments(source)
hash = md5(sourceWithoutMaps)
hash = getHash(sourceWithoutMaps)
converter.setProperty("sources", sources.map(source => source += "?version=" + hash))
adjustedSourcemap = convertSourceMaps.fromObject(offsetSourceMaps(converter.toObject(), 1)).toComment()
} else {
hash = md5(source)
hash = getHash(source)
}

if (entry) {
Expand Down Expand Up @@ -171,6 +174,13 @@ function LiveReactloadPlugin(b, opts = {}) {
function throws(msg) {
throw new Error(msg)
}

function getHash(data) {
const crcHash = leftPad(crc.crc32(data).toString(16), 8, "0")
return new Buffer(crcHash, "hex")
.toString("base64")
.replace(/=/g,"")
}
}

module.exports = LiveReactloadPlugin
31 changes: 4 additions & 27 deletions src/reloading.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* ...
* },
* {
* hash: "md5_hash_from_source",
* hash: "32bit_hash_from_source",
* isEntry: true|false
* }
* ],
Expand All @@ -42,7 +42,6 @@ function loader(mappings, entryPoints, options) {

var scope = {
mappings: mappings,
revNums: {},
cache: {},
reloading: false,
reloadHooks: {},
Expand Down Expand Up @@ -81,10 +80,10 @@ function loader(mappings, entryPoints, options) {
}
}

function compile(mapping, revision) {
function compile(mapping) {
var body = mapping[0];
if (typeof body !== "function") {
debug("Compiling module", mapping[2], "[revision " + revision + " ]")
debug("Compiling module", mapping[2])
var compiled = compileModule(body, mapping[2].sourcemap);
mapping[0] = compiled;
mapping[2].source = body;
Expand Down Expand Up @@ -163,34 +162,12 @@ function loader(mappings, entryPoints, options) {
var mapping = mappings[id];
var meta = mapping[2];
if (!old || old[2].hash !== meta.hash) {
var rev = scope.revNums[id] ? ++scope.revNums[id] : (scope.revNums[id] = 1);
if (old && meta.sourcemap) {
addVersionToSourceMap(meta, rev);
}
compile(mapping, rev);
compile(mapping);
scope.mappings[id] = mapping;
changes.push([id, old]);
}
});
return changes;

// Updates the source map by adding a revision parameter to the filename.
// Without this new filename, browsers will ignore the updated source map.
function addVersionToSourceMap(meta, revision) {
var comment = meta.sourcemap
.replace(/^\/\*/g, '//')
.replace(/\*\/$/g, '');
// decode sourcemap comment and add hash param
comment = comment.split(',').pop();
var sourcemap = JSON.parse(atob(comment));
for (var i = 0; i < sourcemap.sources.length; i++) {
sourcemap.sources[i] += "?rev=" + revision;
}
// re-encode to sourcemap comment
comment = btoa(JSON.stringify(sourcemap));
comment = '//# sourceMappingURL=data:application/json;base64,' + comment;
meta.sourcemap = comment;
}
}

/**
Expand Down

0 comments on commit b6fbe69

Please sign in to comment.