|
| 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 | + del = require('del'), |
| 7 | + runSequence = require('run-sequence'), |
| 8 | + inlineResources = require('./tools/gulp/inline-resources'); |
| 9 | + |
| 10 | +const rootFolder = path.join(__dirname); |
| 11 | +const srcFolder = path.join(rootFolder, 'src'); |
| 12 | +const tmpFolder = path.join(rootFolder, '.tmp'); |
| 13 | +const buildFolder = path.join(rootFolder, 'build'); |
| 14 | +const distFolder = path.join(rootFolder, 'dist'); |
| 15 | + |
| 16 | +/** |
| 17 | + * 1. Delete /dist folder |
| 18 | + */ |
| 19 | +gulp.task('clean:dist', function () { |
| 20 | + return deleteFolders([distFolder]); |
| 21 | +}); |
| 22 | + |
| 23 | +/** |
| 24 | + * 2. Clone the /src folder into /.tmp. If an npm link inside /src has been made, |
| 25 | + * then it's likely that a node_modules folder exists. Ignore this folder |
| 26 | + * when copying to /.tmp. |
| 27 | + */ |
| 28 | +gulp.task('copy:source', function () { |
| 29 | + return gulp.src([`${srcFolder}/**/*`, `!${srcFolder}/node_modules`]) |
| 30 | + .pipe(gulp.dest(tmpFolder)); |
| 31 | +}); |
| 32 | + |
| 33 | +/** |
| 34 | + * 3. Inline template (.html) and style (.css) files into the the component .ts files. |
| 35 | + * We do this on the /.tmp folder to avoid editing the original /src files |
| 36 | + */ |
| 37 | +gulp.task('inline-resources', function () { |
| 38 | + return Promise.resolve() |
| 39 | + .then(() => inlineResources(tmpFolder)); |
| 40 | +}); |
| 41 | + |
| 42 | + |
| 43 | +/** |
| 44 | + * 4. Run the Angular compiler, ngc, on the /.tmp folder. This will output all |
| 45 | + * compiled modules to the /build folder. |
| 46 | + */ |
| 47 | +gulp.task('ngc', function () { |
| 48 | + return ngc({ |
| 49 | + project: `${tmpFolder}/tsconfig.es5.json` |
| 50 | + }) |
| 51 | + .then((exitCode) => { |
| 52 | + if (exitCode === 1) { |
| 53 | + // This error is caught in the 'compile' task by the runSequence method callback |
| 54 | + // so that when ngc fails to compile, the whole compile process stops running |
| 55 | + throw new Error('ngc compilation failed'); |
| 56 | + } |
| 57 | + }); |
| 58 | +}); |
| 59 | + |
| 60 | +/** |
| 61 | + * 5. Run rollup inside the /build folder to generate our Flat ES module and place the |
| 62 | + * generated file into the /dist folder |
| 63 | + */ |
| 64 | +gulp.task('rollup', function () { |
| 65 | + return gulp.src(`${buildFolder}/**/*.js`) |
| 66 | + // transform the files here. |
| 67 | + .pipe(rollup({ |
| 68 | + // any option supported by Rollup can be set here. |
| 69 | + entry: `${buildFolder}/index.js`, |
| 70 | + external: [ |
| 71 | + '@angular/core', |
| 72 | + '@angular/common' |
| 73 | + ], |
| 74 | + format: 'es' |
| 75 | + })) |
| 76 | + .pipe(gulp.dest(distFolder)); |
| 77 | +}); |
| 78 | + |
| 79 | +/** |
| 80 | + * 6. Copy all the files from /build to /dist, except .js files. We ignore all .js from /build |
| 81 | + * because with don't need individual modules anymore, just the Flat ES module generated |
| 82 | + * on step 5. |
| 83 | + */ |
| 84 | +gulp.task('copy:build', function () { |
| 85 | + return gulp.src([`${buildFolder}/**/*`, `!${buildFolder}/**/*.js`]) |
| 86 | + .pipe(gulp.dest(distFolder)); |
| 87 | +}); |
| 88 | + |
| 89 | +/** |
| 90 | + * 7. Copy package.json from /src to /dist |
| 91 | + */ |
| 92 | +gulp.task('copy:manifest', function () { |
| 93 | + return gulp.src([`${srcFolder}/package.json`]) |
| 94 | + .pipe(gulp.dest(distFolder)); |
| 95 | +}); |
| 96 | + |
| 97 | +/** |
| 98 | + * 8. Delete /.tmp folder |
| 99 | + */ |
| 100 | +gulp.task('clean:tmp', function () { |
| 101 | + return deleteFolders([tmpFolder]); |
| 102 | +}); |
| 103 | + |
| 104 | +/** |
| 105 | + * 9. Delete /build folder |
| 106 | + */ |
| 107 | +gulp.task('clean:build', function () { |
| 108 | + return deleteFolders([buildFolder]); |
| 109 | +}); |
| 110 | + |
| 111 | +gulp.task('compile', function () { |
| 112 | + runSequence( |
| 113 | + 'clean:dist', |
| 114 | + 'copy:source', |
| 115 | + 'inline-resources', |
| 116 | + 'ngc', |
| 117 | + 'rollup', |
| 118 | + 'copy:build', |
| 119 | + 'copy:manifest', |
| 120 | + 'clean:build', |
| 121 | + 'clean:tmp', |
| 122 | + function (err) { |
| 123 | + if (err) { |
| 124 | + console.log('ERROR:', err.message); |
| 125 | + deleteFolders([distFolder, tmpFolder, buildFolder]); |
| 126 | + } else { |
| 127 | + console.log('Compilation finished succesfully'); |
| 128 | + } |
| 129 | + }); |
| 130 | +}); |
| 131 | + |
| 132 | +/** |
| 133 | + * Watch for any change in the /src folder and compile files |
| 134 | + */ |
| 135 | +gulp.task('watch', function () { |
| 136 | + gulp.watch(`${srcFolder}/**/*`, ['compile']); |
| 137 | +}); |
| 138 | + |
| 139 | +gulp.task('clean', ['clean:dist', 'clean:tmp', 'clean:build']); |
| 140 | + |
| 141 | +gulp.task('build', ['clean', 'compile']); |
| 142 | +gulp.task('build:watch', ['build', 'watch']); |
| 143 | +gulp.task('default', ['build:watch']); |
| 144 | + |
| 145 | +/** |
| 146 | + * Deletes the specified folder |
| 147 | + */ |
| 148 | +function deleteFolders(folders) { |
| 149 | + return del(folders); |
| 150 | +} |
0 commit comments