-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
148 lines (135 loc) · 3.02 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
const { dest, src, series } = require('gulp');
const path = require('path');
const eslint = require('gulp-eslint');
const babel = require('gulp-babel');
const scss = require('gulp-sass');
const autoprefixer = require('gulp-autoprefixer');
const cssnano = require('gulp-cssnano');
const del = require('del');
const rollup = require('rollup');
const resolve = require('rollup-plugin-node-resolve');
const commonjs = require('rollup-plugin-commonjs');
const ts = require('rollup-plugin-typescript2');
const { babel: rollupBabel } = require('@rollup/plugin-babel');
const replace = require('rollup-plugin-replace');
const { terser } = require('rollup-plugin-terser');
const { name } = require('./package.json');
const toHump = name => name.replace(/\-(\w){1}/, (all, $1) => $1.toUpperCase());
const components = [
'components/**/*.tsx',
'components/**/*.ts',
'!components/**/docs/**',
'!components/**/__tests__/**',
];
const styles = ['components/**/*.scss', 'components/*.scss'];
const FOLDER = {
es: 'es',
lib: 'lib',
dist: 'dist',
};
const Entry = path.join(__dirname, 'components/index.ts');
function compileScripts(babelEnv, destDir) {
// set eslint env
process.env.BABEL_ENV = babelEnv;
return src(components)
.pipe(babel())
.pipe(dest(destDir));
}
function clean(cb) {
del([FOLDER.dist, FOLDER.lib, 'es'])
.then(() => {
cb();
})
.catch(err => {
throw err;
});
}
async function lint(cb) {
src(components)
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
cb();
}
function copyScss(cb) {
src(styles)
.pipe(dest(FOLDER.lib))
.pipe(dest(FOLDER.es));
cb();
}
/**
*
* compile scss
*/
function compileScss(cb) {
src(styles)
.pipe(scss())
.pipe(autoprefixer())
.pipe(cssnano({ zindex: false, reduceIdents: false }))
.pipe(dest(FOLDER.lib))
.pipe(dest(FOLDER.es))
.pipe(dest(FOLDER.dist));
cb();
}
/**
*
* compile cjs ES5
*/
function compileCJS() {
return compileScripts('cjs', FOLDER.lib);
}
/**
*
* compile esm ES6
*/
function compileESM() {
return compileScripts('esm', FOLDER.es);
}
/**
*
* compile dist CDN
*/
async function compileCDN() {
const bundle = await rollup.rollup({
input: Entry,
external: ['react', 'react-dom'],
plugins: [
replace({
'process.env.NODE_ENV': JSON.stringify('production'),
}),
terser(),
ts(),
rollupBabel(),
resolve({
jsnext: true,
main: true,
browser: true,
}),
commonjs({
include: 'node_modules/**',
namedExports: {
'node_modules/react-is/index.js': ['isFragment', 'ForwardRef', 'isMemo'],
},
}),
],
});
return await bundle.write({
file: `${FOLDER.dist}/${name}.min.js`,
format: 'umd',
name: toHump(name),
globals: {
react: 'React',
'react-dom': 'ReactDOM',
},
});
}
exports.compileCDN = series(compileCDN);
exports.default = series(
clean,
lint,
copyScss,
compileScss,
compileCJS,
compileESM,
compileCDN,
);