Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions scripts/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,13 @@ function versionLocalAssets(source, assetVersion) {
.replace(/((?:\.\.\/|\.\/)[^"'#?]+\.css)(?=["'])/g, `$1?v=${assetVersion}`)
}

function rootPublicToolAssets(source, toolId) {
return source.replace(
/((?:href|src)=(["']))\.\/([^"']+)\2/g,
`$1/${toolId}/$3$2`
)
}

function escapeHtml(value) {
return value
.replaceAll('&', '&')
Expand Down Expand Up @@ -228,7 +235,8 @@ async function renderTemplate(
outputPath,
staffTargets = {},
assetVersion,
navigation = {}
navigation = {},
publicToolId = null
) {
const source = await readFile(sourcePath, 'utf8')
const toolsRendered = source.replace(
Expand Down Expand Up @@ -260,7 +268,11 @@ async function renderTemplate(
if (withNavigation.includes('{{tool.') || withNavigation.includes('{{staff.') || withNavigation.includes('{{nav.')) {
throw new Error(`Unresolved template token in ${sourcePath}`)
}
await writeFile(outputPath, versionLocalAssets(withNavigation, assetVersion))
const withVersionedAssets = versionLocalAssets(withNavigation, assetVersion)
const withStableToolAssets = publicToolId
? rootPublicToolAssets(withVersionedAssets, publicToolId)
: withVersionedAssets
await writeFile(outputPath, withStableToolAssets)
}

async function renderPublicTool(toolId, outputRoot, assetVersion) {
Expand All @@ -277,7 +289,8 @@ async function renderPublicTool(toolId, outputRoot, assetVersion) {
resolve(outputPath, 'index.html'),
{},
assetVersion,
{ assetPrefix: '..', current: 'tools' }
{ assetPrefix: '..', current: 'tools' },
toolId
)
}

Expand Down
20 changes: 16 additions & 4 deletions test/site-contract.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,26 @@ test('public API routes include query strings while the Worker enforces exact pa

test('built routes include each public tool and protected DateCalc', async () => {
const outputs = await build()
for (const toolId of ['fio', 'token', 'status', 'orders']) {
const toolAssets = {
balance: ['style.css', 'app.js'],
fio: ['fio.js'],
token: ['token.js'],
status: ['status.js'],
orders: ['orders.js']
}
for (const [toolId, assets] of Object.entries(toolAssets)) {
const html = await readFile(resolve(outputs.mainOutput, toolId, 'index.html'), 'utf8')
assert.match(html, new RegExp(`assets/tool\\.css\\?v=${outputs.assetVersion}`))
if (toolId !== 'balance') {
assert.match(html, new RegExp(`assets/tool\\.css\\?v=${outputs.assetVersion}`))
}
for (const asset of assets) {
assert.match(html, new RegExp(`(?:href|src)="/${toolId}/${asset.replace('.', '\\.')}\\?v=${outputs.assetVersion}"`))
await stat(resolve(outputs.mainOutput, toolId, asset))
}
assert.doesNotMatch(html, /(?:href|src)="\.\//)
assert.doesNotMatch(html, /\{\{nav\./)
}
const balance = await readFile(resolve(outputs.mainOutput, 'balance', 'index.html'), 'utf8')
assert.match(balance, new RegExp(`style\\.css\\?v=${outputs.assetVersion}`))
assert.match(balance, new RegExp(`app\\.js\\?v=${outputs.assetVersion}`))
assert.doesNotMatch(balance, /assets\/intercom\.js|widget\.intercom\.io/)
assert.match(
await readFile(resolve(outputs.mainOutput, 'balance', 'app.js'), 'utf8'),
Expand Down
Loading