-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathgulpfile.js
299 lines (284 loc) · 8.74 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
const gulp = require('gulp');
const ejs = require('gulp-ejs');
const stylus = require('gulp-stylus');
const rename = require('gulp-rename');
const autoprefixer = require('gulp-autoprefixer');
const cleancss = require('gulp-cleancss');
const babel = require('gulp-babel');
const uglify = require('gulp-uglify');
const eslint = require('gulp-eslint');
const fs = require('fs');
const path = require('path');
const { spawn, spawnSync } = require('child_process');
const rm = require('rimraf');
const assign = require('object-assign');
const colors = require('colors/safe');
const inquirer = require('inquirer');
const gulpRun = require('run-sequence');
const semver = require('semver');
const git = require('git-rev');
const webpack = require('webpack');
const ProgressBarPlugin = require('progress-bar-webpack-plugin');
const BrowserSyncPlugin = require('browser-sync-webpack-plugin');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const commonWebpackCfg = require('./webpack.dev.js');
const pkg = require('./package.json');
const cleancssOption = {
advanced: false,
aggressiveMerging: false,
sourceMap: true,
compatibility: 'ie8',
debug: true,
};
colors.setTheme({
info: ['bold', 'green'],
});
const runCmd = (cmd, args = [], fn, stdoutFn) => {
console.log(`Run CMD: ${cmd} ${args.join(' ')}`);
const runner = spawn(cmd, args, {
// keep color
stdio: stdoutFn ? 'pipe' : 'inherit',
});
if (stdoutFn) {
runner.stdout.on('data', (data) => {
stdoutFn(data.toString());
});
}
runner.on('close', (code) => {
if (fn) {
fn(code);
}
});
};
const upperInitWord = str => str.split('-').map(key => (key[0].toUpperCase() + key.slice(1))).join('');
const getQuestions = () => (
new Promise((resolve) => {
git.branch((branch) => {
const defaultBranch = branch;
const questions = [
{
type: 'input',
name: 'version',
message: 'please enter the package version to publish (should be xx.xx.xx)',
default: pkg.version,
validate(input) {
if (semver.valid(input)) {
if (semver.gt(input, pkg.version)) {
return true;
}
return 'the version you entered should be larger than now';
}
return 'the version you entered is not valid';
},
},
{
type: 'input',
name: 'branch',
message: 'which branch you want to push',
default: defaultBranch,
},
];
resolve(questions);
});
})
);
gulp.task('build_style', () => {
const dirs = fs.readdirSync('./src');
const ComponentNames = dirs.filter(dirName => (['Style', 'Utils'].indexOf(dirName) === -1) && !/^\./.test(dirName));
gulp.src('./template/component.styl')
.pipe(ejs({ ComponentNames }))
.pipe(gulp.dest('./style'))
.on('end', () => {
const themes = fs.readdirSync('./style/theme');
themes.forEach((theme) => {
gulp
.src(`./style/theme/${theme}/saltui.styl`)
.pipe(stylus())
.pipe(autoprefixer({
browsers: ['iOS >= 7', 'Android >= 2.3', 'FireFoxAndroid >= 46', '> 1%'],
}))
.pipe(rename(`${theme}.css`))
.pipe(gulp.dest('./build'))
.pipe(cleancss(cleancssOption))
.pipe(rename({
suffix: '.min',
}))
.pipe(gulp.dest('./build'));
});
});
});
gulp.task('build_lib', (cb) => {
rm('./lib/*', () => {
gulp
.src(['./src/**/*.js', './src/**/*.jsx'])
.pipe(babel({
presets: ['react', 'env', 'stage-1'],
plugins: ['add-module-exports'],
}))
.pipe(gulp.dest('./lib'))
.on('end', () => {
console.log('###### build_js done ######');
if (cb) {
cb();
}
});
});
});
gulp.task('make_index', (done) => {
const dirs = fs.readdirSync('./src');
const ComponentNames = dirs.filter(dirName => dirName !== 'Style' && !/^\./.test(dirName));
gulp
.src('./template/buildIndex.js')
.pipe(ejs({ ComponentNames }))
.pipe(rename('index.js'))
.pipe(gulp.dest('./'))
.on('end', () => {
done();
});
});
gulp.task('build_js', ['build_lib', 'make_index'], (done) => {
const plugins = [
// new BundleAnalyzerPlugin(),
new ProgressBarPlugin(),
new webpack.DefinePlugin({
webpack_set_version: `__SALT_VERSION__ = "${pkg.version}"`,
}),
];
const webpackCfg = assign({}, commonWebpackCfg, {
entry: {
index: './index',
},
output: {
path: path.join(process.cwd(), './build'),
filename: 'salt-ui.js',
library: 'SaltUI',
libraryTarget: 'umd',
},
plugins,
externals: {
react: {
root: 'React',
var: 'React',
commonjs: 'react',
commonjs2: 'react',
amd: 'react',
},
'react-dom': {
root: 'ReactDOM',
var: 'ReactDOM',
commonjs: 'react-dom',
commonjs2: 'react-dom',
amd: 'react-dom',
},
},
});
webpack(webpackCfg, (err, stats) => {
if (err) {
console.log(err);
} else {
console.log(`webpack log:${stats.toString({
hash: false,
chunks: false,
children: false,
})}`);
done();
}
});
});
gulp.task('uglify_js', ['build_js'], (done) => {
gulp.src('./build/salt-ui.js')
.pipe(uglify({
mangle: false,
}))
.pipe(rename('salt-ui.min.js'))
.pipe(gulp.dest('./build'))
.on('end', () => {
done();
});
});
gulp.task('build', ['uglify_js', 'build_style']);
gulp.task('pub', () => {
getQuestions().then((questions) => {
inquirer.prompt(questions).then((answers) => {
pkg.version = answers.version;
fs.writeFileSync('package.json', JSON.stringify(pkg, null, ' '));
gulpRun('build', () => {
console.log(colors.info('#### Git Info ####'));
spawnSync('git', ['add', '.'], { stdio: 'inherit' });
spawnSync('git', ['commit', '-m', `ver. ${pkg.version}`], { stdio: 'inherit' });
spawnSync('git', ['tag', `${pkg.version}`], { stdio: 'inherit' });
spawnSync('git', ['push', 'origin', `${pkg.version}`], { stdio: 'inherit' });
spawnSync('git', ['push', 'origin', answers.branch], { stdio: 'inherit' });
console.log(colors.info('#### Npm Info ####'));
spawnSync('npm', ['publish'], { stdio: 'inherit' });
});
}).catch((err) => { console.log(err); });
}).catch((err) => { console.log(err); });
});
gulp.task('server', () => {
/* eslint-disable camelcase */
let component_name = 'button';
const options = process.argv.slice(3);
if (options[0] === '-m' && options[1]) {
component_name = options[1].toLowerCase();
}
gulp
.src('./template/devIndex.jsx')
.pipe(ejs({
ComponentName: upperInitWord(component_name),
component_name,
}))
.pipe(rename('index.jsx'))
.pipe(gulp.dest('./dev/'))
.on('end', () => {
const plugins = [
new BrowserSyncPlugin({
server: {
baseDir: './',
},
startPath: `#${component_name}`,
open: 'external',
}, {
reload: true,
}),
// SourceMap plugin will define process.env.NODE_ENV as development
new webpack.SourceMapDevToolPlugin({
// columns: false,
}),
// new BundleAnalyzerPlugin(),
new ProgressBarPlugin(),
new webpack.NormalModuleReplacementPlugin(
/salt-(.+)/,
(resource) => {
/* eslint-disable no-param-reassign */
if (resource.request.indexOf('salt-icon') === -1 && resource.request.indexOf('salt-ui') === -1) {
resource.request = resource.request.replace(/salt-(.+)/, (match, s1) => require.resolve(`./src/${upperInitWord(s1)}`));
}
/* eslint-enable no-param-reassign */
},
),
];
const compiler = webpack(assign({}, commonWebpackCfg, { plugins }));
compiler.watch({}, (err, stats) => {
console.log(`webpack log:${stats}`);
if (stats.hasErrors()) {
// 异常日志打印到屏幕
fs.writeFileSync('./dist/demo.js', [
'document.body.innerHTML="<pre>',
stats.toJson().errors[0].replace(/[\n\r]/g, '<br>').replace(/\[\d+m/g, '').replace(/"/g, '\\"'),
'</pre>";',
'document.body.firstChild.style.fontFamily="monospace";',
'document.body.firstChild.style.lineHeight="1.5em";',
'document.body.firstChild.style.margin="1em";',
].join(''));
}
console.info('###### pack_demo done ######');
});
});
/* eslint-enable camelcase */
});
gulp.task('eslint', () => {
gulp.src(['./demo/**/*.js', './demo/**/*.jsx'])
.pipe(eslint())
.pipe(eslint.format('checkstyle'));
});