-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
158 lines (131 loc) · 4.32 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
const fs = require('fs');
const path = require('path');
const paths = require('./paths/config-paths');
const gulp = require('gulp');
const webpackStream = require('webpack-stream');
const prodConfig = require('./webpack-configs/prod.webpack.config.js');
const devConfig = require('./webpack-configs/dev.webpack.config.js');
// STYLES
const less = require('gulp-less');
const postcss = require('gulp-postcss');
// POSTCSS PLUGINS
const cssnano = require('cssnano');
const autoprefixer = require('autoprefixer');
// UTILS
const sourcemaps = require('gulp-sourcemaps');
const clean = require('gulp-clean');
const aemsync = require('aemsync');
// DEV ENV
const nodemon = require('gulp-nodemon');
const named = require('vinyl-named');
// const tslint = require("gulp-tslint");
// CHECK IF CURRENT ENV AEM
const isAEM = (() => {
try {
const AEM_PATH = paths.OUTPUT_DIR_AEM;
return fs.existsSync(AEM_PATH);
} catch (e) {
return false;
}
})();
console.log(`ENV: ${isAEM ? 'AEM, LOCAL' : 'LOCAL'}`);
///////////////////////////
function _clean(dir) {
return function cleanPath() {
return gulp.src(path.join(dir, '*.*'), { read: false }).pipe(clean({ force: true }));
}
}
const _cleanTasks = [_clean(paths.OUTPUT_DIR)];
if (isAEM) {
_cleanTasks.push(_clean(paths.OUTPUT_DIR_AEM));
}
const cleanTask = gulp.series.apply(gulp, _cleanTasks);
/**
* @return gulp pipe with css
* */
function buildLessStream(attachSourcemaps = true) {
const css = gulp.src(paths.INPUT_BUNDLE + '*.less')
.pipe(sourcemaps.init())
.pipe(less())
.pipe(postcss([
autoprefixer({
browsers: [
'last 1 version',
'not ie <= 11'
]
}),
cssnano()
]));
return attachSourcemaps ? css.pipe(sourcemaps.write()) : css;
}
/**
* @return gulp pipe with js
* */
function buildWebpackStream(mode = 'dev') {
return gulp.src(path.join(paths.INPUT_BUNDLE, '*.ts'))
.pipe(named())
// TODO: optimize to bypass initialization & loading of dev 'plugins' (like TSChecker and etc.)
.pipe(webpackStream(mode === 'dev' ? devConfig : prodConfig));
}
function target(stream) {
stream = stream.pipe(gulp.dest(paths.OUTPUT_DIR));
if (isAEM) {
stream = stream.pipe(gulp.dest(paths.OUTPUT_DIR_AEM));
}
return stream;
}
const buildLess = () => target(buildLessStream());
const buildWebpack = () => target(buildWebpackStream());
function attachJCRIdentifier() {
return gulp.src('aem-build/.content.xml')
.pipe(gulp.dest(paths.OUTPUT_DIR_AEM));
}
function serveTask() {
const browserSync = require('browser-sync').create();
browserSync.init(null, {
proxy: `http://localhost:${paths.PORT}`,
browser: 'chrome',
port: 8000,
});
nodemon({
script: 'server.js',
// ext: 'js hbs'
}).on('restart', function onRestart() {
console.log('restarted!')
});
gulp.watch(['./src/bundles/*.ts', './src/components/**/' + '*.ts'], { usePolling: true }, function rebuildTS() {
return buildWebpack().pipe(browserSync.stream());
});
gulp.watch(['src/bundles/*.less', 'src/components/**/' + '*.less'], { usePolling: true }, function rebuildLESS() {
return buildLess().pipe(browserSync.stream());
});
}
// GULP tasks declaration
gulp.task('clean', cleanTask);
gulp.task('build-ts', () => buildWebpack());
gulp.task('build-less', () => buildLess());
function aemsyncWatch() {
const workingDirs = paths.AEMSYNC_PATHS;
const onPushEnd = (err, host) => {
if (err) {
return console.log(`Error when pushing package to ${host}.`, err)
}
console.log(`Package pushed to ${host}.`)
};
workingDirs.forEach(function (dir) {
aemsync({
workingDir: dir,
targets: paths.AEMSYNC_TARGETS,
onPushEnd: onPushEnd
});
});
}
gulp.task('aemsync', aemsyncWatch);
gulp.task('serve', serveTask);
gulp.task('devBuild', gulp.series('clean', gulp.parallel('build-less', 'build-ts'), 'serve'));
gulp.task('prod', gulp.parallel(
() => buildLessStream().pipe(gulp.dest(paths.OUTPUT_DIR_AEM)),
() => buildWebpackStream().pipe(gulp.dest(paths.OUTPUT_DIR_AEM)),
() => attachJCRIdentifier()
));
gulp.task('default', gulp.series('devBuild'));