UNPKG

6.48 kBJavaScriptView Raw
1/* eslint-disable */
2var gulp = require('gulp'),
3 path = require('path'),
4 ngc = require('@angular/compiler-cli/src/main').main,
5 rollup = require('gulp-rollup'),
6 rename = require('gulp-rename'),
7 del = require('del'),
8 runSequence = require('run-sequence'),
9 inlineResources = require('./tools/gulp/inline-resources');
10
11const rootFolder = path.join(__dirname);
12const srcFolder = path.join(rootFolder, 'src');
13const tmpFolder = path.join(rootFolder, '.tmp');
14const buildFolder = path.join(rootFolder, 'build');
15const distFolder = path.join(rootFolder, 'dist');
16
17/**
18 * 1. Delete /dist folder
19 */
20gulp.task('clean:dist', function () {
21
22 // Delete contents but not dist folder to avoid broken npm links
23 // when dist directory is removed while npm link references it.
24 return deleteFolders([distFolder + '/**', '!' + distFolder]);
25});
26
27/**
28 * 2. Clone the /src folder into /.tmp. If an npm link inside /src has been made,
29 * then it's likely that a node_modules folder exists. Ignore this folder
30 * when copying to /.tmp.
31 */
32gulp.task('copy:source', function () {
33 return gulp.src([`${srcFolder}/**/*`, `!${srcFolder}/node_modules`])
34 .pipe(gulp.dest(tmpFolder));
35});
36
37/**
38 * 3. Inline template (.html) and style (.css) files into the the component .ts files.
39 * We do this on the /.tmp folder to avoid editing the original /src files
40 */
41gulp.task('inline-resources', function () {
42 return Promise.resolve()
43 .then(() => inlineResources(tmpFolder));
44});
45
46
47/**
48 * 4. Run the Angular compiler, ngc, on the /.tmp folder. This will output all
49 * compiled modules to the /build folder.
50 *
51 * As of Angular 5, ngc accepts an array and no longer returns a promise.
52 */
53gulp.task('ngc', function () {
54 ngc([ '--project', `${tmpFolder}/tsconfig.es5.json` ]);
55 return Promise.resolve()
56});
57
58/**
59 * 5. Run rollup inside the /build folder to generate our Flat ES module and place the
60 * generated file into the /dist folder
61 */
62gulp.task('rollup:fesm', function () {
63 return gulp.src(`${buildFolder}/**/*.js`)
64 // transform the files here.
65 .pipe(rollup({
66
67 // Bundle's entry point
68 // See "input" in https://rollupjs.org/#core-functionality
69 input: `${buildFolder}/index.js`,
70
71 // Allow mixing of hypothetical and actual files. "Actual" files can be files
72 // accessed by Rollup or produced by plugins further down the chain.
73 // This prevents errors like: 'path/file' does not exist in the hypothetical file system
74 // when subdirectories are used in the `src` directory.
75 allowRealFiles: true,
76
77 // A list of IDs of modules that should remain external to the bundle
78 // See "external" in https://rollupjs.org/#core-functionality
79 external: [
80 '@angular/core',
81 '@angular/common'
82 ],
83
84 // Format of generated bundle
85 // See "format" in https://rollupjs.org/#core-functionality
86 format: 'es'
87 }))
88 .pipe(gulp.dest(distFolder));
89});
90
91/**
92 * 6. Run rollup inside the /build folder to generate our UMD module and place the
93 * generated file into the /dist folder
94 */
95gulp.task('rollup:umd', function () {
96 return gulp.src(`${buildFolder}/**/*.js`)
97 // transform the files here.
98 .pipe(rollup({
99
100 // Bundle's entry point
101 // See "input" in https://rollupjs.org/#core-functionality
102 input: `${buildFolder}/index.js`,
103
104 // Allow mixing of hypothetical and actual files. "Actual" files can be files
105 // accessed by Rollup or produced by plugins further down the chain.
106 // This prevents errors like: 'path/file' does not exist in the hypothetical file system
107 // when subdirectories are used in the `src` directory.
108 allowRealFiles: true,
109
110 // A list of IDs of modules that should remain external to the bundle
111 // See "external" in https://rollupjs.org/#core-functionality
112 external: [
113 '@angular/core',
114 '@angular/common'
115 ],
116
117 // Format of generated bundle
118 // See "format" in https://rollupjs.org/#core-functionality
119 format: 'umd',
120
121 // Export mode to use
122 // See "exports" in https://rollupjs.org/#danger-zone
123 exports: 'named',
124
125 // The name to use for the module for UMD/IIFE bundles
126 // (required for bundles with exports)
127 // See "name" in https://rollupjs.org/#core-functionality
128 name: '@mello-labs/api-tools',
129
130 // See "globals" in https://rollupjs.org/#core-functionality
131 globals: {
132 typescript: 'ts'
133 }
134
135 }))
136 .pipe(rename('mello-labs-api-tools.umd.js'))
137 .pipe(gulp.dest(distFolder));
138});
139
140/**
141 * 7. Copy all the files from /build to /dist, except .js files. We ignore all .js from /build
142 * because with don't need individual modules anymore, just the Flat ES module generated
143 * on step 5.
144 */
145gulp.task('copy:build', function () {
146 return gulp.src([`${buildFolder}/**/*`, `!${buildFolder}/**/*.js`])
147 .pipe(gulp.dest(distFolder));
148});
149
150/**
151 * 8. Copy package.json from /src to /dist
152 */
153gulp.task('copy:manifest', function () {
154 return gulp.src([`${srcFolder}/package.json`])
155 .pipe(gulp.dest(distFolder));
156});
157
158/**
159 * 9. Copy README.md from / to /dist
160 */
161gulp.task('copy:readme', function () {
162 return gulp.src([path.join(rootFolder, 'README.MD')])
163 .pipe(gulp.dest(distFolder));
164});
165
166/**
167 * 10. Delete /.tmp folder
168 */
169gulp.task('clean:tmp', function () {
170 return deleteFolders([tmpFolder]);
171});
172
173/**
174 * 11. Delete /build folder
175 */
176gulp.task('clean:build', function () {
177 return deleteFolders([buildFolder]);
178});
179
180gulp.task('compile', function () {
181 runSequence(
182 'clean:dist',
183 'copy:source',
184 'inline-resources',
185 'ngc',
186 'rollup:fesm',
187 'rollup:umd',
188 'copy:build',
189 'copy:manifest',
190 'copy:readme',
191 'clean:build',
192 'clean:tmp',
193 function (err) {
194 if (err) {
195 console.log('ERROR:', err.message);
196 deleteFolders([distFolder, tmpFolder, buildFolder]);
197 } else {
198 console.log('Compilation finished succesfully');
199 }
200 });
201});
202
203/**
204 * Watch for any change in the /src folder and compile files
205 */
206gulp.task('watch', function () {
207 gulp.watch(`${srcFolder}/**/*`, ['compile']);
208});
209
210gulp.task('clean', ['clean:dist', 'clean:tmp', 'clean:build']);
211
212gulp.task('build', ['clean', 'compile']);
213gulp.task('build:watch', ['build', 'watch']);
214gulp.task('default', ['build:watch']);
215
216/**
217 * Deletes the specified folder
218 */
219function deleteFolders(folders) {
220 return del(folders);
221}