UNPKG

3.84 kBJavaScriptView Raw
1const fs = require('fs-extra')
2const path = require('path')
3
4const getPublicPath = require('./get-public-dir')
5const getChunkmap = require('./get-chunkmap')
6
7
8/**
9 * 获取浏览器环境中指定文件的访问路径
10 * @param {String} filename 要查找的文件的文件名。根据打包文件对应表 (chunkmap) 查询文件名和实际打包结果文件的对应关系
11 * @param {String} [localeId] 当前语言
12 * @param {Boolean} [isPathname = false] 如果标记为 true,表示提供的 filename 为确切的访问地址,无需查询对照表,直接返回结果
13 * @returns {String} 浏览器环境中的访问路径或空字符串
14 */
15const getFilePath = (filename, localeId, isPathname = false) => {
16 // 如果第一个参数为 true,表示标记为 pathname
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 // const localeId = 'zh'
33
34 // 如果标记为 pathname,直接返回结果
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 // 如果没有找到 chunkmap 或是 chunkmap 中未找到目标项目,转为过滤文件形式
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 // const segs = pathname.split('/').filter(seg => seg !== '/')
79 // const file = segs.pop()
80 // const dir = segs.length ? `${segs.join('/')}/` : ''
81 // return `/${dir}${
82 // require('./filterTargetFile')(
83 // require('./readFilesInPath')(`./${distPathname}/public/${appName ? `${appName}/` : ''}${dir}`),
84 // file
85 // )}`
86}
87
88module.exports = getFilePath
89// module.exports = (pathname, pathDist = 'dist') => {
90// if (__DEV__) {
91// return `http://localhost:${process.env.WEBPACK_DEV_SERVER_PORT || '3001'}/dist/${pathname}`
92// } else {
93// const segs = pathname.split('/').filter(seg => seg !== '/')
94// const file = segs.pop()
95// const dir = segs.length ? `${segs.join('/')}/` : ''
96// return `/${dir}${
97// require('./filterTargetFile')(
98// require('./readFilesInPath')(`./${pathDist}/public/${dir}`),
99// file
100// )}`
101// }
102// }