1 | const fs = require('fs-extra')
|
2 | const path = require('path')
|
3 |
|
4 | const getPublicPath = require('./get-public-dir')
|
5 | const getChunkmap = require('./get-chunkmap')
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | const getFilePath = (filename, localeId, isPathname = false) => {
|
16 |
|
17 | if (filename === true) return getFilePath(localeId, isPathname || undefined, true)
|
18 |
|
19 | if (typeof localeId === 'undefined') {
|
20 | try {
|
21 | localeId = require('../i18n').localeId
|
22 | } catch (e) { }
|
23 | }
|
24 |
|
25 | const pathPublic = getPublicPath()
|
26 |
|
27 | const i18nType = JSON.parse(process.env.KOOT_I18N)
|
28 | ? JSON.parse(process.env.KOOT_I18N_TYPE)
|
29 | : undefined
|
30 | const isI18nDefault = (i18nType === 'default')
|
31 | const isDev = (process.env.WEBPACK_BUILD_ENV === 'dev' || (typeof __DEV__ !== 'undefined' && __DEV__))
|
32 |
|
33 |
|
34 |
|
35 | if (isPathname) return pathPublic + filename.replace(/(^\.\/|^)public\//, '')
|
36 |
|
37 | const chunkmap = getChunkmap(localeId)
|
38 |
|
39 | if (typeof chunkmap === 'object' &&
|
40 | typeof chunkmap['.files'] === 'object' &&
|
41 | typeof chunkmap['.files'][filename] === 'string'
|
42 | ) {
|
43 | return pathPublic + chunkmap['.files'][filename].replace(/(^\.\/|^)public\//, '')
|
44 | }
|
45 |
|
46 | if (isDev) return pathPublic + (isI18nDefault ? localeId : '') + `.${filename}`
|
47 |
|
48 | if (typeof chunkmap === 'object') {
|
49 |
|
50 | const extname = path.extname(filename)
|
51 | const key = path.basename(filename, extname)
|
52 | let result
|
53 | if (Array.isArray(chunkmap[key])) {
|
54 | chunkmap[key].some(value => {
|
55 | if (path.extname(value) === extname) {
|
56 | result = value
|
57 | return true
|
58 | }
|
59 | return false
|
60 | })
|
61 | }
|
62 | if (result)
|
63 | return `${pathPublic}${result.replace(/(^\.\/|^)public\//, '')}`
|
64 | }
|
65 |
|
66 |
|
67 | if (fs.existsSync(path.resolve(
|
68 | pathPublic,
|
69 | filename
|
70 | ))) {
|
71 | return '/' + filename
|
72 | }
|
73 |
|
74 | console.warn(`File not found:` + (isI18nDefault ? `[${localeId}] ` : '') + ` ${filename}`)
|
75 |
|
76 | return ''
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | }
|
87 |
|
88 | module.exports = getFilePath
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|