forked from alfa-laboratory/library-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulp-tasks.js
196 lines (167 loc) · 6.47 KB
/
gulp-tasks.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
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
const path = require('path');
const fs = require('fs');
const gulp = require('gulp');
const babel = require('gulp-babel');
const sourcemaps = require('gulp-sourcemaps');
const postcss = require('gulp-postcss');
const rename = require('gulp-rename');
const filter = require('gulp-filter');
const clone = require('gulp-clone');
const es = require('event-stream');
const ts = require('gulp-typescript');
const del = require('del');
const componentPackage = require('./gulp/component-package');
const componentTypings = require('./gulp/component-typings');
const componentDocs = require('./gulp/component-docs');
const libraryDoc = require('./gulp/library-doc');
const defaultOptions = {
publishDir: '.publish',
docsDir: 'docs',
tsConfigFilename: 'tsconfig.json',
componentsGlob: ['src/*/*.jsx', '!src/*/*-test.jsx', '!src/*/*-benchmark.jsx'],
tsComponentsGlob: ['src/*/*.tsx'],
jsGlob: ['src/**/*.{js,jsx}', '!src/**/*-test.{js,jsx}', '!src/**/*-benchmark.{js,jsx}'],
autoDtsGlob: [
'src/**/*.{js,jsx}', '!src/**/index.{js,jsx}', '!src/**/*-test.{js,jsx}', '!src/**/*-benchmark.{js,jsx}'
],
tsGlob: ['src/**/*.{ts,tsx}', '!src/**/*-test.{ts,tsx}', '!src/**/*-benchmark.{ts,tsx}'],
cssGlob: ['src/**/*.css', '!src/vars/**/*.css', '!src/vars*.css'],
cssCopyGlob: ['src/**/vars/**/*.css', 'src/vars*.css'],
resourcesGlob: ['src/**/*.{png,gif,jpg,svg,ttf,woff,json}'],
publishFilesGlob: ['package.json', '*.md', 'LICENSE']
};
const errors = [];
function handleError(error) {
errors.push(error);
}
function checkErrors() {
if (errors.length > 0) {
process.exit(1);
}
}
function createTasks(packageName, options = {}) {
options = Object.assign({}, defaultOptions, options);
const tsConfigPath = path.resolve(process.cwd(), options.tsConfigFilename);
const isTsEnabled = fs.existsSync(tsConfigPath);
gulp.task('clean', () => del([options.publishDir]));
gulp.task('clean:docs', () => del([options.publishDir]));
gulp.task(
'js', ['clean'],
() => gulp.src(options.jsGlob)
.pipe(sourcemaps.init())
.pipe(babel())
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(options.publishDir))
);
gulp.task('ts:compile', ['clean'], () => {
const tsProject = ts.createProject(options.tsConfigFilename, { declaration: true });
const tsResult = gulp.src(options.tsGlob)
.pipe(sourcemaps.init())
.pipe(tsProject())
.on('error', handleError);
return es
.merge(tsResult.js, tsResult.dts)
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(options.publishDir))
.on('error', handleError);
});
gulp.task(
'ts:packages', ['clean'],
() => gulp.src(options.tsComponentsGlob)
.pipe(componentPackage())
.pipe(gulp.dest(options.publishDir))
.on('error', handleError)
);
gulp.task('ts', ['ts:compile', 'ts:packages']);
gulp.task('typings', ['clean'], () => {
const components = gulp.src(options.componentsGlob);
const packages = components
.pipe(clone())
.pipe(componentPackage())
.on('error', handleError);
const typingFiles = components
.pipe(clone())
.pipe(componentTypings())
.on('error', handleError);
return es
.merge(packages, typingFiles)
.pipe(gulp.dest(options.publishDir))
.on('error', handleError);
});
gulp.task(
'css:compile', ['clean'],
() => gulp.src(options.cssGlob)
.pipe(sourcemaps.init())
.pipe(postcss())
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(options.publishDir))
.on('error', handleError)
);
gulp.task(
'css:copy', ['clean'],
() => gulp.src(options.cssCopyGlob)
.pipe(gulp.dest(options.publishDir))
.on('error', handleError)
);
gulp.task('css', ['css:copy', 'css:compile']);
gulp.task(
'resources', ['clean'],
() => gulp.src(options.resourcesGlob)
.pipe(gulp.dest(options.publishDir))
.on('error', handleError)
);
gulp.task(
'publish-files', ['clean'],
() => gulp.src(options.publishFilesGlob)
.pipe(gulp.dest(options.publishDir))
.on('error', handleError)
);
gulp.task('docs', ['clean:docs'], () => {
let tsToJs;
let tsDocs;
if (isTsEnabled) {
const tsDocsProject = ts.createProject(options.tsConfigFilename, { jsx: 'preserve', target: 'es6' });
tsToJs = gulp.src(['src/*/*.tsx'])
.pipe(tsDocsProject(ts.reporter.nullReporter())).js;
tsDocs = tsToJs.pipe(clone())
.pipe(componentDocs(packageName));
}
const jsDocs = gulp.src(options.componentsGlob)
.pipe(componentDocs(packageName));
const indexFile = es.merge([gulp.src(options.componentsGlob)].concat(isTsEnabled ? [tsToJs] : []))
.pipe(libraryDoc(packageName))
.pipe(rename((filePath) => {
filePath.dirname = '';
filePath.basename = 'README';
filePath.extname = '.md';
}));
return es
.merge([jsDocs, indexFile].concat(isTsEnabled ? [tsDocs] : []))
.pipe(gulp.dest(options.docsDir));
});
gulp.task('dts', ['js', 'publish-files', 'typings'], () => {
const tsOptions = {
declaration: true,
allowSyntheticDefaultImports: true,
lib: ['dom', 'es2015', 'es2016']
};
return gulp.src(options.autoDtsGlob)
.pipe(rename((path) => {
// typescript compiler won't compile files with non-ts extensions
path.extname = path.extname === '.jsx' ? '.tsx' : '.ts';
}))
.pipe(filter(file => !fs.existsSync(
// ignore all files, that already emit d.ts file
path.join(process.cwd(), options.publishDir, file.relative)
.replace(/\.tsx?$/, '.d.ts')
)))
.pipe(ts(tsOptions, ts.reporter.nullReporter())) // ignore all errors at compile time
.dts.pipe(gulp.dest(options.publishDir));
});
gulp.task(
'compile',
['js', 'css', 'resources', 'typings', 'publish-files', 'dts'].concat(isTsEnabled ? ['ts'] : []),
checkErrors
);
}
module.exports = createTasks;