|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var _ = require('lodash'); |
| 4 | +var bs = require('browser-sync').create(); |
| 5 | +var gulp = require('gulp'); |
| 6 | +var path = require('path'); |
| 7 | +var webpack = require("webpack"); |
| 8 | + |
| 9 | +gulp.task('default', ['develop']); |
| 10 | + |
| 11 | +var webpackOptions = { |
| 12 | + entry: { |
| 13 | + app: "./src/app.js", |
| 14 | + vendor: ["react", "react-dom", "muicss", "stellar-sdk", "axios", "d3", "fbemitter"] |
| 15 | + }, |
| 16 | + devtool: "source-map", |
| 17 | + resolve: { |
| 18 | + root: [ |
| 19 | + path.resolve('src'), |
| 20 | + path.resolve('common') |
| 21 | + ], |
| 22 | + modulesDirectories: ["node_modules"] |
| 23 | + }, |
| 24 | + module: { |
| 25 | + loaders: [ |
| 26 | + {test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader', query: {presets: ['es2015', 'react']}}, |
| 27 | + {test: /\.json$/, loader: 'json'}, |
| 28 | + {test: /\.html$/, loader: 'file?name=[name].html'}, |
| 29 | + ] |
| 30 | + }, |
| 31 | + plugins: [ |
| 32 | + new webpack.IgnorePlugin(/ed25519/) |
| 33 | + ] |
| 34 | +}; |
| 35 | + |
| 36 | +gulp.task('develop', function(done) { |
| 37 | + var options = merge(webpackOptions, { |
| 38 | + output: { |
| 39 | + filename: "[name].js", |
| 40 | + path: './.tmp/admin' |
| 41 | + }, |
| 42 | + plugins: [ |
| 43 | + new webpack.optimize.CommonsChunkPlugin("vendor", "vendor.js") |
| 44 | + ] |
| 45 | + }); |
| 46 | + |
| 47 | + var watchOptions = { |
| 48 | + aggregateTimeout: 300 |
| 49 | + }; |
| 50 | + |
| 51 | + var bsInitialized = false; |
| 52 | + |
| 53 | + var compiler = webpack(options); |
| 54 | + compiler.purgeInputFileSystem(); |
| 55 | + compiler.watch(watchOptions, function(error, stats) { |
| 56 | + if (!bsInitialized) { |
| 57 | + gulp.watch(".tmp/**/*").on("change", bs.reload); |
| 58 | + bs.init({ |
| 59 | + port: 3000, |
| 60 | + online: false, |
| 61 | + notify: false, |
| 62 | + server: "./.tmp", |
| 63 | + startPath: '/admin/index.html', |
| 64 | + socket: { |
| 65 | + // Golang httputil.ReverseProxy removes trailing slash from request before passing |
| 66 | + // it to destination folder. This made the socket.io used by browser-sync fail. |
| 67 | + // Make sure socket.io client connects to the destination server (not use proxy). |
| 68 | + domain: 'localhost:3000' |
| 69 | + } |
| 70 | + }); |
| 71 | + bsInitialized = true; |
| 72 | + } |
| 73 | + console.log(stats.toString({ |
| 74 | + hash: false, |
| 75 | + version: false, |
| 76 | + timings: true, |
| 77 | + chunks: false, |
| 78 | + colors: true |
| 79 | + })); |
| 80 | + }); |
| 81 | +}); |
| 82 | + |
| 83 | +gulp.task('build', function(done) { |
| 84 | + var options = merge(webpackOptions, { |
| 85 | + bail: true, |
| 86 | + output: { |
| 87 | + // TODO chunkhash |
| 88 | + filename: "[name].js",//"[name]-[chunkhash].js", |
| 89 | + path: './dist' |
| 90 | + }, |
| 91 | + plugins: [ |
| 92 | + new webpack.optimize.CommonsChunkPlugin("vendor", "vendor.js"), |
| 93 | + new webpack.optimize.DedupePlugin(), |
| 94 | + new webpack.optimize.OccurenceOrderPlugin(), |
| 95 | + new webpack.DefinePlugin({ |
| 96 | + 'process.env': { |
| 97 | + NODE_ENV: JSON.stringify('production'), |
| 98 | + } |
| 99 | + }), |
| 100 | + new webpack.optimize.UglifyJsPlugin() |
| 101 | + ] |
| 102 | + }); |
| 103 | + |
| 104 | + var compiler = webpack(options); |
| 105 | + compiler.purgeInputFileSystem(); |
| 106 | + compiler.run(done); |
| 107 | +}); |
| 108 | + |
| 109 | + |
| 110 | +function merge(object1, object2) { |
| 111 | + return _.mergeWith(object1, object2, function(a, b) { |
| 112 | + if (_.isArray(a)) { |
| 113 | + return a.concat(b); |
| 114 | + } |
| 115 | + }); |
| 116 | +} |
0 commit comments