2020-02-28 10:31:13 -08:00
|
|
|
const glob = require('./lib/glob');
|
2020-03-08 14:01:00 -07:00
|
|
|
const { ROOT, readFile, resolve } = require('./resolve');
|
2020-02-28 10:27:52 -08:00
|
|
|
const actions = require('./actions');
|
|
|
|
const File = require('./file');
|
|
|
|
const Promise = require('bluebird');
|
|
|
|
const { minify } = require('terser');
|
2020-03-08 14:01:00 -07:00
|
|
|
const rollup = require('./rollup');
|
2020-02-28 10:27:52 -08:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2020-03-08 14:01:00 -07:00
|
|
|
const files = await Promise.map(glob('js/*.{js,jsx}', { cwd: ROOT, nodir: true }), async (filepath) => {
|
2020-02-28 10:27:52 -08:00
|
|
|
const f = new ClientScript(filepath);
|
2020-03-08 14:01:00 -07:00
|
|
|
if (f.globalScript) return false;
|
|
|
|
|
2020-02-28 10:27:52 -08:00
|
|
|
await f.load(prod);
|
2020-03-08 14:01:00 -07:00
|
|
|
|
2020-02-28 10:27:52 -08:00
|
|
|
return f;
|
|
|
|
}).filter(Boolean);
|
|
|
|
|
|
|
|
const tasks = files.map((f) => f.tasks()).flat();
|
|
|
|
|
|
|
|
tasks.push(...globalScript.tasks());
|
|
|
|
|
|
|
|
return tasks;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class ClientScript extends File {
|
|
|
|
|
2020-03-08 14:01:00 -07:00
|
|
|
_basename (file) {
|
|
|
|
super._basename(file);
|
|
|
|
|
|
|
|
this.globalScript = false;
|
|
|
|
if (file.name[0] === '_') {
|
|
|
|
this.globalScript = true;
|
|
|
|
file.name = file.name.slice(1);
|
|
|
|
file.base = file.base.slice(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.rollup = false;
|
|
|
|
if (file.name[0] === '$') {
|
|
|
|
this.rollup = true;
|
|
|
|
file.name = file.name.slice(1);
|
|
|
|
file.base = file.base.slice(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-28 10:27:52 -08:00
|
|
|
_dir (dir) {
|
|
|
|
dir = dir.split('/');
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
async load (prod) {
|
2020-03-08 14:01:00 -07:00
|
|
|
if (this.rollup) {
|
|
|
|
this.content = await rollup(resolve(this.input), prod);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-28 10:27:52 -08:00
|
|
|
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,
|
|
|
|
} ];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|