Skip to content
This repository has been archived by the owner on Dec 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2 from geowarin/implement-transform
Browse files Browse the repository at this point in the history
Support inline source maps
  • Loading branch information
geowarin committed Mar 9, 2016
2 parents bf627f1 + d6652ee commit ac577a5
Show file tree
Hide file tree
Showing 15 changed files with 385 additions and 182 deletions.
2 changes: 1 addition & 1 deletion compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
// Can be used as a mocha compiler
// mocha --compilers electron-hot/compiler.js
// This will not instrument the files and hot reload will be disabled
require('./src/jsxTransform').install({doNotInstrument: true});
require('./src/jsxTransform').install({doNotInstrument: true, sourceMapInline: false});
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
},
"homepage": "https://github.com/geowarin/electron-hot-loader#readme",
"dependencies": {
"escodegen": "1.8.0",
"esprima": "2.7.2",
"estraverse": "4.1.1",
"esprima-fb": "^15001.1.0-dev-harmony-fb",
"jstransform": "11.0.3",
"react-deep-force-update": "2.0.1",
"react-proxy": "2.0.3",
Expand Down
96 changes: 0 additions & 96 deletions src/instrument.js

This file was deleted.

36 changes: 24 additions & 12 deletions src/jsxTransform.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
"use strict";

var installed = false;
var inlineSourceMap = require('jstransform/src/inline-source-map');

module.exports = {
install,
transform
};

function transform(source, options) {
function transform(filename, source, options) {

var jstransform = require('jstransform/simple');
var content = jstransform.transform(source, options).code;
if (options.doNotInstrument === true) {
return content;
const jsxVisitors = require('./transforms/react-jsx-visitors').visitorList;
const requireVisitor = require('./transforms/custom-require-visitor');
const topLevelVisitor = require('./transforms/top-level-render-visitor');
const jstransform = require('jstransform');

let visitors = [];
if (options.doNotInstrument !== true) {
visitors = visitors.concat(requireVisitor).concat(topLevelVisitor);
}
visitors = visitors.concat(jsxVisitors);

let result;
if (options.sourceMapInline) {
result = jstransform.transform(visitors, source, {sourceMap: true, filename: filename, doNotInstrument: options.doNotInstrument});
var map = inlineSourceMap(result.sourceMap, source, filename);
result.code = result.code + '\n' + map;
} else {
result = jstransform.transform(visitors, source, {doNotInstrument: options.doNotInstrument});
}

var instrument = require('./instrument');
return instrument(content);
return result.code;
}

function install(options) {
Expand All @@ -31,19 +45,17 @@ function install(options) {
options = options || {};

Module._extensions[options.extension || '.jsx'] = function(module, filename) {
if (!options.hasOwnProperty('react')) {
options.react = true
}
if (!options.hasOwnProperty('sourceMapInline')) {
options.sourceMapInline = true
}

var content = fs.readFileSync(filename, 'utf8');
try {
var instrumented = transform(content, options);
var instrumented = transform(filename, content, options);
module._compile(instrumented, filename)
} catch (e) {
console.error("Error compiling " + filename, e)
console.error("Error compiling " + filename, e);
console.error(e.stack);
}
};

Expand Down
48 changes: 0 additions & 48 deletions src/nodeTypes.js

This file was deleted.

34 changes: 34 additions & 0 deletions src/transforms/custom-require-visitor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
var jstransform = require('jstransform');
var utils = require('jstransform/src/utils');
var requireRegister = require.resolve('../index');

function requireVisitor(traverse, node, path, state) {

if (!state.g.alreadyAddedElectronHotRequire) {
utils.append("var __electronHot__ = require('" + requireRegister + "');\n", state);
state.g.alreadyAddedElectronHotRequire = true;
}

var requireNodesMap = state.g.requireNodesMap;
if (!requireNodesMap) {
requireNodesMap = {};
}
requireNodesMap[node.declarations[0].id.name] = node.declarations[0].init.arguments[0].value;

state.g.requireNodesMap = requireNodesMap;

utils.catchup(node.range[1], state);
}
requireVisitor.test = function(node, path, state) {
return (
node.type === 'VariableDeclaration' &&
node.declarations[0] &&
node.declarations[0].type === 'VariableDeclarator' &&
node.declarations[0].init &&
node.declarations[0].init.type === 'CallExpression' &&
node.declarations[0].init.callee.type === 'Identifier' &&
node.declarations[0].init.callee.name === 'require'
);
};

module.exports = requireVisitor;
Loading

0 comments on commit ac577a5

Please sign in to comment.