forked from ampproject/amp.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
143 lines (124 loc) · 4 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
const autoprefixer = require('gulp-autoprefixer');
const exec = require('child_process').exec;
const plumber = require('gulp-plumber');
const gulp = require('gulp');
const sass = require('gulp-sass');
const svgSprite = require('gulp-svg-sprite');
const swBuild = require('workbox-build');
const fs = require('fs');
const Path = {
CSS_SOURCES: './assets/sass/**/*.scss',
CSS_OUT_DIR: './assets/css/'
};
// build example snippets that are used as sample embeds in docs
gulp.task('build-examples', function() {
const expath = require('path');
const abe = require('amp-by-example');
const config = {
src: expath.join(__dirname, 'examples/src'), // root folder w examples
destRoot: expath.join(__dirname, 'build'), // target dir for generated embeds
destDir: '/examples', // optional sub dir
host: 'https://ampproject-b5f4c.firebaseapp.com' // where embeds are to be served
}
abe.generatePreview(config);
gulp.src('./examples/src/images/*')
.pipe(gulp.dest('build/examples/images/'));
gulp.src('./examples/src/videos/*')
.pipe(gulp.dest('build/examples/videos/'));
});
gulp.task('import-docs', function (cb) {
exec('cd ./scripts && ./import_docs.js', function (err, stdout, stderr) {
if (err instanceof Error) {
cb(err);
}
//console.log(stdout);
cb();
});
});
gulp.task('optimize-images', function (cb) {
return gulp.src('./assets/img/symbols/*.svg')
.pipe(svgSprite({
mode: {
css: {
sprite: "../sprite.svg",
bust: false,
render: {
scss: {
template: "./assets/img/symbols/template.scss",
dest: "../../sass/_sprite_generated.scss"
}
}
}
}
}))
.pipe(gulp.dest('./assets/img/'));
});
gulp.task('update-blog-links', function (cb) {
exec('cd ./scripts && ./update_blog_links.js', function (err, stdout, stderr) {
if (err instanceof Error) {
cb(err);
}
cb();
});
});
gulp.task('update-tweets', function (cb) {
exec('cd ./scripts && ./update_tweets.js', function (err, stdout, stderr) {
if (err instanceof Error) {
cb(err);
}
cb();
});
});
gulp.task('update-platforms-page', ['import-docs'], function (cb) {
return exec('cd ./scripts && ./update_platforms_page.js', function (err, stdout, stderr) {
if (err instanceof Error) {
cb(err);
}
cb();
});
});
gulp.task('generate-asset-manifest', function (cb) {
swBuild.getFileManifestEntries({
globDirectory: './assets',
staticFileGlobs: [
'img/*.{svg,png,jpg}',
'img/nav/*.{svg,png,jpg}',
'img/footer/*.{svg,png,jpg}'
]
}).then(entries => {
// Add "static" to the path
entries.forEach(entry => {
entry.url = '/static' + entry.url;
});
fs.readFile('./pwa/service-worker.js', 'utf8', (err, data) => {
if (err) throw err;
// Inline precache manifest directly into the Service Worker
data = data.replace(/\/\* START_PRECACHE_MANIFEST \*\/.*\/\* END_PRECACHE_MANIFEST \*\//, "/* START_PRECACHE_MANIFEST */" + JSON.stringify(entries) + "/* END_PRECACHE_MANIFEST */");
fs.writeFile('./pwa/service-worker.js', data, (err) => {
if (err) throw err;
cb();
});
});
});
});
gulp.task('sass', function() {
return gulp.src(Path.CSS_SOURCES)
.pipe(plumber())
.pipe(sass({
outputStyle: 'compressed'
}).on('error', sass.logError))
.pipe(autoprefixer({
browsers: ['> 10%']
}))
.pipe(gulp.dest(Path.CSS_OUT_DIR));
});
gulp.task('watch', function() {
gulp.watch([Path.CSS_SOURCES], ['sass']);
gulp.watch([
'./assets\/img\/*.{svg,png,jpg}',
'./assets\/img\/nav/*.{svg,png,jpg}',
'./assets\/img\/footer/*.{svg,png,jpg}'
], ['generate-asset-manifest']);
});
gulp.task('build', [ 'update-blog-links', 'update-tweets', 'import-docs', 'update-platforms-page', 'optimize-images', 'sass', 'build-examples', 'generate-asset-manifest' ]);
gulp.task('default', [ 'update-platforms-page', 'sass', 'generate-asset-manifest', 'watch' ]);