-
Notifications
You must be signed in to change notification settings - Fork 11
/
gulpfile.js
147 lines (126 loc) · 5.05 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* gulpfile
*/
(function () {
'use strict';
var gulp = require('gulp'),
clean = require('gulp-clean'),
sass = require('gulp-sass'),
autoprefixer = require('autoprefixer'),
postcss = require('gulp-postcss'),
cleancss = require('gulp-clean-css'),
pixrem = require('pixrem'),
runSequence = require('run-sequence'),
path = require('path'),
fs = require('fs'),
flatmap = require('gulp-flatmap'),
htmlmin = require('gulp-htmlmin'),
replace = require('gulp-replace'),
ts = require('gulp-typescript'),
merge = require('merge2'),
sourcemaps = require('gulp-sourcemaps'),
Config = require('./gulpfile.config');
var config = new Config();
var tsDistProject = ts.createProject('tsconfig.dist.json');
const exec = require('child_process').exec;
gulp.task('tsc.compile.dist', function () {
var tsResult = tsDistProject.src().pipe(sourcemaps.init()).pipe(tsDistProject());
return merge([
tsResult.js.pipe(gulp.dest('dist')),
tsResult.js.pipe(sourcemaps.write('./', {includeContent: false})).pipe(gulp.dest('dist')),
tsResult.dts.pipe(gulp.dest('dist'))
]);
});
gulp.task('inline.template.and.styles.to.component', function () {
return gulp.src('./tmp/**/*.component.ts')
.pipe(flatmap(function (stream, file) {
var tsFile = file.path;
var htmlFile = tsFile.slice(0, -2) + 'html';
var htmlFileName = path.parse(htmlFile).base;
var cssFile = tsFile.slice(0, -2) + 'css';
var scssFile = tsFile.slice(0, -2) + 'scss';
var scssFileName = path.parse(scssFile).base;
var styles = fs.readFileSync(cssFile, 'utf-8');
var htmlTpl = fs.readFileSync(htmlFile, 'utf-8');
return gulp.src([file.path])
.pipe(replace('styleUrls: [' + '\'' + './' + scssFileName + '\'' + '],', 'styles: [' + '`' + styles + '`' + '],'))
.pipe(replace('templateUrl: ' + '\'' + './' + htmlFileName + '\'' + ',', 'template: `' + htmlTpl + '`' + ','))
.pipe(gulp.dest(function (file) {
return file.base;
}));
}));
});
gulp.task('minify.css', function () {
var processors = [
pixrem(),
autoprefixer({
browsers: ['last 8 version', '> 1%', 'ie 9', 'ie 8', 'ie 7', 'ios 6', 'Firefox <= 20'],
cascade: false
})
];
return gulp.src(config.allSass)
.pipe(sass())
.pipe(postcss(processors))
.pipe(cleancss({compatibility: 'ie8'}))
.pipe(gulp.dest(config.tmpOutputPath));
});
gulp.task('minify.html', function () {
return gulp.src(config.allHtml)
.pipe(htmlmin({collapseWhitespace: true, caseSensitive: true}))
.pipe(gulp.dest(config.tmpOutputPath));
});
gulp.task('clean', function () {
return gulp.src(['./dist', './npmdist', config.tmpOutputPath], {read: false}).pipe(clean());
});
gulp.task('backup.ts.tmp', function () {
return gulp.src(config.allTs).pipe(gulp.dest(config.tmpOutputPath));
});
gulp.task('delete.tmp', function () {
return gulp.src([config.tmpOutputPath], {read: false}).pipe(clean());
});
gulp.task('copy.src.to.npmdist.dir', function () {
return gulp.src([config.alltmpTs, '!' + config.alltmpSpecTs]).pipe(gulp.dest('./npmdist/src'));
});
gulp.task('copy.dist.to.npmdist.dir', function () {
return gulp.src(config.allDistFiles).pipe(gulp.dest('./npmdist/dist'));
});
gulp.task('copy.root.files.to.npmdist.dir', function () {
return gulp.src(
[
'./index.ts',
'./index.js',
'./LICENSE',
'./package.json',
'./README.md'
]).pipe(gulp.dest('./npmdist'));
});
gulp.task('all', function (cb) {
runSequence(
'clean',
'backup.ts.tmp',
'minify.css',
'minify.html',
'inline.template.and.styles.to.component',
'ngc',
'copy.src.to.npmdist.dir',
'copy.dist.to.npmdist.dir',
'copy.root.files.to.npmdist.dir',
'delete.tmp',
cb
)
});
gulp.task('ngc', function (cb) {
var cmd = 'node_modules/.bin/ngc -p tsconfig-aot.json';
return run_proc(cmd, cb);
});
const run_proc = function (cmd, callBack, options) {
var proc = exec(cmd, function (err, stdout, stderr) {
if (options === undefined) options = {};
if (options.outFilter !== undefined) stdout = options.outFilter(stdout);
if (options.errFilter !== undefined) stderr = options.errFilter(stderr);
process.stdout.write(stdout);
process.stdout.write(stderr);
callBack(err);
});
};
})();