-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgulpfile.js
190 lines (161 loc) · 6.08 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
const gulp = require('gulp');
const del = require("del");
const concat = require('gulp-concat');
const rename = require('gulp-rename');
const uglify = require('gulp-uglify-es').default;
const minifyCSS = require('gulp-clean-css');
const inject = require('gulp-inject');
const browserSync = require('browser-sync').create();
const connect = require('gulp-connect');
const mergeStream = require('merge-stream');
const sourcemaps = require('gulp-sourcemaps');
const babel = require('gulp-babel');
const packageName = 'plate-map';
const PATH = {
source: {
css: ['src/css/*.css'],
js: ['src/js/*.js'],
},
example: {
js: 'example/example.js',
html: 'example/index.html'
},
dependencies: {
css: [
'node_modules/select2/dist/css/select2.css'
],
js: [
'node_modules/jquery/dist/jquery.js',
'node_modules/jquery-ui-dist/jquery-ui.js',
'node_modules/select2/dist/js/select2.js',
'node_modules/svgjs/dist/svg.js',
'node_modules/clipboard/dist/clipboard.js'
]
},
destination: {
prod: {
css: 'build/prod/css',
js: 'build/prod/js',
root: 'build/prod'
},
dev: {
css: 'build/dev/css',
js: 'build/dev/js',
root: 'build/dev'
},
pack: {
css: 'dist/css',
js: 'dist/js',
root: 'dist'
}
}
};
let config = {destination: {css: '', js: '', root: ''}};
function concat_minify_css(name, source, destination) {
return gulp.src(source)
.pipe(sourcemaps.init())
.pipe(concat(name + '.css'))
.pipe(gulp.dest(destination))
.pipe(minifyCSS())
.pipe(rename(name + '.min.css'))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(destination));
}
function concat_uglify_js(name, source, destination) {
return gulp.src(source)
.pipe(sourcemaps.init())
.pipe(concat(name + '.js'))
.pipe(babel({
presets: ['@babel/env']
}))
.pipe(gulp.dest(destination))
.pipe(uglify())
.pipe(rename(name + '.min.js'))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(destination));
}
function config_env(env) {
config.destination.css = PATH.destination[env].css;
config.destination.js = PATH.destination[env].js;
config.destination.root = PATH.destination[env].root;
}
gulp.task('config.prod', async () => {
config_env('prod');
});
gulp.task('config.dev', async () => {
config_env('dev');
});
gulp.task('config.pack', async () => {
config_env('pack');
});
gulp.task('clean', () => {
return del([config.destination.root]);
});
gulp.task('css', () => {
return concat_minify_css(packageName, PATH.source.css, config.destination.css);
});
gulp.task('js', () => {
return concat_uglify_js(packageName, PATH.source.js, config.destination.js);
});
gulp.task('copy.other', () => {
const css = gulp.src(PATH.dependencies.css)
.pipe(gulp.dest(config.destination.css));
const js = gulp.src(PATH.dependencies.js)
.pipe(gulp.dest(config.destination.js));
const ex_js = gulp.src(PATH.example.js)
.pipe(gulp.dest(config.destination.js));
return mergeStream(css, js, ex_js);
});
gulp.task('copy.src', () => {
const css = gulp.src(PATH.source.css)
.pipe(gulp.dest(config.destination.css));
const js = gulp.src(PATH.source.js)
.pipe(gulp.dest(config.destination.js));
return mergeStream(css, js);
});
function jsLink (filePath) {
const fileName = filePath.substring(filePath.lastIndexOf("/") + 1, filePath.length);
return '<script src="js/' + fileName + '"></script>';
}
function cssLink (filePath) {
const fileName = filePath.substring(filePath.lastIndexOf("/") + 1, filePath.length);
return '<link rel="stylesheet" href="css/' + fileName + '">';
}
gulp.task('inject.prod', () => {
return gulp.src(PATH.example.html)
.pipe(inject(gulp.src(PATH.dependencies.css, {read: false}), {transform: cssLink, name: 'dependencies'}))
.pipe(inject(gulp.src(PATH.dependencies.js, {read: false}), {transform: jsLink, name: 'dependencies'}))
.pipe(inject(gulp.src([`${config.destination.css}/${packageName}.min.css`, `${config.destination.js}/${packageName}.min.js`], {read: false}),
{
ignorePath: config.destination.root,
addRootSlash: false
}))
.pipe(inject(gulp.src(PATH.example.js, {read: false}), {transform: jsLink, name: 'example'}))
.pipe(gulp.dest(config.destination.root));
});
gulp.task('inject.dev', () => {
return gulp.src(PATH.example.html)
.pipe(inject(gulp.src(PATH.dependencies.css, {read: false}), {transform: cssLink, name: 'dependencies'}))
.pipe(inject(gulp.src(PATH.dependencies.js, {read: false}), {transform: jsLink, name: 'dependencies'}))
.pipe(inject(gulp.src(PATH.source.css, {read: false}), {transform: cssLink}))
.pipe(inject(gulp.src(PATH.source.js, {read: false}), {transform: jsLink}))
.pipe(inject(gulp.src(PATH.example.js, {read: false}), {transform: jsLink, name: 'example'}))
.pipe(gulp.dest(config.destination.root));
});
gulp.task('server.dev', async () => {
browserSync.init({server: PATH.destination.dev.root});
gulp.watch(PATH.source.css.concat(PATH.source.js), gulp.series('build.dev', browserSync.reload));
});
gulp.task('server.prod', async () => {
connect.server({
name: 'App [PRODUCTION MODE]',
root: PATH.destination.prod.root
});
});
gulp.task('build.dist', gulp.series('config.pack', 'clean', 'css', 'js'));
gulp.task('build.dev', gulp.series('config.dev', 'clean', 'copy.other', 'copy.src', 'inject.dev'));
gulp.task('serve.dev', gulp.series('build.dev', 'server.dev'));
gulp.task('build.prod', gulp.series('config.prod', 'clean', 'copy.other', 'css', 'js', 'inject.prod'));
gulp.task('serve.prod', gulp.series('build.prod', 'server.prod'));
gulp.task('default', gulp.series('serve.dev'));
gulp.task('build.all', gulp.series('build.dev', 'build.prod', 'build.dist'));