jellyfin-web/gulpfile.js

202 lines
5.3 KiB
JavaScript
Raw Normal View History

'use strict';
2020-02-27 11:11:35 -07:00
2020-02-28 01:01:22 -07:00
const { src, dest, series, parallel, watch } = require('gulp');
2020-02-27 14:03:11 -07:00
const browserSync = require('browser-sync').create();
2020-02-27 11:11:35 -07:00
const del = require('del');
const babel = require('gulp-babel');
2020-02-27 14:03:11 -07:00
const concat = require('gulp-concat');
2020-02-27 11:11:35 -07:00
const terser = require('gulp-terser');
const htmlmin = require('gulp-htmlmin');
const imagemin = require('gulp-imagemin');
2020-02-27 11:11:35 -07:00
const sourcemaps = require('gulp-sourcemaps');
2020-02-28 01:01:22 -07:00
const mode = require('gulp-mode')({
2020-03-16 10:12:05 -07:00
modes: ["development", "production"],
2020-02-28 01:01:22 -07:00
default: "development",
verbose: false
2020-03-09 14:55:47 -07:00
});
2020-03-16 07:30:14 -07:00
const stream = require('webpack-stream');
2020-02-27 14:03:11 -07:00
const inject = require('gulp-inject');
const postcss = require('gulp-postcss');
2020-02-28 01:49:01 -07:00
const sass = require('gulp-sass');
2020-03-26 01:15:44 -07:00
const gulpif = require('gulp-if');
const lazypipe = require('lazypipe');
2020-02-27 14:03:11 -07:00
sass.compiler = require('node-sass');
2020-02-28 06:36:42 -07:00
2020-03-25 14:26:28 -07:00
let config;
2020-03-16 10:12:05 -07:00
if (mode.production()) {
config = require('./webpack.prod.js');
2020-02-28 06:36:42 -07:00
} else {
config = require('./webpack.dev.js');
2020-02-28 06:36:42 -07:00
}
2020-03-26 01:15:44 -07:00
const options = {
javascript: {
query: ['src/**/*.js', '!src/bundle.js', '!src/standalone.js', '!src/scripts/apploader.js']
},
apploader: {
query: ['src/standalone.js', 'src/scripts/apploader.js']
},
css: {
query: ['src/**/*.css', 'src/**/*.scss']
},
html: {
query: ['src/**/*.html', '!src/index.html']
},
images: {
query: ['src/**/*.png', 'src/**/*.jpg', 'src/**/*.gif', 'src/**/*.svg']
},
copy: {
query: ['src/**/*.json', 'src/**/*.ico']
},
injectBundle: {
query: 'src/index.html'
}
};
2020-02-28 01:01:22 -07:00
function serve() {
2020-02-27 14:03:11 -07:00
browserSync.init({
server: {
baseDir: "./dist"
},
port: 8080
});
2020-02-28 01:01:22 -07:00
2020-03-26 01:15:44 -07:00
let events = ['add', 'change'];
watch(options.javascript.query).on('all', function (event, path) {
if (events.includes(event)) {
javascript(path);
}
});
watch(options.apploader.query, apploader(true));
2020-02-28 01:01:22 -07:00
watch('src/bundle.js', webpack);
2020-02-27 14:03:11 -07:00
2020-03-26 01:15:44 -07:00
watch(options.css.query).on('all', function (event, path) {
if (events.includes(event)) {
css(path);
}
});
watch(options.html.query).on('all', function (event, path) {
if (events.includes(event)) {
html(path);
}
});
watch(options.images.query).on('all', function (event, path) {
if (events.includes(event)) {
images(path);
}
});
watch(options.copy.query).on('all', function (event, path) {
if (events.includes(event)) {
copy(path);
}
});
watch(options.injectBundle.query, injectBundle);
2020-02-27 14:03:11 -07:00
}
2020-02-27 11:11:35 -07:00
function clean() {
return del(['dist/']);
2020-02-27 11:11:35 -07:00
}
2020-03-26 01:15:44 -07:00
let pipelineJavascript = lazypipe()
.pipe(function () {
return mode.development(sourcemaps.init({ loadMaps: true }));
})
.pipe(function () {
return babel({
2020-03-09 14:55:47 -07:00
presets: [
['@babel/preset-env']
]
2020-03-26 01:15:44 -07:00
});
})
.pipe(function () {
return terser({
2020-03-09 14:55:47 -07:00
keep_fnames: true,
mangle: false
2020-03-26 01:15:44 -07:00
});
})
.pipe(function () {
return mode.development(sourcemaps.write('.'));
});
function javascript(query) {
return src(typeof query !== 'function' ? query : options.javascript.query, { base: './src/' })
.pipe(pipelineJavascript())
2020-03-09 14:55:47 -07:00
.pipe(dest('dist/'))
2020-03-26 01:15:44 -07:00
.pipe(browserSync.stream());
}
function apploader(standalone) {
function task() {
return src(options.apploader.query, { base: './src/' })
.pipe(gulpif(standalone, concat('scripts/apploader.js')))
.pipe(pipelineJavascript())
.pipe(dest('dist/'))
.pipe(browserSync.stream());
};
task.displayName = 'apploader';
return task;
2020-02-27 11:11:35 -07:00
}
2020-02-27 14:03:11 -07:00
function webpack() {
2020-03-16 07:30:14 -07:00
return stream(config)
2020-03-09 14:55:47 -07:00
.pipe(dest('dist/'))
.pipe(browserSync.stream());
2020-02-27 14:03:11 -07:00
}
2020-03-26 01:15:44 -07:00
function css(query) {
return src(typeof query !== 'function' ? query : options.css.query, { base: './src/' })
2020-03-16 07:30:14 -07:00
.pipe(mode.development(sourcemaps.init({ loadMaps: true })))
2020-03-09 14:55:47 -07:00
.pipe(sass().on('error', sass.logError))
.pipe(postcss())
.pipe(mode.development(sourcemaps.write('.')))
.pipe(dest('dist/'))
.pipe(browserSync.stream());
2020-02-27 11:11:35 -07:00
}
2020-03-26 01:15:44 -07:00
function html(query) {
return src(typeof query !== 'function' ? query : options.html.query, { base: './src/' })
2020-03-16 10:12:05 -07:00
.pipe(mode.production(htmlmin({ collapseWhitespace: true })))
2020-03-09 14:55:47 -07:00
.pipe(dest('dist/'))
.pipe(browserSync.stream());
2020-02-27 11:11:35 -07:00
}
2020-03-26 01:15:44 -07:00
function images(query) {
return src(typeof query !== 'function' ? query : options.images.query, { base: './src/' })
2020-03-16 10:12:05 -07:00
.pipe(mode.production(imagemin()))
2020-03-09 14:55:47 -07:00
.pipe(dest('dist/'))
.pipe(browserSync.stream());
}
2020-03-26 01:15:44 -07:00
function copy(query) {
return src(typeof query !== 'function' ? query : options.copy.query, { base: './src/' })
2020-03-09 14:55:47 -07:00
.pipe(dest('dist/'))
.pipe(browserSync.stream());
}
2020-02-27 14:03:11 -07:00
function injectBundle() {
2020-03-26 01:15:44 -07:00
return src(options.injectBundle.query, { base: './src/' })
2020-03-09 14:55:47 -07:00
.pipe(inject(
2020-03-16 07:30:14 -07:00
src(['src/scripts/apploader.js'], { read: false }, { base: './src/' }), { relative: true }
2020-03-09 14:55:47 -07:00
))
.pipe(dest('dist/'))
.pipe(browserSync.stream());
2020-02-27 14:03:11 -07:00
}
2020-03-26 01:15:44 -07:00
function build(standalone) {
return series(clean, parallel(javascript, apploader(standalone), webpack, css, html, images, copy), injectBundle);
}
exports.default = build(false);
exports.standalone = build(true);
exports.serve = series(exports.standalone, serve);