|
1 |
| -const gulp = require('gulp'); |
2 |
| -const inlineNg2Template = require('gulp-inline-ng2-template'); |
3 |
| - |
4 |
| -gulp.task('inline-templates', function () { |
5 |
| - return gulp.src('dist/**/*.ts') |
6 |
| - .pipe(inlineNg2Template({ |
7 |
| - useRelativePaths: true, |
8 |
| - indent: 0, |
9 |
| - removeLineBreaks: true, |
10 |
| - })) |
11 |
| - .pipe(gulp.dest('dist')); |
| 1 | +/* eslint-disable */ |
| 2 | +var 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 | + |
| 11 | +const rootFolder = path.join(__dirname); |
| 12 | +const srcFolder = path.join(rootFolder, 'src'); |
| 13 | +const tmpFolder = path.join(rootFolder, '.tmp'); |
| 14 | +const buildFolder = path.join(rootFolder, 'build'); |
| 15 | +const distFolder = path.join(rootFolder, 'dist'); |
| 16 | + |
| 17 | +/** |
| 18 | + * 1. Delete /dist folder |
| 19 | + */ |
| 20 | +gulp.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 | + */ |
| 32 | +gulp.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 | + */ |
| 41 | +gulp.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 | +gulp.task('ngc', function () { |
| 52 | + return ngc({ |
| 53 | + project: `${tmpFolder}/tsconfig.es5.json` |
| 54 | + }) |
| 55 | + .then((exitCode) => { |
| 56 | + if (exitCode === 1) { |
| 57 | + // This error is caught in the 'compile' task by the runSequence method callback |
| 58 | + // so that when ngc fails to compile, the whole compile process stops running |
| 59 | + throw new Error('ngc compilation failed'); |
| 60 | + } |
| 61 | + }); |
| 62 | +}); |
| 63 | + |
| 64 | +/** |
| 65 | + * 5. Run rollup inside the /build folder to generate our Flat ES module and place the |
| 66 | + * generated file into the /dist folder |
| 67 | + */ |
| 68 | +gulp.task('rollup:fesm', function () { |
| 69 | + return gulp.src(`${buildFolder}/**/*.js`) |
| 70 | + // transform the files here. |
| 71 | + .pipe(rollup({ |
| 72 | + |
| 73 | + // Bundle's entry point |
| 74 | + // See https://github.com/rollup/rollup/wiki/JavaScript-API#entry |
| 75 | + entry: `${buildFolder}/index.js`, |
| 76 | + |
| 77 | + // Allow mixing of hypothetical and actual files. "Actual" files can be files |
| 78 | + // accessed by Rollup or produced by plugins further down the chain. |
| 79 | + // This prevents errors like: 'path/file' does not exist in the hypothetical file system |
| 80 | + // when subdirectories are used in the `src` directory. |
| 81 | + allowRealFiles: true, |
| 82 | + |
| 83 | + // A list of IDs of modules that should remain external to the bundle |
| 84 | + // See https://github.com/rollup/rollup/wiki/JavaScript-API#external |
| 85 | + external: [ |
| 86 | + '@angular/core', |
| 87 | + '@angular/common', |
| 88 | + 'ionic-angular', |
| 89 | + 'rxjs/Subject', |
| 90 | + ], |
| 91 | + |
| 92 | + // Format of generated bundle |
| 93 | + // See https://github.com/rollup/rollup/wiki/JavaScript-API#format |
| 94 | + format: 'es' |
| 95 | + })) |
| 96 | + .pipe(gulp.dest(distFolder)); |
12 | 97 | });
|
| 98 | + |
| 99 | +/** |
| 100 | + * 6. Run rollup inside the /build folder to generate our UMD module and place the |
| 101 | + * generated file into the /dist folder |
| 102 | + */ |
| 103 | +gulp.task('rollup:umd', function () { |
| 104 | + return gulp.src(`${buildFolder}/**/*.js`) |
| 105 | + // transform the files here. |
| 106 | + .pipe(rollup({ |
| 107 | + |
| 108 | + // Bundle's entry point |
| 109 | + // See https://github.com/rollup/rollup/wiki/JavaScript-API#entry |
| 110 | + entry: `${buildFolder}/index.js`, |
| 111 | + |
| 112 | + // Allow mixing of hypothetical and actual files. "Actual" files can be files |
| 113 | + // accessed by Rollup or produced by plugins further down the chain. |
| 114 | + // This prevents errors like: 'path/file' does not exist in the hypothetical file system |
| 115 | + // when subdirectories are used in the `src` directory. |
| 116 | + allowRealFiles: true, |
| 117 | + |
| 118 | + // A list of IDs of modules that should remain external to the bundle |
| 119 | + // See https://github.com/rollup/rollup/wiki/JavaScript-API#external |
| 120 | + external: [ |
| 121 | + '@angular/core', |
| 122 | + '@angular/common', |
| 123 | + 'ionic-angular', |
| 124 | + 'rxjs/Subject', |
| 125 | + ], |
| 126 | + |
| 127 | + // Format of generated bundle |
| 128 | + // See https://github.com/rollup/rollup/wiki/JavaScript-API#format |
| 129 | + format: 'umd', |
| 130 | + |
| 131 | + // Export mode to use |
| 132 | + // See https://github.com/rollup/rollup/wiki/JavaScript-API#exports |
| 133 | + exports: 'named', |
| 134 | + |
| 135 | + // The name to use for the module for UMD/IIFE bundles |
| 136 | + // (required for bundles with exports) |
| 137 | + // See https://github.com/rollup/rollup/wiki/JavaScript-API#modulename |
| 138 | + moduleName: 'ionic-gallery-modal', |
| 139 | + |
| 140 | + // See https://github.com/rollup/rollup/wiki/JavaScript-API#globals |
| 141 | + globals: { |
| 142 | + typescript: 'ts' |
| 143 | + } |
| 144 | + |
| 145 | + })) |
| 146 | + .pipe(rename('ionic-gallery-modal.umd.js')) |
| 147 | + .pipe(gulp.dest(distFolder)); |
| 148 | +}); |
| 149 | + |
| 150 | +/** |
| 151 | + * 7. Copy all the files from /build to /dist, except .js files. We ignore all .js from /build |
| 152 | + * because with don't need individual modules anymore, just the Flat ES module generated |
| 153 | + * on step 5. |
| 154 | + */ |
| 155 | +gulp.task('copy:build', function () { |
| 156 | + return gulp.src([`${buildFolder}/**/*`, `!${buildFolder}/**/*.js`]) |
| 157 | + .pipe(gulp.dest(distFolder)); |
| 158 | +}); |
| 159 | + |
| 160 | +/** |
| 161 | + * 8. Copy package.json from /src to /dist |
| 162 | + */ |
| 163 | +gulp.task('copy:manifest', function () { |
| 164 | + return gulp.src([`${srcFolder}/package.json`]) |
| 165 | + .pipe(gulp.dest(distFolder)); |
| 166 | +}); |
| 167 | + |
| 168 | +/** |
| 169 | + * 9. Copy README.md from / to /dist |
| 170 | + */ |
| 171 | +gulp.task('copy:readme', function () { |
| 172 | + return gulp.src([path.join(rootFolder, 'README.MD')]) |
| 173 | + .pipe(gulp.dest(distFolder)); |
| 174 | +}); |
| 175 | + |
| 176 | +/** |
| 177 | + * 10. Delete /.tmp folder |
| 178 | + */ |
| 179 | +gulp.task('clean:tmp', function () { |
| 180 | + return deleteFolders([tmpFolder]); |
| 181 | +}); |
| 182 | + |
| 183 | +/** |
| 184 | + * 11. Delete /build folder |
| 185 | + */ |
| 186 | +gulp.task('clean:build', function () { |
| 187 | + return deleteFolders([buildFolder]); |
| 188 | +}); |
| 189 | + |
| 190 | +gulp.task('compile', function () { |
| 191 | + runSequence( |
| 192 | + 'clean:dist', |
| 193 | + 'copy:source', |
| 194 | + 'inline-resources', |
| 195 | + 'ngc', |
| 196 | + 'rollup:fesm', |
| 197 | + 'rollup:umd', |
| 198 | + 'copy:build', |
| 199 | + 'copy:manifest', |
| 200 | + 'copy:readme', |
| 201 | + 'clean:build', |
| 202 | + 'clean:tmp', |
| 203 | + function (err) { |
| 204 | + if (err) { |
| 205 | + console.log('ERROR:', err.message); |
| 206 | + deleteFolders([distFolder, tmpFolder, buildFolder]); |
| 207 | + } else { |
| 208 | + console.log('Compilation finished succesfully'); |
| 209 | + } |
| 210 | + }); |
| 211 | +}); |
| 212 | + |
| 213 | +/** |
| 214 | + * Watch for any change in the /src folder and compile files |
| 215 | + */ |
| 216 | +gulp.task('watch', function () { |
| 217 | + gulp.watch(`${srcFolder}/**/*`, ['compile']); |
| 218 | +}); |
| 219 | + |
| 220 | +gulp.task('clean', ['clean:dist', 'clean:tmp', 'clean:build']); |
| 221 | + |
| 222 | +gulp.task('build', ['clean', 'compile']); |
| 223 | +gulp.task('build:watch', ['build', 'watch']); |
| 224 | +gulp.task('default', ['build:watch']); |
| 225 | + |
| 226 | +/** |
| 227 | + * Deletes the specified folder |
| 228 | + */ |
| 229 | +function deleteFolders(folders) { |
| 230 | + return del(folders); |
| 231 | +} |
0 commit comments