-
Notifications
You must be signed in to change notification settings - Fork 5
/
gulpfile.js
325 lines (256 loc) · 9.19 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Dependencies
var gulp = require('gulp');
var util = require('util');
var tap = require('gulp-tap');
var fs = require('fs');
var browser_sync = require('browser-sync');
var del = require('del');
var sass = require('gulp-sass');
var rename = require("gulp-rename");
var child_process = require('child_process');
var path = require('path');
var responsive = require('gulp-responsive');
var newy = require('gulp-newy');
var gulp_if = require('gulp-if');
var image_size = require('image-size');
// Global
var shopify_image_sizes = {
'pico': 16,
'icon': 32,
'thumb': 50,
'small': 100,
'compact': 160,
'medium': 240,
'large': 480,
'grande': 600,
'1024x1024': 1024,
'original': 1024,
'2048x2048': 2048,
'master': 2048
};
var shopify_image_extensions = ["jpg", "jpeg", "gif", "png"];
function escape_shell (cmd) {
return '\'' + cmd.replace(/\'/g, "'\\''") + '\'';
};
function capitalize (s) {
return s.toLowerCase().replace( /\b./g, function(a){ return a.toUpperCase(); } );
};
function is_liquid_file (string) {
return string.indexOf('.liquid') > -1;
};
function build_liquid (file) {
var template = file.contents.toString();
var data = {};
var is_no_layout = template.indexOf('{% layout none %}') > -1;
var is_asset = file.path.indexOf('.css') > -1 || file.path.indexOf('.scss') > -1 || file.path.indexOf('.js') > -1;
var is_alt_layout = template.indexOf('{% layout') > -1 && !is_no_layout;
var use_layout = ( is_no_layout || is_asset ? false : true );
if( use_layout ) {
var layout_dir = 'src/theme/layout/';
var layout_path = layout_dir + 'theme.liquid';
if( is_alt_layout ) {
layout_path = template.match(/\{% layout ["|']((?:\\.|[^"\\])*)["|'] \%}/)[0];
layout_path = layout_path.match(/["|'](.*?)["|']/)[1];
layout_path = layout_dir + layout_path + '.liquid';
}
var layout = fs.readFileSync(layout_path, 'utf8');
var layout_top = layout.split('{{ content_for_layout }}', 2)[0];
var layout_bottom = layout.split('{{ content_for_layout }}', 2)[1];
template = layout_top + template + layout_bottom;
}
var page_title = path.basename(file.path, '.liquid'),
page_title = page_title.replace('-', ' '),
page_title = capitalize(page_title);
(function hydrate_data () {
fs.readdirSync('src/data').forEach(function(file) {
delete require.cache[require.resolve('./src/data/' + file)];
data[path.basename(file, '.js')] = require('./src/data/' + file);
});
data.template = path.basename(file.path, '.liquid');
data.page_title = page_title;
data.page.title = page_title;
data.canonical_url = path.basename(file.path);
// Non-Shopify so prefixed with a `_`
data._pages = fs.readdirSync('src/theme/templates');
})();
(function fix_circular_deps () {
data = JSON.stringify(data);
var regex = /"{{ [a-z]*.js }}"/g;
data.replace(regex, function(match) {
var file_name = match
.replace('"{{ ', '')
.replace(' }}"', '')
.replace('.js', '');
try {
var file = require('./src/data/' + file_name + '.js');
data = data
.replace( /"{{ [a-z]*.js }}"/g, JSON.stringify(file) )
.replace( /"{{ [a-z]*.js }}"/g, JSON.stringify(file) )
.replace( new RegExp( '"' + file_name + '":"{{ ' + file_name + '.js }}"', 'g' ), '' );
} catch (ex) {
return;
}
});
})();
(function fix_double_commas () {
data = data.replace(/",,"/g, '","').replace(/,,"/g, ',"').replace(/",,/g, '",');
})();
(function write_compiled_data () {
var dir = 'site';
if ( !fs.existsSync(dir) ) {
fs.mkdirSync(dir);
}
var dir = 'site/temp';
if ( !fs.existsSync(dir) ) {
fs.mkdirSync(dir);
}
fs.writeFileSync( 'site/temp/' + path.basename(file.path) + '.json', data );
})();
(function run_ruby_liquid_command () {
template = escape_shell(template);
dataFileName = escape_shell(path.basename(file.path));
var command = 'ruby liquid.rb ' + template + ' ' + dataFileName;
template = child_process.execSync(command, { encoding: 'utf8' });
})();
(function replace_common_urls () {
template = template
.replace(/href=["|']\/["|']/g, 'href="index.html"')
.replace(/href=["|']\/cart["|']/g, 'href="cart.html"')
.replace(/action=["|']\/search["|']/g, 'action="search.html"');
})();
(function size_asset_images () {
var regex = new RegExp( "..\/src\/theme\/assets\/([A-Za-z0-9\-\_]+)_(" + Object.keys(shopify_image_sizes).join("|") + ")(_cropped)?\.(" + shopify_image_extensions.join("|") + ")", "g" );
template = template.replace( regex, "assets/$1_$2$3.$4" );
})();
file.contents = new Buffer( String(template) );
}
// Server
gulp.task('browser-sync', function() {
browser_sync({
server: {
baseDir: '.'
},
notify: false,
startPath: '/site/templates.html'
});
});
// HTML
function pre_html ( files, newy_enabled ) {
return gulp.src(files)
.pipe(gulp_if(newy_enabled, newy(function (projectDir, srcFile, absSrcFile) {
return projectDir + '/site/' + srcFile.replace('.liquid', '.html');
})))
.pipe(tap(function(file) {
build_liquid(file);
}))
.pipe(rename(function (path) {
path.extname = '.html'
}))
.pipe(gulp.dest('site'));
}
gulp.task('pre-html', function() {
return pre_html( ['src/templates.liquid', 'src/theme/templates/*.liquid'], false );
});
gulp.task('pre-html:newy', function() {
return pre_html( ['src/templates.liquid', 'src/theme/templates/*.liquid'], true );
});
gulp.task('html', ['pre-html'], browser_sync.reload);
gulp.task('html:newy', ['pre-html:newy'], browser_sync.reload);
// CSS
gulp.task('styles:css', function() {
return gulp.src(['src/theme/assets/*.css', 'src/theme/assets/*.css.liquid'])
.pipe(tap(function (file) {
if( is_liquid_file(file.path) ) {
build_liquid(file);
}
}))
.pipe(tap(function(file) {
file.path = file.path
.replace('.css.liquid', '.css')
.replace('.css.css', '.css');
}))
.pipe(gulp.dest('site/assets'))
.pipe(browser_sync.reload({
stream: true
}));
});
// Sass
gulp.task('styles:sass', function() {
return gulp.src(['src/theme/assets/*.scss', 'src/theme/assets/*.scss.liquid'])
.pipe(tap(function (file) {
if( is_liquid_file(file.path) ) {
build_liquid(file);
}
}))
.pipe(sass())
.pipe(tap(function(file) {
file.path = file.path
.replace('.scss.liquid', '.css')
.replace('.scss.css', '.css');
}))
.pipe(gulp.dest('site/assets'))
.pipe(browser_sync.reload({
stream: true
}));
});
// JS
gulp.task('javascript', function() {
return gulp.src(['src/theme/assets/*.js', 'src/theme/assets/*.js.liquid'])
.pipe(tap(function (file) {
if( is_liquid_file(file.path) ) {
build_liquid(file);
}
}))
.pipe(tap(function(file) {
file.path = file.path.replace('.js.liquid', '.js');
}))
.pipe(gulp.dest('site/assets'))
.pipe(browser_sync.reload({
stream: true
}));
});
// Generate asset images with all Shopify sizes
gulp.task('pre-resize-assets', function () {
var config = {};
fs.readdirSync('src/theme/assets').forEach(function(file) {
var basename = path.basename(file);
var extname = path.extname(file);
if( ! shopify_image_extensions.some(function(v) { return extname.indexOf(v) >= 0; }) ) {
return;
}
var dimensions = image_size( 'src/theme/assets/' + basename );
config[basename] = [];
for( var size in shopify_image_sizes ) {
['', '_cropped'].forEach(function (value) {
config[basename].push({
rename: basename.replace('.', '_' + size + value + '.'),
max: ( value ? false : true ),
crop: ( value ? 'center' : false ),
withoutEnlargement: ( value ? false : true ),
width: ( dimensions.width >= shopify_image_sizes[size] || value ? shopify_image_sizes[size] : null ),
height: ( dimensions.height >= shopify_image_sizes[size] || value ? shopify_image_sizes[size] : null )
});
});
}
});
return gulp.src('src/theme/assets/*.{' + shopify_image_extensions.join(',') + '}')
.pipe(responsive( config ))
.pipe(gulp.dest('site/assets'));
});
gulp.task('resize-assets', ['pre-resize-assets'], browser_sync.reload);
// Build
gulp.task('build', ['html', 'styles:css', 'styles:sass', 'javascript', 'resize-assets']);
// Build and serve
gulp.task('build:serve', ['build'], function() {
gulp.start('browser-sync');
});
// Default task
gulp.task('default', ['build:serve'], function() {
gulp.watch(['src/theme/layout/*.liquid', 'src/theme/snippets/*.liquid'], ['html']);
gulp.watch(['src/templates.liquid', 'src/theme/templates/*.liquid'], ['html:newy']);
gulp.watch(['src/theme/assets/*.css', 'src/theme/assets/*.css.liquid'], ['styles:css']);
gulp.watch(['src/theme/assets/*.scss', 'src/theme/assets/*.scss.liquid'], ['styles:sass']);
gulp.watch(['src/theme/assets/*.js', 'src/theme/assets/*.js.liquid'], ['javascript']);
gulp.watch(['src/theme/assets/*.{' + shopify_image_extensions.join(',') + '}'], ['resize-assets']);
gulp.watch(['src/data/**/*'], ['html', 'styles:css', 'styles:sass', 'javascript']);
});