I've created a React component and I want to distribute a built-in version (built with gulp) of this component, so in gulpfile.js
I have:
var gulp = require('gulp');
var browserify = require('browserify');
var babelify = require('babelify');
var source = require('vinyl-source-stream');
gulp.task('build-js', function() {
return browserify('./src/Foo.js')
.transform(babelify)
.bundle()
.pipe(source('bundle.js'))
.pipe(gulp.dest('./dist'));
});
gulp.task('build', ['build-js']);
In .babelrc
:
{
"presets": ["es2015", "react", "stage-0"],
}
And these are the dependencies in package.json
:
"dependencies": {
"react": "^0.14.7"
}
"devDependencies": {
"babel-polyfill": "^6.3.14",
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"babel-preset-stage-0": "^6.3.13",
"babelify": "^7.2.0",
"browserify": "^13.0.0",
"gulp": "^3.9.1",
"react": "^0.14.7",
"react-dom": "^0.14.7",
"vinyl-source-stream": "^1.1.0"
}
When I run gulp build
(after npm install
) build.js
file is created under /dist
, but when I try to use this component from other React app I get the error:
Error: Cannot find module ' ./emptyFunction'.
If I dig into this file (build.js
), I can see these lines:
var emptyFunction = require('./emptyFunction');
...
var camelize = require('./camelize');
These files does not exist under ./dist
, so the error is thrown when I try to build the new React app calling the component:
import Foo from 'my-components-in-node-modules';
What am I missing?
Edit:
As I can see, the weird requires come from requiring React within the component file:
var React = require('react');
// or import React from 'react';
class Foo extends React.Component {
static propTypes = {...};
static defaultProps = {...};
render() {...}
}
export default Foo;
If I remove var React = require('react');
those requires (emptyFunction, camelize) disappear, and the error is React is not defined
, obviously.
Edit2:
As @JMM suggests, I should have the dependencies (React in my case) in the dist
folder, but how should I achieve it? What if my component has more dependencies? I thought that I just had to have the dependencies defined in package.json
.
Edit3:
I finally realized that I don't need browserify
, just gulp-babel
:
var gulp = require('gulp');
var babel = require('gulp-babel');
gulp.task('bundle', bundle);
function bundle () {
gulp.src('./src/*.js')
.pipe(babel())
.pipe(gulp.dest('./dist'));
}
gulp.task('build', ['bundle']);
And that's all. The full example is here: react-svg-components.