babel-plugin-flow-react-proptypes icon indicating copy to clipboard operation
babel-plugin-flow-react-proptypes copied to clipboard

Importing type from local file is left unresolved by rollup

Open sohkai opened this issue 8 years ago • 2 comments

In ComponentA.js:

... 
export type ComponentAType = string
...

This gets transpiled with the plugin to:

var babelPluginFlowReactPropTypes_proptype_ComponentAType$1 = require('prop-types').string;

if (typeof exports !== 'undefined') Object.defineProperty(exports, 'babelPluginFlowReactPropTypes_proptype_ComponentAType', {
  value: babelPluginFlowReactPropTypes_proptype_ComponentAType$1,
  configurable: true
});

Now in ComponentB.js:

...
import type { ComponentAType } from './ComponentA'
...

This gets transpiled with the plugin to:

var babelPluginFlowReactPropTypes_proptype_ComponentAType = require('./ComponentA').babelPluginFlowReactPropTypes_proptype_ComponentAType || require('prop-types').any;

This require('./ComponentA') doesn't seem to get picked up by rollup, making it break bundled builds. Not sure if this is actually fixable in this plugin, since I would think it's rollup's job to figure how the require()s should be linked, but wondering if anyone's solved this before or if this also happens with webpack?

My rollup.config.js currently includes these plugins:

babel({ exclude: 'node_modules/**', plugins: ['external-helpers'] }),
resolve(),
commonjs(),

sohkai avatar Dec 18 '17 06:12 sohkai

I don't have an experience with rollup, but what does "doesn't seem to get picked up by rollup" mean? Is there an error?

brigand avatar Dec 18 '17 07:12 brigand

I assume rollup should be noticing require('./ComponentA') and replacing it with a proper link to the bundled module, but instead it's leaving it alone. If I replace require('./ComponentA') with babelPluginFlowReactPropTypes_proptype_ComponentAType$1, everything works.

This might have something to do with rollup not supporting require() natively (it just assumes ES6 imports / exports), but I tried pointing the commonjs plugin to src/** to no avail :(.

sohkai avatar Dec 18 '17 08:12 sohkai