-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
85 lines (74 loc) · 2.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { fileURLToPath } from "url"
import { readFileSync, writeFileSync } from "fs"
import { resolve } from "path"
import { JSDOM } from "jsdom"
const files = fileURLToPath(new URL("./files", import.meta.url).href)
/** @type {import('.')} */
export default function ({
pages = "build",
assets = pages,
fallback = null,
indexPath = "index.php",
shadow = false,
shortcode = "svelte-kit-shortcode",
prefix = "skawpsc_" + shortcode.replace(/[^\w]/g, "_"),
renderHead = head =>
[...head.querySelectorAll(`link[rel="modulepreload"]`)]
.map(element => element.outerHTML)
.join(""),
renderBody = body => body.innerHTML
} = {}) {
return {
name: "sveltekit-adapter-wordpress-shortcode",
async adapt(builder) {
if (!builder.config.kit.paths.base)
builder.log.warn(
"You should set config.kit.paths.base to something like `/wp-content/plugins/my-shortcode-plugin`"
)
if (!builder.config.kit.paths.assets)
builder.log.warn(
"You should set config.kit.paths.assets to something like `https://example.com/wp-content/plugins/my-shortcode-plugin`"
)
builder.rimraf(assets)
builder.rimraf(pages)
builder.writeClient(assets)
builder.writePrerendered(pages, { fallback })
const pageFiles = [...builder.prerendered.pages.values()].map(page => page.file)
for (const file of pageFiles) if (file !== "index.html") builder.rimraf(resolve(pages, file))
if (!pageFiles.includes("index.html")) {
builder.log.error(
`sveltekit-adapter-wordpress-shortcode: root route must be prerendered (unless using the 'fallback' option — see https://github.com/sveltejs/kit/tree/master/packages/adapter-static#spa-mode). Try adding \`export const prerender = true\` to your root layout.js — see https://kit.svelte.dev/docs/page-options#prerender for more details`
)
throw new Error("Encountered non rendered root route")
}
// copy php files
builder.copy(indexPath, resolve(pages, "index.php"))
builder.copy(files, pages, {
replace: {
SHORTCODE_CODE: shortcode,
SHORTCODE_SHADOW: String(shadow),
...Object.fromEntries(
["shortcodeHead", "shortcodeBody", "shortcodeData"].map(funcName => [
`SHORTCODE_PREFIX_${funcName}`,
`${prefix}_${funcName}`
])
)
}
})
// read, remove, and transform index.html
const indexHtmlPath = resolve(pages, "index.html")
const dom = new JSDOM(readFileSync(indexHtmlPath, "utf8"))
builder.rimraf(indexHtmlPath)
writeFileSync(
resolve(pages, "svelte_kit_shortcode_head.html"),
renderHead(dom.window.document.head)
)
writeFileSync(
resolve(pages, "svelte_kit_shortcode_body.html"),
renderBody(dom.window.document.body)
)
if (pages === assets) builder.log(`Wrote site to "${pages}"`)
else builder.log(`Wrote pages to "${pages}" and assets to "${assets}"`)
}
}
}