packaton 0.0.8 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/index.d.ts +1 -1
- package/package.json +1 -1
- package/src/app-prod.js +3 -3
- package/src/config.js +1 -1
- package/src/plugins-prod/netiflyAndCloudflareHeadersPlugin.js +8 -3
- package/src/router.js +3 -3
- package/src/utils/http-response.js +1 -1
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@ computes their corresponding CSP nonce and injects it as well.
|
|
|
12
12
|
|
|
13
13
|
|
|
14
14
|
## Images and Videos (immutable naming)
|
|
15
|
-
For long-term caching, [media-remaper.js](src/plugins-prod/media-remaper.js) appends
|
|
15
|
+
For long-term caching, [media-remaper.js](src/plugins-prod/media-remaper.js) appends an SHA-1 hash
|
|
16
16
|
to the filenames and takes care of rewriting their `src` in HTML (**only in HTML**).
|
|
17
17
|
|
|
18
18
|
If you want to use media files in CSS, create a similar function to
|
|
@@ -45,6 +45,6 @@ To avoid minifying, you can pass `a=>a`
|
|
|
45
45
|
- can't write inline scripts or css (all must be in an external file, packaton inlines them)
|
|
46
46
|
- must have an index
|
|
47
47
|
- Ignored Documents start with `_`, so you can't have routes that begin with _
|
|
48
|
-
- Non-Documents and Files outside
|
|
48
|
+
- Non-Documents and Files outside config.assetsDir are not automatically copied over,
|
|
49
49
|
you need to specify them.
|
|
50
|
-
-
|
|
50
|
+
- assets/media only files at the top level get hashed-named. But files within subdirs are not (by design).
|
package/index.d.ts
CHANGED
package/package.json
CHANGED
package/src/app-prod.js
CHANGED
|
@@ -19,11 +19,11 @@ import { netiflyAndCloudflareHeadersPlugin } from './plugins-prod/netiflyAndClou
|
|
|
19
19
|
*/
|
|
20
20
|
export async function buildStaticPages(config) {
|
|
21
21
|
return new Promise((resolve, reject) => {
|
|
22
|
-
const MEDIA_REL_URL = join(config.
|
|
22
|
+
const MEDIA_REL_URL = join(config.assetsDir, 'media')
|
|
23
23
|
|
|
24
24
|
const pSource = config.srcPath
|
|
25
25
|
const pDist = config.outputDir
|
|
26
|
-
const
|
|
26
|
+
const pDistAssets = join(config.outputDir, config.assetsDir)
|
|
27
27
|
const pDistMedia = join(pDist, MEDIA_REL_URL)
|
|
28
28
|
|
|
29
29
|
const server = createServer(router(config))
|
|
@@ -32,7 +32,7 @@ export async function buildStaticPages(config) {
|
|
|
32
32
|
docs.init(config.srcPath, config.ignore)
|
|
33
33
|
try {
|
|
34
34
|
removeDir(pDist)
|
|
35
|
-
cpSync(join(pSource, config.
|
|
35
|
+
cpSync(join(pSource, config.assetsDir), pDistAssets, {
|
|
36
36
|
recursive: true,
|
|
37
37
|
dereference: true,
|
|
38
38
|
filter(src) {
|
package/src/config.js
CHANGED
|
@@ -17,7 +17,7 @@ import { minifyHTML } from './plugins-prod/minifyHTML.js'
|
|
|
17
17
|
const schema = {
|
|
18
18
|
mode: ['development', val => ['development', 'production'].includes(val)],
|
|
19
19
|
srcPath: [resolve('src'), isDirectory],
|
|
20
|
-
|
|
20
|
+
assetsDir: ['assets', optional(String)],
|
|
21
21
|
ignore: [/^_/, optional(RegExp)],
|
|
22
22
|
|
|
23
23
|
// Development
|
|
@@ -2,8 +2,13 @@ import { join } from 'node:path'
|
|
|
2
2
|
import { write } from '../utils/fs-utils.js'
|
|
3
3
|
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
/**
|
|
6
|
+
* @param {Config} config
|
|
7
|
+
* @param {string} cspByRoute
|
|
8
|
+
* @param {string} relMediaURL
|
|
9
|
+
*/
|
|
10
|
+
export function netiflyAndCloudflareHeadersPlugin(config, cspByRoute, relMediaURL) {
|
|
11
|
+
const out = join(join(config.outputDir, config.assetsDir), '_headers')
|
|
7
12
|
|
|
8
13
|
const cspHeaders = cspByRoute.map(([route, csp]) => {
|
|
9
14
|
const r = route === '/index'
|
|
@@ -15,7 +20,7 @@ export function netiflyAndCloudflareHeadersPlugin(config, cspByRoute, MEDIA_URL)
|
|
|
15
20
|
` Cache-Control: public,max-age=60`
|
|
16
21
|
].join('\n')
|
|
17
22
|
})
|
|
18
|
-
cspHeaders.push(
|
|
23
|
+
cspHeaders.push(`/${relMediaURL}/*`)
|
|
19
24
|
cspHeaders.push(' Cache-Control: public,max-age=31536000,immutable')
|
|
20
25
|
|
|
21
26
|
write(out, cspHeaders.join('\n'))
|
package/src/router.js
CHANGED
|
@@ -4,7 +4,7 @@ import { readFile } from 'node:fs/promises'
|
|
|
4
4
|
import { docs } from './app.js'
|
|
5
5
|
import { mimeFor } from './utils/mimes.js'
|
|
6
6
|
import { devClientWatcher } from './plugins-dev/WatcherDevClient.js'
|
|
7
|
-
import { sendError, sendJSON, servePartialContent,
|
|
7
|
+
import { sendError, sendJSON, servePartialContent, serveAsset } from './utils/http-response.js'
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
const WATCHER_DEV = '/plugins-dev/watcherDev.js'
|
|
@@ -26,7 +26,7 @@ export function router({ srcPath, ignore, mode }) {
|
|
|
26
26
|
longPollDevHotReload(req, response)
|
|
27
27
|
|
|
28
28
|
else if (url === WATCHER_DEV)
|
|
29
|
-
|
|
29
|
+
serveAsset(response, join(import.meta.dirname, url))
|
|
30
30
|
|
|
31
31
|
else if (docs.hasRoute(url))
|
|
32
32
|
await serveDocument(response, docs.fileFor(url), isDev)
|
|
@@ -38,7 +38,7 @@ export function router({ srcPath, ignore, mode }) {
|
|
|
38
38
|
await servePartialContent(response, req.headers, join(srcPath, url))
|
|
39
39
|
|
|
40
40
|
else
|
|
41
|
-
|
|
41
|
+
serveAsset(response, join(srcPath, url))
|
|
42
42
|
}
|
|
43
43
|
catch (error) {
|
|
44
44
|
sendError(response, error)
|
|
@@ -15,7 +15,7 @@ export function sendJSON(response, payload) {
|
|
|
15
15
|
response.end(JSON.stringify(payload))
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
export function
|
|
18
|
+
export function serveAsset(response, file) {
|
|
19
19
|
response.setHeader('Content-Type', mimeFor(file))
|
|
20
20
|
const reader = fs.createReadStream(file)
|
|
21
21
|
reader.on('open', function () { this.pipe(response) })
|