Skip to content

Instantly share code, notes, and snippets.

@wrouesnel
Last active March 6, 2016 04:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wrouesnel/74d13002828cee55014d to your computer and use it in GitHub Desktop.
Save wrouesnel/74d13002828cee55014d to your computer and use it in GitHub Desktop.
Lightweight gulpfile for developing Webpack'd go-applications with CerebralJS.
/**
* Created by will on 6/09/15.
*/
var path = require('path');
var gulp = require('gulp');
var run = require('gulp-run');
var gutil = require('gulp-util');
var minimist = require('minimist');
var options = minimist(process.argv.slice(2), {
string: 'env',
default: { env: process.env.NODE_ENV || 'development' }
});
// Start inline webpack config
var webpack = require("webpack");
var WebpackDevServer = require("webpack-dev-server");
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var nodeModulesPath = path.resolve(__dirname, 'node_modules');
// Define optional webpack plugins
var extra_plugins = [];
if (options.env == "development") {
extra_plugins = [
new webpack.DefinePlugin({'process.env.NODE_ENV': '"development"'}),
new webpack.HotModuleReplacementPlugin()
]
} else {
extra_plugins = [
new webpack.DefinePlugin({'process.env.NODE_ENV': '"production"'}),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin({compress: {warnings: false}})
]
}
// Define react transforms for babel
var babelReactTransforms = []
if (options.env == "development") {
babelReactTransforms = [{
transform: "react-transform-hmr",
imports: ["react"],
locals: ["module"]
}];
}
var webpackConfig = {
//devtool: 'source-map',
entry: {
main: ['./web/main.js']
},
output: {
path: path.resolve('./assets/static/'),
filename: '[name].js',
publicPath: '/static'
},
plugins: [
new HtmlWebpackPlugin({
template: './web/index.tmpl.html',
inject: 'body',
filename: 'index.html'
}),
new ExtractTextPlugin('[name].min.css'),
new webpack.optimize.OccurenceOrderPlugin(),
].concat(extra_plugins),
module: {
loaders: [{
test: /\.js?$/,
exclude: [nodeModulesPath],
loader: 'babel-loader',
query: {
presets: ["react", "es2015", "stage-0", "react-hmre"],
plugins: [
["babel-project-relative-import", {
projectPathSuffix: "web/"
}],
["transform-decorators-legacy"]
]
}
}, {
test: /\.json?$/,
loader: 'json'
}, {
test: /\.css$/,
loader: ExtractTextPlugin.extract("style-loader", "css-loader")
}, {
test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url?name=[name]-[hash].[ext]&limit=10000&mimetype=application/font-woff'
}, {
test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url?name=[name]-[hash].[ext]&limit=10000&mimetype=application/octet-stream'
}, {
test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
loader: 'file?name=[name]-[hash].[ext]'
}, {
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url?name=[name]-[hash].[ext]&limit=10000&mimetype=image/svg+xml'
}]
}
};
// Finish inline webpack config
// Webpack bundling
gulp.task('bundle', function(callback) {
webpack(webpackConfig, function(err, stats){
if(err) throw new gutil.PluginError("webpack:build", err);
gutil.log("[bundle]", stats.toString({
colors: true
}));
callback();
});
});
gulp.task('live', function(callback){
// modify some webpack config options
var cfg = Object.create(webpackConfig);
cfg.devtool = "inline-source-map";
cfg.debug = true;
cfg.entry = cfg.entry.main.concat([
'webpack-dev-server/client?http://localhost:23182',
'webpack/hot/dev-server']);
cfg.plugins = cfg.plugins.concat(
new webpack.NoErrorsPlugin()
);
// Start a webpack-dev-server
new WebpackDevServer(webpack(cfg), {
publicPath: '/',
contentBase: './assets/static/',
stats: {
colors: true
},
hot: true
}).listen(23182, "localhost", function(err) {
if(err) throw new gutil.PluginError("webpack-dev-server", err);
gutil.log("[live]", "http://localhost:23182/webpack-dev-server/index.html");
});
});
// Go bindata packaging
gulp.task("assetfs", ['bundle'], function () {
if (options.env == 'development') {
return run('go-bindata -debug -dev -o assets/bindata.go -pkg=assets -prefix=assets/static assets/static/...').exec();
} else {
return run('go-bindata -pkg=assets -o assets/bindata.go -ignore=bindata\.go -ignore=.*\.map$ -prefix=assets/static assets/static/...').exec();
}
});
gulp.task('default', ['bundle', 'assetfs']);
{
"name": "<name>",
"version": "1.0.0",
"description": "",
"main": "Gulpfile.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "<author>",
"license": "<license>",
"dependencies": {
"babel": "^6.5.2",
"babel-core": "^6.6.5",
"babel-eslint": "^4.1.3",
"babel-jscs": "^2.0.4",
"babel-loader": "^6.2.4",
"babel-plugin-react-transform": "^1.0.5",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"babel-preset-react-hmre": "^1.1.1",
"babel-preset-stage-0": "^6.5.0",
"babel-project-relative-import": "^1.1.0",
"babel-runtime": "^6.2.0",
"baobab": "^2.1.1",
"bootstrap": "3.3.6",
"cerebral": "0.33.30",
"cerebral-addons": "0.5.2",
"cerebral-model-baobab": "0.4.7",
"cerebral-module-devtools": "^0.5.4",
"cerebral-module-http": "^0.1.1",
"cerebral-view-react": "0.11.8",
"css-loader": "^0.23.0",
"extract-text-webpack-plugin": "^0.9.1",
"file-loader": "^0.8.5",
"gulp": "^3.9.0",
"gulp-concat": "^2.6.0",
"gulp-if": "^1.2.5",
"gulp-livereload": "^3.8.1",
"gulp-run": "^1.6.11",
"gulp-sourcemaps": "^1.6.0",
"gulp-util": "^3.0.7",
"html-webpack-plugin": "^1.7.0",
"lodash": "^3.10.1",
"minimist": "^1.2.0",
"moment": "^2.10.6",
"react": "^0.14.2",
"react-dom": "^0.14.2",
"react-loader": "^2.0.0",
"react-transform-hmr": "^1.0.1",
"style-loader": "^0.13.0",
"superagent": "^1.4.0",
"url-loader": "^0.5.7",
"vinyl-source-stream": "^1.1.0",
"watchify": "^3.6.0",
"webpack": "1.12.14",
"webpack-dev-server": "1.14.1",
"webpack-hot-middleware": "2.9.1",
"webpack-split-by-path": "0.0.8"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment