From 72d182d4fd047d35fed15c6ae2abb418ab077fbf Mon Sep 17 00:00:00 2001 From: Eugene Zaretskiy Date: Fri, 10 Mar 2017 21:03:34 -0800 Subject: [PATCH] Fixes #147 , revision numbers removed, hash always present --- package.json | 3 ++- src/browserify-plugin/main.js | 16 +++++++++++++--- src/reloading.js | 31 ++++--------------------------- 3 files changed, 19 insertions(+), 31 deletions(-) diff --git a/package.json b/package.json index a7f546c..b746ab7 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/browserify-plugin/main.js b/src/browserify-plugin/main.js index e2a52e8..cedbb81 100644 --- a/src/browserify-plugin/main.js +++ b/src/browserify-plugin/main.js @@ -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" @@ -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) { @@ -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 diff --git a/src/reloading.js b/src/reloading.js index 55a6c51..8c5d9b4 100644 --- a/src/reloading.js +++ b/src/reloading.js @@ -17,7 +17,7 @@ * ... * }, * { - * hash: "md5_hash_from_source", + * hash: "32bit_hash_from_source", * isEntry: true|false * } * ], @@ -42,7 +42,6 @@ function loader(mappings, entryPoints, options) { var scope = { mappings: mappings, - revNums: {}, cache: {}, reloading: false, reloadHooks: {}, @@ -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; @@ -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; - } } /**