-
Notifications
You must be signed in to change notification settings - Fork 785
/
gulpfile.js
207 lines (190 loc) · 5.25 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
const rollup = require('rollup').rollup;
const resolve = require('rollup-plugin-node-resolve');
const babel = require('rollup-plugin-babel');
const gulp = require('gulp');
const packages = require('/www/package.json');
const $ = require('gulp-load-plugins')({
config: packages
});
const browserSync = require('browser-sync').create();
const nunjucks = require('nunjucks');
const path = require('path');
const fs = require('fs');
let sourcemapsDest = 'sourcemaps';
let libName = 'tiny-slider',
testName = 'tests',
modulePostfix = '.module',
helperIEPostfix = '.helper.ie8',
script = libName + '.js',
moduleScript = libName + modulePostfix + '.js',
helperIEScript = libName + helperIEPostfix + '.js',
testScript = testName + '.js',
sassFile = libName + '.scss',
pathSrc = 'src/',
pathDest = 'dist/',
pathTest = 'tests/js/',
scriptSources = [pathSrc + '**/*.js', '!' + pathSrc + moduleScript, '!' + pathSrc + helperIEScript];
function errorlog (error) {
console.error.bind(error);
this.emit('end');
}
function readfiles (dir, arr) {
fs.readdirSync(dir).forEach( file => {
if (path.extname(file) === '.njk') {
arr.push(dir+file);
}
});
}
gulp.task('njk', function() {
let files = [];
readfiles('./template/demo/', files);
readfiles('./template/test/', files);
files.forEach(function(file) {
let dest = path.dirname(file).replace('/template', '');
return gulp.src(file)
.pipe($.plumber())
.pipe($.nunjucks.compile({}, {
watch: true,
noCache: true
}))
.pipe($.rename(function (path) {
path.extname = '.html';
}))
.pipe($.htmltidy({
doctype: 'html5',
wrap: 0,
hideComments: false,
indent: true,
'indent-attributes': false,
'drop-empty-elements': false,
'force-output': true
}))
.pipe(gulp.dest(dest));
});
});
function sassTask(src, dest) {
return gulp.src(src)
.pipe($.sourcemaps.init())
.pipe($.sass({
outputStyle: 'compressed',
precision: 7
}).on('error', $.sass.logError))
.pipe($.sourcemaps.write(sourcemapsDest))
.pipe(gulp.dest(dest))
.pipe(browserSync.stream());
}
// SASS Task
gulp.task('sass', function () {
sassTask(pathSrc + sassFile, pathDest);
});
// Script Task
gulp.task('script', function () {
return rollup({
input: pathSrc + script,
context: 'window',
treeshake: false,
plugins: [
resolve({
jsnext: true,
main: true,
browser: true,
}),
],
}).then(function (bundle) {
return bundle.write({
file: pathDest + libName + '.js',
format: 'es',
// moduleName: 'tns',
});
});
});
gulp.task('helper-ie8', function () {
return rollup({
input: pathSrc + helperIEScript,
}).then(function (bundle) {
return bundle.write({
file: pathDest + helperIEScript,
format: 'es',
});
});
});
gulp.task('editPro', ['script'], function() {
return gulp.src(pathDest + libName + '.js')
.pipe($.change(function (content) {
return 'var tns = (function (){\n' + content.replace('export { tns }', 'return tns') + '})();';
}))
.pipe(gulp.dest(pathDest))
});
gulp.task('makeDevCopy', function() {
return gulp.src(pathSrc + script)
// .pipe($.change(function (content) {
// return content
// .replace('IIFE', 'ES MODULE')
// .replace(/bower_components/g, '..');
// }))
.pipe($.rename({ basename: libName + modulePostfix }))
.pipe(gulp.dest(pathSrc))
});
gulp.task('min', ['editPro'], function () {
return gulp.src(pathDest + '*.js')
.pipe($.sourcemaps.init())
.pipe($.uglify())
.pipe($.sourcemaps.write('../' + sourcemapsDest))
.pipe(gulp.dest(pathDest + 'min'))
})
// browser-sync
gulp.task('server', function() {
browserSync.init({
server: {
baseDir: './'
},
ghostMode: {
clicks: false,
forms: false,
scroll: false
},
port: '3000',
open: false,
notify: false
});
gulp.watch('template/**/*.njk', function (e) {
var dir = path.parse(e.path).dir;
if (e.type !== 'deleted' && dir.indexOf('parts') < 0) {
return gulp.src(e.path)
.pipe($.plumber())
.pipe($.nunjucks.compile({}, {
watch: true,
noCache: true
}))
.pipe($.rename(function (path) {
path.extname = '.html';
}))
.pipe($.htmltidy({
doctype: 'html5',
wrap: 0,
hideComments: false,
indent: true,
'indent-attributes': false,
'drop-empty-elements': false,
'force-output': true
}))
.pipe(gulp.dest(dir.replace('/template', '')));
}
});
gulp.watch(pathSrc + sassFile, function (e) {
sassTask(pathSrc + sassFile, pathDest);
});
gulp.watch(pathSrc + script, ['makeDevCopy']);
gulp.watch(scriptSources, ['min']);
gulp.watch(pathSrc + helperIEScript, ['helper-ie8']);
// gulp.watch([pathTest + testScript], ['test']);
gulp.watch(['**/*.html', pathTest + '*.js', '!' + pathTest + 'tests-async.js', pathDest + '*.css', pathDest + 'min/*.js']).on('change', browserSync.reload);
});
// Default Task
gulp.task('default', [
// 'sass',
// 'min',
// 'helper-ie8',
// 'makeDevCopy',
'server',
]);