-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.js
64 lines (61 loc) · 1.89 KB
/
vite.config.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
import { sveltekit } from "@sveltejs/kit/vite"
import basicSsl from "@vitejs/plugin-basic-ssl"
import * as fs from 'fs'
import * as path from 'path'
/** doRewrap - wraps an assembled by @sveltejs/adapter-static project
* into a module that can be loaded/called from another application. */
const doRewrap = ({ cssClass }) => {
try {
if (fs.existsSync(path.resolve(__dirname, 'dist/bundle.js'))) {
return
}
} catch(e) {}
console.log("\nStart re-wrapping...")
fs.readFile(path.resolve(__dirname, 'dist/bundle.html'), 'utf8', function(err, data){
if (!data) {
console.log(`[Error]: No bundle.html generated, check svelte.config.js -> config.kit.adapter -> fallback: "bundle.html"`)
return
}
let matchData = data.match(/(?<=<script\b[^>]*>)([\s\S]*?)(?=<\/script>)/gm)
if (matchData) {
let cleanData = matchData[0].trim()
.replace(`document.querySelector('[data-sveltekit-hydrate="45h"]').parentNode`, `document.querySelector(".${cssClass}")`)
fs.writeFile(path.resolve(__dirname, 'dist/bundle.js'), cleanData, (err) => {
if (err)
console.log(err)
else {
try {
fs.rename(path.resolve(__dirname,'dist/index.page'), path.resolve(__dirname, 'dist/index.html'), (err) => { })
} catch (e) { }
try {
fs.unlinkSync(path.resolve(__dirname, 'dist/bundle.html'))
} catch (e) { }
console.log("Finished: bundle.js + index.html have been regenerated.\n")
}
})
} else
console.log(`[Error]: No proper <script> tag found in bundle.html`)
})
}
/** @type {import('vite').UserConfig} */
const config = {
server: {
https: true,
port: 5005
},
plugins: [sveltekit(), basicSsl(),
{
name: 'postbuild-command',
closeBundle: {
order: 'post',
handler() {
setTimeout(() => doRewrap({ cssClass: "overlay" }), Math.random()*500+500)
}
}
}
],
test: {
include: ["src/**/*.{test,spec}.{js,ts}"]
}
}
export default config