forked from choerodon/iam-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
89 lines (82 loc) · 2.49 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
const path = require('path');
const gulp = require('gulp');
const rimraf = require('rimraf');
const babel = require('gulp-babel');
const through2 = require('through2');
const cwd = process.cwd();
const libDir = path.join(cwd, 'lib');
function compileAssets() {
return gulp.src(['react/**/*.@(jpg|png|svg|scss|less|html|ico)']).pipe(gulp.dest(libDir));
}
function getBabelCommonConfig() {
const plugins = [
require.resolve('babel-plugin-syntax-dynamic-import'),
require.resolve('babel-plugin-transform-decorators-legacy'),
require.resolve('babel-plugin-transform-es3-member-expression-literals'),
require.resolve('babel-plugin-transform-es3-property-literals'),
require.resolve('babel-plugin-transform-object-assign'),
require.resolve('babel-plugin-transform-class-properties'),
require.resolve('babel-plugin-transform-object-rest-spread'),
[require.resolve('babel-plugin-transform-runtime'), {
polyfill: false,
}],
[
require.resolve('babel-plugin-import'),
[
{
libraryName: 'choerodon-ui',
style: true,
},
{
libraryName: 'choerodon-ui/pro',
style: true,
},
],
],
require.resolve('babel-plugin-lodash'),
];
return {
presets: [
require.resolve('babel-preset-react'),
require.resolve('babel-preset-es2015'),
require.resolve('babel-preset-stage-1'),
],
plugins,
};
}
function babelify(js, dir = '') {
const babelConfig = getBabelCommonConfig();
const stream = js.pipe(babel(babelConfig));
return stream
// eslint-disable-next-line func-names
.pipe(through2.obj(function (file, encoding, next) {
const matches = file.path.match(/(routes|dashboard|guide|entry|entrywithoutsider)\.nunjucks\.(js|jsx)/);
if (matches) {
const content = file.contents.toString(encoding);
file.contents = Buffer.from(content
.replace(`'{{ ${matches[1]} }}'`, `{{ ${matches[1]} }}`)
// eslint-disable-next-line quotes
.replace(`'{{ home }}'`, '{{ home }}')
// eslint-disable-next-line quotes
.replace(`'{{ master }}'`, '{{ master }}'));
}
this.push(file);
next();
}))
.pipe(gulp.dest(path.join(libDir, dir)));
}
function compileFile() {
const source = [
'react/**/*.js',
'react/**/*.jsx',
];
return babelify(gulp.src(source));
}
function compile() {
rimraf.sync(libDir);
compileAssets();
compileFile();
}
gulp.task('compile', () => {
compile();
});