infernojs / babel-plugin-inferno

Transforms JSX to InfernoJS vNodes
MIT License
79 stars 26 forks source link

Variables declaration is removed after rebuilding module #52

Closed harimambura closed 6 years ago

harimambura commented 6 years ago

Hello! I'm use rollup watch with few babel plugins, in particular inferno plugin for convert JSX syntax. After running the rollup watch first build is ok (has valid and runnable code), but after changing the source file and rebuilding bundle some of variable declarations is removed and bundle code is broken. I made the example to reproduce bug. Take a look at declaration of 'createVNode' and 'createTextVNode' variables.

here the repository with example https://github.com/harimambura/inferno_bug

Havunen commented 6 years ago

Interesting, thanks for opening the issue

Havunen commented 6 years ago

If you use imports: false you need to use node module resolver, and do const Inferno = require("inferno"); at the top of every file. If you do imports: true then babel plugin will automatically add the imports

harimambura commented 6 years ago

I have Inferno as a global variable , that’s why I use “imports: false”

Havunen commented 6 years ago

Okay I understand now, so you still should use imports: true and then in rollup config you define Inferno as external and global.

something like this:

const rollupResolve = require('rollup-plugin-node-resolve');
const rollupBabel = require('rollup-plugin-babel');
const rollupUglify = require('rollup-plugin-uglify');
const babelConfig = require('./babel.config');

module.exports = {
    input: "test.js",
  external: ['inferno'],
    plugins: [
    rollupBabel(babelConfig),
    rollupResolve()
    ],
    output: [
        {
            name: "bundle",
            format: 'iife',
            file: "bundle.js",
      globals: {
        'inferno': 'Inferno'
      },
        }
    ],
}

which transpiles into this code:

var bundle = (function (inferno) {
    'use strict';

    var obj = {
      getNode: function getNode() {
        return inferno.createVNode(1, "span", null, inferno.createTextVNode("some text"), 2);
      }
    };

    return obj;

}(Inferno));
harimambura commented 6 years ago

Thanks a lot, it seems working!