-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
/
Copy pathloadFileConfig.js
102 lines (92 loc) · 2.82 KB
/
loadFileConfig.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
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
// @ts-check
const { existsSync, unlinkSync, promises } = require('fs')
const { join, extname, isAbsolute, dirname } = require('path')
const { build } = require('esbuild')
// @ts-ignore
const { loadModule } = require('@vue/cli-shared-utils')
const POSSIBLE_CONFIG_PATHS = [
process.env.VUE_CLI_SERVICE_CONFIG_PATH,
'vue.config.js',
'vue.config.cjs',
'vue.config.mjs',
'vue.config.ts'
].filter((i) => !!i)
const removeFile = (p) => {
if (existsSync(p)) unlinkSync(p)
}
const bundleConfig = async (p, ctx, mjs = false) => {
const outFile = p.replace(/\.[a-z]+$/, '.bundled.js')
const readConfig = () => {
// eslint-disable-next-line
delete eval(`require.cache`)[outFile]
const result = mjs ? import(outFile) : require(outFile)
removeFile(outFile)
return result.default || result
}
try {
await build({
absWorkingDir: ctx,
entryPoints: [p],
outfile: outFile,
platform: 'node',
bundle: true,
target: 'es2017',
format: mjs ? 'esm' : 'cjs',
plugins: [
{
name: 'ignore',
setup (bld) {
bld.onResolve({ filter: /.*/ }, (args) => {
// eslint-disable-next-line
if (!isAbsolute(args.path) && !/^[\.\/]/.test(args.path)) {
return { external: true }
}
})
bld.onLoad(
{ filter: /\.(js|ts|mjs|cjs)$/ },
// @ts-ignore
async (args) => {
const contents = await promises.readFile(args.path, 'utf8')
return {
contents: contents
.replace(/\b__dirname\b/g, JSON.stringify(dirname(args.path)))
.replace(/\b__filename\b/g, JSON.stringify(args.path))
.replace(/\bimport\.meta\.url\b/g, JSON.stringify(`file://${args.path}`)),
loader: args.path.endsWith('.ts') ? 'ts' : 'js'
}
}
)
}
}
]
})
return readConfig()
} catch (e) {
removeFile(outFile)
throw e
}
}
function loadFileConfig (context) {
let fileConfig
const formatPath = (p) => join(context, p)
const getPkg = () => {
try {
return require(formatPath('package.json'))
} catch (e) {
return {}
}
}
const fileConfigPath = POSSIBLE_CONFIG_PATHS.map(formatPath).find((p) => existsSync(p))
const ext = extname(fileConfigPath || '')
const isMjs = (ext === '.js' && getPkg().type === 'module') || ext === '.mjs'
if (isMjs) {
fileConfig = import(fileConfigPath)
} else if (ext === '.ts') {
// use esbuild to compile .ts config
fileConfig = bundleConfig(fileConfigPath, context, true)
} else if (ext) {
fileConfig = loadModule(fileConfigPath, context)
}
return { fileConfig, fileConfigPath }
}
module.exports = loadFileConfig