Skip to content

Commit dad7491

Browse files
authored
fix(spec): move specification to root dir, symlink to public (#633)
1 parent f216df6 commit dad7491

21 files changed

+66
-3
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,7 @@ pnpm-debug.log*
3535
# lock file
3636
bun.lockb
3737
bun.lock
38-
package-lock.json
38+
package-lock.json
39+
40+
# Dynamically generated custom files
41+
public/specification

.prettierignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ dist
88
**/*.min.js
99

1010
# Don't disagree with W3C style on the spec
11-
src/pages/specification
11+
/specification

astro.config.mjs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { defineConfig } from 'astro/config'
2+
import path from 'node:path'
3+
import { symlink } from 'node:fs/promises'
4+
import { fileURLToPath } from 'node:url'
25
import starlight from '@astrojs/starlight'
36
import starlightLinksValidator from 'starlight-links-validator'
47
import starlightFullViewMode from 'starlight-fullview-mode'
@@ -273,7 +276,8 @@ export default defineConfig({
273276
]
274277
}
275278
]
276-
})
279+
}),
280+
specSymlink()
277281
],
278282
redirects: {
279283
'/link-tag': '/tools/link-tag',
@@ -319,3 +323,59 @@ export default defineConfig({
319323
port: 1100
320324
}
321325
})
326+
327+
/**
328+
* Create a symlink to the WM spec folder build time (to /public/specification),
329+
* so that it works on Linux/Mac as well as Windows.
330+
*
331+
* Ideally, we wanted a relative symlink public/specification ->
332+
* ../specification, but that caused platform compatibility issues. So we create
333+
* absolute symlinks at build time, and ignore them from git.
334+
*
335+
* @returns {import('astro').AstroIntegration}
336+
*/
337+
function specSymlink() {
338+
return {
339+
name: 'spec-symlink',
340+
hooks: {
341+
'astro:config:done': async ({ config, logger }) => {
342+
const { publicDir, root } = config
343+
const isWindows = process.platform === 'win32'
344+
const target = path.join(fileURLToPath(root), 'specification')
345+
const link = path.join(fileURLToPath(publicDir), 'specification')
346+
try {
347+
await symlink(target, link, isWindows ? 'junction' : 'dir')
348+
logger.info(`Created symlink from ${link} to ${target}`)
349+
} catch (error) {
350+
if (error.code === 'EEXIST') return
351+
throw error
352+
}
353+
},
354+
'astro:server:setup': ({ server }) => {
355+
// rewrite to index.html during dev
356+
server.middlewares.use((req, res, next) => {
357+
const base = '/specification'
358+
if (!req.url?.startsWith(base)) {
359+
next()
360+
return
361+
}
362+
363+
if (!req.url.endsWith('/') && !req.url.includes('.')) {
364+
res.statusCode = 302
365+
res.setHeader('Location', req.url + '/')
366+
res.end()
367+
return
368+
}
369+
370+
const { pathname } = new URL(req.url, `http://${req.headers.host}`)
371+
if (pathname === `${base}/`) {
372+
req.url = `${base}/index.html`
373+
} else if (pathname === `${base}/flows/`) {
374+
req.url = `${base}/flows/index.html`
375+
}
376+
next()
377+
})
378+
}
379+
}
380+
}
381+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)