forked from victrme/Bonjourr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
161 lines (135 loc) · 4.08 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
const { series, parallel, src, dest, watch } = require('gulp'),
concat = require('gulp-concat'),
terser = require('gulp-terser'),
htmlmin = require('gulp-htmlmin'),
csso = require('gulp-csso'),
rename = require('gulp-rename'),
replace = require('gulp-replace'),
sass = require('gulp-sass')(require('sass'))
function html(platform, prod) {
//
// Index & settings minified
// Multiple scripts tags => only main.js
//
return () => {
const findScriptTags = /<script[\s\S]*?>[\s\S]*?<\/script>/gi
const stream = src('*.html')
if (platform === 'online') {
stream.pipe(replace(`<!-- manifest -->`, `<link rel="manifest" href="manifest.webmanifest">`))
}
if (prod) {
stream.pipe(htmlmin({ collapseWhitespace: true, removeComments: true }))
}
return stream
.pipe(
replace(findScriptTags, (match) => (match.includes('script.js') ? match.replace('script.js', 'main.js') : ''))
)
.pipe(dest(`release/${platform}`))
}
}
function scripts(platform, prod) {
//
// All scripts except background
// Online: replaces chrome.storage with homemade storage
// Chrome & Firefox build: just minify
//
return () => {
const stream = src([
'src/scripts/lang.js',
'src/scripts/utils.js',
'src/scripts/script.js',
'src/scripts/settings.js',
]).pipe(concat('main.js'))
if (platform === 'online') {
stream
.pipe(replace('chrome.storage.', 'lsOnlineStorage.'))
.pipe(replace('sync.clear(', 'clear('))
.pipe(replace('sync.get(', 'get(false, '))
.pipe(replace('local.get(', 'get(true, '))
.pipe(replace('sync.set(', 'set('))
.pipe(replace('local.set(', 'setLocal('))
.pipe(replace('sync.remove(', 'remove(false, '))
.pipe(replace('local.remove(', 'remove(true, '))
}
if (prod) {
stream.pipe(terser())
}
stream.pipe(dest(`release/${platform}/src/scripts`))
return stream
}
}
function ressources(platform) {
return () => {
const assetPath = ['src/assets/**', '!src/assets/bonjourr.png']
if (platform !== 'online') assetPath.push('!src/assets/screenshots/**')
return src(assetPath).pipe(dest(`release/${platform}/src/assets`))
}
}
function worker(platform) {
return () => {
const file = {
origin: `src/scripts/${platform === 'online' ? 'service-worker.js' : 'background.js'}`,
destination: platform === 'online' ? `release/${platform}` : `release/${platform}/src/scripts/`,
}
return src(file.origin).pipe(dest(file.destination))
}
}
function manifest(platform) {
return () => {
return platform === 'online'
? src(`src/manifests/manifest.webmanifest`).pipe(dest(`release/${platform}`))
: src(`src/manifests/${platform}.json`)
.pipe(rename('manifest.json'))
.pipe(dest(`release/${platform}`))
}
}
function styles(platform) {
return () =>
src('src/styles/scss/style.scss')
.pipe(sass.sync().on('error', sass.logError))
.pipe(csso())
.pipe(dest(`release/${platform}/src/styles/`))
}
function addBackground(platform) {
return () => src('src/scripts/background.js').pipe(dest(`release/${platform}/src/scripts`))
}
function locales(platform) {
return () => src('_locales/**').pipe(dest(`release/${platform}/_locales/`))
}
//
// Tasks
//
// Watches style map to make sure everything is compiled
const filesToWatch = ['*.html', './src/scripts/*.js', './src/styles/scss/*.scss', './src/manifests/*.json']
// prettier-ignore
const taskOnline = (prod) => [
html('online', prod),
styles('online'),
worker('online'),
manifest('online'),
scripts('online', prod),
ressources('online', false),
]
const taskExtension = (from, prod) => [
scripts(from, prod),
html(from, prod),
worker(from),
styles(from),
locales(from),
manifest(from),
ressources(from),
addBackground(from),
]
//
// All Exports
//
exports.online = async function () {
watch(filesToWatch, series(parallel(...taskOnline())))
}
exports.chrome = async function () {
watch(filesToWatch, series(parallel(...taskExtension('chrome'))))
}
exports.firefox = async function () {
watch(filesToWatch, series(parallel(...taskExtension('firefox'))))
}
exports.build = parallel(...taskOnline(true), ...taskExtension('firefox', true), ...taskExtension('chrome', true))