-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
197 lines (184 loc) · 4.67 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* Copyright (c) 2015-present, Joshua Hollenbeck
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var babel = require('gulp-babel')
, babelPluginDEV = require('./scripts/dev-expression')
// , babelPluginRewriteModules = require('./scripts/rewrite-modules')
, del = require('del')
, gulp = require('gulp')
, derequire = require('gulp-derequire')
, gulpUtil = require('gulp-util')
, header = require('gulp-header')
, runSequence = require('run-sequence')
, webpackStream = require('webpack-stream')
, sourcemaps = require('gulp-sourcemaps')
, mocha = require('gulp-mocha')
, ts = require('gulp-typescript');
const HEADER = [
'/**',
' * Refrax v<%= version %>',
' *',
' * Copyright (c) 2015-present, Joshua Hollenbeck',
' * All rights reserved.',
' *',
' * This source code is licensed under the BSD-style license found in the',
' * LICENSE file in the root directory of this source tree.',
' */'
].join('\n') + '\n';
const buildDist = (opts) => {
var webpackOpts = {
externals: /^[-\/a-zA-Z0-9]+$/,
output: {
filename: opts.output,
libraryTarget: 'umd',
library: 'Refrax'
},
plugins: [
new webpackStream.webpack.LoaderOptionsPlugin({
debug: opts.debug
}),
new webpackStream.webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(
opts.debug ? 'development' : 'production'
)
}),
new webpackStream.webpack.optimize.OccurrenceOrderPlugin(),
new webpackStream.webpack.optimize.DedupePlugin()
]
};
if (!opts.debug) {
webpackOpts.plugins.push(
new webpackStream.webpack.optimize.UglifyJsPlugin({
compress: {
hoist_vars: true,
screw_ie8: true,
warnings: false
}
})
);
}
return webpackStream(webpackOpts, null, (err, stats) => {
if (err) {
throw new gulpUtil.PluginError('webpack', err);
}
if (stats.compilation.errors.length) {
throw new gulpUtil.PluginError('webpack', stats.toString());
}
}).on('error', (error) => {
gulpUtil.log(gulpUtil.colors.red('JS Compile Error: '), error.message);
});
};
const paths = {
dist: 'dist',
lib: 'lib',
test: 'test',
entry: 'lib/refrax.js',
src: [
'src/**/*.ts',
'src/**/*.tsx',
'!src/**/*.d.ts'
],
srcTest: [
'src/**/*.ts',
'src/**/*.tsx',
'scripts/**/*.ts',
'!src/**/*.d.ts'
]
};
gulp.task('clean', (cb) => {
return del([paths.dist, paths.lib, paths.test], cb);
});
gulp.task('modules', () => {
return gulp
.src(paths.src)
.pipe(ts.createProject('tsconfig.json', {
declaration: true
})())
.pipe(babel({
ignore: ['*.d.ts'],
plugins: [
babelPluginDEV
]
}))
.pipe(gulp.dest(paths.lib));
});
gulp.task('modules-test', () => {
return gulp
.src(paths.srcTest)
.pipe(sourcemaps.init())
.pipe(ts.createProject('tsconfig.json', {
declaration: false
})())
.pipe(babel({
plugins: [
babelPluginDEV,
['babel-plugin-module-resolver', {
alias: {
'actions': './test/actions',
'adapters': './test/adapters',
'resource': './test/resource',
'response': './test/response',
'schema': './test/schema',
'store': './test/store',
'util': './test/util',
'test': './test/test'
}
}]
]
}))
.pipe(sourcemaps.write({ sourceRoot: '/src' }))
.pipe(gulp.dest(paths.test));
});
gulp.task('dist', function() {
var distOpts = {
debug: true,
output: 'refrax.js'
};
gulp.src(paths.entry)
.pipe(buildDist(distOpts))
.pipe(derequire())
.pipe(header(HEADER, {
version: process.env.npm_package_version
}))
.pipe(gulp.dest(paths.dist));
});
gulp.task('dist:min', function() {
var distOpts = {
debug: false,
output: 'refrax.min.js'
};
return gulp.src(paths.entry)
.pipe(buildDist(distOpts))
.pipe(header(HEADER, {
version: process.env.npm_package_version
}))
.pipe(gulp.dest(paths.dist));
});
gulp.task('testMocha', function() {
return gulp
.src([
'test/**/*.spec.js'
], {read: false})
.pipe(
mocha({
require: [
'test/test/ChaiDeepMatch.js',
'test/test/TestSupport.js'
]
})
.on('error', (error) => {
this.emit('end');
})
);
});
gulp.task('test', (cb) => {
runSequence('clean', 'modules-test', 'testMocha', cb);
});
gulp.task('default', (cb) => {
runSequence('clean', 'modules', ['dist', 'dist:min'], cb);
});