-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
250 lines (218 loc) · 7.28 KB
/
gulpfile.js
File metadata and controls
250 lines (218 loc) · 7.28 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
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
// node_modules requires
var gulp = require('gulp')
var concat = require('gulp-concat')
var stylus = require('gulp-stylus')
var base64 = require('gulp-base64')
var base64Settings = {
extensions: ['svg'],
maxImageSize: 10 * 1024,
debug: false
}
var autoprefixer = require('gulp-autoprefixer')
var autoprefixerSettings = {
browsers: ['iOS >= 7'],
cascade: false
}
var through = require('through2')
var fs = require('fs')
var http = require('http')
var argv = require('minimist')(process.argv)
var spawn = require('child_process').spawn
var execSync = require('child_process').execSync
var browserSync = require('browser-sync').create()
// lib requires
var beast = require('./lib/beast.js')
var conf = require('./lib/conf.js')
// Заготовка для gulp-плагина
var pipeToString = (callback, ext) => {
return through.obj(function(file, encoding, cb) {
if (file.isNull()) return cb(null, file)
if (file.isStream()) return cb(new PluginError('gulp-beast', 'Streaming not supported'))
if (ext === undefined || file.path.split('.').pop() === ext) {
file.contents = new Buffer(
callback(file.contents.toString(), file)
)
}
cb (null, file)
})
}
var quoteBase64 = (string, file) => string.replace(/url\(([^)]+)\)/g, "url('$1')")
if (!conf.project) {
console.log(`Please, specify --project`)
process.exit(1)
}
if (!fs.existsSync('./build')) {
fs.mkdirSync('./build')
}
if (!fs.existsSync(conf.path.build)) {
fs.mkdirSync(conf.path.build)
}
function js() {
return gulp.src(conf.path.js)
.pipe(pipeToString(
string => beast.parseBML(string),
'bml'
))
.pipe(concat('build.js'))
.pipe(gulp.dest(conf.path.build))
.on('end', function() {
browserSync.reload();
})
}
function css() {
var filenames = []
var splitString = '\n/* CUT THE FILE HERE */\n'
return gulp.src(conf.path.css)
.pipe(base64(base64Settings))
.pipe(concat('build.styl', {newLine: splitString}))
.pipe(pipeToString(quoteBase64, 'styl'))
.pipe(
stylus({
paths: [__dirname + '/blocks'],
})
)
.pipe(autoprefixer(autoprefixerSettings))
.pipe(gulp.dest(conf.path.build))
.on('end', function() {
browserSync.reload('*.css');
})
}
function createExpTasks() {
var expNames = []
var expTasks = []
// Get full list of experiment names
if (fs.existsSync(conf.path.exp)) {
expNames = expNames.concat(
fs.readdirSync(conf.path.exp).filter(
expName => expName.indexOf('.') === -1
)
)
}
// If no experiments, return a dummy task
if (expNames.length === 0) {
return function(cb) { cb(); };
}
// Build for each experiment
for (let i = 0, ii = expNames.length; i < ii; i++) {
let expName = expNames[i]
let js = [`${conf.path.exp}/${expName}/**/*.bml`]
let css = [`${conf.path.exp}/${expName}/**/*.styl`]
if (!fs.existsSync(`${conf.path.build}/exp/`)) {
fs.mkdirSync(`${conf.path.build}/exp/`)
}
fs.writeFileSync(`${conf.path.build}/exp/${expName}.js`, '')
fs.writeFileSync(`${conf.path.build}/exp/${expName}.css`, '')
// Create JS task for this experiment
const expJs = function() {
return gulp.src(js)
.pipe(pipeToString(string => beast.parseBML(string), 'bml'))
.pipe(concat(`${expName}.js`))
.pipe(gulp.dest(`${conf.path.build}/exp`))
}
gulp.task(`exp-js-${expName}`, expJs)
// Create CSS task for this experiment
const expCss = function() {
return gulp.src(css)
.pipe(base64(base64Settings))
.pipe(concat(`${expName}.styl`))
.pipe(pipeToString(quoteBase64, 'styl'))
.pipe(stylus({
paths: [__dirname + '/blocks']
}))
.pipe(autoprefixer(autoprefixerSettings))
.pipe(gulp.dest(`${conf.path.build}/exp`))
}
gulp.task(`exp-css-${expName}`, expCss)
if (!argv['no-watch']) {
gulp.watch(js, gulp.series(`exp-js-${expName}`))
gulp.watch(css, gulp.series(`exp-css-${expName}`))
}
expTasks.push(gulp.parallel(`exp-js-${expName}`, `exp-css-${expName}`))
}
return gulp.parallel(...expTasks)
}
let server
function startServer(cb) {
server && server.kill()
server = spawn('node', ['lib/server-project.js', '--port', conf.port.dev, '--dev', '--project', conf.project])
server.stdout.on('data', (data) => {
console.log(data.toString())
})
server.stderr.on('data', (data) => {
console.log(data.toString())
server.kill()
})
server.on('error', (data) => {
console.log(data.toString())
server.kill()
})
process.on('exit', (code) => {
server.kill()
})
// Simpler Browser-Sync configuration
browserSync.init({
proxy: `http://localhost:8080`,
port: 3000,
ui: false,
open: false,
notify: false,
ghostMode: false,
reloadOnRestart: true
}, function(err, bs) {
console.log('\n---------------------------------');
console.log('Access your site at: http://localhost:3000');
console.log('---------------------------------\n');
});
cb()
}
// Define public tasks
gulp.task('js', js)
gulp.task('css', css)
// Build task combines js, css, and exp tasks
const build = gulp.parallel(js, css, createExpTasks())
gulp.task('build', build)
// Now we can use build since it's defined
gulp.task('server', gulp.series(build, startServer))
// Default task
function watchFiles(cb) {
// Watch all .bml files in blocks and project
gulp.watch(`${__dirname}/blocks/**/*.bml`, { ignoreInitial: false },
gulp.series(js, function(done) {
browserSync.reload();
done();
})
);
gulp.watch(`${__dirname}/projects/${conf.project}/**/*.bml`, { ignoreInitial: false },
gulp.series(js, function(done) {
browserSync.reload();
done();
})
);
// Watch all .styl files in blocks and project
gulp.watch(`${__dirname}/blocks/**/*.styl`, { ignoreInitial: false },
gulp.series(css)
);
gulp.watch(`${__dirname}/projects/${conf.project}/**/*.styl`, { ignoreInitial: false },
gulp.series(css)
);
// Watch experiment files if they exist
if (fs.existsSync(conf.path.exp)) {
gulp.watch(`${conf.path.exp}/**/*.bml`, { ignoreInitial: false }, gulp.series(build))
.on('change', function(path) {
console.log(`File ${path} was changed`);
});
gulp.watch(`${conf.path.exp}/**/*.styl`, { ignoreInitial: false }, gulp.series(build))
.on('change', function(path) {
console.log(`File ${path} was changed`);
});
}
console.log('Watching for file changes...');
console.log('Working directory:', __dirname);
console.log('Project:', conf.project);
cb();
}
// Update the default task
gulp.task('default', gulp.series(
build,
gulp.parallel(watchFiles, startServer)
));