Skip to content

Commit e132e76

Browse files
committed
add build scripts
1 parent 6e8d751 commit e132e76

3 files changed

Lines changed: 205 additions & 0 deletions

File tree

build/index.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* # Build
3+
*
4+
* Handles the scripts defined in 'package.json'
5+
*/
6+
7+
require('babel-register')
8+
9+
var path = require('path')
10+
11+
// environment (default mode: development)
12+
global.__DEVELOPMENT__ = !(process.env.NODE_ENV === 'production' || process.argv.length > 2)
13+
14+
var env = {
15+
SRC: path.resolve(__dirname, '../src'),
16+
LIB: path.resolve(__dirname, '../lib'),
17+
DIST: path.resolve(__dirname, '../dist')
18+
}
19+
20+
var SourceLibrary = require('./tasks/src-lib')
21+
var SourceDistribution = require('./tasks/src-dist')
22+
23+
SourceLibrary(env).then(function () {
24+
return SourceDistribution(env)
25+
})
26+
.then(function(){
27+
console.log('[BUILD]', __DEVELOPMENT__ ? 'WATCH' : 'RELEASE')
28+
})
29+
.catch(console.error.bind(console))

build/tasks/src-dist.js

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
}

build/tasks/src-lib.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* # Task: Source - Library
3+
*
4+
*
5+
*/
6+
7+
import { emptyDirSync } from 'fs-extra'
8+
import gulp from 'gulp'
9+
const $ = require('gulp-load-plugins')()
10+
11+
/**
12+
* [description]
13+
* @param {[type]} env [description]
14+
* @return {[type]} [description]
15+
*/
16+
export default (env) => {
17+
emptyDirSync(env.LIB)
18+
return new Promise((resolve, reject) => {
19+
const MATCH_ALL = `${env.SRC}/**/*`
20+
const target = `${env.LIB}/`
21+
transform(gulp.src(MATCH_ALL), target).on('end', () => {
22+
if (__DEVELOPMENT__) {
23+
$.watch(MATCH_ALL, (file) => {
24+
transform(gulp.src(file.path, { base: `${env.SRC}` }), target)
25+
.on('end', () => console.log('[CHANGE]', $.util.colors.yellow(file.path)))
26+
})
27+
}
28+
return resolve()
29+
})
30+
})
31+
}
32+
33+
/**
34+
* Lazypipe alternative re-usable code
35+
* @param {[type]} stream [description]
36+
* @param {[type]} target [description]
37+
* @return {[type]} [description]
38+
*/
39+
function transform (stream, target) {
40+
return stream
41+
.pipe($.plumber(::console.error))
42+
.pipe($.sourcemaps.init())
43+
.pipe($.babel({/** see .babelrc **/}))
44+
.pipe($.sourcemaps.write())
45+
.pipe($.plumber.stop())
46+
.pipe(gulp.dest(target))
47+
}

0 commit comments

Comments
 (0)