-
Notifications
You must be signed in to change notification settings - Fork 75
/
gulpfile.js
302 lines (250 loc) · 8.42 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/** Что-бы все не пропало! **/
process.on('uncaughtException', function (err) {
console.log(err)
});
const { src, dest, series, parallel } = require('gulp');
var concat = require('gulp-concat'),
chokidar = require('chokidar'),
uglify = require('gulp-uglify-es').default,
uglifycss = require('gulp-uglifycss'),
browser = require('browser-sync').create(),
newer = require('gulp-newer'),
sass = require('gulp-sass')(require('sass')),
autoprefixer = require('gulp-autoprefixer'),
fileinclude = require('gulp-file-include'),
replace = require('gulp-replace'),
fs = require('fs'),
worker = require('rollup-plugin-web-worker-loader')
var source = require('vinyl-source-stream');
var buffer = require('vinyl-buffer');
var rollup = require('@rollup/stream');
// *Optional* Depends on what JS features you want vs what browsers you need to support
// *Not needed* for basic ES6 module import syntax support
var babel = require('@rollup/plugin-babel').babel;
// Add support for require() syntax
var commonjs = require('@rollup/plugin-commonjs');
// Add support for importing from node_modules folder like import x from 'module-name'
var nodeResolve = require('@rollup/plugin-node-resolve');
var regenerator = require('rollup-plugin-regenerator');
var cache;
var srcFolder = './src/';
var dstFolder = './dest/';
var pubFolder = './public/';
var bulFolder = './build/';
var idxFolder = './index/';
var plgFolder = './plugins/';
var isDebugEnabled = false;
function merge(done) {
let plugins = [babel({
babelHelpers: 'bundled',
presets: ['@babel/preset-env']
}), commonjs, nodeResolve, worker()]
rollup({
// Point to the entry file
input: srcFolder+"app.js",
// Apply plugins
plugins: plugins,
// Use cache for better performance
//cache: cache,
// Note: these options are placed at the root level in older versions of Rollup
output: {
// Output bundle is intended for use in browsers
// (iife = "Immediately Invoked Function Expression")
format: 'iife',
sourcemap: isDebugEnabled ? 'inline' : false
},
onwarn: function ( message ) {
return;
}
})
.on('bundle', function(bundle) {
// Update cache data after every bundle is created
//cache = bundle;
})
// Name of the output file.
.pipe(source('app.js'))
.pipe(buffer())
//.pipe(uglify())
.pipe(replace(/return kIsNodeJS/g, "return false"))
.pipe(replace(/return kIsNodeJS/g, "return false"))
// Where to send the output file
.pipe(dest(dstFolder));
done();
}
function bubbleFile(name){
let plug = [babel({
babelHelpers: 'bundled',
presets: ['@babel/preset-env']
}), commonjs, nodeResolve]
rollup({
input: plgFolder+name,
plugins: plug,
output: {
format: 'iife',
sourcemap: isDebugEnabled ? 'inline' : false,
sourcemapPathTransform: isDebugEnabled ? pluginSourcemapPathTransform : undefined
},
onwarn: function ( message ) {
return;
}
})
.pipe(source(name))
.pipe(buffer())
.pipe(fileinclude({
prefix: '@@',
basepath: '@file'
}))
.pipe(dest(dstFolder));
}
function plugins(done) {
fs.readdirSync(plgFolder).filter(function (file) {
return fs.statSync(plgFolder+'/'+file).isDirectory();
}).forEach(folder => {
bubbleFile(folder+'/'+folder+'.js')
plugin_sass(plgFolder+'/'+folder)
});
done();
}
function plugin_sass(plugin_src){
return src(plugin_src+'/css/*.scss')
.pipe(sass.sync().on('error', sass.logError)) // Преобразуем Sass в CSS посредством gulp-sass
.pipe(autoprefixer(['last 100 versions', '> 1%', 'ie 8', 'ie 7', 'ios 6', 'android 4'], { cascade: true })) // Создаем префиксы
.pipe(uglifycss({
"maxLineLen": 80,
"uglyComments": true
}))
.pipe(replace(/\n/g, ''))
.pipe(replace(/"/g, "'"))
.pipe(dest(plugin_src+'/css'))
}
var copy_timer;
/** Обновляем файл для WEB **/
function build_web(done){
clearTimeout(copy_timer)
//таймер сила!
copy_timer = setTimeout(()=>{
src([dstFolder+'app.js']).pipe(dest(bulFolder+'web/'));
fs.readdirSync(dstFolder).filter(function (file) {
return fs.statSync(dstFolder+'/'+file).isDirectory();
}).forEach(folder => {
src([dstFolder+folder+'/'+folder+'.js']).pipe(dest(bulFolder+'web/plugins'));
});
},500)
done();
}
/** Публикуем для WEB платформы **/
function public_task(path){
return src(dstFolder + '/app.min.js').pipe(dest(bulFolder+path));
}
function lang_task(){
return src(srcFolder + '/lang/*.js').pipe(dest(pubFolder + '/lang'));
}
function public_webos(){
return public_task('webos/');
}
function public_tizen(){
return public_task('tizen/');
}
function public_github(){
return public_task('github/lampa/');
}
function index_webos(){
return src(idxFolder + '/webos/**/*').pipe(dest(bulFolder+'webos/'));
}
function index_tizen(){
return src(idxFolder + '/tizen/**/*').pipe(dest(bulFolder+'tizen/'));
}
function index_github(){
return src(idxFolder + '/github/**/*').pipe(dest(bulFolder+'github/lampa/'));
}
/** Сверяем файлы **/
function sync_task(path){
return src([pubFolder + '**/*'])
.pipe(newer(bulFolder+path))
.pipe(dest(bulFolder+path));
}
function sync_web(){
return sync_task('web/');
}
function sync_webos(){
return sync_task('webos/');
}
function sync_tizen(){
return sync_task('tizen/');
}
function sync_github(){
return sync_task('github/lampa/');
}
/** Следим за изменениями в файлах **/
function watch(done){
var watcher = chokidar.watch([srcFolder,pubFolder,plgFolder], { persistent: true, ignored: [pubFolder + '/lang']});
var timer;
var change = function(path){
clearTimeout(timer)
if(path.indexOf('.css') > -1) return;
timer = setTimeout(
series(merge, plugins, sass_task, lang_task, sync_web, build_web)
,5000)
}
watcher.on('add', function(path) {
console.log('File', path, 'has been added');
change(path)
})
.on('change', function(path) {
console.log('File', path, 'has been changed');
change(path)
})
.on('unlink', function(path) {
console.log('File', path, 'has been unlink');
change(path)
})
done();
}
function browser_sync(done) {
browser.init({
server: {
baseDir: bulFolder+'web/'
},
open: false,
notify: false,
ghostMode: false,
});
done();
}
function sass_task(){
return src(srcFolder+'/sass/*.scss')
.pipe(sass.sync().on('error', sass.logError)) // Преобразуем Sass в CSS посредством gulp-sass
.pipe(autoprefixer(['last 100 versions', '> 1%', 'ie 8', 'ie 7', 'ios 6', 'android 4'], { cascade: true })) // Создаем префиксы
.pipe(dest(pubFolder+'/css'))
.pipe(browser.reload({stream: true}))
}
function uglify_task() {
return src([dstFolder+'app.js']).pipe(concat('app.min.js')).pipe(dest(dstFolder));
}
function test(done){
lang_task()
done();
}
function enable_debug_mode(done){
console.log("build with sourcemaps!")
isDebugEnabled = true;
done()
}
/**
* преобразует путь к исходному файлу
* @param {string} relativeSourcePath
* @param {string} sourcemapPath
* @returns {string} a new path to source
*/
function pluginSourcemapPathTransform(relativeSourcePath, sourcemapPath) {
const plgFolderLen = plgFolder.length-2;
const newPath = relativeSourcePath.substring(plgFolderLen);
return newPath;
}
exports.pack_webos = series(sync_webos, uglify_task, public_webos, index_webos);
exports.pack_tizen = series(sync_tizen, uglify_task, public_tizen, index_tizen);
exports.pack_github = series(sync_github, uglify_task, public_github, index_github);
exports.pack_plugins = series(plugins);
exports.test = series(test);
exports.default = parallel(watch, browser_sync);
exports.debug = series(enable_debug_mode, this.default)