@dcloudio/uni-cli-shared 2.0.0-alpha-32620210831002 → 2.0.0-alpha-32720210917002
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/file-loader.js +17 -0
- package/lib/i18n.js +99 -0
- package/lib/url-loader.js +3 -17
- package/package.json +2 -2
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
const path = require('path')
|
|
2
|
+
|
|
3
|
+
const isWin = /^win/.test(process.platform)
|
|
4
|
+
|
|
5
|
+
const normalizePath = path => (isWin ? path.replace(/\\/g, '/') : path)
|
|
6
|
+
|
|
7
|
+
module.exports = {
|
|
8
|
+
loader: 'file-loader',
|
|
9
|
+
options: {
|
|
10
|
+
publicPath (url, resourcePath, context) {
|
|
11
|
+
return '/' + normalizePath(path.relative(process.env.UNI_INPUT_DIR, resourcePath))
|
|
12
|
+
},
|
|
13
|
+
outputPath (url, resourcePath, context) {
|
|
14
|
+
return normalizePath(path.relative(process.env.UNI_INPUT_DIR, resourcePath))
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
package/lib/i18n.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
const fs = require('fs')
|
|
2
|
+
const path = require('path')
|
|
3
|
+
const {
|
|
4
|
+
parseJson
|
|
5
|
+
} = require('./json')
|
|
6
|
+
const {
|
|
7
|
+
getManifestJson
|
|
8
|
+
} = require('./manifest')
|
|
9
|
+
|
|
10
|
+
const delimiters = ['%', '%']
|
|
11
|
+
|
|
12
|
+
function initI18nOptions (
|
|
13
|
+
platform,
|
|
14
|
+
inputDir,
|
|
15
|
+
warning = false,
|
|
16
|
+
withMessages = true
|
|
17
|
+
) {
|
|
18
|
+
const locales = initLocales(path.resolve(inputDir, 'locale'), withMessages)
|
|
19
|
+
if (!Object.keys(locales).length) {
|
|
20
|
+
return
|
|
21
|
+
}
|
|
22
|
+
const manifestJson = getManifestJson()
|
|
23
|
+
const fallbackLocale = manifestJson.fallbackLocale || manifestJson.locale
|
|
24
|
+
const locale = resolveI18nLocale(
|
|
25
|
+
platform,
|
|
26
|
+
Object.keys(locales),
|
|
27
|
+
fallbackLocale
|
|
28
|
+
)
|
|
29
|
+
if (warning) {
|
|
30
|
+
if (!fallbackLocale) {
|
|
31
|
+
console.warn()
|
|
32
|
+
} else if (locale !== fallbackLocale) {
|
|
33
|
+
console.warn()
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
locale,
|
|
38
|
+
locales,
|
|
39
|
+
delimiters
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const localeJsonRE = /uni-app.*.json/
|
|
44
|
+
|
|
45
|
+
function isUniAppLocaleFile (filepath) {
|
|
46
|
+
if (!filepath) {
|
|
47
|
+
return false
|
|
48
|
+
}
|
|
49
|
+
return localeJsonRE.test(path.basename(filepath))
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function parseLocaleJson (filepath) {
|
|
53
|
+
let jsonObj = parseJson(fs.readFileSync(filepath, 'utf8'))
|
|
54
|
+
if (isUniAppLocaleFile(filepath)) {
|
|
55
|
+
jsonObj = jsonObj.common || {}
|
|
56
|
+
}
|
|
57
|
+
return jsonObj
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function initLocales (dir, withMessages = true) {
|
|
61
|
+
if (!fs.existsSync(dir)) {
|
|
62
|
+
return {}
|
|
63
|
+
}
|
|
64
|
+
return fs.readdirSync(dir).reduce((res, filename) => {
|
|
65
|
+
if (path.extname(filename) === '.json') {
|
|
66
|
+
const locale = path
|
|
67
|
+
.basename(filename)
|
|
68
|
+
.replace(/(uni-app.)?(.*).json/, '$2')
|
|
69
|
+
if (withMessages) {
|
|
70
|
+
Object.assign(
|
|
71
|
+
res[locale] || (res[locale] = {}),
|
|
72
|
+
parseLocaleJson(path.join(dir, filename))
|
|
73
|
+
)
|
|
74
|
+
} else {
|
|
75
|
+
res[locale] = {}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return res
|
|
79
|
+
}, {})
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function resolveI18nLocale (platfrom, locales, locale) {
|
|
83
|
+
if (locale && locales.includes(locale)) {
|
|
84
|
+
return locale
|
|
85
|
+
}
|
|
86
|
+
const defaultLocales = ['zh-Hans', 'zh-Hant']
|
|
87
|
+
if (platfrom === 'app' || platfrom === 'h5') {
|
|
88
|
+
defaultLocales.unshift('en')
|
|
89
|
+
} else {
|
|
90
|
+
// 小程序
|
|
91
|
+
defaultLocales.push('en')
|
|
92
|
+
}
|
|
93
|
+
return defaultLocales.find(locale => locales.includes(locale)) || locales[0]
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
module.exports = {
|
|
97
|
+
initLocales,
|
|
98
|
+
initI18nOptions
|
|
99
|
+
}
|
package/lib/url-loader.js
CHANGED
|
@@ -1,22 +1,8 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
3
|
-
const isWin = /^win/.test(process.platform)
|
|
4
|
-
|
|
5
|
-
const normalizePath = path => (isWin ? path.replace(/\\/g, '/') : path)
|
|
1
|
+
const fileLoader = require('./file-loader.js')
|
|
6
2
|
|
|
7
3
|
const defaultOptions = {
|
|
8
4
|
limit: -1,
|
|
9
|
-
fallback:
|
|
10
|
-
loader: 'file-loader',
|
|
11
|
-
options: {
|
|
12
|
-
publicPath (url, resourcePath, context) {
|
|
13
|
-
return '/' + normalizePath(path.relative(process.env.UNI_INPUT_DIR, resourcePath))
|
|
14
|
-
},
|
|
15
|
-
outputPath (url, resourcePath, context) {
|
|
16
|
-
return normalizePath(path.relative(process.env.UNI_INPUT_DIR, resourcePath))
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
5
|
+
fallback: fileLoader
|
|
20
6
|
}
|
|
21
7
|
|
|
22
8
|
const inlineLimit =
|
|
@@ -60,4 +46,4 @@ module.exports = {
|
|
|
60
46
|
}
|
|
61
47
|
},
|
|
62
48
|
rewriteUrl
|
|
63
|
-
}
|
|
49
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dcloudio/uni-cli-shared",
|
|
3
|
-
"version": "2.0.0-alpha-
|
|
3
|
+
"version": "2.0.0-alpha-32720210917002",
|
|
4
4
|
"description": "uni-cli-shared",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"repository": {
|
|
@@ -23,5 +23,5 @@
|
|
|
23
23
|
"postcss-urlrewrite": "^0.2.2",
|
|
24
24
|
"strip-json-comments": "^2.0.1"
|
|
25
25
|
},
|
|
26
|
-
"gitHead": "
|
|
26
|
+
"gitHead": "73661effe661588ff28ef675a239856e1afee918"
|
|
27
27
|
}
|