|
| 1 | +/** |
| 2 | + * # Task: Source - Distribution |
| 3 | + * |
| 4 | + * Configuration to build browser packages. |
| 5 | + */ |
| 6 | + |
| 7 | +import { emptyDirSync } from 'fs-extra' |
| 8 | +import webpack from 'webpack' |
| 9 | +const merge = require('deep-merge')((target, source) => { |
| 10 | + if (target instanceof Array) { |
| 11 | + return [].concat(target, source) |
| 12 | + } |
| 13 | + return source |
| 14 | +}) |
| 15 | + |
| 16 | +/** |
| 17 | + * [default description] |
| 18 | + * @param {[type]} env [description] |
| 19 | + * @return {[type]} [description] |
| 20 | + */ |
| 21 | +export default (env) => { |
| 22 | + emptyDirSync(env.DIST) |
| 23 | + return new Promise((resolve, reject) => { |
| 24 | + |
| 25 | + var config = { |
| 26 | + target: 'web', |
| 27 | + entry: `${env.SRC}/index.js`, |
| 28 | + resolve: { |
| 29 | + extensions: ['', '.js'] |
| 30 | + }, |
| 31 | + output: { |
| 32 | + path: env.DIST, |
| 33 | + filename: 'redux-sync.js', |
| 34 | + library: 'ReduxSync', |
| 35 | + libraryTarget: 'umd' |
| 36 | + }, |
| 37 | + module: { |
| 38 | + loaders: [ |
| 39 | + { |
| 40 | + test: /\.js$/, |
| 41 | + include: env.SRC, |
| 42 | + loader: 'babel' |
| 43 | + } |
| 44 | + ] |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // = development |
| 49 | + if (__DEVELOPMENT__) { |
| 50 | + const DevConfig = merge(config, { |
| 51 | + debug: true, |
| 52 | + devtool: 'inline-source-map' |
| 53 | + }) |
| 54 | + var ready = false |
| 55 | + return webpack(DevConfig).watch(100, (error, stats) => { |
| 56 | + if (ready) { |
| 57 | + if (error) { |
| 58 | + return console.error(error) |
| 59 | + } |
| 60 | + return console.log(new Date().toISOString(), ' - [ReduxSync]', stats.toString()) |
| 61 | + } |
| 62 | + if (error) { |
| 63 | + return reject(error) |
| 64 | + } |
| 65 | + ready = true |
| 66 | + return resolve() |
| 67 | + }) |
| 68 | + } |
| 69 | + |
| 70 | + // = production:debug |
| 71 | + const ProductionDebugConfig = merge(config, { |
| 72 | + debug: true, |
| 73 | + devtool: 'sourcemap', |
| 74 | + plugins: [ |
| 75 | + new webpack.DefinePlugin({ |
| 76 | + 'process.env': { |
| 77 | + 'NODE_ENV': JSON.stringify('development') |
| 78 | + } |
| 79 | + }), |
| 80 | + new webpack.optimize.OccurenceOrderPlugin(), |
| 81 | + new webpack.optimize.DedupePlugin(), |
| 82 | + new webpack.optimize.UglifyJsPlugin({ |
| 83 | + sourceMap: true, |
| 84 | + compress: { |
| 85 | + warnings: false, |
| 86 | + screw_ie8: true |
| 87 | + } |
| 88 | + }) |
| 89 | + ] |
| 90 | + }) |
| 91 | + |
| 92 | + // = production:min |
| 93 | + const ProductionMinConfig = merge(config, { |
| 94 | + debug: false, |
| 95 | + output: { |
| 96 | + filename: config.output.filename.replace('.js', '.min.js') |
| 97 | + }, |
| 98 | + plugins: [ |
| 99 | + new webpack.DefinePlugin({ |
| 100 | + 'process.env': { |
| 101 | + 'NODE_ENV': JSON.stringify('production') |
| 102 | + } |
| 103 | + }), |
| 104 | + new webpack.optimize.OccurenceOrderPlugin(), |
| 105 | + new webpack.optimize.DedupePlugin(), |
| 106 | + new webpack.optimize.UglifyJsPlugin({ |
| 107 | + sourceMap: false, |
| 108 | + compress: { |
| 109 | + warnings: false, |
| 110 | + screw_ie8: true |
| 111 | + } |
| 112 | + }) |
| 113 | + ] |
| 114 | + }) |
| 115 | + |
| 116 | + return webpack(ProductionDebugConfig).run((error, stats) => { |
| 117 | + if (error) { |
| 118 | + return rejec(error) |
| 119 | + } |
| 120 | + return webpack(ProductionMinConfig).run((error, stats) => { |
| 121 | + if (error) { |
| 122 | + return reject(error) |
| 123 | + } |
| 124 | + return resolve() |
| 125 | + }) |
| 126 | + }) |
| 127 | + |
| 128 | + }) |
| 129 | +} |
0 commit comments