-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.mjs
109 lines (95 loc) · 3.88 KB
/
index.mjs
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import chalk from 'chalk'
import path from 'path'
import lodash from 'lodash'
import { createServer } from 'vite'
import { createRequire } from 'module'
const require = createRequire(import.meta.url)
export default new class {
get plugin() {
return {
// middleware to translate paths from /page to /public/page.html
middleware: {
name: 'middleware',
apply: 'serve',
configureServer: (viteDevServer) => {
return () => {
viteDevServer.middlewares.use(async(context, res, next) => {
if (!context.originalUrl.endsWith('.html') && context.originalUrl !== '/') {
context.url = `/${this.options.output}` + context.originalUrl + '.html'
} else if (context.url === '/index.html') {
context.url = `/${this.options.output}` + context.url
}
next()
})
}
}
},
// reload page if there is change in public directory (it doesn't work with vite by default)
reload: {
name: 'reload',
handleHotUpdate: ({ file }) => {
if ((!this.options.reloadPublic && !file.includes('.json') && !file.includes('.html') && file.includes(`/${this.options.output}/`)) ||
(this.options.reloadPublic && file.includes(`/${this.options.output}/`)) || this.options.reloadFiles(file)) {
this.reload(file)
}
}
}
}
}
reload(file) {
// you can use this function to reload page in any gulp task you want, or anywhere in generally
if (typeof this.server !== 'undefined') {
this.server.ws.send({
type: 'full-reload',
path: '*'
})
this.server.config.logger.info(
chalk.green('page reload ') + chalk.dim(typeof file !== 'undefined' ? file.replace(`${this.options.root}/`, '') : '*.html'),
{ clear: true, timestamp: true }
)
}
}
init(userOptions = {}) {
const options = {
output: 'public',
root: process.cwd(),
ignored: [],
reloadPublic: true,
reloadFiles: () => false
}
lodash.merge(options, userOptions)
const viteOptions = {
vite: {
plugins: [this.plugin.middleware, this.plugin.reload],
publicDir: path.join(options.root, options.output),
server: {
open: '/',
host: true,
fsServe: {
strict: false
},
watch: {
// default vite watch ignore files and additional files to ignore, reload for templates files is handled manually
ignored: options.ignored.concat(['**/node_modules/**', '**/.git/**'])
}
},
root: options.root
}
}
if (!options.reloadPublic) {
viteOptions.vite.server.watch.ignored.push(`**/${options.output}/*.html`)
}
lodash.merge(options, viteOptions)
this.options = options
return new Promise(async resolve => {
// defines server instance in the Serve class
this.server = await createServer(this.options.vite)
// starts the server
await this.server.listen()
console.log(chalk.cyan(`\n vite v${require('vite/package.json').version}`) + chalk.green(' dev server running at:\n'))
this.server.printUrls()
console.log('')
resolve()
})
}
}()