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 | * @param {String} filename 要查找的文件的文件名。根据打包文件对应表 (chunkmap) 查询文件名和实际打包结果文件的对应关系
|
11 | * @param {String} [localeId] 当前语言
|
12 | * @param {Boolean} [isPathname = false] 如果标记为 true,表示提供的 filename 为确切的访问地址,无需查询对照表,直接返回结果
|
13 | * @returns {String} 浏览器环境中的访问路径或空字符串
|
14 | */
|
15 | const 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('../index').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 | // console.log('----------')
|
40 | // console.log(filename)
|
41 | // console.log(chunkmap['.files'])
|
42 | // console.log(chunkmap['.files'][filename])
|
43 | // console.log(pathPublic + chunkmap['.files'][filename].replace(/(^\.\/|^)public\//, ''))
|
44 | // console.log('----------')
|
45 |
|
46 | if (typeof chunkmap === 'object' &&
|
47 | typeof chunkmap['.files'] === 'object' &&
|
48 | typeof chunkmap['.files'][filename] === 'string'
|
49 | ) {
|
50 | // console.log(filename, chunkmap['.files'][filename].replace(/(^\.\/|^)public\//, ''))
|
51 | return pathPublic + chunkmap['.files'][filename].replace(/(^\.\/|^)public\//, '')
|
52 | }
|
53 |
|
54 | if (isDev) {
|
55 | const prefix = pathPublic + (isI18nDefault ? localeId : '')
|
56 | if (
|
57 | typeof chunkmap['.files'] === 'object' &&
|
58 | typeof chunkmap['.files'][filename] === 'string'
|
59 | )
|
60 | return prefix + chunkmap['.files'][filename]
|
61 | return prefix + `.${filename}`
|
62 | }
|
63 |
|
64 | if (typeof chunkmap === 'object') {
|
65 |
|
66 | const extname = path.extname(filename)
|
67 | const key = path.basename(filename, extname)
|
68 | let result
|
69 | if (Array.isArray(chunkmap[key])) {
|
70 | chunkmap[key].some(value => {
|
71 | if (path.extname(value) === extname) {
|
72 | result = value
|
73 | return true
|
74 | }
|
75 | return false
|
76 | })
|
77 | }
|
78 | if (result)
|
79 | return `${pathPublic}${result.replace(/(^\.\/|^)public\//, '')}`
|
80 | }
|
81 |
|
82 | // 如果没有找到 chunkmap 或是 chunkmap 中未找到目标项目,转为过滤文件形式
|
83 | if (fs.existsSync(path.resolve(
|
84 | pathPublic,
|
85 | filename
|
86 | ))) {
|
87 | return '/' + filename
|
88 | }
|
89 |
|
90 | console.warn(`File not found:` + (isI18nDefault ? `[${localeId}] ` : '') + ` ${filename}`)
|
91 |
|
92 | return ''
|
93 |
|
94 | // const segs = pathname.split('/').filter(seg => seg !== '/')
|
95 | // const file = segs.pop()
|
96 | // const dir = segs.length ? `${segs.join('/')}/` : ''
|
97 | // return `/${dir}${
|
98 | // require('./filterTargetFile')(
|
99 | // require('./readFilesInPath')(`./${distPathname}/public/${appName ? `${appName}/` : ''}${dir}`),
|
100 | // file
|
101 | // )}`
|
102 | }
|
103 |
|
104 | module.exports = getFilePath
|
105 | // module.exports = (pathname, pathDist = 'dist') => {
|
106 | // if (__DEV__) {
|
107 | // return `http://localhost:${process.env.WEBPACK_DEV_SERVER_PORT || '3001'}/dist/${pathname}`
|
108 | // } else {
|
109 | // const segs = pathname.split('/').filter(seg => seg !== '/')
|
110 | // const file = segs.pop()
|
111 | // const dir = segs.length ? `${segs.join('/')}/` : ''
|
112 | // return `/${dir}${
|
113 | // require('./filterTargetFile')(
|
114 | // require('./readFilesInPath')(`./${pathDist}/public/${dir}`),
|
115 | // file
|
116 | // )}`
|
117 | // }
|
118 | // }
|