107 lines
3.5 KiB
JavaScript
107 lines
3.5 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
const { spawnSync } = require('child_process');
|
|
|
|
const root = path.join(__dirname, '..');
|
|
const entry = path.join(root, 'src', '.generated-tailwind.css');
|
|
const componentsFile = path.join(root, 'src', 'components.css');
|
|
const tailwindSource = path.join(root, 'src', 'tailwind.css');
|
|
const criticalFile = path.join(root, 'assets', 'critical.css');
|
|
const output = path.join(root, 'src', '.tailwind.min.css');
|
|
const snippetFile = path.join(root, 'snippets', 'tailwind-css.liquid');
|
|
const publicCss = path.join(root, 'assets', 'tailwind.css');
|
|
const cli = path.join(root, 'node_modules', 'tailwindcss', 'lib', 'cli.js');
|
|
|
|
const stylesheetPattern = /\{%-?\s*stylesheet\s*-?%\}[\t ]*\r?\n?([\s\S]*?)\{%-?\s*endstylesheet\s*-?%\}[\t ]*\r?\n?/g;
|
|
|
|
function walk(dir, files) {
|
|
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
if (entry.name === 'node_modules' || entry.name === '.git') continue;
|
|
const full = path.join(dir, entry.name);
|
|
if (entry.isDirectory()) walk(full, files);
|
|
else if (entry.name.endsWith('.liquid')) files.push(full);
|
|
}
|
|
return files;
|
|
}
|
|
|
|
function extractStylesheets() {
|
|
const files = walk(root, []);
|
|
const chunks = [];
|
|
|
|
for (const file of files) {
|
|
const text = fs.readFileSync(file, 'utf8');
|
|
if (!text.includes('stylesheet')) continue;
|
|
|
|
const parts = [];
|
|
const next = text.replace(stylesheetPattern, (_, css) => {
|
|
parts.push(css.trim());
|
|
return '\n';
|
|
});
|
|
|
|
if (!parts.length) continue;
|
|
|
|
const relative = path.relative(root, file).replace(/\\/g, '/');
|
|
chunks.push(`/* ${relative} */\n${parts.join('\n\n')}`);
|
|
fs.writeFileSync(file, next.replace(/\n{3,}/g, '\n\n'));
|
|
}
|
|
|
|
if (!chunks.length) return false;
|
|
|
|
const banner = '/* Component CSS compiled with Tailwind CSS v3. Moved out of stylesheet tags. */\n\n';
|
|
fs.writeFileSync(componentsFile, banner + chunks.join('\n\n') + '\n');
|
|
return true;
|
|
}
|
|
|
|
function assemble() {
|
|
const critical = fs.readFileSync(criticalFile, 'utf8').trim();
|
|
const tailwind = fs.readFileSync(tailwindSource, 'utf8').trim();
|
|
const components = fs.existsSync(componentsFile) ? fs.readFileSync(componentsFile, 'utf8').trim() : '';
|
|
|
|
fs.writeFileSync(
|
|
entry,
|
|
`${critical}\n\n${tailwind}\n\n${components}\n`
|
|
);
|
|
}
|
|
|
|
function writeSnippet() {
|
|
const css = fs.readFileSync(output, 'utf8').trim();
|
|
if (css.includes('{{') || css.includes('{%') || css.includes('</style>')) {
|
|
console.error('Minified CSS contains Liquid or a closing style tag, so it cannot be printed inline.');
|
|
process.exit(1);
|
|
}
|
|
|
|
const snippet = `{% doc %}
|
|
Prints the minified Tailwind CSS v3 bundle in the document head.
|
|
Generated by scripts/build-css.js. Do not edit by hand.
|
|
{% enddoc %}
|
|
<style>${css}</style>
|
|
`;
|
|
fs.writeFileSync(snippetFile, snippet);
|
|
if (fs.existsSync(publicCss)) fs.unlinkSync(publicCss);
|
|
}
|
|
|
|
function compile() {
|
|
if (!fs.existsSync(cli)) {
|
|
console.error('Missing tailwindcss. Run npm install in the theme folder.');
|
|
process.exit(1);
|
|
}
|
|
|
|
const watch = process.argv.includes('--watch');
|
|
const args = [cli, '-i', entry, '-o', output, '--minify'];
|
|
if (watch) args.push('--watch');
|
|
|
|
if (watch) {
|
|
fs.watchFile(output, { interval: 400 }, () => {
|
|
if (fs.existsSync(output)) writeSnippet();
|
|
});
|
|
}
|
|
|
|
const result = spawnSync(process.execPath, args, { cwd: root, stdio: 'inherit' });
|
|
if (!watch && result.status === 0) writeSnippet();
|
|
if (result.status !== 0) process.exit(result.status || 1);
|
|
}
|
|
|
|
extractStylesheets();
|
|
assemble();
|
|
compile();
|