-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgulpfile.js
290 lines (253 loc) · 9.04 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
/******************************************************
* CUSTOM PATTERN LAB NODE GULP TWIG
*
* The gulp wrapper around patternlab-node core, providing tasks to interact with the core library and move supporting frontend assets.
******************************************************/
var gulp = require('gulp'),
path = require('path'),
browserSync = require('browser-sync').create(),
sass = require('gulp-sass'),
chalk = require('chalk');
flatten = require('gulp-flatten');
fs = require('fs');
importer = require('node-sass-globbing');
config = require('./gulp-config.json');
shell = require('gulp-shell');
/**
* Normalize all paths to be plain, paths with no leading './',
* relative to the process root, and with backslashes converted to
* forward slashes. Should work regardless of how the path was
* written. Accepts any number of parameters, and passes them along to
* path.resolve(). Testing change.
*
* This is intended to avoid all known limitations of gulp.watch().
*
* @param {...string} pathFragment - A directory, filename, or glob.
*/
function normalizePath() {
return path
.relative(
process.cwd(),
path.resolve.apply(this, arguments)
)
.replace(/\\/g, "/");
}
/******************************************************
* COMPILATION TASKS - compile scss etc.
******************************************************/
// SASS Compilation
var sass_config = {
importer: importer,
includePaths: config.sass_config.includePaths
};
gulp.task('pl-sass', function(){
return gulp.src(normalizePath(paths().source.css) + '**/*.scss')
.pipe(sass(sass_config).on('error', sass.logError))
.pipe(gulp.dest(normalizePath(paths().public.root)));
});
/******************************************************
* COPY TASKS - stream assets from source to destination
******************************************************/
// JS copy
gulp.task('pl-copy:js', function () {
return gulp.src('**/*.js', {cwd: normalizePath(paths().source.js)} )
.pipe(gulp.dest(normalizePath(paths().public.js)));
});
// Images copy
gulp.task('pl-copy:img', function () {
return gulp.src('**/*.*',{cwd: normalizePath(paths().source.images)} )
.pipe(gulp.dest(normalizePath(paths().public.images)));
});
// Favicon copy
gulp.task('pl-copy:favicon', function () {
return gulp.src('favicon.ico', {cwd: normalizePath(paths().source.root)} )
.pipe(gulp.dest(normalizePath(paths().public.root)));
});
// Fonts copy
gulp.task('pl-copy:font', function () {
return gulp.src('**/*.*', {cwd: normalizePath(paths().source.fonts)})
.pipe(flatten())
.pipe(gulp.dest(normalizePath(paths().public.fonts)));
});
// CSS Copy
gulp.task('pl-copy:css', function () {
return gulp.src(normalizePath(paths().source.css) + '/*.css')
.pipe(gulp.dest(normalizePath(paths().public.css)))
.pipe(browserSync.stream());
});
// SASS Copy for development
gulp.task('pl-copy:sass', function () {
return gulp.src(normalizePath(paths().source.css) + '/**/*.*')
.pipe(gulp.dest(normalizePath(paths().public.sass)))
.pipe(browserSync.stream());
});
// Styleguide Copy everything but css
gulp.task('pl-copy:styleguide', function () {
return gulp.src(normalizePath(paths().source.styleguide) + '/**/!(*.css)')
.pipe(gulp.dest(normalizePath(paths().public.root)))
.pipe(browserSync.stream());
});
// Styleguide Copy and flatten css
gulp.task('pl-copy:styleguide-css', function () {
return gulp.src(normalizePath(paths().source.styleguide) + '/**/*.css')
.pipe(gulp.dest(function (file) {
//flatten anything inside the styleguide into a single output dir per http://stackoverflow.com/a/34317320/1790362
file.path = path.join(file.base, path.basename(file.path));
return normalizePath(path.join(paths().public.styleguide, '/css'));
}))
.pipe(browserSync.stream());
});
// Examples Directory copy
gulp.task('pl-copy:exampleDirs', function () {
return gulp.src('**/*.*', {cwd: normalizePath(paths().source.examples)})
.pipe(gulp.dest(normalizePath(paths().public.examples)));
});
// Examples CSS Resources copy
gulp.task('pl-copy:exampleResourcesCSS', function () {
return gulp.src(normalizePath(paths().public.css) + '/style.css')
.pipe(gulp.dest(normalizePath(paths().public.examples + '/php/resources/')));
});
// Examples Styleguide Resources copy
gulp.task('pl-copy:exampleResourcesSG', function () {
return gulp.src(normalizePath(paths().public.styleguide) + '/css/styleguide.css')
.pipe(gulp.dest(normalizePath(paths().public.examples + '/php/resources/')));
});
// Examples JS Resources copy
gulp.task('pl-copy:exampleResourcesJS', function () {
return gulp.src(normalizePath(paths().public.js) + '/script.js')
.pipe(gulp.dest(normalizePath(paths().public.examples + '/php/resources/')));
});
gulp.task('pl-copy:examples', gulp.series('pl-copy:exampleDirs', 'pl-copy:exampleResourcesCSS', 'pl-copy:exampleResourcesSG', 'pl-copy:exampleResourcesJS'));
/******************************************************
* PATTERN LAB CONFIGURATION - API with core library
******************************************************/
//read all paths from our namespaced config file
function paths() {
return config.patternlab.paths;
}
/**
* Return gulp task location
*/
function getTask(task) {
return require('./gulp-tasks/' + task)(gulp, config);
}
//Task kicked off from js.js for all js compliation tasks
gulp.task('js:compile', getTask('js'));
gulp.task('pl-assets', gulp.series(
'js:compile',
'pl-copy:sass',
'pl-copy:img',
'pl-copy:favicon',
'pl-copy:font',
gulp.series('pl-sass', 'pl-copy:css', function(done){done();}),
'pl-copy:styleguide',
'pl-copy:styleguide-css',
'pl-copy:examples'
));
gulp.task('pl-build', shell.task([
'php pattern-lab-source/core/console --generate'
]))
gulp.task('patternlab:build', gulp.series('pl-build', 'pl-assets'));
/******************************************************
* SERVER AND WATCH TASKS
******************************************************/
/**
* Reloads BrowserSync.
* Note: Exits more reliably when used with a done callback.
*/
function reload(done) {
browserSync.reload();
done();
}
/**
* Reloads BrowserSync, with CSS injection.
* Note: Exits more reliably when used with a done callback.
*/
function reloadCSS(done) {
browserSync.reload('*.css');
done();
}
function watch() {
const watchers = [
{
name: 'CSS',
paths: [normalizePath(paths().source.css, '**', '*.css')],
config: { awaitWriteFinish: true },
tasks: gulp.series('pl-copy:css', reloadCSS)
},
{
name: 'SCSS',
paths: [normalizePath(paths().source.css, '**', '*.scss')],
config: { awaitWriteFinish: true },
tasks: gulp.series('pl-sass', reloadCSS)
},
{
name: 'Styleguide Files',
paths: [normalizePath(paths().source.styleguide, '**', '*')],
config: { awaitWriteFinish: true },
tasks: gulp.series('pl-copy:styleguide', 'pl-copy:styleguide-css', reloadCSS)
},
{
name: 'Source Files',
paths: [
normalizePath(paths().source.patterns, '**', '*.json'),
normalizePath(paths().source.patterns, '**', '*.md'),
normalizePath(paths().source.patterns, '**', '*.twig'),
normalizePath(paths().source.data, '**', '*.json'),
normalizePath(paths().source.fonts, '**', '*'),
normalizePath(paths().source.images, '**', '*'),
normalizePath(paths().source.js, '**', '*'),
normalizePath(paths().source.meta, '**', '*'),
normalizePath(paths().source.layouts, '**', '*'),
normalizePath(paths().source.macros, '**', '*'),
normalizePath(paths().source.annotations, '**', '*'),
],
config: { awaitWriteFinish: true },
tasks: gulp.series('patternlab:build', reload)
}
];
watchers.forEach(watcher => {
console.log('\n' + chalk.bold('Watching ' + watcher.name + ':'));
watcher.paths.forEach(p => console.log(' ' + p));
gulp.watch(watcher.paths, watcher.config, watcher.tasks);
});
console.log();
}
gulp.task('patternlab:connect', gulp.series(function (done) {
browserSync.init({
server: {
baseDir: normalizePath(paths().public.root)
},
startPath: '/?p=pages-basic-page-intro',
snippetOptions: {
// Ignore all HTML files within the templates folder
blacklist: ['/index.html', '/', '/?*']
},
notify: {
styles: [
'display: none',
'padding: 15px',
'font-family: sans-serif',
'position: fixed',
'font-size: 1em',
'z-index: 9999',
'bottom: 0px',
'right: 0px',
'border-top-left-radius: 5px',
'background-color: #1B2032',
'opacity: 0.4',
'margin: 0',
'color: white',
'text-align: center'
]
}
}, function () {
done();
});
}));
/******************************************************
* COMPOUND TASKS
******************************************************/
gulp.task('default', gulp.series('patternlab:build'));
gulp.task('patternlab:watch', gulp.series('patternlab:build', watch));
gulp.task('patternlab:serve', gulp.series('patternlab:build', 'patternlab:connect', watch));