Switch to npm published build tool.

This commit is contained in:
flash 2024-06-11 00:24:12 +00:00
parent ad43f4b47d
commit 7812fb5f2e
6 changed files with 296 additions and 577 deletions

237
build.js
View file

@ -1,215 +1,38 @@
// IMPORTS
const assproc = require('@railcomm/assproc');
const { join: pathJoin } = require('path');
const fs = require('fs');
const swc = require('@swc/core');
const path = require('path');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const postcss = require('postcss');
const htmlminify = require('html-minifier-terser').minify;
const childProcess = require('child_process');
const utils = require('./assets/utils.js');
const assproc = require('./assets/assproc.js');
// CONFIG
const rootDir = __dirname;
const srcDir = path.join(rootDir, 'assets');
const srcCurrentInfo = path.join(srcDir, 'current.json');
const pubDir = path.join(rootDir, 'public');
const pubAssetsDir = path.join(pubDir, 'assets');
const isDebugBuild = fs.existsSync(path.join(rootDir, '.debug'));
const buildTasks = {
js: [
{ source: 'makai.js', target: '/assets', name: 'makai.{hash}.js', },
],
css: [
{ source: 'errors.css', target: '/', name: 'errors.css', },
{ source: 'makai.css', target: '/assets', name: 'makai.{hash}.css', },
],
twig: [
{ source: 'errors/401', target: '/', name: 'error-401.html', },
{ source: 'errors/403', target: '/', name: 'error-403.html', },
{ source: 'errors/404', target: '/', name: 'error-404.html', },
{ source: 'errors/500', target: '/', name: 'error-500.html', },
{ source: 'errors/503', target: '/', name: 'error-503.html', },
],
};
// PREP
const postcssPlugins = [ require('autoprefixer')({ remove: false }) ];
if(!isDebugBuild)
postcssPlugins.push(require('cssnano')({
preset: [
'cssnano-preset-default',
{
minifyGradients: false,
reduceIdents: false,
zindex: true,
}
],
}));
const swcJscOptions = {
target: 'es2020',
loose: false,
externalHelpers: false,
keepClassNames: true,
preserveAllComments: false,
transform: {},
parser: {
syntax: 'ecmascript',
jsx: true,
dynamicImport: false,
privateMethod: false,
functionBind: false,
exportDefaultFrom: false,
exportNamespaceFrom: false,
decorators: false,
decoratorsBeforeExport: false,
topLevelAwait: true,
importMeta: false,
},
transform: {
react: {
runtime: 'classic',
pragma: '$er',
},
},
};
const htmlMinifyOptions = {
collapseBooleanAttributes: true,
collapseWhitespace: true,
conservativeCollapse: false,
decodeEntities: false,
quoteCharacter: '"',
removeAttributeQuotes: true,
removeComments: true,
removeEmptyAttributes: true,
removeOptionalTags: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
sortAttributes: true,
sortClassName: true,
};
// BUILD
(async () => {
const files = {};
const isDebug = fs.existsSync(pathJoin(__dirname, '.debug'));
console.log('Ensuring assets directory exists...');
fs.mkdirSync(pubAssetsDir, { recursive: true });
const env = {
root: __dirname,
source: pathJoin(__dirname, 'assets'),
public: pathJoin(__dirname, 'public'),
debug: isDebug,
swc: {
es: 'es2020',
},
};
const tasks = {
js: [
{ source: 'makai.js', target: '/assets', name: 'makai.{hash}.js', },
],
css: [
{ source: 'errors.css', target: '/', name: 'errors.css', },
{ source: 'makai.css', target: '/assets', name: 'makai.{hash}.css', },
],
twig: [
{ source: 'errors/401', target: '/', name: 'error-401.html', },
{ source: 'errors/403', target: '/', name: 'error-403.html', },
{ source: 'errors/404', target: '/', name: 'error-404.html', },
{ source: 'errors/500', target: '/', name: 'error-500.html', },
{ source: 'errors/503', target: '/', name: 'error-503.html', },
],
};
console.log();
console.log('JS assets');
for(const info of buildTasks.js) {
console.log(`=> Building ${info.source}...`);
const files = await assproc.process(env, tasks);
let origTarget = undefined;
if('es' in info) {
origTarget = swcJscOptions.target;
swcJscOptions.target = info.es;
}
const assprocOpts = {
prefix: '#',
entry: info.entry || 'main.js',
};
const swcOpts = {
filename: info.source,
sourceMaps: false,
isModule: false,
minify: !isDebugBuild,
jsc: swcJscOptions,
};
const pubName = await assproc.process(path.join(srcDir, info.source), assprocOpts)
.then(output => swc.transform(output, swcOpts))
.then(output => {
const name = utils.strtr(info.name, { hash: utils.shortHash(output.code) });
const pubName = path.join(info.target || '', name);
console.log(` Saving to ${pubName}...`);
fs.writeFileSync(path.join(pubDir, pubName), output.code);
return pubName;
});
if(origTarget !== undefined)
swcJscOptions.target = origTarget;
files[info.source] = pubName;
}
console.log();
console.log('CSS assets');
for(const info of buildTasks.css) {
console.log(`=> Building ${info.source}...`);
const sourcePath = path.join(srcDir, info.source);
const assprocOpts = {
prefix: '@',
entry: info.entry || 'main.css',
};
const postcssOpts = { from: sourcePath };
files[info.source] = await assproc.process(sourcePath, assprocOpts)
.then(output => postcss(postcssPlugins).process(output, postcssOpts)
.then(output => {
const name = utils.strtr(info.name, { hash: utils.shortHash(output.css) });
const pubName = path.join(info.target || '', name);
console.log(` Saving to ${pubName}...`);
fs.writeFileSync(path.join(pubDir, pubName), output.css);
return pubName;
}));
}
console.log();
console.log('Twig assets');
const renderCommand = path.join(rootDir, 'tools/render-tpl');
for(const info of buildTasks.twig) {
console.log(`=> Building ${info.source}...`);
const { stdout, stderr } = await exec(`${renderCommand} ${info.source}`);
let data = stdout;
if(data.trim() === '') {
console.error(stderr);
continue;
}
if(!isDebugBuild)
data = await htmlminify(data, htmlMinifyOptions);
const name = utils.strtr(info.name, { hash: utils.shortHash(data) });
const pubName = path.join(info.target || '', name);
const fullPath = path.join(pubDir, pubName);
const dirPath = path.dirname(fullPath);
if(!fs.existsSync(dirPath))
fs.mkdirSync(dirPath, { recursive: true });
fs.writeFileSync(fullPath, data);
files[info.source] = pubName;
}
console.log();
console.log('Writing assets info...');
fs.writeFileSync(srcCurrentInfo, JSON.stringify(files));
console.log();
console.log('Cleaning up old builds...');
assproc.housekeep(pubAssetsDir);
fs.writeFileSync(pathJoin(__dirname, 'assets/current.json'), JSON.stringify(files));
})();

623
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,9 +1,5 @@
{
"dependencies": {
"@swc/core": "^1.5.24",
"autoprefixer": "^10.4.19",
"cssnano": "^6.1.2",
"html-minifier-terser": "^7.2.0",
"postcss": "^8.4.38"
"@railcomm/assproc": "^1.0.0"
}
}

View file

@ -1,4 +1,4 @@
#!/usr/bin/env php8.2
#!/usr/bin/env php
<?php
use Index\Data\Migration\FsDbMigrationRepo;

View file

@ -1,4 +1,4 @@
#!/usr/bin/env php8.2
#!/usr/bin/env php
<?php
use Index\Data\Migration\FsDbMigrationRepo;

View file

@ -1,8 +1,7 @@
#!/usr/bin/env php8.2
#!/usr/bin/env php
<?php
require_once __DIR__ . '/../makai.php';
$makai->startTemplating();
$templating = $makai->getTemplating();
$path = implode(' ', array_slice($argv, 1));