-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
164 lines (147 loc) Β· 4.53 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
/* eslint-disable */
var gulp = require('gulp'),
path = require('path'),
ngc = require('@angular/compiler-cli/src/main').main,
rollup = require('gulp-rollup'),
del = require('del'),
runSequence = require('run-sequence'),
inlineResources = require('./tools/gulp/inline-resources'),
jeditor = require("gulp-json-editor");
const rootPackageVersion = require('./package.json').version;
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');
/**
* 1. Delete /dist folder
*/
gulp.task('clean:dist', function () {
return deleteFolders([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));
});
/**
* 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 Promise.resolve()
.then(() => inlineResources(tmpFolder));
});
/**
* 4. Run the Angular compiler, ngc, on the /.tmp folder. This will output all
* compiled modules to the /build folder.
*/
gulp.task('ngc', function () {
return ngc({
project: `${tmpFolder}/tsconfig.es5.json`
})
.then((exitCode) => {
if (exitCode === 1) {
// This error is caught in the 'compile' task by the runSequence method callback
// so that when ngc fails to compile, the whole compile process stops running
throw new Error('ngc compilation failed');
}
});
});
/**
* 5. Run rollup inside the /build folder to generate our Flat ES module and place the
* generated file into the /dist folder
*/
gulp.task('rollup', function () {
return gulp.src(`${buildFolder}/**/*.js`)
// transform the files here.
.pipe(rollup({
// any option supported by Rollup can be set here.
entry: `${buildFolder}/index.js`,
external: [
'@angular/core',
'@angular/common'
],
format: 'es'
}))
.pipe(gulp.dest(distFolder));
});
/**
* 6. 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 () {
return gulp.src([`${buildFolder}/**/*`, `!${buildFolder}/**/*.js`])
.pipe(gulp.dest(distFolder));
});
/**
* 7. Copy package.json from /src to /dist and add rootPackage version
*/
gulp.task('copy:manifest', function () {
return gulp.src([`${srcFolder}/package.json`])
.pipe(jeditor({
'version': rootPackageVersion
}))
.pipe(gulp.dest(distFolder));
});
/**
* 8. Copy README.md from / to /dist
*/
gulp.task('copy:readme', function () {
return gulp.src([`README.MD`])
.pipe(gulp.dest(distFolder));
});
/**
* 9. Delete /.tmp folder
*/
gulp.task('clean:tmp', function () {
return deleteFolders([tmpFolder]);
});
/**
* 10. Delete /build folder
*/
gulp.task('clean:build', function () {
return deleteFolders([buildFolder]);
});
gulp.task('compile', function () {
runSequence(
'clean:dist',
'copy:source',
'inline-resources',
'ngc',
'rollup',
'copy:build',
'copy:manifest',
'copy:readme',
'clean:build',
'clean:tmp',
function (err) {
if (err) {
console.log('ERROR:', err.message);
deleteFolders([distFolder, tmpFolder, buildFolder]);
} else {
console.log('Compilation finished succesfully');
}
});
});
/**
* Watch for any change in the /src folder and compile files
*/
gulp.task('watch', function () {
gulp.watch(`${srcFolder}/**/*`, ['compile']);
});
gulp.task('clean', ['clean:dist', 'clean:tmp', 'clean:build']);
gulp.task('build', ['clean', 'compile']);
gulp.task('build:watch', ['build', 'watch']);
gulp.task('default', ['build:watch']);
/**
* Deletes the specified folder
*/
function deleteFolders(folders) {
return del(folders);
}