UNPKG

1.22 kBJavaScriptView Raw
1const path = require('path');
2const gulp = require('gulp');
3const gutil = require('gulp-util');
4const plumber = require('gulp-plumber');
5const through = require('through2');
6const ranma = require('ranma');
7
8function cjsify() {
9 return through.obj(function render(file, enc, cb) {
10 if (file.isNull()) {
11 this.push(file);
12 return cb();
13 }
14
15 if (file.isStream()) {
16 this.emit('error', new gutil.PluginError('cmd2commonjs', 'Streaming not supported'));
17 }
18
19 try {
20 gutil.log(file.path);
21 const content = ranma.cjsify(file.contents.toString('utf8'));
22 file.contents = new Buffer(content);
23 } catch (err) {
24 this.emit('error', new gutil.PluginError('cmd2commonjs', err.toString()));
25 }
26
27 this.push(file);
28 return cb();
29 });
30}
31
32// 这个脚本不能多次执行,因此在临时文件夹内进行
33gulp.task(
34 'cmd2commonjs',
35 () => gulp.src(path.resolve(__dirname, '../temp/cmd/**/*.js')) // FIXME modify this to specify target
36 .pipe(plumber())
37 .pipe(cjsify())
38 .on('error', (err) => {
39 gutil.log(gutil.colors.red(err.message));
40 })
41 .pipe(gulp.dest(path.resolve(__dirname, '../temp/commonjs/'))) // FIXME modify this to specify target
42);