forked from fabric8-launcher/ngx-launcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
213 lines (193 loc) · 5.75 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
var gulp = require('gulp'),
less = require('gulp-less'),
LessAutoprefix = require('less-plugin-autoprefix'),
autoprefix = new LessAutoprefix({ browsers: ['last 2 versions'] }),
autoprefixer = require('gulp-autoprefixer'),
lesshint = require('gulp-lesshint'),
cssmin = require('gulp-cssmin'),
stylelint = require('gulp-stylelint'),
postcss = require('postcss'),
sourcemaps = require('gulp-sourcemaps'),
runSequence = require('run-sequence'),
del = require('del'),
replace = require('gulp-string-replace'),
sourcemaps = require('gulp-sourcemaps'),
exec = require('child_process').exec,
ngc = require('gulp-ngc'),
changed = require('gulp-changed'),
path = require('path'),
util = require('gulp-util'),
shell = require('gulp-shell'),
runSequence = require('run-sequence')
var appSrc = 'src';
var libraryDist = 'dist';
var watchDist = 'dist-watch';
// copies files to the libraryDist directory.
function copyToDist(srcArr) {
return gulp.src(srcArr)
.pipe(gulp.dest(function (file) {
return libraryDist + file.base.slice(__dirname.length); // save directly to dist
}));
}
// copies files from the libraryDist to the watchDist.
function updateWatchDist() {
return gulp
.src(libraryDist + '/**')
.pipe(changed(watchDist))
.pipe(gulp.dest(watchDist));
}
function transpileLESS(src, debug) {
return gulp.src(src)
.pipe(sourcemaps.init())
.pipe(less({
paths: [
path.join('src/assets/stylesheets/shared/'),
path.join('node_modules/'),
path.join('node_modules/patternfly/dist/less/'),
path.join('node_modules/patternfly/node_modules/')],
}).on('error', function (err) {
console.log(err);
}))
.pipe(autoprefixer({
browsers: ['last 2 versions'],
cascade: false
}))
.pipe(cssmin().on('error', function(err) {
console.log(err);
}))
.pipe(sourcemaps.write())
.pipe(gulp.dest(function (file) {
return libraryDist + file.base.slice(__dirname.length);
}));
combined.on('error', console.error.bind(console));
return combined;
}
function minifyCSS(file) {
try {
var minifiedFile = stylus.render(file);
minifiedFile = postcss([autoprefixer]).process(minifiedFile).css;
minifiedFile = csso.minify(minifiedFile).css;
return minifiedFile;
} catch (err) {
console.log(err);
}
}
/*
* Tasks
*/
// Stylelint task for build process
gulp.task('lint-less', function lintLessTask() {
return gulp
.src('src/**/*.less')
.pipe(stylelint({
failAfterError: true,
reporters: [{
formatter: 'string', console: true
}]
}));
});
// Stylelint task for npm script - uses verbose format for error messages
gulp.task('stylelint', function lintLessTask() {
return gulp
.src('src/**/*.less')
.pipe(stylelint({
failAfterError: true,
reporters: [{
formatter: 'verbose', console: true
}]
}));
});
// FIXME: why do we need that?
// replaces templateURL/styleURL with require statements in js.
gulp.task('post-transpile', ['transpile'], function () {
return gulp.src(['dist/src/**/*.js'])
.pipe(replace(/templateUrl:\s/g, "template: require("))
.pipe(replace(/\.html',/g, ".html'),"))
.pipe(replace(/styleUrls: \[/g, "styles: [require("))
.pipe(replace(/\.less']/g, ".css').toString()]"))
.pipe(gulp.dest(function (file) {
return file.base; // because of Angular 2's encapsulation, it's natural to save the css where the less-file was
}));
});
// Transpile and minify less, storing results in libraryDist.
gulp.task('transpile-less', ['lint-less'], function () {
return transpileLESS([
appSrc + '/**/*.less',
'!src/demo/**/*.less'
]);
});
// transpiles the ts sources to js using the tsconfig.
gulp.task('transpile', ['transpile-less'], function () {
return ngc('tsconfig.json')
});
// copies the template html files to libraryDist.
gulp.task('copy-html', function () {
return copyToDist([
'src/**/*.html',
'!src/demo/**/*.html',
'!src/demo.html'
]);
});
// copies images to libraryDist.
gulp.task('copy-images', function () {
return copyToDist([
'src/assets/images/**/*.*'
]);
});
// require transpile to finish before copying the css
gulp.task('copy-css', ['transpile'], function () {
return copyToDist([
'src/**/*.css',
'!src/demo/**/*.css'
]);
});
// copies the static asset files to libraryDist.
gulp.task('copy-static-assets', function () {
return gulp.src([
'LICENSE',
'README.adoc',
'package.json',
]).pipe(gulp.dest(libraryDist));
});
gulp.task('bundle', shell.task('npm run bundle-webpack'));
gulp.task('build:library',
[
'post-transpile',
'copy-html',
'copy-images',
'bundle',
'copy-static-assets'
]);
gulp.task('copy-watch', ['post-transpile'], function () {
return updateWatchDist();
});
gulp.task('copy-watch-all', ['build:library'], function () {
return updateWatchDist();
});
gulp.task('watch', ['copy-watch-all'], function () {
gulp.watch([appSrc + '/app/**/*.ts', '!' + appSrc + '/app/**/*.spec.ts']).on('change', function (e) {
util.log(util.colors.cyan(e.path) + ' has been changed. Compiling.');
runSequence(
'post-transpile'
);
});
gulp.watch([appSrc + '/app/**/*.less']).on('change', function (e) {
util.log(util.colors.cyan(e.path) + ' has been changed. Updating.');
runSequence(
'post-transpile',
'bundle'
);
});
gulp.watch([appSrc + '/app/**/*.html']).on('change', function (e) {
util.log(util.colors.cyan(e.path) + ' has been changed. Updating.');
runSequence(
'copy-html',
'bundle'
);
});
util.log('Now run');
util.log('');
util.log(util.colors.red(' npm link', path.resolve(watchDist), ' --production'));
util.log('');
util.log('in the npm module you want to link this one to');
});