forked from sidvishnoi/dashboard.ducs.in
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
178 lines (165 loc) · 4.37 KB
/
gulpfile.js
File metadata and controls
178 lines (165 loc) · 4.37 KB
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
const path = require('path');
const gulp = require('gulp');
const babel = require('gulp-babel');
const sass = require('gulp-sass');
const uglifyJs = require('gulp-uglify');
const cleanCSS = require('gulp-clean-css');
const clean = require('gulp-clean');
const hash = require('gulp-hash');
const nodeSass = require('node-sass');
const srcDir = path.join(process.cwd(), './src-assets');
const distDir = path.join(process.cwd(), './assets');
const isProduction = process.env.NODE_ENV === 'production';
const assetBasePath = isProduction
? 'https://cdn.ducs.in'
: 'http://localhost:8001'; // for cdn.ducs.in/{type}/www
const paths = {
css: {
src: `${srcDir}/css/**/*.css`,
dest: `${distDir}/css/dash`,
},
sass: {
src: `${srcDir}/sass/**/*.sass`,
dest: `${distDir}/css/dash`,
},
js: {
src: [`${srcDir}/js/**/*.js`, `!${srcDir}/js/**/*.min.js`],
dest: `${distDir}/js/dash`,
},
jsCopy: {
src: `${srcDir}/js/**/*.min.js`,
dest: `${distDir}/js/dash`,
},
assetManifest: path.join(process.cwd(), '/src/templates/assets.json'),
};
const options = {
uglifyJs: {
mangle: {
toplevel: true,
// reserved: [],
},
},
babel: {
presets: ['es2015'],
},
sass: {
outputStyle: 'compressed',
functions: {
'mapImg($img)': function _mapImg(img) {
const p = `${assetBasePath}${img.getValue()}`;
return new nodeSass.types.String(p);
},
},
},
hash: {
hash: {
hashLength: 6,
},
js: {
deleteOld: true,
sourceDir: paths.js.dest,
},
css: {
deleteOld: true,
sourceDir: paths.css.dest,
},
},
};
const cleanDist = () =>
gulp.src([
`${distDir}/css`,
`${distDir}/js`,
], {
read: false,
})
.pipe(clean());
// minify and copy js
const minifyJs = () => {
if (isProduction) {
return gulp.src(paths.js.src)
.pipe(hash(options.hash.hash))
.pipe(babel(options.babel))
.pipe(uglifyJs(options.uglifyJs))
.pipe(gulp.dest(paths.js.dest))
.pipe(hash.manifest(paths.assetManifest, options.hash.js))
.pipe(gulp.dest('.'));
}
return gulp.src(paths.js.src, {
since: gulp.lastRun(minifyJs),
})
.pipe(hash(options.hash.hash))
.pipe(gulp.dest(paths.js.dest))
.pipe(hash.manifest(paths.assetManifest, options.hash.js))
.pipe(gulp.dest('.'));
};
// copy js directly
const copyJs = () => {
if (isProduction) {
return gulp.src(paths.jsCopy.src)
.pipe(hash(options.hash.hash))
.pipe(gulp.dest(paths.jsCopy.dest))
.pipe(hash.manifest(paths.assetManifest, options.hash.js))
.pipe(gulp.dest('.'));
}
return gulp.src(paths.jsCopy.src, {
since: gulp.lastRun(copyJs),
})
.pipe(hash(options.hash.hash))
.pipe(gulp.dest(paths.js.dest))
.pipe(hash.manifest(paths.assetManifest, options.hash.js))
.pipe(gulp.dest('.'));
};
const copyCss = () => {
if (isProduction) {
return gulp.src(paths.css.src)
.pipe(hash(options.hash.hash))
.pipe(cleanCSS())
.pipe(gulp.dest(paths.css.dest))
.pipe(hash.manifest(paths.assetManifest, options.hash.css))
.pipe(gulp.dest('.'));
}
return gulp.src(paths.css.src, {
since: gulp.lastRun(copyCss),
})
.pipe(hash(options.hash.hash))
.pipe(gulp.dest(paths.css.dest))
.pipe(hash.manifest(paths.assetManifest, options.hash.css))
.pipe(gulp.dest('.'));
};
const compileSass = () => {
if (isProduction) {
return gulp.src(paths.sass.src)
.pipe(hash(options.hash.hash))
.pipe(sass(options.sass).on('error', sass.logError))
.pipe(cleanCSS())
.pipe(gulp.dest(paths.sass.dest))
.pipe(hash.manifest(paths.assetManifest, options.hash.css))
.pipe(gulp.dest('.'));
}
return gulp.src(paths.sass.src)
.pipe(sass(options.sass).on('error', sass.logError))
.pipe(hash(options.hash.hash))
.pipe(gulp.dest(paths.sass.dest))
.pipe(hash.manifest(paths.assetManifest, options.hash.css))
.pipe(gulp.dest('.'));
};
const build = gulp.parallel(
minifyJs,
compileSass,
copyCss,
copyJs,
);
const watch = () => {
gulp.watch(paths.sass.src, compileSass);
gulp.watch(paths.css.src, copyCss);
gulp.watch(paths.js.src, minifyJs);
gulp.watch(paths.jsCopy.src, copyJs);
};
module.exports = {
default: build,
clean: cleanDist,
build,
watch: gulp.series(build, watch),
scripts: gulp.parallel(copyJs, minifyJs),
styles: gulp.parallel(copyCss, compileSass),
};