lila/ui/gulpfile.js

87 lines
2.5 KiB
JavaScript
Raw Normal View History

2019-02-12 21:07:04 -07:00
const gulp = require('gulp');
const sass = require('gulp-sass');
const sourcemaps = require('gulp-sourcemaps');
const autoprefixer = require('gulp-autoprefixer');
2019-02-12 21:40:03 -07:00
const sassInheritance = require('gulp-sass-inheritance')
2019-02-12 21:07:04 -07:00
const rename = require('gulp-rename');
2019-02-12 21:40:03 -07:00
const cached = require('gulp-cached');
const gulpif = require('gulp-if');
const filter = require('gulp-filter');
2019-02-12 21:07:04 -07:00
const fs = require('fs');
2019-02-13 01:52:52 -07:00
const glob = require('glob');
2019-02-12 21:07:04 -07:00
const themes = ['light', 'dark', 'transp'];
const sassOptions = {
errLogToConsole: true,
outputStyle: 'expanded'
};
const destination = () => gulp.dest('../public/css/');
const sourcesGlob = './*/css/**/*.scss';
const buildsGlob = './*/css/build/*.scss';
2019-02-12 21:40:03 -07:00
const build = () => gulp.src(sourcesGlob)
2019-02-13 01:52:52 -07:00
//filter out unchanged scss files, only works when watching
2019-02-12 21:40:03 -07:00
.pipe(gulpif(global.isWatching, cached('sass')))
2019-02-13 01:52:52 -07:00
//find files that depend on the files that have changed
2019-02-12 21:40:03 -07:00
.pipe(sassInheritance({dir: '.',debug: false}))
2019-02-13 01:52:52 -07:00
//filter out internal imports (folders and files starting with "_" )
2019-02-12 21:40:03 -07:00
.pipe(filter(file => !/\/_/.test(file.path) || !/^_/.test(file.relative)))
2019-02-12 21:07:04 -07:00
.pipe(sourcemaps.init())
.pipe(sass(sassOptions).on('error', sass.logError))
.pipe(sourcemaps.write())
.pipe(renameAs('dev'))
.pipe(destination());
2019-02-12 21:40:03 -07:00
const setWatching = async () => { global.isWatching = true; };
2019-02-13 09:54:24 -07:00
const startWatching = () => gulp.watch(sourcesGlob, build);
2019-02-12 21:07:04 -07:00
gulp.task('css', gulp.series([
2019-02-13 01:52:52 -07:00
createThemedBuilds,
2019-02-12 21:40:03 -07:00
setWatching,
2019-02-12 21:07:04 -07:00
build,
2019-02-13 09:54:24 -07:00
startWatching
2019-02-12 21:07:04 -07:00
]));
2019-02-13 01:52:52 -07:00
gulp.task('css-dev', gulp.series([createThemedBuilds, build]));
2019-02-12 21:07:04 -07:00
2019-04-08 18:58:12 -06:00
gulp.task('css-prod', () => gulp
.src(sourcesGlob)
2019-04-08 18:58:12 -06:00
.pipe(sass({
...sassOptions,
...{ outputStyle: 'compressed' }
}).on('error', sass.logError))
2019-06-04 04:40:27 -06:00
.pipe(autoprefixer())
2019-04-08 18:58:12 -06:00
.pipe(renameAs('min'))
.pipe(destination())
);
2019-02-12 21:07:04 -07:00
2019-04-08 18:58:12 -06:00
function renameAs(ext) {
return rename(path => {
path.dirname = '';
path.basename = `${path.basename}.${ext}`;
return path;
});
}
2019-02-12 21:07:04 -07:00
2019-04-08 18:58:12 -06:00
function createThemedBuilds(cb) {
glob(buildsGlob, {}, (err, files) => {
files
.filter(file => file.match(/\/_.+\.scss$/))
.forEach(file => {
themes.forEach(theme => {
const themed = file.replace(/\/_(.+)\.scss$/, `/$1.${theme}.scss`);
if (!fs.existsSync(themed)) {
const buildName = file.replace(/.+\/_(.+)\.scss$/, '$1');
const code = `@import '../../../common/css/theme/${theme}';\n@import '${buildName}';\n`;
console.log(`Create missing SCSS themed build: ${themed}`);
fs.writeFileSync(themed, code);
}
2019-02-13 01:52:52 -07:00
});
2019-04-08 18:58:12 -06:00
});
cb();
});
}