Skip to content

Commit

Permalink
Merge pull request #35 from infernojs/import-as-needed
Browse files Browse the repository at this point in the history
use import instead global Inferno reference
  • Loading branch information
Havunen authored Jan 6, 2017
2 parents 026836e + 25c3193 commit eb5d8d3
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 33 deletions.
8 changes: 6 additions & 2 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
'use strict';

const babel = require('babel-core');

const plugin = require('./lib');

const code = `
<div>Hello world, { ['Foo! ', 'Bar!'] }</div>
<div>
<span>Hello</span>
<div>Another</div>
</div>
`;

console.log(
babel.transform(code, {
presets: ['es2015'],
presets: [['es2015', {modules: false}]],
plugins: [
[plugin],
'syntax-jsx'
Expand Down
21 changes: 0 additions & 21 deletions lib/flags.js

This file was deleted.

15 changes: 6 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const isComponent = require('./helpers/is-component');
const isNullOrUndefined = require('./helpers/is-null-or-undefined');
const VNodeFlags = require('./flags');
const VNodeFlags = require('inferno-vnode-flags');

function _stringLiteralTrimmer(lastNonEmptyLine, lineCount, line, i) {
const isFirstLine = (i === 0);
Expand Down Expand Up @@ -64,16 +64,13 @@ function getHoistedNode(lastNode, path) {
}
}

function addHoistedCreateVNode(t, toInsert) {
function addCreateVNodeImportStatement(t, toInsert) {
const node = toInsert.node;
const index = toInsert.index;

node.body.splice(index, 0, t.VariableDeclaration('var', [
t.VariableDeclarator(
t.Identifier('createVNode'),
t.memberExpression(t.identifier('Inferno'), t.identifier('createVNode'))
)
]));
node.body.splice(index, 0, t.importDeclaration([
t.ImportSpecifier(t.identifier('createVNode'), t.identifier('createVNode')),
], t.stringLiteral('inferno')));
}

function getVNodeType(t, type) {
Expand Down Expand Up @@ -383,7 +380,7 @@ module.exports = function (options) {
path.replaceWith(node);
if (!opts.hoistCreateVNode) {
opts.hoistCreateVNode = true;
addHoistedCreateVNode(t, getHoistedNode(path.node, path.parentPath));
addCreateVNodeImportStatement(t, getHoistedNode(path.node, path.parentPath));
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"jsx"
],
"dependencies": {
"babel-plugin-syntax-jsx": "^6.18.0"
"babel-plugin-syntax-jsx": "^6.18.0",
"inferno-vnode-flags": "^1.0.7"
},
"engines": {
"node": ">= 4.6.0"
Expand Down

0 comments on commit eb5d8d3

Please sign in to comment.