forked from formio/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
358 lines (314 loc) · 11.2 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/* eslint-disable */
const { spawn } = require('child_process');
var gulp = require('gulp'),
path = require('path'),
rollup = require('gulp-better-rollup'),
sass = require('gulp-sass'),
cleanCSS = require('gulp-clean-css'),
replace = require('gulp-replace'),
rename = require('gulp-rename'),
fs = require('fs-extra'),
runSequence = require('run-sequence'),
inlineResources = require('./tools/gulp/inline-resources');
const rootFolder = path.join(__dirname);
const srcFolder = path.join(rootFolder, 'src');
const tmpFolder = path.join(rootFolder, '.tmp');
const buildFolder = path.join(rootFolder, 'build');
const distFolder = path.join(rootFolder, 'dist');
// Execute the ngc command from command line.
const ngc = function(commands, cb) {
const child = spawn('ngc', commands);
child.on('close', cb);
};
gulp.task('package-version', function packageVersion() {
const pkg = require('./package.json');
return gulp.src([
`${distFolder}/package.json`,
`${distFolder}/auth/package.json`,
`${distFolder}/resource/package.json`,
`${distFolder}/grid/package.json`
], {base: distFolder})
.pipe(replace(/"version": ""/, `"version": "${pkg.version}"`))
.pipe(replace(/"dependencies": {}/, `"dependencies": ${JSON.stringify(pkg.dependencies, null, 2)}`))
.pipe(replace(/"peerDependencies": {}/, `"peerDependencies": ${JSON.stringify(pkg.peerDependencies, null, 2)}`))
.pipe(gulp.dest(distFolder));
});
/**
* 1. Delete /dist folder
*/
gulp.task('clean:dist', function cleanDist() {
// Delete contents but not dist folder to avoid broken npm links
// when dist directory is removed while npm link references it.
return fs.emptyDir(distFolder);
});
/**
* 2. Clone the /src folder into /.tmp. If an npm link inside /src has been made,
* then it's likely that a node_modules folder exists. Ignore this folder
* when copying to /.tmp.
*/
gulp.task('copy:source', function () {
return gulp.src([`${srcFolder}/**/*`, `!${srcFolder}/node_modules`])
.pipe(gulp.dest(tmpFolder));
});
gulp.task('styles-formio', () => {
return gulp.src([`${tmpFolder}/components/formio/formio.component.scss`])
.pipe(sass().on('error', sass.logError))
.pipe(cleanCSS({compatibility: 'ie8'}))
.pipe(gulp.dest(`${tmpFolder}/components/formio`));
});
gulp.task('styles-builder', () => {
return gulp.src([`${tmpFolder}/components/formbuilder/formbuilder.component.scss`])
.pipe(sass().on('error', sass.logError))
.pipe(cleanCSS({compatibility: 'ie8'}))
.pipe(gulp.dest(`${tmpFolder}/components/formbuilder`));
});
gulp.task('formio-css', () => {
return gulp.src([`${tmpFolder}/components/formio/formio.component.ts`])
.pipe(replace("formio.component.scss", "formio.component.css"))
.pipe(gulp.dest(`${tmpFolder}/components/formio`));
});
gulp.task('builder-css', () => {
return gulp.src([`${tmpFolder}/components/formbuilder/formbuilder.component.ts`])
.pipe(replace("formbuilder.component.scss", "formbuilder.component.css"))
.pipe(gulp.dest(`${tmpFolder}/components/formbuilder`));
});
/**
* 3. Inline template (.html) and style (.css) files into the the component .ts files.
* We do this on the /.tmp folder to avoid editing the original /src files
*/
gulp.task('inline-resources', function () {
return inlineResources(tmpFolder);
});
/**
* 4. Run the Angular compiler, ngc, on the /.tmp folder. This will output all
* compiled modules to the /build folder.
*
* As of Angular 5, ngc accepts an array and no longer returns a promise.
*/
gulp.task('ngc', function (done) {
ngc(['--project', `${tmpFolder}/tsconfig.es5.json`], done);
});
gulp.task('ngc-angular', function (done) {
ngc(['--project', `${tmpFolder}/tsconfig.angular.json`], done);
});
gulp.task('ngc-auth', function (done) {
ngc(['--project', `${tmpFolder}/auth/tsconfig.es5.json`], done);
});
gulp.task('ngc-auth-angular', function (done) {
ngc(['--project', `${tmpFolder}/auth/tsconfig.angular.json`], done);
});
gulp.task('ngc-manager', function (done) {
ngc(['--project', `${tmpFolder}/manager/tsconfig.es5.json`], done);
});
gulp.task('ngc-manager-angular', function (done) {
ngc(['--project', `${tmpFolder}/manager/tsconfig.angular.json`], done);
});
gulp.task('ngc-grid', function (done) {
ngc(['--project', `${tmpFolder}/grid/tsconfig.es5.json`], done);
});
gulp.task('ngc-grid-angular', function (done) {
ngc(['--project', `${tmpFolder}/grid/tsconfig.angular.json`], done);
});
gulp.task('ngc-resource', function (done) {
ngc(['--project', `${tmpFolder}/resource/tsconfig.es5.json`], done);
});
gulp.task('ngc-resource-angular', function (done) {
ngc(['--project', `${tmpFolder}/resource/tsconfig.angular.json`], done);
});
/**
* 5. Run rollup inside the /build folder to generate our Flat ES module and place the
* generated file into the /dist folder
*/
const rollupFesm = function(name, path) {
path = path || '';
return gulp.src(`${buildFolder}${path}/**/*.js`)
// transform the files here.
.pipe(rollup({
// Bundle's entry point
// See "input" in https://rollupjs.org/#core-functionality
input: `${buildFolder}${path}/index.js`,
// Allow mixing of hypothetical and actual files. "Actual" files can be files
// accessed by Rollup or produced by plugins further down the chain.
// This prevents errors like: 'path/file' does not exist in the hypothetical file system
// when subdirectories are used in the `src` directory.
allowRealFiles: true,
// A list of IDs of modules that should remain external to the bundle
// See "external" in https://rollupjs.org/#core-functionality
external: [
'@angular/core',
'@angular/common'
],
output: {
// Format of generated bundle
// See "format" in https://rollupjs.org/#core-functionality
format: 'es'
}
}))
.pipe(gulp.dest(`${distFolder}${path}`));
};
gulp.task('rollup:fesm', () => rollupFesm('angular-formio'));
gulp.task('rollup-auth:fesm', () => rollupFesm('formio-auth', '/auth'));
gulp.task('rollup-manager:fesm', () => rollupFesm('formio-manager', '/manager'));
gulp.task('rollup-grid:fesm', () => rollupFesm('formio-grid', '/grid'));
gulp.task('rollup-resource:fesm', () => rollupFesm('formio-resource', '/resource'));
/**
* 6. Run rollup inside the /build folder to generate our UMD module and place the
* generated file into the /dist folder
*/
const rollupUmd = function(name, path) {
path = path || '';
return gulp.src(`${buildFolder}${path}/**/*.js`)
// transform the files here.
.pipe(rollup({
// Bundle's entry point
// See "input" in https://rollupjs.org/#core-functionality
input: `${buildFolder}${path}/index.js`,
// Allow mixing of hypothetical and actual files. "Actual" files can be files
// accessed by Rollup or produced by plugins further down the chain.
// This prevents errors like: 'path/file' does not exist in the hypothetical file system
// when subdirectories are used in the `src` directory.
allowRealFiles: true,
// A list of IDs of modules that should remain external to the bundle
// See "external" in https://rollupjs.org/#core-functionality
external: [
'rxjs',
'formiojs',
'@angular/core',
'@angular/common',
'@angular/router'
],
output: {
// The name to use for the module for UMD/IIFE bundles
// (required for bundles with exports)
// See "name" in https://rollupjs.org/#core-functionality
name: name,
// See "globals" in https://rollupjs.org/#core-functionality
globals: {
typescript: 'ts'
},
// Format of generated bundle
// See "format" in https://rollupjs.org/#core-functionality
format: 'umd',
// Export mode to use
// See "exports" in https://rollupjs.org/#danger-zone
exports: 'named'
}
}))
.pipe(rename(`${name}.umd.js`))
.pipe(gulp.dest(`${distFolder}${path}`));
};
gulp.task('rollup:umd', () => rollupUmd('angular-formio'));
gulp.task('rollup-auth:umd', () => rollupUmd('formio-auth', '/auth'));
gulp.task('rollup-manager:umd', () => rollupUmd('formio-manager', '/manager'));
gulp.task('rollup-grid:umd', () => rollupUmd('formio-grid', '/grid'));
gulp.task('rollup-resource:umd', () => rollupUmd('formio-resource', '/resource'));
/**
* 7. Copy all the files from /build to /dist, except .js files. We ignore all .js from /build
* because with don't need individual modules anymore, just the Flat ES module generated
* on step 5.
*/
gulp.task('copy:build', function copyBuild() {
return gulp.src([
`${buildFolder}/**/*`
])
.pipe(gulp.dest(distFolder));
});
/**
* 8. Copy package.json from /src to /dist
*/
gulp.task('copy:manifest', function copyManifest() {
return gulp.src([`${srcFolder}/package.json`])
.pipe(gulp.dest(distFolder));
});
gulp.task('copy-auth:manifest', function copyAuthManifest() {
return gulp.src([`${srcFolder}/auth/package.json`])
.pipe(gulp.dest(`${distFolder}/auth`));
});
gulp.task('copy-manager:manifest', function copyManagerManifest() {
return gulp.src([`${srcFolder}/manager/package.json`])
.pipe(gulp.dest(`${distFolder}/auth`));
});
gulp.task('copy-grid:manifest', function copyGridManifest() {
return gulp.src([`${srcFolder}/grid/package.json`])
.pipe(gulp.dest(`${distFolder}/grid`));
});
gulp.task('copy-resource:manifest', function copyResourceManifest() {
return gulp.src([`${srcFolder}/resource/package.json`])
.pipe(gulp.dest(`${distFolder}/resource`));
});
/**
* 9. Copy README.md from / to /dist
*/
gulp.task('copy:readme', function cleanReadme() {
return gulp.src([path.join(rootFolder, 'README.md')])
.pipe(gulp.dest(distFolder));
});
/**
* 10. Delete /.tmp folder
*/
gulp.task('clean:tmp', function cleanTemp() {
return fs.remove(tmpFolder);
});
/**
* 11. Delete /build folder
*/
gulp.task('clean:build', function cleanBuild() {
return fs.remove(buildFolder);
});
gulp.task('compile', gulp.series(
'clean:dist',
'copy:source',
gulp.parallel(
'styles-formio',
'styles-builder',
'formio-css',
'builder-css'
),
'inline-resources',
gulp.parallel(
'ngc',
'ngc-angular',
'ngc-auth',
'ngc-auth-angular',
'ngc-manager',
'ngc-manager-angular',
'ngc-grid',
'ngc-grid-angular',
'ngc-resource',
'ngc-resource-angular'
),
gulp.parallel(
'rollup:fesm',
'rollup-auth:fesm',
'rollup-grid:fesm',
'rollup-resource:fesm',
'rollup:umd',
'rollup-auth:umd',
'rollup-manager:umd',
'rollup-grid:umd',
'rollup-resource:umd'
),
'copy:build',
gulp.series(
'copy:manifest',
'copy-auth:manifest',
'copy-manager:manifest',
'copy-grid:manifest',
'copy-resource:manifest',
'copy:readme'
),
gulp.parallel(
'clean:build',
'clean:tmp'
)
));
/**
* Watch for any change in the /src folder and compile files
*/
gulp.task('watch', function watch() {
return gulp.watch(`${srcFolder}/**/*`, gulp.series('compile'));
});
gulp.task('clean', gulp.parallel('clean:dist', 'clean:tmp', 'clean:build'));
gulp.task('build', gulp.series('clean', 'compile'));
gulp.task('build:watch', gulp.series('build', 'watch'));
gulp.task('default', gulp.series('build:watch'));