Last active
October 28, 2015 18:21
-
-
Save andyfangdz/003a2c074946ef1b9cdd to your computer and use it in GitHub Desktop.
buildmatrix - gulp
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 gulp = require('gulp'); | |
| var webpackStream = require('webpack-stream'); | |
| var named = require('vinyl-named'); | |
| var argv = require('yargs').argv; | |
| var glob = require("glob"); | |
| var path = require("path"); | |
| var plumber = require('gulp-plumber'); | |
| var format = require('string-format'); | |
| var webpack = require("webpack"); | |
| var gutil = require('gulp-util'); | |
| var CommonsChunkPlugin = require("webpack/lib/optimize/CommonsChunkPlugin"); | |
| format.extend(String.prototype); | |
| function getSrcByParam() { | |
| var commonPath = 'frontend/apps/{}/main-**.js'; | |
| if (argv.all) { | |
| return glob.sync(commonPath.format('*')); | |
| } else { | |
| var appName = argv.app; | |
| return glob.sync(commonPath.format(appName)); | |
| } | |
| } | |
| function getEntries() { | |
| var commonPath = 'frontend/apps/*/main-**.js'; | |
| var result = {}; | |
| var globs = glob.sync(commonPath); | |
| for (var entry in globs) { | |
| var re = /frontend\/apps\/(\w+)\//g; | |
| reResult = re.exec(globs[entry]); | |
| result[reResult[reResult.length - 1]] = './' + globs[entry]; | |
| } | |
| return result; | |
| } | |
| var commonLoaders = [{ | |
| test: /\.js$/, | |
| exclude: /node_modules/, | |
| loader: 'babel' | |
| }, { | |
| test: /\.jsx$/, | |
| exclude: /node_modules/, | |
| loader: 'babel' | |
| }]; | |
| var commonPlugins = [ | |
| new webpack.ProvidePlugin({ | |
| 'fetch': 'imports?this=>global!exports?global.fetch!whatwg-fetch' | |
| }) | |
| ] | |
| gulp.task("build", function(callback) { | |
| if (argv.production) { | |
| var config = { | |
| entry: getEntries(), | |
| output: { | |
| path: path.join(__dirname, "static/build"), | |
| filename: "[name].entry.js" | |
| }, | |
| plugins: [ | |
| new CommonsChunkPlugin("commons.chunk.js"), | |
| new webpack.optimize.UglifyJsPlugin({ | |
| minimize: true | |
| }) | |
| ] + commonPlugins, | |
| module: { | |
| loaders: commonLoaders | |
| } | |
| }; | |
| webpack(config, function(err, stats) { | |
| if (err) throw new gutil.PluginError("webpack", err); | |
| gutil.log("[webpack]", stats.toString()); | |
| callback(); | |
| }); | |
| } else { | |
| return gulp.src(getSrcByParam()) | |
| .pipe(named()) | |
| .pipe(webpackStream({ | |
| module: { | |
| loaders: commonLoaders | |
| }, | |
| plugins: commonPlugins | |
| })) | |
| .pipe(gulp.dest('static/build/')); | |
| } | |
| }); | |
| gulp.task('watch',['build'], function () { | |
| gulp.watch('frontend/**/*' , ['build']); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment