packaton 0.0.3 → 0.0.6
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 +2 -2
- package/index.d.ts +1 -0
- package/package.json +1 -1
- package/src/HtmlCompiler.js +4 -2
- package/src/app-prod.js +40 -13
- package/src/config.js +1 -0
- package/src/media-remaper.js +9 -5
package/README.md
CHANGED
|
@@ -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 /static are not automatically copied over,
|
|
49
49
|
you need to specify them.
|
|
50
|
-
-
|
|
50
|
+
- static/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/HtmlCompiler.js
CHANGED
|
@@ -10,6 +10,7 @@ export class HtmlCompiler {
|
|
|
10
10
|
pSource = ''
|
|
11
11
|
css = ''
|
|
12
12
|
scriptsJs = ''
|
|
13
|
+
mediaRelUrl = ''
|
|
13
14
|
scriptsNonJs = ''
|
|
14
15
|
externalScripts = []
|
|
15
16
|
externalCSS = []
|
|
@@ -17,12 +18,13 @@ export class HtmlCompiler {
|
|
|
17
18
|
#minifyCSS = a => a
|
|
18
19
|
#minifyHTML = a => a
|
|
19
20
|
|
|
20
|
-
constructor(html, pSource = '', { minifyJS, minifyCSS, minifyHTML }) {
|
|
21
|
+
constructor(html, pSource = '', { minifyJS, minifyCSS, minifyHTML, mediaRelUrl }) {
|
|
21
22
|
this.html = html
|
|
22
23
|
this.pSource = pSource
|
|
23
24
|
this.#minifyJS = minifyJS
|
|
24
25
|
this.#minifyCSS = minifyCSS
|
|
25
26
|
this.#minifyHTML = minifyHTML
|
|
27
|
+
this.mediaRelUrl = mediaRelUrl
|
|
26
28
|
}
|
|
27
29
|
|
|
28
30
|
// Removes comments and format multi-line tags (needed for `removeLineContaining`)
|
|
@@ -31,7 +33,7 @@ export class HtmlCompiler {
|
|
|
31
33
|
}
|
|
32
34
|
|
|
33
35
|
remapMedia(mediaHashes) {
|
|
34
|
-
this.html = remapMediaInHTML(mediaHashes, this.html)
|
|
36
|
+
this.html = remapMediaInHTML(mediaHashes, this.html, this.mediaRelUrl)
|
|
35
37
|
}
|
|
36
38
|
|
|
37
39
|
async inlineMinifiedCSS() {
|
package/src/app-prod.js
CHANGED
|
@@ -6,7 +6,7 @@ import { docs } from './app.js'
|
|
|
6
6
|
import { router } from './app-router.js'
|
|
7
7
|
import { reportSizes } from './reportSizes.js'
|
|
8
8
|
import { HtmlCompiler } from './HtmlCompiler.js'
|
|
9
|
-
import { write, removeDir } from './fs-utils.js'
|
|
9
|
+
import { write, removeDir, isFile } from './fs-utils.js'
|
|
10
10
|
import { renameMediaWithHashes } from './media-remaper.js'
|
|
11
11
|
|
|
12
12
|
|
|
@@ -16,13 +16,19 @@ import { renameMediaWithHashes } from './media-remaper.js'
|
|
|
16
16
|
*/
|
|
17
17
|
export async function buildStaticPages(config) {
|
|
18
18
|
return new Promise((resolve, reject) => {
|
|
19
|
+
const MEDIA_REL_URL = join(config.staticDir, 'media')
|
|
20
|
+
|
|
19
21
|
const pSource = config.srcPath
|
|
20
22
|
const pDist = config.outputPath
|
|
21
|
-
const
|
|
23
|
+
const pDistStatic = join(config.outputPath, config.staticDir)
|
|
24
|
+
const pDistMedia = join(pDist, MEDIA_REL_URL)
|
|
22
25
|
const pDistSitemap = join(pDist, 'sitemap.txt')
|
|
23
|
-
const
|
|
26
|
+
const pDistRobots = join(pDist, 'robots.txt')
|
|
24
27
|
const pSizesReport = 'packed-sizes.json'
|
|
25
28
|
|
|
29
|
+
const pDistCspNginxMap = join(pDist, '.csp-map.nginx')
|
|
30
|
+
const CLOUDFLARE_HEADERS_FILE = join(pDistStatic, '_headers')
|
|
31
|
+
|
|
26
32
|
const server = http.createServer(router(config))
|
|
27
33
|
server.listen(0, '127.0.0.1', async error => {
|
|
28
34
|
docs.init(config.srcPath, config.ignore)
|
|
@@ -33,7 +39,7 @@ export async function buildStaticPages(config) {
|
|
|
33
39
|
}
|
|
34
40
|
|
|
35
41
|
removeDir(pDist)
|
|
36
|
-
cpSync(join(pSource,
|
|
42
|
+
cpSync(join(pSource, config.staticDir), pDistStatic, {
|
|
37
43
|
recursive: true,
|
|
38
44
|
dereference: true,
|
|
39
45
|
filter(src) {
|
|
@@ -48,7 +54,12 @@ export async function buildStaticPages(config) {
|
|
|
48
54
|
|
|
49
55
|
const cspByRoute = []
|
|
50
56
|
for (const [route, rawHtml] of pages) {
|
|
51
|
-
const doc = new HtmlCompiler(rawHtml, join(pSource, dirname(route)),
|
|
57
|
+
const doc = new HtmlCompiler(rawHtml, join(pSource, dirname(route)), {
|
|
58
|
+
minifyJS: config.minifyJS,
|
|
59
|
+
minifyCSS: config.minifyCSS,
|
|
60
|
+
minifyHTML: config.minifyHTML,
|
|
61
|
+
mediaRelUrl: MEDIA_REL_URL
|
|
62
|
+
})
|
|
52
63
|
await doc.minifyHTML()
|
|
53
64
|
doc.remapMedia(mediaHashes)
|
|
54
65
|
// TODO remap media in css and js
|
|
@@ -58,24 +69,27 @@ export async function buildStaticPages(config) {
|
|
|
58
69
|
cspByRoute.push([route, doc.csp()])
|
|
59
70
|
}
|
|
60
71
|
|
|
61
|
-
if (config.sitemapDomain)
|
|
72
|
+
if (config.sitemapDomain) {
|
|
62
73
|
write(pDistSitemap, docs.routes
|
|
63
74
|
.filter(r => r !== '/index')
|
|
64
75
|
.map(r => `https://${config.sitemapDomain + r}`)
|
|
65
76
|
.join('\n'))
|
|
66
77
|
|
|
78
|
+
if (!isFile(pDistRobots))
|
|
79
|
+
write(pDistRobots,
|
|
80
|
+
`Sitemap: https://${config.sitemapDomain}/sitemap.txt`)
|
|
81
|
+
}
|
|
82
|
+
|
|
67
83
|
if (config.cspMapEnabled) {
|
|
68
84
|
write(pDistCspNginxMap, cspByRoute.map(([route, csp]) =>
|
|
69
85
|
`${route} "${csp}";`).join('\n'))
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
const r = route === '/index' ? '/' : route
|
|
74
|
-
return `${r}\n Content-Security-Policy: ${csp}`
|
|
75
|
-
}).join('\n'))
|
|
86
|
+
|
|
87
|
+
write(CLOUDFLARE_HEADERS_FILE,
|
|
88
|
+
makeHeadersFile(cspByRoute, MEDIA_REL_URL))
|
|
76
89
|
}
|
|
77
90
|
|
|
78
|
-
reportSizes(pSizesReport, pDist,
|
|
91
|
+
reportSizes(pSizesReport, pDist,
|
|
92
|
+
docs.routes.map(f => f + config.outputExtension))
|
|
79
93
|
}
|
|
80
94
|
catch (error) {
|
|
81
95
|
reject(error)
|
|
@@ -90,6 +104,19 @@ export async function buildStaticPages(config) {
|
|
|
90
104
|
})
|
|
91
105
|
}
|
|
92
106
|
|
|
107
|
+
// TODO @ThinkAbout in Nginx we need to use `/index` but in CF `/`
|
|
108
|
+
function makeHeadersFile(cspByRoute, MEDIA_URL) {
|
|
109
|
+
const cspHeaders = cspByRoute.map(([route, csp]) => {
|
|
110
|
+
const r = route === '/index'
|
|
111
|
+
? '/'
|
|
112
|
+
: route
|
|
113
|
+
return `${r}\n Content-Security-Policy: ${csp}`
|
|
114
|
+
})
|
|
115
|
+
cspHeaders.push(`${MEDIA_URL}/*`)
|
|
116
|
+
cspHeaders.push(' Cache-Control: public,max-age=31536000,immutable')
|
|
117
|
+
return cspHeaders.join('\n')
|
|
118
|
+
}
|
|
119
|
+
|
|
93
120
|
|
|
94
121
|
async function crawlRoutes({ address, port }, routes) {
|
|
95
122
|
const pages = []
|
package/src/config.js
CHANGED
|
@@ -17,6 +17,7 @@ import { minifyHTML } from './minifyHTML.js'
|
|
|
17
17
|
const schema = {
|
|
18
18
|
mode: ['development', val => ['development', 'production'].includes(val)],
|
|
19
19
|
srcPath: [resolve('src'), isDirectory],
|
|
20
|
+
staticDir: ['static', optional(String)],
|
|
20
21
|
ignore: [/^_/, optional(RegExp)],
|
|
21
22
|
|
|
22
23
|
// Development
|
package/src/media-remaper.js
CHANGED
|
@@ -21,7 +21,6 @@ export async function renameMediaWithHashes(dir) {
|
|
|
21
21
|
return mediaHashes
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
// TODO if media is made configurable, we'd need to espace the regex for example .media -> \.media
|
|
25
24
|
// Having one dir is kinda nice for nginx headers, but that's not an excuse nor solves nested dirs with same filename
|
|
26
25
|
|
|
27
26
|
// TODO for (b of base) find and replace base with new hash
|
|
@@ -35,17 +34,22 @@ export async function renameMediaWithHashes(dir) {
|
|
|
35
34
|
* If you want to handle CSS files, edit the regex so
|
|
36
35
|
* instead of checking `="` (e.g. src="img.png") also checks for `url(`
|
|
37
36
|
**/
|
|
38
|
-
export function remapMediaInHTML(mediaHashes, html) {
|
|
39
|
-
const
|
|
40
|
-
const
|
|
37
|
+
export function remapMediaInHTML(mediaHashes, html, mediaRelUrl) {
|
|
38
|
+
const mURL = escapeForRegex(mediaRelUrl)
|
|
39
|
+
const reFindMedia = new RegExp(`(="${mURL}/.*?)"`, 'g')
|
|
40
|
+
const reFindMediaKey = new RegExp(`="${mURL}/`)
|
|
41
41
|
|
|
42
42
|
for (const [, url] of html.matchAll(reFindMedia)) {
|
|
43
43
|
const hashedName = mediaHashes.get(url.replace(reFindMediaKey, ''))
|
|
44
44
|
if (!hashedName)
|
|
45
45
|
throw `ERROR: Missing ${url}\n`
|
|
46
|
-
html = html.replace(url, `="
|
|
46
|
+
html = html.replace(url, `="${mediaRelUrl}/${hashedName}`)
|
|
47
47
|
}
|
|
48
48
|
return html
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
|
|
52
|
+
function escapeForRegex(literal) {
|
|
53
|
+
return literal.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
54
|
+
}
|
|
55
|
+
|