Last active
September 26, 2017 21:25
-
-
Save eirikb/06f92fd3857063b58512 to your computer and use it in GitHub Desktop.
Gulp + Babel + JSX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var gulp = require('gulp'); | |
var fs = require('fs'); | |
var browserify = require('browserify'); | |
var babelify = require('babelify'); | |
var watchify = require('watchify'); | |
var source = require('vinyl-source-stream'); | |
var livereload = require('gulp-livereload'); | |
var less = require('gulp-less'); | |
var concat = require('gulp-concat'); | |
var connect = require('gulp-connect'); | |
var cors = function (req, res, next) { | |
res.setHeader('Access-Control-Allow-Origin', '*'); | |
res.setHeader('Access-Control-Allow-Headers', '*'); | |
next(); | |
}; | |
var extensions = ['.js', '.json', '.es6', '.jsx']; | |
var bundler = watchify(browserify({ | |
debug: false, | |
extensions: extensions | |
})) | |
.transform(babelify.configure({ | |
extensions: extensions, | |
presets: ['react', 'es2015'] | |
})) | |
.require('./src/index.jsx', {entry: true}); | |
livereload.listen(); | |
function bundle() { | |
return bundler | |
.bundle() | |
.on('error', function (err) { | |
console.error('' + err); | |
}) | |
.pipe(source('app.js')) | |
.pipe(gulp.dest('dist/')) | |
.pipe(livereload()); | |
} | |
gulp.task('bundle', bundle); | |
bundler.on('update', bundle); | |
bundler.on('log', console.log); | |
gulp.task('less', function () { | |
return gulp.src('./src/less/*.less') | |
.pipe(less({compress: true})) | |
.pipe(concat('app.css')) | |
.pipe(gulp.dest('dist/')) | |
.pipe(livereload()); | |
}); | |
gulp.task('watch', function () { | |
gulp.watch('./src/less/*.less', ['less']); | |
gulp.watch('./src/index.html', ['html']); | |
}); | |
gulp.task('webserver', function () { | |
connect.server({ | |
root: 'dist', | |
livereload: true, | |
port: 8000, | |
middleware: function () { | |
return [cors]; | |
} | |
}); | |
}); | |
gulp.task('html', function () { | |
gulp.src('./src/index.html') | |
.pipe(gulp.dest('dist/')); | |
}); | |
gulp.task('default', ['bundle', 'less', 'html', 'watch', 'webserver']); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
class HelloWorld extends React.Component { | |
render() { | |
return <p>Hello, world!</p>; | |
} | |
} | |
export default HelloWorld; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<link rel="stylesheet" href="app.css"> | |
</head> | |
<body> | |
<script src="app.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import HelloWorld from './hello-world'; | |
React.render( | |
<HelloWorld />, | |
document.body | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
npm install --save-dev gulp gulp-concat gulp-connect gulp-less gulp-livereload vinyl-source-stream browserify babelify watchify babel-preset-es2015 babel-preset-react react |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment