85 lines
5.1 KiB
JavaScript
85 lines
5.1 KiB
JavaScript
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
import { tmpdir } from 'node:os';
|
|
import { join } from 'node:path';
|
|
import { pathToFileURL } from 'node:url';
|
|
import { versionSiteAssets } from '../scripts/lib/version-site-assets.mjs';
|
|
|
|
test('HTML entries, styles and transitive lazy modules share a build version', async t => {
|
|
const root = mkdtempSync(join(tmpdir(), 'make-version-'));
|
|
t.after(() => rmSync(root, { recursive: true, force: true }));
|
|
mkdirSync(join(root, 'js'));
|
|
writeFileSync(join(root, 'package.json'), '{"type":"module"}');
|
|
const put = (file, text) => writeFileSync(join(root, file), text);
|
|
put('js/entry.js', `import { value } from './middle.js'; export const get = async () => value + (await import('./late.js')).value;`);
|
|
put('js/middle.js', `export { value } from './budget.js';`);
|
|
put('js/late.js', `import { value } from './budget.js'; export { value };`);
|
|
put('js/budget.js', `export const value = 10;`);
|
|
put('js/vendor-test.js', `import * as THREE from 'three'; import { x } from '../vendor/x.js'; export const text = "import('./fake.js')"; console.log(THREE, x);`);
|
|
put('index.html', `<script src="/js/entry.js"></script><link rel="stylesheet" href='css/assembly.css'><link href="/css/style.css?v=old"><script src="/vendor/keep.js"></script>`);
|
|
const entry = pathToFileURL(join(root, 'js/entry.js')).href;
|
|
await versionSiteAssets(root, 'first', ['index.html']);
|
|
assert.equal(await (await import(entry + '?v=first')).get(), 20);
|
|
put('js/budget.js', `export const value = 40;`);
|
|
await versionSiteAssets(root, 'second', ['index.html']);
|
|
// First graph is still cached. A fresh entry must not reuse its old dependency.
|
|
assert.equal(await (await import(entry + '?v=second')).get(), 80);
|
|
for (const file of ['entry.js', 'middle.js', 'late.js']) {
|
|
const code = readFileSync(join(root, 'js', file), 'utf8');
|
|
assert.match(code, /\?v=second/);
|
|
assert.doesNotMatch(code, /v=first/);
|
|
}
|
|
const html = readFileSync(join(root, 'index.html'), 'utf8');
|
|
assert.match(html, /entry\.js\?v=second/);
|
|
assert.match(html, /assembly\.css\?v=second/);
|
|
assert.match(html, /style\.css\?v=second/);
|
|
assert.match(html, /src="\/vendor\/keep\.js"/);
|
|
const vendor = readFileSync(join(root, 'js/vendor-test.js'), 'utf8');
|
|
assert.match(vendor, /from "three"/);
|
|
assert.match(vendor, /from "\.\.\/vendor\/x.js"/);
|
|
assert.match(vendor, /import\('\.\/fake.js'\)/);
|
|
});
|
|
|
|
test('missing app dependencies fail the build before packaging', async t => {
|
|
const root = mkdtempSync(join(tmpdir(), 'make-version-missing-'));
|
|
t.after(() => rmSync(root, { recursive: true, force: true }));
|
|
mkdirSync(join(root, 'js'));
|
|
writeFileSync(join(root, 'js/entry.js'), `import './missing.js';`);
|
|
await assert.rejects(versionSiteAssets(root, 'test', []), /Missing application module/);
|
|
});
|
|
|
|
test('the site leaves independently published CAD imports unversioned and does not require their sources', async t => {
|
|
const root = mkdtempSync(join(tmpdir(), 'make-version-external-'));
|
|
t.after(() => rmSync(root, { recursive: true, force: true }));
|
|
mkdirSync(join(root, 'js'));
|
|
writeFileSync(join(root, 'js/workspace.js'), `import { CadApi } from './cad/api.js'; console.log(CadApi);`);
|
|
await versionSiteAssets(root, 'site-release', [], { externalDirectories: ['js/cad'] });
|
|
const code = readFileSync(join(root, 'js/workspace.js'), 'utf8');
|
|
assert.match(code, /["']\.\/cad\/api\.js["']/);
|
|
assert.doesNotMatch(code, /site-release/);
|
|
await versionSiteAssets(root, 'site-release', [], {
|
|
externalDirectories: ['js/cad'], externalUrls: { 'js/cad': '/cad/js/cad' },
|
|
});
|
|
assert.match(readFileSync(join(root, 'js/workspace.js'), 'utf8'), /["']\/cad\/js\/cad\/api\.js["']/);
|
|
});
|
|
|
|
test('self-contained CAD versions shared modules and HTML assets under its public mount', async t => {
|
|
const root = mkdtempSync(join(tmpdir(), 'make-version-cad-'));
|
|
t.after(() => rmSync(root, { recursive: true, force: true }));
|
|
mkdirSync(join(root, 'js/cad'), { recursive: true });
|
|
writeFileSync(join(root, 'js/cad/app.js'), `import { CAD } from './config.js'; import { auth } from '/cad/js/api.js'; console.log(CAD, auth);`);
|
|
writeFileSync(join(root, 'js/cad/config.js'), `export const CAD = {};`);
|
|
writeFileSync(join(root, 'js/api.js'), `export const auth = {};`);
|
|
writeFileSync(join(root, 'index.html'), '<script src="/cad/js/cad/app.js?v=old"></script><link href="/cad/css/cad.css"><link href="/cad/vendor/keep.css"><link href="/css/site.css">');
|
|
await versionSiteAssets(root, 'cad-release', ['index.html'], { publicPath: '/cad/' });
|
|
const code = readFileSync(join(root, 'js/cad/app.js'), 'utf8');
|
|
assert.match(code, /config\.js\?v=cad-release/);
|
|
assert.match(code, /["']\/cad\/js\/api\.js\?v=cad-release["']/);
|
|
const html = readFileSync(join(root, 'index.html'), 'utf8');
|
|
assert.match(html, /\/cad\/js\/cad\/app\.js\?v=cad-release/);
|
|
assert.match(html, /\/cad\/css\/cad\.css\?v=cad-release/);
|
|
assert.match(html, /href="\/cad\/vendor\/keep.css"/);
|
|
assert.match(html, /href="\/css\/site.css"/);
|
|
});
|