-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathbuild.js
88 lines (85 loc) · 3.03 KB
/
build.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
'use strict'
const autoprefixer = require('autoprefixer')
const browserify = require('browserify')
const buffer = require('vinyl-buffer')
const concat = require('gulp-concat')
const cssnano = require('cssnano')
const fs = require('fs-extra')
const imagemin = require('gulp-imagemin')
const { obj: map } = require('through2')
const merge = require('merge-stream')
const ospath = require('path')
const path = ospath.posix
const postcss = require('gulp-postcss')
const postcssCalc = require('postcss-calc')
const postcssImport = require('postcss-import')
const postcssUrl = require('postcss-url')
const postcssVar = require('postcss-custom-properties')
const uglify = require('gulp-uglify')
const vfs = require('vinyl-fs')
module.exports = (src, dest, preview) => () => {
const opts = { base: src, cwd: src }
const sourcemaps = preview || process.env.SOURCEMAPS === 'true'
const postcssPlugins = [
postcssImport,
postcssUrl([
{
filter: '**/~typeface-*/files/*',
url: (asset) => {
const relpath = asset.pathname.substr(1)
const abspath = require.resolve(relpath)
const basename = ospath.basename(abspath)
const destpath = ospath.join(dest, 'font', basename)
if (!fs.pathExistsSync(destpath)) fs.copySync(abspath, destpath)
return path.join('..', 'font', basename)
},
},
]),
postcssVar({ preserve: preview ? 'preserve-computed' : false }),
preview ? postcssCalc : () => {},
autoprefixer,
preview ? () => {} : cssnano({ preset: 'default' }),
]
return merge(
vfs
.src('js/+([0-9])-*.js', { ...opts, sourcemaps })
.pipe(uglify())
.pipe(concat('js/site.js')),
vfs
.src('js/vendor/*.js', { ...opts, read: false })
.pipe(
// see https://gulpjs.org/recipes/browserify-multiple-destination.html
map((file, enc, next) => {
if (file.relative.endsWith('.bundle.js')) {
file.contents = browserify(file.relative, { basedir: src, detectGlobals: false })
.plugin('browser-pack-flat/plugin')
.bundle()
file.path = file.path.slice(0, file.path.length - 10) + '.js'
next(null, file)
} else {
fs.readFile(file.path, 'UTF-8').then((contents) => {
file.contents = Buffer.from(contents)
next(null, file)
})
}
})
)
.pipe(buffer())
.pipe(uglify()),
vfs.src('css/site.css', { ...opts, sourcemaps }).pipe(postcss(postcssPlugins)),
vfs.src('font/*.{ttf,woff*(2)}', opts),
vfs
.src('img/**/*.{gif,ico,jpg,png,svg}', opts)
.pipe(
imagemin([
imagemin.gifsicle(),
imagemin.mozjpeg(),
imagemin.optipng(),
imagemin.svgo({ plugins: [{ removeViewBox: false }] }),
].reduce((accum, it) => it ? accum.concat(it) : accum, []))
),
vfs.src('helpers/*.js', opts),
vfs.src('layouts/*.hbs', opts),
vfs.src('partials/*.hbs', opts)
).pipe(vfs.dest(dest, { sourcemaps: sourcemaps && '.' }))
}