-
-
Notifications
You must be signed in to change notification settings - Fork 381
/
util.js
47 lines (41 loc) · 1.32 KB
/
util.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
// Use __non_webpack_require__ to prevent Webpack from compiling it
// when the server-side code is compiled with Webpack
// eslint-disable-next-line camelcase, no-undef, global-require, import/no-dynamic-require, no-eval
const getRequire = () =>
typeof __non_webpack_require__ !== 'undefined'
? __non_webpack_require__
: eval('require')
export const clearModuleCache = moduleName => {
const { cache } = getRequire()
const m = cache[moduleName]
if (m) {
// remove self from own parents
if (m.parent && m.parent.children) {
m.parent.children = m.parent.children.filter(x => x !== m)
}
// remove self from own children
if (m.children) {
m.children.forEach(child => {
if (child.parent && child.parent === m) {
child.parent = null
}
})
}
delete cache[moduleName]
}
}
export const smartRequire = modulePath => {
if (process.env.NODE_ENV !== 'production' && module.hot) {
clearModuleCache(modulePath)
}
return getRequire()(modulePath)
}
export const joinURLPath = (publicPath, filename) => {
if (publicPath.substr(-1) === '/') {
return `${publicPath}${filename}`
}
return `${publicPath}/${filename}`
}
export const readJsonFileSync = (inputFileSystem, jsonFilePath) => {
return JSON.parse(inputFileSystem.readFileSync(jsonFilePath))
}