UNPKG

2.89 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 *
11 * @param {string} filename 要查找的文件的文件名
12 * @param {string} localeId 当前语言
13 * @memberof ReactIsomorphic
14 * @returns {string} 文件路径或空
15 */
16const getFilePath = (filename, localeId) => {
17 if (typeof localeId === 'undefined') {
18 try {
19 localeId = require('super-project/i18n').localeId
20 } catch (e) { }
21 }
22
23 const pathPublic = getPublicPath()
24
25 const i18nType = JSON.parse(process.env.SUPER_I18N)
26 ? JSON.parse(process.env.SUPER_I18N_TYPE)
27 : undefined
28 const isI18nDefault = (i18nType === 'default')
29 // const localeId = 'zh'
30
31 if (process.env.WEBPACK_BUILD_ENV === 'dev' || (typeof __DEV__ !== 'undefined' && __DEV__))
32 return pathPublic + (isI18nDefault ? localeId : '') + `.${filename}`
33
34 const chunkmap = getChunkmap(localeId)
35
36 if (typeof chunkmap === 'object') {
37 const extname = path.extname(filename)
38 const key = path.basename(filename, extname)
39 let result
40 if (Array.isArray(chunkmap[key])) {
41 chunkmap[key].some(value => {
42 if (path.extname(value) === extname) {
43 result = value
44 return true
45 }
46 return false
47 })
48 }
49 if (result)
50 return `${pathPublic}${result.replace(/(^\.\/|^)public\//, '')}`
51 }
52
53 // 如果没有找到 chunkmap 或是 chunkmap 中未找到目标项目,转为过滤文件形式
54 if (fs.existsSync(path.resolve(
55 pathPublic,
56 filename
57 ))) {
58 return '/' + filename
59 }
60
61 console.warn(`File not found:` + (isI18nDefault ? `[${localeId}] ` : '') + ` ${filename}`)
62
63 return ''
64
65 // const segs = pathname.split('/').filter(seg => seg !== '/')
66 // const file = segs.pop()
67 // const dir = segs.length ? `${segs.join('/')}/` : ''
68 // return `/${dir}${
69 // require('./filterTargetFile')(
70 // require('./readFilesInPath')(`./${distPathname}/public/${appName ? `${appName}/` : ''}${dir}`),
71 // file
72 // )}`
73}
74
75module.exports = getFilePath
76// module.exports = (pathname, pathDist = 'dist') => {
77// if (__DEV__) {
78// return `http://localhost:${process.env.WEBPACK_DEV_SERVER_PORT || '3001'}/dist/${pathname}`
79// } else {
80// const segs = pathname.split('/').filter(seg => seg !== '/')
81// const file = segs.pop()
82// const dir = segs.length ? `${segs.join('/')}/` : ''
83// return `/${dir}${
84// require('./filterTargetFile')(
85// require('./readFilesInPath')(`./${pathDist}/public/${dir}`),
86// file
87// )}`
88// }
89// }