From b6f574b60c63aa9651f9fd1f9395ef6a5d7400c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sampo=20Kivist=C3=B6?= Date: Fri, 20 Jan 2017 00:08:17 +0200 Subject: [PATCH] Plugin will now by default use global reference to Inferno and if options.imports is used then modules --- README.md | 15 +++++++++++++++ lib/index.js | 21 +++++++++++++++------ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 59455a5..d8d0a16 100644 --- a/README.md +++ b/README.md @@ -40,3 +40,18 @@ InfernoDOM.render(
Hello world
, container); InfernoDOM.render(
, 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}]] +} +``` diff --git a/lib/index.js b/lib/index.js index 2b71b6c..5dc28af 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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) { @@ -366,7 +375,7 @@ function createVNode(t, astNode) { } } -module.exports = function (options) { +module.exports = function (options, a,b, c) { var t = options.types; return { @@ -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); } } }