-
Notifications
You must be signed in to change notification settings - Fork 90
/
gulpfile.ts
276 lines (240 loc) · 7.43 KB
/
gulpfile.ts
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
import { task, src, dest, watch } from "gulp";
import * as rename from "gulp-rename";
import * as changed from "gulp-changed";
import * as runSequence from "run-sequence";
import * as path from "path";
import * as del from "del";
import * as ts from "gulp-typescript";
import * as ngc from "gulp-ngc";
import * as merge from "merge2";
const rollup = require("gulp-rollup");
const inlineNg2Template = require("gulp-inline-ng2-template");
const ROLLUP_GLOBALS = {
"@angular/core": "_angular_core",
"@angular/common": "_angular_common",
"@angular/forms": "_angular_forms",
};
const ROLLUP_EXTERNAL = Object.keys(ROLLUP_GLOBALS);
const rootFolder = path.join(__dirname);
const srcFolder = path.join(rootFolder, "src");
const tmpFolder = path.join(rootFolder, ".tmp");
const tmpBundlesFolder = path.join(tmpFolder, ".bundles");
const buildFolder = path.join(rootFolder, "build");
const distFolder = path.join(rootFolder, "dist");
const {
name: libName,
main: bandleNameUMD,
module: bundleNameES5,
es2015: bundleNameES2015,
} = require("./package.json") as { [key: string]: string };
/**
* 0. Prepare dist to debug. This copies all files from /src to /dist
* and rename inline-editor.module.ts to index.ts, so typescript import works.
*/
task("debug", () => merge(
src(
[
`!${srcFolder}/inline-editor.module.ts`,
`!${srcFolder}/tsconfig.json`,
`${srcFolder}/**/*`,
])
.pipe(changed(distFolder))
.pipe(dest(distFolder)),
src([`${srcFolder}/inline-editor.module.ts`])
.pipe(changed(distFolder, { transformPath: () => `${distFolder}/index.ts` } as any))
.pipe(rename("index.ts"))
.pipe(dest(distFolder)),
));
/**
* 1. Clean /dist
*/
/**
* 1.1 Delete /dist folder
*/
task("clean:dist", () => deleteFolders([distFolder]));
/**
* 1.2 Delete /dist/* files
*/
task("clean:dist:files", () => deleteFolders([`${distFolder}/*`]));
/**
* 2. Clone the /src folder into /.tmp and Inline template (.html) and style (.css) files
* into the component .ts files. We do this on the /.tmp folder to avoid editing
* the original /src files. 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.
*/
task("copy:source", () => src([`${srcFolder}/**/*`])
.pipe(inlineNg2Template({
base: srcFolder,
target: "es5",
useRelativePaths: true,
removeLineBreaks: true,
}))
.pipe(dest(tmpFolder)),
);
/**
* 3. Run the Angular compiler, ngc, on the /.tmp folder. This will output all
* compiled modules to the /build folder.
*/
task("ngc", () => ngc(`${tmpFolder}/tsconfig.json`));
/**
* 4. Run rollup inside the /build folder to generate ours Flat ES modules and place the
* generated files into the /.tmp/bundles folder.
*
* rollup:es2015 -> Create bundle using ES2015 javascript.
* rollup:es5 -> Using bundle generated from rollup:es2015, transpile to ES5 using tsc.
* rollup:umd -> Using es5 transpiled code from rollup:es5, re-rollup to bundle as an UMD module.
*/
task("rollup", () => runSequence("rollup:es2015", "rollup:es5", "rollup:umd"));
/**
* We cached the bundles to use on watch mode.
*/
const rollupES2015Caches = {};
task("rollup:es2015", () => src(`${buildFolder}/**/*.js`)
.pipe(rollup({
entry: `${buildFolder}/inline-editor.module.js`,
external: ROLLUP_EXTERNAL,
format: "es",
separateCaches: rollupES2015Caches,
}))
.on("bundle", (bundle, name) => {
rollupES2015Caches[name] = bundle;
})
.pipe(rename(bundleNameES2015))
.pipe(dest(tmpBundlesFolder)),
);
/**
* We create a TS Project to use incremental compilation feature of gulp-typescript when
* we use the watch mode.
*/
const tsProject = ts.createProject({
target: "es5",
module: "es2015",
lib: [
"es2016",
],
allowJs: true,
typescript: require("typescript"),
});
task("rollup:es5", () => {
const tsResult = src(`${tmpBundlesFolder}/${bundleNameES2015}`)
.pipe(tsProject());
return merge([
tsResult.js.pipe(rename(bundleNameES5)).pipe(dest(tmpBundlesFolder)),
// There are not .d.ts files, it can be removed or used
tsResult.dts.pipe(dest(tmpBundlesFolder)),
]);
});
/**
* We cached the bundles to use on watch mode.
*/
const rollupUMDCaches = {};
task("rollup:umd", () => src(`${tmpBundlesFolder}/${bundleNameES5}`)
.pipe(rollup({
entry: `${tmpBundlesFolder}/${bundleNameES5}`,
globals: ROLLUP_GLOBALS,
external: ROLLUP_EXTERNAL,
format: "umd",
moduleName: libName,
separateCaches: rollupUMDCaches,
onwarn(message) {
if (message.code === "THIS_IS_UNDEFINED") {
return;
}
console.warn(message);
},
}))
.on("bundle", (bundle, name) => {
rollupUMDCaches[name] = bundle;
})
.pipe(rename(bandleNameUMD))
.pipe(dest(tmpBundlesFolder)),
);
/**
* 5. 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 modules generated
* on step 4.
* Copy themes from .tmp/themes to /dist
*/
task("copy:build", () => runSequence("copy:buildTS", "copy:buildCSS"));
task("copy:buildTS", () => src(
[
`${buildFolder}/**/*`,
`!${buildFolder}/**/*.js`,
])
.pipe(dest(distFolder)),
);
task("copy:buildCSS", () => src(
[
`${srcFolder}/themes/**/*`,
])
.pipe(dest(`${distFolder}/themes`)),
);
/**
* 6. Copy bundle files from /.tmp/bundles to /dist. There must be only three javascript files.
* The files generated on step 4.
*/
task("copy:bundles", () => src(`${tmpBundlesFolder}/*.js`).pipe(dest(distFolder)));
/**
* 7. Copy package.json and README.md from /src to /dist
*/
task("copy:manifest", () => src([
`${rootFolder}/package.json`,
`${rootFolder}/README.md`,
]).pipe(dest(distFolder)));
/**
* 8. Delete /.tmp folder
*/
task("clean:tmp", () => deleteFolders([tmpFolder]));
/**
* 9. Delete /build folder
*/
task("clean:build", () => deleteFolders([buildFolder]));
task("compile", () => runSequence(
"copy:source",
"ngc",
"rollup",
"clean:dist:files",
"copy:buildTS",
"copy:buildCSS",
"copy:bundles",
"copy:manifest",
"clean:build",
"clean:tmp",
(err?: any) => {
if (err) {
console.log("ERROR:", err.message);
deleteFolders([distFolder, tmpFolder, buildFolder]);
} else {
console.log("Compilation finished succesfully");
}
}),
);
task("compile:debug", () => runSequence(
"clean:dist:files",
"copy:manifest",
"debug",
(err?: any) => {
if (err) {
console.log("ERROR:", err.message);
deleteFolders([distFolder]);
} else {
console.log("Compilation finished succesfully");
}
}),
);
/**
* Watch for any change in the /src folder and compile files
*/
task("watch", ["compile"], () => watch(`${srcFolder}/**/*`, ["compile"]));
task("watch:debug", ["compile:debug"], () => watch(`${srcFolder}/**/*`, ["debug"]));
task("clean", ["clean:dist", "clean:tmp", "clean:build"]);
task("build", ["clean", "compile"]);
task("build:watch", ["build", "watch"]);
task("default", ["build:watch"]);
/**
* Deletes the specified folder
*/
function deleteFolders(folders: string | string[]) {
return del(folders);
}