forked from jellyfin/jellyfin-tizen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.babel.js
135 lines (114 loc) · 4.01 KB
/
gulpfile.babel.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
import fs from 'fs';
import gulp from 'gulp';
import gulpif from 'gulp-if';
import { deleteAsync as del } from 'del';
import dom from 'gulp-dom';
import path from 'path';
import scan from 'gulp-scan';
// Allow overriding of jellyfin-web directory
let WEB_DIR = process.env.JELLYFIN_WEB_DIR || 'node_modules/jellyfin-web/dist';
WEB_DIR = path.resolve(WEB_DIR);
console.info('Using jellyfin-web from', WEB_DIR);
const DISCARD_UNUSED_FONTS = !!process.env.DISCARD_UNUSED_FONTS;
const paths = {
assets: {
src: [
WEB_DIR + '/**/*',
'!' + WEB_DIR + '/index.html'
],
dest: 'www/'
},
index: {
src: WEB_DIR + '/index.html',
dest: 'www/'
}
};
// Clean the www directory
function clean() {
return del([
'www'
]);
}
// Search for used fonts and add them to assets
function searchFonts() {
if (!DISCARD_UNUSED_FONTS) return Promise.resolve('skipped');
const assets = paths.assets.src;
assets.push('!' + WEB_DIR + '/*.woff2');
return gulp.src(WEB_DIR + '/main*.js')
.pipe(scan({
term: /[a-z0-9._-]*\.woff2/gi,
fn: function (match) {
const font = WEB_DIR + '/' + match;
if (!assets.includes(font) && fs.existsSync(font)) {
console.debug(`Found font ${match}`);
assets.push(font);
}
}
}));
}
// Copy unmodified assets
function copy() {
return gulp.src(paths.assets.src, { encoding: false })
.pipe(gulp.dest(paths.assets.dest));
}
// Add required tags to index.html
function modifyIndex() {
return gulp.src(paths.index.src)
.pipe(dom(function() {
// inject CSP meta tag
const meta = this.createElement('meta');
meta.setAttribute('http-equiv', 'Content-Security-Policy');
meta.setAttribute('content', 'default-src * \'self\' \'unsafe-inline\' \'unsafe-eval\' data: gap: file: filesystem: ws: wss:;');
this.head.appendChild(meta);
// Search for injected main.bundle
let apploader = this.querySelector('script[src^=main]');
if (apploader) {
console.debug('Found injected main.bundle');
apploader.setAttribute('defer', '');
} else {
// Search for injected apploader
apploader = this.body.querySelector('script[src*="apploader"]');
if (apploader) {
console.debug('Found injected apploader');
apploader.setAttribute('defer', '');
} else {
console.debug('Inject apploader');
// inject apploader.js
apploader = this.createElement('script');
apploader.setAttribute('src', 'scripts/apploader.js');
apploader.setAttribute('defer', '');
this.body.appendChild(apploader);
}
}
const injectTarget = apploader.parentNode;
// inject webapis.js
const webapis = this.createElement('script');
webapis.setAttribute('src', '$WEBAPIS/webapis/webapis.js');
injectTarget.insertBefore(webapis, apploader);
// inject appMode script
const appMode = this.createElement('script');
appMode.text = 'window.appMode=\'cordova\';';
injectTarget.insertBefore(appMode, apploader);
// inject tizen.js
const tizen = this.createElement('script');
tizen.setAttribute('src', '../tizen.js');
tizen.setAttribute('defer', '');
injectTarget.insertBefore(tizen, apploader);
return this;
}))
.pipe(gulp.dest(paths.index.dest))
}
// Default build task
const build = gulp.series(
clean,
searchFonts,
gulp.parallel(copy, modifyIndex)
);
// Export tasks so they can be run individually
export {
clean,
copy,
modifyIndex
};
// Export default task
export default build;