-
-
Save harshavardhana/e648783f10a70470b488 to your computer and use it in GitHub Desktop.
webpack, react, golang self-contained gulpfile
This file contains hidden or 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 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: { | |
stage: 0, | |
optional: ["runtime"], | |
plugins: ["react-transform"], | |
extra: { | |
"react-transform" : { | |
transforms : babelReactTransforms | |
} | |
} | |
} | |
}, { | |
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: cfg.output.publicPath, | |
contentBase: './assets', | |
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 -ignore=bindata\.go -prefix=assets assets/...').exec(); | |
} else { | |
return run('go-bindata -pkg=assets -o assets/bindata.go -ignore=bindata\.go -ignore=.*\.map$ -prefix=assets assets/...').exec(); | |
} | |
}); | |
gulp.task('default', ['bundle', 'assetfs']); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment