Skip to content

Commit

Permalink
Plugin will now by default use global reference to Inferno and if opt…
Browse files Browse the repository at this point in the history
…ions.imports is used then modules
  • Loading branch information
Havunen committed Jan 19, 2017
1 parent 9542384 commit b6f574b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,18 @@ InfernoDOM.render(<div>Hello world</div>, container);
InfernoDOM.render(<div autoFocus='true' />, container);

```

## Options

By default babel-plugin-inferno ships imports false. This is same behavior with ReactJS. You need to have Inferno declared in every JSX file. Even if not used by the code. Compiled code will have reference to global Inferno object.

If the environment supports modules (Webpack / Rollup) you can enable "imports" option which will import createVNode from Inferno. This allows tree-shaking to happen and Inferno does not need to be imported if not needed by the user land code.

Setting imports true can be done following way inside babelrc file

```js
{
"presets": [ "es2015" ],
"plugins": [["inferno", {"imports": true}]]
}
```
21 changes: 15 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,22 @@ function getHoistedNode(lastNode, path) {
}
}

function addCreateVNodeImportStatement(t, toInsert) {
function addCreateVNodeImportStatement(t, toInsert, opts) {
var node = toInsert.node;
var index = toInsert.index;

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

function getVNodeType(t, type) {
Expand Down Expand Up @@ -366,7 +375,7 @@ function createVNode(t, astNode) {
}
}

module.exports = function (options) {
module.exports = function (options, a,b, c) {
var t = options.types;

return {
Expand All @@ -379,7 +388,7 @@ module.exports = function (options) {
path.replaceWith(node);
if (!opts.hoistCreateVNode) {
opts.hoistCreateVNode = true;
addCreateVNodeImportStatement(t, getHoistedNode(path.node, path.parentPath));
addCreateVNodeImportStatement(t, getHoistedNode(path.node, path.parentPath), opts);
}
}
}
Expand Down

0 comments on commit b6f574b

Please sign in to comment.