From 868158d9fffe84df60c2069c9ff779392061a47d Mon Sep 17 00:00:00 2001 From: Jake Riesterer Date: Tue, 26 Aug 2014 21:55:15 -0500 Subject: [PATCH 1/4] Implement sourcemaps --- index.js | 53 +++++++++++++++++++++++++++++++++++----------------- package.json | 3 ++- 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index b985af2c9..fd1ac78a2 100644 --- a/index.js +++ b/index.js @@ -1,30 +1,32 @@ var path = require('path'); +var SourceNode = require('source-map').SourceNode; +var SourceMapConsumer = require('source-map').SourceMapConsumer; +var SourceMapGenerator = require('source-map').SourceMapGenerator; -module.exports = function (source) { +var CREATE_CLASS_REGEX = /React\.createClass\s*\(\s*\{/g; +var REPLACEMENT_TEXT = '__HUA.createClass({'; + +module.exports = function (source, map) { if (this.cacheable) { this.cacheable(); } var filename = path.basename(this.resourcePath), - matches = 0, - processedSource; - - processedSource = source.replace(/React\.createClass\s*\(\s*\{/g, function (match) { - matches++; - return '__hotUpdateAPI.createClass({'; - }); - - if (!matches) { - return source; - } + prependText, + appendText, + processedSource, + node, + strWithMap; - return [ - 'var __hotUpdateAPI = (function () {', + prependText = [ + 'var __HUA = (function () {', ' var React = require("react");', ' var getHotUpdateAPI = require(' + JSON.stringify(require.resolve('./getHotUpdateAPI')) + ');', ' return getHotUpdateAPI(React, ' + JSON.stringify(filename) + ', module.id);', - '})();', - processedSource, + '})();' + ].join('\n'); + + appendText = [ 'if (module.hot) {', ' module.hot.accept(function (err) {', ' if (err) {', @@ -33,8 +35,25 @@ module.exports = function (source) { ' });', ' module.hot.dispose(function () {', ' var nextTick = require(' + JSON.stringify(require.resolve('next-tick')) + ');', - ' nextTick(__hotUpdateAPI.updateMountedInstances);', + ' nextTick(__HUA.updateMountedInstances);', ' });', '}' ].join('\n'); + + processedSource = source.replace(CREATE_CLASS_REGEX, REPLACEMENT_TEXT); + + // No sourcemaps + if (!map) { + return this.callback(null, [prependText, source, appendText].join('\n')); + } + + // Handle sourcemaps + node = new SourceNode(null, null, null, [ + new SourceNode(null, null, null, prependText), + SourceNode.fromStringWithSourceMap(source, new SourceMapConsumer(map)), + new SourceNode(null, null, null, appendText) + ]).join('\n'); + + strWithMap = node.toStringWithSourceMap(); + this.callback(null, strWithMap.code, strWithMap.map.toString()); }; diff --git a/package.json b/package.json index 92d17f20f..48cbb9c78 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,8 @@ "example": "example" }, "dependencies": { - "next-tick": "0.2.2" + "next-tick": "0.2.2", + "source-map": "^0.1.38" }, "devDependencies": { "jsx-loader": "0.11.0", From 809eaa6b325f951fbce40815be0e0d9d348cce55 Mon Sep 17 00:00:00 2001 From: Jake Riesterer Date: Tue, 26 Aug 2014 22:34:26 -0500 Subject: [PATCH 2/4] Fix incorrect variable reference --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index fd1ac78a2..186644052 100644 --- a/index.js +++ b/index.js @@ -44,13 +44,13 @@ module.exports = function (source, map) { // No sourcemaps if (!map) { - return this.callback(null, [prependText, source, appendText].join('\n')); + return this.callback(null, [prependText, processedSource, appendText].join('\n')); } // Handle sourcemaps node = new SourceNode(null, null, null, [ new SourceNode(null, null, null, prependText), - SourceNode.fromStringWithSourceMap(source, new SourceMapConsumer(map)), + SourceNode.fromStringWithSourceMap(processedSource, new SourceMapConsumer(map)), new SourceNode(null, null, null, appendText) ]).join('\n'); From fb38536881999c82bf9aa7222d581ce0cf9221eb Mon Sep 17 00:00:00 2001 From: Jake Riesterer Date: Tue, 26 Aug 2014 23:05:38 -0500 Subject: [PATCH 3/4] Re-add ability to skip loader effects when no 'createClass' call is found and fix issue with javascript global regexes and their persistent state --- index.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 186644052..eb26d808b 100644 --- a/index.js +++ b/index.js @@ -3,14 +3,15 @@ var SourceNode = require('source-map').SourceNode; var SourceMapConsumer = require('source-map').SourceMapConsumer; var SourceMapGenerator = require('source-map').SourceMapGenerator; -var CREATE_CLASS_REGEX = /React\.createClass\s*\(\s*\{/g; -var REPLACEMENT_TEXT = '__HUA.createClass({'; - module.exports = function (source, map) { if (this.cacheable) { this.cacheable(); } + if (!/React\.createClass\s*\(\s*\{/.test(source)) { + return this.callback(null, source, map); + } + var filename = path.basename(this.resourcePath), prependText, appendText, @@ -40,7 +41,7 @@ module.exports = function (source, map) { '}' ].join('\n'); - processedSource = source.replace(CREATE_CLASS_REGEX, REPLACEMENT_TEXT); + processedSource = source.replace(/React\.createClass\s*\(\s*\{/g, '__HUA.createClass({'); // No sourcemaps if (!map) { From 4b239ba525bc25b0ecf3d75fc02f1187523e0b4f Mon Sep 17 00:00:00 2001 From: Jake Riesterer Date: Wed, 3 Sep 2014 12:14:22 -0500 Subject: [PATCH 4/4] Don't emit source maps if the 'sourceMap' flag is set to false --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index eb26d808b..87012c0f6 100644 --- a/index.js +++ b/index.js @@ -44,7 +44,7 @@ module.exports = function (source, map) { processedSource = source.replace(/React\.createClass\s*\(\s*\{/g, '__HUA.createClass({'); // No sourcemaps - if (!map) { + if (!map || this.sourceMap === false) { return this.callback(null, [prependText, processedSource, appendText].join('\n')); }