1 | const resolve = require('path').resolve
|
2 |
|
3 | const loadJSConfig = getAttemptModuleRequireFn(function onFail(configPath, requirePath) {
|
4 | console.log(`Unable to find JS config at "${configPath}". Attempted to require as "${requirePath}"`)
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | return undefined
|
10 | })
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | function getModuleRequirePath(moduleName) {
|
18 | return moduleName[0] === '.' ? resolve(process.cwd(), moduleName) : moduleName
|
19 | }
|
20 |
|
21 | function getAttemptModuleRequireFn(onFail) {
|
22 | return function attemptModuleRequire(moduleName) {
|
23 | const requirePath = getModuleRequirePath(moduleName)
|
24 | try {
|
25 | return requireDefaultFromModule(requirePath)
|
26 | } catch (e) {
|
27 | if (e.constructor.name === 'SyntaxError') {
|
28 | throw e
|
29 | }
|
30 | return onFail(moduleName, requirePath)
|
31 | }
|
32 | }
|
33 | }
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | function requireDefaultFromModule(modulePath) {
|
41 |
|
42 | const mod = require(modulePath)
|
43 | if (mod.__esModule) {
|
44 | return mod.default
|
45 | } else {
|
46 | return mod
|
47 | }
|
48 | }
|
49 |
|
50 | function loadConfig(configPath) {
|
51 |
|
52 | return loadJSConfig(configPath)
|
53 | }
|
54 |
|
55 | module.exports.loadConfig = loadConfig
|