forked from pearofducks/rollup-plugin-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.js
63 lines (57 loc) · 1.6 KB
/
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
import Joi from 'joi'
import { prettifier } from './src/logger.js'
const proxyItem = Joi.object({
from: Joi.string().uri({ relativeOnly: true }),
to: Joi.string().uri(),
opts: Joi.object()
})
const schema = Joi.alternatives().try(
Joi.string(),
Joi.object({
silent: Joi.boolean(),
force: Joi.boolean(),
proxy: Joi.array().items(proxyItem),
dirs: Joi.array().items(Joi.string()),
dirname: Joi.string(),
spa: [Joi.boolean(), Joi.string()],
port: Joi.number().port(),
host: [Joi.string().ip(), Joi.string().hostname()],
basePath: Joi.string().uri({ relativeOnly: true }),
extend: Joi.function(),
server: Joi.object(),
onListen: Joi.function()
})
)
export const serverDefaults = Object.freeze({
ignoreTrailingSlash: true,
disableRequestLogging: true
})
const pluginServer = Object.freeze({
logger: { prettyPrint: { suppressFlushSyncWarning: true }, prettifier }
})
export const defaults = {
proxy: [],
dirs: ['.'],
port: 8080,
host: 'localhost',
spa: false,
silent: false,
force: false,
server: {
...pluginServer,
...serverDefaults
},
basePath: undefined,
extend: undefined,
dirname: undefined,
onListen: undefined
}
export const normalize = (rollupOptions = {}) => {
const parsed = Joi.attempt(rollupOptions, schema)
const normalized = (typeof parsed === 'string') ? { dirs: [parsed] } : parsed
const serverConfig = Object.assign({}, defaults.server, normalized.server)
const config = Object.assign({}, defaults, normalized)
config.server = serverConfig
if (config.silent) config.server.logger = false
return config
}