1 | const path = require('path');
|
2 | const gulp = require('gulp');
|
3 | const gutil = require('gulp-util');
|
4 | const plumber = require('gulp-plumber');
|
5 | const through = require('through2');
|
6 | const ranma = require('ranma');
|
7 |
|
8 | function 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 |
|
33 | gulp.task(
|
34 | 'amd2commonjs',
|
35 | () => gulp.src(path.resolve(__dirname, '../temp/amd/**/*.js'))
|
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/')))
|
42 | );
|