refactor: reformat code for improved readability and consistent style

This commit is contained in:
2026-10-31 01:24:04 +01:00
parent 000baf912d
commit 53945a56df
14 changed files with 477 additions and 97 deletions
+55 -7
View File
@@ -1,12 +1,60 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { mkdtemp, mkdir, readFile, readdir, writeFile } from 'node:fs/promises';
import {mkdtemp, mkdir, readFile, readdir, writeFile} from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { build } from '../dist/index.js';
import {build} from '../dist/index.js';
async function fixture(files) { const root = await mkdtemp(path.join(os.tmpdir(), 'iceberg-')); for (const [name, content] of Object.entries(files)) { const file = path.join(root, name); await mkdir(path.dirname(file), { recursive: true }); await writeFile(file, content); } return root; }
async function inRoot(root, fn) { const previous = process.cwd(); process.chdir(root); try { return await fn(); } finally { process.chdir(previous); } }
test('builds JavaScript and CSS and rewrites HTML', async () => { const root = await fixture({ 'index.html': '<!doctype html><head><title>Keep me</title></head><body><script type="module" src="/src/main.js"></script></body>', 'src/main.js': "import './style.css'; document.body.dataset.ready = 'yes';", 'src/style.css': '.unused { color: red } body { color: blue; }' }); await inRoot(root, () => build()); const html = await readFile(path.join(root, 'dist/index.html'), 'utf8'); assert.match(html, /<title>Keep me<\/title>/); assert.match(html, /assets\/main-[^"/]+\.js/); assert.match(html, /assets\/index-[^"/]+\.css/); const assets = await readdir(path.join(root, 'dist/assets')); const css = await readFile(path.join(root, 'dist/assets', assets.find(file => file.endsWith('.css'))), 'utf8'); assert.match(css, /body/); });
test('supports TypeScript and custom output configuration', async () => { const root = await fixture({ 'index.html': '<script type="module" src="/src/main.ts"></script>', 'src/main.ts': 'const answer: number = 42; console.log(answer);', 'iceberg.config.js': "export default { build: { outDir: 'build', minify: false, sourcemap: true } };" }); await inRoot(root, () => build()); const files = await readdir(path.join(root, 'build/assets')); assert.ok(files.some(file => file.endsWith('.js'))); assert.ok(files.some(file => file.endsWith('.js.map'))); const js = await readFile(path.join(root, 'build/assets', files.find(file => file.endsWith('.js'))), 'utf8'); assert.match(js, /42/); assert.doesNotMatch(js, /: number/); });
test('rejects an HTML entry without a module script', async () => { const root = await fixture({ 'index.html': '<h1>No script</h1>' }); await assert.rejects(() => inRoot(root, () => build()), /module script/); });
async function fixture(files) {
const root = await mkdtemp(path.join(os.tmpdir(), 'iceberg-'));
for (const [name, content] of Object.entries(files)) {
const file = path.join(root, name);
await mkdir(path.dirname(file), {recursive: true});
await writeFile(file, content);
}
return root;
}
async function inRoot(root, fn) {
const previous = process.cwd();
process.chdir(root);
try {
return await fn();
} finally {
process.chdir(previous);
}
}
test('builds JavaScript and CSS and rewrites HTML', async () => {
const root = await fixture({
'index.html': '<!doctype html><head><title>Keep me</title></head><body><script type="module" src="/src/main.js"></script></body>',
'src/main.js': "import './style.css'; document.body.dataset.ready = 'yes';",
'src/style.css': '.unused { color: red } body { color: blue; }'
});
await inRoot(root, () => build());
const html = await readFile(path.join(root, 'dist/index.html'), 'utf8');
assert.match(html, /<title>Keep me<\/title>/);
assert.match(html, /assets\/main-[^"/]+\.js/);
assert.match(html, /assets\/index-[^"/]+\.css/);
const assets = await readdir(path.join(root, 'dist/assets'));
const css = await readFile(path.join(root, 'dist/assets', assets.find(file => file.endsWith('.css'))), 'utf8');
assert.match(css, /body/);
});
test('supports TypeScript and custom output configuration', async () => {
const root = await fixture({
'index.html': '<script type="module" src="/src/main.ts"></script>',
'src/main.ts': 'const answer: number = 42; console.log(answer);',
'iceberg.config.js': "export default { build: { outDir: 'build', minify: false, sourcemap: true } };"
});
await inRoot(root, () => build());
const files = await readdir(path.join(root, 'build/assets'));
assert.ok(files.some(file => file.endsWith('.js')));
assert.ok(files.some(file => file.endsWith('.js.map')));
const js = await readFile(path.join(root, 'build/assets', files.find(file => file.endsWith('.js'))), 'utf8');
assert.match(js, /42/);
assert.doesNotMatch(js, /: number/);
});
test('rejects an HTML entry without a module script', async () => {
const root = await fixture({'index.html': '<h1>No script</h1>'});
await assert.rejects(() => inRoot(root, () => build()), /module script/);
});