1
0
mirror of https://github.com/GenderDysphoria/GenderDysphoria.fyi.git synced 2025-11-26 04:52:43 +00:00

Site JS is now compiled inside of content. No more gulp streams.

This commit is contained in:
Jocelyn Badgley (Twipped)
2020-02-28 10:27:52 -08:00
parent 695637c3e7
commit 823f662743
13 changed files with 105 additions and 633 deletions

View File

@@ -12,6 +12,8 @@ const { resolve } = require('./resolve');
const favicon = require('./favicon');
const scss = require('./scss');
const svg = require('./svg');
const scripts = require('./scripts');
exports.everything = function (prod = false) {
const fn = async () => {
@@ -30,23 +32,20 @@ exports.everything = function (prod = false) {
const tasks = await Promise.all([
PublicFiles.tasks,
scss(prod),
scripts(prod),
svg(prod),
favicon(prod),
]);
async function crankTasks () {
if (!tasks.length) return;
const cache = new Cache({ prod });
await cache.load();
await evaluate(tasks.flat(), cache);
await cache.save();
}
await fs.writeFile(resolve('pages.json'), JSON.stringify(pages.map((p) => p.toJson()), null, 2));
await Promise.all([
fs.writeFile(resolve('pages.json'), JSON.stringify(pages.map((p) => p.toJson()), null, 2)),
pageWriter(pages, prod),
crankTasks(),
]);
await fs.ensureDir(resolve('dist'));
const cache = new Cache({ prod });
await cache.load();
await evaluate(tasks.flat(), cache);
await cache.save();
await pageWriter(pages, prod);
};
const ret = () => fn().catch((err) => { console.log(err.trace || err); throw err; });

View File

@@ -2,7 +2,7 @@
const path = require('path');
const ROOT = path.resolve(__dirname, '../..');
const fs = require('fs-extra');
const { is: _is, re } = require('../lib/util');
const { is: _is } = require('../lib/util');
function is (...args) {
const fn = _is(...args);
@@ -135,10 +135,8 @@ exports.readFile = function readFile (fpath) {
exports.resolve = function resolve (...args) {
args = args.filter(Boolean);
let fpath = args.shift();
const fpath = args.shift();
if (!fpath) return ROOT;
if (fpath[0] === '/') throw new Error('Did you mean to resolve this? ' + fpath);
// if (fpath[0] === '/') fpath = fpath.slice(1);
return path.resolve(ROOT, fpath, ...args);
};

75
gulp/content/scripts.js Normal file
View File

@@ -0,0 +1,75 @@
const glob = require('../lib/glob');
const { ROOT, readFile } = require('./resolve');
const actions = require('./actions');
const File = require('./file');
const Promise = require('bluebird');
const { minify } = require('terser');
module.exports = exports = async function scripts (prod) {
const globalFiles = await glob('js/_*.js', { cwd: ROOT, nodir: true });
globalFiles.unshift(
require.resolve('jquery'),
require.resolve('magnific-popup'),
require.resolve('popper.js/dist/umd/popper.js'),
require.resolve('bootstrap/js/dist/util.js'),
require.resolve('bootstrap/js/dist/dropdown.js'),
);
const globalScript = new ClientScript('js/global.js');
await globalScript.concat(globalFiles, prod);
const files = await Promise.map(glob('js/*.js', { cwd: ROOT, nodir: true }), async (filepath) => {
const f = new ClientScript(filepath);
if (f.preprocessed) return false;
await f.load(prod);
return f;
}).filter(Boolean);
const tasks = files.map((f) => f.tasks()).flat();
tasks.push(...globalScript.tasks());
return tasks;
};
class ClientScript extends File {
_dir (dir) {
dir = dir.split('/');
return dir;
}
async load (prod) {
let contents = (await readFile(this.input).catch(() => '')).toString('utf8');
if (prod) {
const { code, error } = minify(contents);
if (error) throw new Error(error);
contents = code;
}
this.content = contents;
}
async concat (files, prod) {
let contents = await Promise.map(files, readFile);
contents = contents.join('\n\n');
if (prod) {
const { code, error } = minify(contents);
if (error) throw new Error(error);
contents = code;
}
this.content = contents;
}
tasks () {
return [ {
input: this.input,
output: this.out,
content: this.content,
action: actions.write,
nocache: true,
} ];
}
}