123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- 'use strict';
- const gulp = require('gulp'),
- ts = require('gulp-typescript'),
- tsProject = ts.createProject('tsconfig.json'),
- replace = require('gulp-batch-replace'),
- htmlMin = require('gulp-htmlmin'),
- sass = require('gulp-sass'),
- clean = require('gulp-clean'),
- browserSync = require('browser-sync').create(),
- autoPrefixer = require('gulp-autoprefixer'),
- cleanCSS = require('gulp-clean-css'),
- imageMin = require('gulp-imagemin'),
- ifElse = require('gulp-if-else'),
- runSequence = require('run-sequence');
- var config = {
- product: false,
- src: "src",
- dist: "dist",
- serverRoot: "./dist"
- }
- gulp.task('typescript', function () {
- tsProject.src()
- .pipe(tsProject())
- .js
- .pipe(ifElse(config.product, replace.bind(this, [
- ['\n', ' '],
- [/\s*=\s*/g, '='],
- [/\s*,\s*/g, ','],
- [/\s*\{\s*/g, '{'],
- [/\s*}\s*/g, '}'],
- [/\s+/g, ' ']
- ])))
- .pipe(gulp.dest(config.dist));
- });
- gulp.task('html', function () {
- return gulp.src(config.src + '/**/*.html')
- .pipe(ifElse(config.product, htmlMin.bind(this, {
- collapseWhitespace: true
- })))
- .pipe(gulp.dest(config.dist));
- });
- gulp.task('sass', function () {
- return gulp.src(config.src + '/**/*.scss')
- .pipe(sass().on('error', sass.logError))
- .pipe(autoPrefixer({
- browsers: ["Firefox >= 38",
- "Safari >= 7",
- "Chrome >= 26",
- "IE >= 10",
- "Opera >= 12"
- ]
- }))
- .pipe(ifElse(config.product, cleanCSS))
- .pipe(gulp.dest(config.dist));
- });
- gulp.task('copy', function () {
- return gulp.src([
- config.src + '/**/*.txt',
- config.src + '/**/*.xml',
- config.src + '/**/*.pdf'
- ])
- .pipe(gulp.dest(config.dist));
- });
- gulp.task('font', function () {
- return gulp.src([
- config.src + '/**/*.{eot,svg,ttf,woff,woff2}'
- ])
- .pipe(gulp.dest(config.dist));
- });
- gulp.task('css', function () {
- return gulp.src([
- config.src + '/**/*.css'
- ])
- .pipe(autoPrefixer({
- browsers: ["Firefox >= 38",
- "Safari >= 7",
- "Chrome >= 26",
- "IE >= 10",
- "Opera >= 12"
- ]
- }))
- .pipe(ifElse(config.product, cleanCSS))
- .pipe(gulp.dest(config.dist));
- });
- gulp.task('js', function () {
- return gulp.src([
- config.src + '/**/*.js'
- ])
- .pipe(ifElse(config.product, replace.bind(this, [
- ['\n', ' '],
- [/\s*=\s*/g, '='],
- [/\s*,\s*/g, ','],
- [/\s*\{\s*/g, '{'],
- [/\s*}\s*/g, '}'],
- [/\s+/g, ' ']
- ])))
- .pipe(gulp.dest(config.dist));
- });
- gulp.task('image', function () {
- gulp.src(config.src + '/**/*.{png,jpeg,jpg,gif,ico,cur}')
- .pipe(ifElse(config.product, imageMin))
- .pipe(gulp.dest(config.dist));
- });
- gulp.task('json', function () {
- return gulp.src(config.src + '/**/*.json')
- .pipe(ifElse(config.product, replace.bind(this, [
- ['\n', ' '],
- [/\s*,\s*/g, ','],
- [/\s*\{\s*/g, '{'],
- [/\s*}\s*/g, '}'],
- [/\s+/g, ' ']
- ])))
- .pipe(gulp.dest(config.dist))
- });
- gulp.task('clean', function () {
- return gulp.src(config.dist, {
- read: false
- })
- .pipe(clean());
- });
- gulp.task('serve', function () {
- browserSync.init({
- server: {
- baseDir: config.serverRoot
- }
- });
- });
- gulp.task('reload', function () {
- browserSync.reload();
- });
- gulp.task('serve:watch', ['serve'], function () {
- gulp.watch(config.src + '/**/*.ts', function () {
- runSequence(['typescript', 'reload']);
- });
- gulp.watch(config.src + '/**/*.json', function () {
- runSequence(['json', 'reload']);
- });
- gulp.watch(config.src + '/**/*.html', function () {
- runSequence(['html', 'reload']);
- });
- gulp.watch(config.src + '/**/*.scss', function () {
- runSequence(['sass', 'reload']);
- });
- gulp.watch(config.src + '/**/*.js', function () {
- runSequence(['js', 'reload']);
- });
- gulp.watch(config.src + '/**/*.css', function () {
- runSequence(['css', 'reload']);
- });
- gulp.watch(config.src + '/**/*.{png,jpeg,jpg,gif,ico}', function () {
- runSequence(['image', 'reload']);
- });
- gulp.watch([
- config.src + '/**/*.txt',
- config.src + '/**/*.xml'
- ], function () {
- runSequence(['copy', 'reload']);
- });
- });
- gulp.task('default', ['clean'], function () {
- gulp.start(['typescript', 'json', 'sass', 'image', 'copy', 'font', 'css', 'js', 'html']);
- });
- gulp.task('product', ['clean'], function () {
- config.product = true;
- gulp.start(['typescript', 'json', 'sass', 'image', 'copy', 'font', 'css', 'js', 'html']);
- });
|