gulpfile.js
7.08 KB
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
const fs = require('fs');
const path = require('path');
const gulp = require('gulp');
const gulpSequence = require('gulp-sequence');
const gulpUglify = require('gulp-uglify');
const gulpConcat = require('gulp-concat');
const gulpCleanCSS = require('gulp-clean-css');
const gulpEslint = require('gulp-eslint');
const gulpRename = require('gulp-rename');
const gulpHeader = require('gulp-header');
const gulpStylus = require('gulp-stylus');
const babel = require('rollup-plugin-babel');
const eslint = require('rollup-plugin-eslint');
const pkg = require('../package.json');
const walkByRollup = require('./rollupbuild').walk;
const banner = ['/*!',
' * <%= pkg.name %> v<%= pkg.version %>',
' * (c) 2017-<%= date %> <%= pkg.author %>',
' * Released under the <%= pkg.license %> License.',
' * <%= pkg.homepage %>',
' */',
'',
].join('\n').replace(/<%=\s([^%]+)\s%>/g, ($0, $1) => ($1 === 'date' ? new Date().getFullYear() : (pkg[$1.split('.')[1]] || '')));
const RELEASE_ROOT_PATH = 'dist';
const RELEASE_DEBUG_PATH = 'dist/debug';
const RELEASE_PUBLIC_PATH = 'dist';
const SOURCE_ROOT_PATH = 'src';
const isSourceMap = false;
function resolvePath(p) {
return path.resolve(__dirname, '../', p);
}
if (!fs.existsSync(resolvePath(RELEASE_ROOT_PATH))) {
fs.mkdirSync(resolvePath(RELEASE_ROOT_PATH));
}
if (!fs.existsSync(resolvePath(RELEASE_DEBUG_PATH))) {
fs.mkdirSync(resolvePath(RELEASE_DEBUG_PATH));
}
function buildByRollUp(input, output, name) {
return walkByRollup([{
input,
plugins: [
eslint({
exclude: 'node_modules/**',
}),
babel({
exclude: 'node_modules/**',
// rollup使用"transform-runtime"只能通过配置
// 可以将所有的垫片函数集成到一起,避免重复冗余
runtimeHelpers: true,
// 使用class时,external-helpers会莫名其妙引入asyncGenerator
// 而runtime不会
// plugins: ['external-helpers'],
}),
],
output: {
file: output,
},
format: 'umd',
name,
banner,
sourcemap: isSourceMap,
}]);
}
gulp.task('build_main', () => buildByRollUp(
resolvePath(`${SOURCE_ROOT_PATH}/index.js`),
resolvePath(`${RELEASE_DEBUG_PATH}/minirefresh.js`),
'MiniRefreshTools'));
gulp.task('build_themes', () => {
const basePath = resolvePath(`${SOURCE_ROOT_PATH}/themes/`);
const pa = fs.readdirSync(basePath);
if (!fs.existsSync(resolvePath(`${RELEASE_DEBUG_PATH}/themes/`))) {
fs.mkdirSync(resolvePath(`${RELEASE_DEBUG_PATH}/themes/`));
}
pa.forEach((ele) => {
const info = fs.statSync(path.resolve(basePath, ele));
if (info.isDirectory()) {
// console.log(`dir:${ele}`);
if (!fs.existsSync(resolvePath(`${RELEASE_DEBUG_PATH}/themes/${ele}`))) {
fs.mkdirSync(resolvePath(`${RELEASE_DEBUG_PATH}/themes/${ele}`));
}
buildByRollUp(
resolvePath(`${SOURCE_ROOT_PATH}/themes/${ele}/${ele}.js`),
resolvePath(`${RELEASE_DEBUG_PATH}/themes/${ele}/minirefresh.theme.${ele}.js`),
'MiniRefresh');
// css
gulp.src([
resolvePath(`${SOURCE_ROOT_PATH}/themes/${ele}/${ele}.styl`),
])
.pipe(gulpStylus())
.pipe(gulpConcat(`minirefresh.theme.${ele}.css`))
.pipe(gulp.dest(resolvePath(`${RELEASE_DEBUG_PATH}/themes/${ele}/`)));
// 资源
gulp.src([
resolvePath(`${SOURCE_ROOT_PATH}/themes/${ele}/images/*`),
])
.pipe(gulp.dest(resolvePath(`${RELEASE_DEBUG_PATH}/themes/${ele}/images/`)));
}
});
});
// eslint代码检查打包文件以外的文件
gulp.task('eslint_others', () => gulp.src([
resolvePath('build/**/*.js'),
resolvePath('test/**/*.js'),
// 主动ignore
`!${resolvePath('test/inner/promise.js')}`,
])
.pipe(gulpEslint())
.pipe(gulpEslint.format()));
// 开启后如果报错会退出
// .pipe(gulpEslint.failAfterError());
gulp.task('concat_css', () => gulp.src([
resolvePath(`${SOURCE_ROOT_PATH}/styl/index.styl`),
])
.pipe(gulpStylus())
.pipe(gulpConcat('minirefresh.css'))
.pipe(gulp.dest(resolvePath(RELEASE_DEBUG_PATH))));
// 打包 核心文件的资源css和资源的打包
gulp.task('pack_resources', () => gulp.src([
resolvePath(`${SOURCE_ROOT_PATH}/styl/**/*`),
'!PATH'.replace('PATH', resolvePath(`${SOURCE_ROOT_PATH}/**/*.styl`)),
])
.pipe(gulp.dest(resolvePath(RELEASE_DEBUG_PATH))));
gulp.task('build', ['build_main', 'concat_css', 'pack_resources', 'build_themes', 'eslint_others']);
gulp.task('dist_js_uglify', () => gulp.src([
resolvePath(`${RELEASE_DEBUG_PATH}/**/*.js`),
'!PATH'.replace('PATH', resolvePath(`${RELEASE_DEBUG_PATH}/**/*.min.js`)),
])
.pipe(gulpUglify())
.on('error', (err) => {
console.log('line number: %d, message: %s', err.lineNumber, err.message);
this.end();
})
.pipe(gulpRename({
suffix: '.min',
}))
.pipe(gulpHeader(banner))
.pipe(gulp.dest(resolvePath(`${RELEASE_PUBLIC_PATH}/`))));
// 压缩core css
gulp.task('clean_css', () => gulp.src([
resolvePath(`${RELEASE_DEBUG_PATH}/**/*.css`),
'!PATH'.replace('PATH', resolvePath(`${RELEASE_DEBUG_PATH}/**/*.min.css`)),
])
.pipe(gulpCleanCSS())
.pipe(gulpRename({
suffix: '.min',
}))
.pipe(gulp.dest(resolvePath(`${RELEASE_PUBLIC_PATH}/`))));
gulp.task('pack_resources_release', () => gulp.src([
resolvePath(`${RELEASE_DEBUG_PATH}/**/*`),
'!PATH'.replace('PATH', resolvePath(`${RELEASE_DEBUG_PATH}/**/*.css`)),
'!PATH'.replace('PATH', resolvePath(`${RELEASE_DEBUG_PATH}/**/*.js`)),
])
.pipe(gulp.dest(resolvePath(`${RELEASE_PUBLIC_PATH}/`))));
gulp.task('dist', [
'pack_resources_release',
'dist_js_uglify',
'clean_css',
]);
gulp.task('concat_minirefresh_theme_js', () => gulp.src([
resolvePath(`${RELEASE_DEBUG_PATH}/minirefresh.js`),
resolvePath(`${RELEASE_DEBUG_PATH}/themes/default/minirefresh.theme.default.js`),
])
.pipe(gulpConcat('minirefresh.js'))
.pipe(gulp.dest(resolvePath(RELEASE_DEBUG_PATH))));
gulp.task('concat_minirefresh_theme_css', () => gulp.src([
resolvePath(`${RELEASE_DEBUG_PATH}/minirefresh.css`),
resolvePath(`${RELEASE_DEBUG_PATH}/themes/default/minirefresh.theme.default.css`),
])
.pipe(gulpConcat('minirefresh.css'))
.pipe(gulp.dest(resolvePath(RELEASE_DEBUG_PATH))));
/*
* 合并主题方式暂时放弃,改为代码内部自动选择引入对应主题
*/
gulp.task('concat_theme', [
'concat_minirefresh_theme_js',
'concat_minirefresh_theme_css',
]);
gulp.task('default', (callback) => {
gulpSequence('build', 'dist')(callback);
});
gulp.task('watch', () => {
gulp.watch([
resolvePath(`${SOURCE_ROOT_PATH}/**/**/*.js`),
resolvePath(`${SOURCE_ROOT_PATH}/**/**/*.styl`),
resolvePath('build/**/*.js'),
resolvePath('test/**/*.js'),
], ['default']);
});