Skip to content

Commit

Permalink
Switch to react-hot-api rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Dec 12, 2014
1 parent 74ee1f9 commit 773d6e1
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 241 deletions.
19 changes: 0 additions & 19 deletions getHotUpdateAPI.js

This file was deleted.

48 changes: 31 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,63 @@ var path = require('path'),
SourceMapConsumer = require('source-map').SourceMapConsumer,
makeIdentitySourceMap = require('./makeIdentitySourceMap');

var REACT_CLASS_RE = /[Rr]eact\.createClass\s*\(/g;
var REACT_MARKER_RE = /render\s*[=:\(]/g;

module.exports = function (source, map) {
if (this.cacheable) {
this.cacheable();
}

if (!source.match(REACT_CLASS_RE)) {
var resourcePath = this.resourcePath;
if (!source.match(REACT_MARKER_RE)) {
return this.callback(null, source, map);
}

var resourcePath = this.resourcePath,
filename = path.basename(resourcePath),
var filename = path.basename(resourcePath),
separator = '\n\n',
prependText,
processedSource,
appendText,
node,
result;

prependText = [
'var __HUA = (function () {',
'var React = require("react");',
'var getHotUpdateAPI = require(' + JSON.stringify(require.resolve('./getHotUpdateAPI')) + ');',
'return getHotUpdateAPI(React, ' + JSON.stringify(filename) + ', module.id);',
'})();',
'/* REACT HOT LOADER */',
'if (module.hot) {',
'(function () {',
'var reactHotApi = require(' + JSON.stringify(require.resolve('react-hot-api')) + ');',
'if (typeof reactHotApi !== "function") {',
' return;',
'}',
'',
'module.makeHot = module.hot.data ?',
'module.hot.data.makeHot :',
'reactHotApi(require("react/lib/ReactMount"));',
'})();',
'}'
].join(' ');

appendText = [
'/* REACT HOT LOADER */',
'if (module.hot) {',
'module.hot.accept(function (err) {',
'if (err) {',
'console.error("Cannot not apply hot update to " + ' + JSON.stringify(filename) + ' + ": " + err.message);',
'}',
'});',
'module.hot.dispose(function () {',
'var nextTick = require(' + JSON.stringify(require.resolve('next-tick')) + ');',
'nextTick(__HUA.updateMountedInstances);',
'module.hot.dispose(function (data) {',
' data.makeHot = module.makeHot;',
'});',
'if (module.exports && module.makeHot) {',
' module.exports = module.makeHot(module.exports, "__MODULE_EXPORTS")',
'}',
'}'
].join(' ');

processedSource = source.replace(REACT_CLASS_RE, '__HUA.createClass(');

if (this.sourceMap === false) {
return this.callback(null, [
prependText,
processedSource
source,
appendText
].join(separator));
}

Expand All @@ -58,7 +71,8 @@ module.exports = function (source, map) {

node = new SourceNode(null, null, null, [
new SourceNode(null, null, this.resourcePath, prependText),
SourceNode.fromStringWithSourceMap(processedSource, new SourceMapConsumer(map))
SourceNode.fromStringWithSourceMap(source, new SourceMapConsumer(map)),
new SourceNode(null, null, this.resourcePath, appendText)
]).join(separator);

result = node.toStringWithSourceMap();
Expand Down
155 changes: 0 additions & 155 deletions makeComponentUpdater.js

This file was deleted.

48 changes: 0 additions & 48 deletions makeModuleUpdater.js

This file was deleted.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-hot-loader",
"version": "0.5.0",
"version": "1.0.0-alpha",
"description": "Webpack loader that enables live-editing React components without unmounting or losing their state",
"main": "index.js",
"scripts": {
Expand All @@ -10,9 +10,12 @@
"example": "example"
},
"dependencies": {
"next-tick": "0.2.2",
"react-hot-api": "^0.1.0",
"source-map": "0.1.40"
},
"peerDependencies": {
"react": ">=0.11.0"
},
"devDependencies": {
"jsx-loader": "0.12.0",
"react": "0.12.0",
Expand Down

0 comments on commit 773d6e1

Please sign in to comment.