gaf-ng-notifications
Version:
236 lines (204 loc) • 6.83 kB
JavaScript
/* eslint-disable */
var gulp = require('gulp'),
path = require('path'),
tsc = require('gulp-typescript'),
rollup = require('gulp-rollup'),
rollupNodeResolve = require('rollup-plugin-node-resolve'),
rollupCommonJS = require('rollup-plugin-commonjs'),
rename = require('gulp-rename'),
del = require('del'),
runSequence = require('run-sequence'),
sourcemaps = require('gulp-sourcemaps'),
inlineResources = require('./tools/gulp/utils/inline-resources');
const moduleName = 'gaf-ng-notifications';
const rootFolder = path.join(__dirname);
const srcFolder = path.join(rootFolder, 'src', 'lib');
const tmpFolder = path.join(rootFolder, '.tmp');
const buildFolder = path.join(rootFolder, 'build');
const distFolder = path.join(rootFolder, 'dist', moduleName);
/**
* 1. Delete /dist folder
*/
gulp.task('clean:dist', function () {
// Delete contents but not dist folder to avoid broken npm links
// when dist directory is removed while npm link references it.
return deleteFolders([distFolder + '/**', '!' + 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 Typescript compiler, tsc, on the /.tmp folder. This will output all
* compiled modules to the /build folder.
*/
gulp.task('tsc', function () {
var tsProject = tsc.createProject(`${tmpFolder}/tsconfig.es5.json`);
return tsProject.src()
.pipe(tsProject())
.js.pipe(gulp.dest(buildFolder));
});
/**
* 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:fesm', function () {
return gulp.src(`${buildFolder}/**/*.js`)
.pipe(rollup(Object.assign({}, baseRollupConfig, {
// Format of generated bundle
// See https://github.com/rollup/rollup/wiki/JavaScript-API#format
format: 'es'
})))
.pipe(rename(moduleName + '.js'))
.pipe(gulp.dest(distFolder));
});
/**
* 6. Run rollup inside the /build folder to generate our UMD module and place the
* generated file into the /dist folder
*/
gulp.task('rollup:umd', function () {
return gulp.src(`${buildFolder}/**/*.js`)
//.pipe(sourcemaps.init())
.pipe(rollup(Object.assign({}, baseRollupConfig, {
// Format of generated bundle
// See https://github.com/rollup/rollup/wiki/JavaScript-API#format
format: 'umd',
// Export mode to use
// See https://github.com/rollup/rollup/wiki/JavaScript-API#exports
exports: 'named',
// The name to use for the module for UMD/IIFE bundles
// (required for bundles with exports)
// See https://rollupjs.org/#name-n-name-
name: moduleName,
// See https://github.com/rollup/rollup/wiki/JavaScript-API#globals
globals: {
// angular: 'angular'
}
})))
//.pipe(sourcemaps.write())
.pipe(rename(moduleName + '.umd.js'))
.pipe(gulp.dest(distFolder));
});
/**
* 7. 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));
});
/**
* 8. Copy package.json from /src to /dist
*/
gulp.task('copy:manifest', function () {
return gulp.src([path.join(srcFolder, 'package.json')])
.pipe(gulp.dest(distFolder));
});
/**
* 9. Copy README.md from / to /dist
*/
gulp.task('copy:readme', function () {
return gulp.src([path.join(srcFolder, 'README.MD')])
.pipe(gulp.dest(distFolder));
});
/**
* 10. Delete /.tmp folder
*/
gulp.task('clean:tmp', function () {
return deleteFolders([tmpFolder]);
});
/**
* 11. Delete /build folder
*/
gulp.task('clean:build', function () {
return deleteFolders([buildFolder]);
});
gulp.task('compile', function () {
runSequence(
'clean:dist',
'copy:source',
'inline-resources',
'tsc',
'rollup:fesm',
'rollup:umd',
'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);
}
var baseRollupConfig = {
// Bundle's entry point
// See https://github.com/rollup/rollup/wiki/JavaScript-API#entry
entry: `${buildFolder}/index.js`,
// Allow mixing of hypothetical and actual files. "Actual" files can be files
// accessed by Rollup or produced by plugins further down the chain.
// This prevents errors like: 'path/file' does not exist in the hypothetical file system
// when subdirectories are used in the `src` directory.
allowRealFiles: true,
// A list of IDs of modules that should remain external to the bundle
// See https://github.com/rollup/rollup/wiki/JavaScript-API#external
external: [
// '@angular/core',
// '@angular/common',
'ng-metadata/core',
'angular',
'moment',
'moment-timezone',
'rxjs'
],
onwarn: function(warning) {
// Skip certain warnings
// should intercept ... but doesn't in some rollup versions
if ( warning.code === 'THIS_IS_UNDEFINED' ) { return; }
// console.warn everything else
console.warn( warning.message );
},
plugins: [
// Allow bundling cjs modules (unlike webpack, rollup doesn't understand cjs)
rollupCommonJS(),
// Allow node_modules resolution, so you can use 'external' to control
// which external modules to include in the bundle
// https://github.com/rollup/rollup-plugin-node-resolve#usage
rollupNodeResolve()
]
//sourceMap: true
}