1 | const { DEPLOY_ENV, PUBLIC_PATH } = require('../config/const')
|
2 | const { ensureSlash } = require('@mara/devkit')
|
3 |
|
4 |
|
5 | function template(str, replaceMap) {
|
6 | return Object.keys(replaceMap).reduce((res, k) => {
|
7 | const reg = new RegExp(`{{${k}}}`, 'ig')
|
8 |
|
9 | return res.replace(reg, replaceMap[k])
|
10 | }, str)
|
11 | }
|
12 |
|
13 | module.exports = function resolvePublicPath(publicPath, deployEnv, replaceMap) {
|
14 | function format(str) {
|
15 | const path = ensureSlash(str)
|
16 |
|
17 | return replaceMap ? template(path, replaceMap) : path
|
18 | }
|
19 |
|
20 | if (typeof publicPath === 'string') {
|
21 | return format(publicPath)
|
22 | }
|
23 |
|
24 | const isEmptyPath =
|
25 | publicPath === undefined || !Object.keys(publicPath).length
|
26 | let defaultPath = PUBLIC_PATH
|
27 |
|
28 | if (isEmptyPath) {
|
29 | return format(defaultPath)
|
30 | }
|
31 |
|
32 | if (!deployEnv) {
|
33 | deployEnv = DEPLOY_ENV.ONLINE
|
34 | }
|
35 |
|
36 | defaultPath =
|
37 | publicPath.default === undefined ? defaultPath : publicPath.default
|
38 |
|
39 | return format(
|
40 | publicPath[deployEnv] === undefined ? defaultPath : publicPath[deployEnv]
|
41 | )
|
42 | }
|