diff --git a/packages/uni-cli-shared/lib/i18n.js b/packages/uni-cli-shared/lib/i18n.js index 54e678bef242e4a200eeca93055d7fa06cae3032..a40977db65c345cf07aa6a93eba74f5b22d719d2 100644 --- a/packages/uni-cli-shared/lib/i18n.js +++ b/packages/uni-cli-shared/lib/i18n.js @@ -1,7 +1,11 @@ const fs = require('fs') const path = require('path') -const { parseJson } = require('./json') -const { getManifestJson } = require('./manifest') +const { + parseJson +} = require('./json') +const { + getManifestJson +} = require('./manifest') const delimiters = ['%', '%'] @@ -36,17 +40,40 @@ function initI18nOptions ( } } +const localeJsonRE = /uni-app.*.json/ + +function isUniAppLocaleFile (filepath) { + if (!filepath) { + return false + } + return localeJsonRE.test(path.basename(filepath)) +} + +function parseLocaleJson (filepath) { + let jsonObj = parseJson(fs.readFileSync(filepath, 'utf8')) + if (isUniAppLocaleFile(filepath)) { + jsonObj = jsonObj.common || {} + } + return jsonObj +} + function initLocales (dir, withMessages = true) { if (!fs.existsSync(dir)) { return {} } return fs.readdirSync(dir).reduce((res, filename) => { if (path.extname(filename) === '.json') { - try { - res[path.basename(filename).replace('.json', '')] = withMessages - ? parseJson(fs.readFileSync(path.join(dir, filename), 'utf8')) - : {} - } catch (e) {} + const locale = path + .basename(filename) + .replace(/(uni-app.)?(.*).json/, '$2') + if (withMessages) { + Object.assign( + res[locale] || (res[locale] = {}), + parseLocaleJson(path.join(dir, filename)) + ) + } else { + res[locale] = {} + } } return res }, {}) @@ -69,4 +96,4 @@ function resolveI18nLocale (platfrom, locales, locale) { module.exports = { initLocales, initI18nOptions -} +} diff --git a/packages/uni-migration/bin/uni-migration.js b/packages/uni-migration/bin/uni-migration.js old mode 100644 new mode 100755 index 2cdb7f33c4304c43624a3aedf334417032f83be1..1bcd175d9747443e81da1a11fad75da3a41719dc --- a/packages/uni-migration/bin/uni-migration.js +++ b/packages/uni-migration/bin/uni-migration.js @@ -1,4 +1,4 @@ -#!/usr/bin/env node +#!/usr/bin/env node const path = require('path') const Program = require('commander') diff --git a/packages/webpack-uni-pages-loader/lib/index-new.js b/packages/webpack-uni-pages-loader/lib/index-new.js index 01df0f05f72c2be9bf81f69e632a936298712db7..049081ac64e29073ad867c7633779f3f655b611c 100644 --- a/packages/webpack-uni-pages-loader/lib/index-new.js +++ b/packages/webpack-uni-pages-loader/lib/index-new.js @@ -16,7 +16,10 @@ const { updateProjectJson } = require('@dcloudio/uni-cli-shared/lib/cache') -const { initTheme, parseTheme } = require('@dcloudio/uni-cli-shared/lib/theme') +const { + initTheme, + parseTheme +} = require('@dcloudio/uni-cli-shared/lib/theme') const { // pagesJsonJsFileName, @@ -27,11 +30,15 @@ const uniI18n = require('@dcloudio/uni-cli-i18n') const parseStyle = require('./util').parseStyle -const { initI18nOptions } = require('@dcloudio/uni-cli-shared/lib/i18n') -const { parseI18nJson } = require('@dcloudio/uni-i18n') +const { + initI18nOptions +} = require('@dcloudio/uni-cli-shared/lib/i18n') +const { + parseI18nJson +} = require('@dcloudio/uni-i18n') // 将开发者手动设置的 usingComponents 调整名称,方便与自动解析到的 usingComponents 做最后合并 -function renameUsingComponents (jsonObj) { +function renameUsingComponents(jsonObj) { if (jsonObj.usingComponents) { jsonObj.customUsingComponents = jsonObj.usingComponents delete jsonObj.usingComponents @@ -39,7 +46,7 @@ function renameUsingComponents (jsonObj) { return jsonObj } -module.exports = function (content, map) { +module.exports = function(content, map) { this.cacheable && this.cacheable() initTheme() @@ -60,6 +67,7 @@ module.exports = function (content, map) { ) // this.addDependency(pagesJsonJsPath) + this.addContextDependency(path.resolve(process.env.UNI_INPUT_DIR, 'locale')) this.addDependency(manifestJsonPath) let pagesJson = parsePagesJson(content, { @@ -117,20 +125,24 @@ module.exports = function (content, map) { true ) if (i18nOptions) { - const { locale, locales, delimiters } = i18nOptions + const { + locale, + locales, + delimiters + } = i18nOptions parseI18nJson(pagesJson, locales[locale], delimiters) } } if (!process.env.UNI_USING_V3) { parsePages( pagesJson, - function (page) { + function(page) { updatePageJson( page.path, renameUsingComponents(parseStyle(page.style)) ) }, - function (root, page) { + function(root, page) { updatePageJson( normalizePath(path.join(root, page.path)), renameUsingComponents(parseStyle(page.style, root)) @@ -202,4 +214,4 @@ module.exports = function (content, map) { } this.callback(null, '', map) -} +} diff --git a/packages/webpack-uni-pages-loader/lib/platforms/app-plus/index.js b/packages/webpack-uni-pages-loader/lib/platforms/app-plus/index.js index de25a954d58ffab2a1abc41bed70153a19399ca7..ea9e5d49ae9e3566aba346851d2eef7c2231be53 100644 --- a/packages/webpack-uni-pages-loader/lib/platforms/app-plus/index.js +++ b/packages/webpack-uni-pages-loader/lib/platforms/app-plus/index.js @@ -3,10 +3,20 @@ const fsExtra = require('fs-extra') const path = require('path') const merge = require('merge') -const { normalizePath, getFlexDirection } = require('@dcloudio/uni-cli-shared') -const { compileI18nJsonStr } = require('@dcloudio/uni-i18n') -const { initI18nOptions } = require('@dcloudio/uni-cli-shared/lib/i18n') -const { hasOwn, parseStyle } = require('../../util') +const { + normalizePath, + getFlexDirection +} = require('@dcloudio/uni-cli-shared') +const { + compileI18nJsonStr +} = require('@dcloudio/uni-i18n') +const { + initI18nOptions +} = require('@dcloudio/uni-cli-shared/lib/i18n') +const { + hasOwn, + parseStyle +} = require('../../util') const wxPageOrientationMapping = { auto: [ @@ -95,7 +105,9 @@ function updateFileFlag (appJson) { } module.exports = function (pagesJson, userManifestJson, isAppView) { - const { app } = require('../mp')(pagesJson, userManifestJson) + const { + app + } = require('../mp')(pagesJson, userManifestJson) const manifest = { name: 'manifest' @@ -123,8 +135,7 @@ module.exports = function (pagesJson, userManifestJson, isAppView) { // 用户配置覆盖默认配置 manifestJson = merge.recursive( true, - manifestJson, - { + manifestJson, { id: userManifestJson.appid || '', name: userManifestJson.name || '', description: userManifestJson.description || '', @@ -133,8 +144,7 @@ module.exports = function (pagesJson, userManifestJson, isAppView) { code: userManifestJson.versionCode }, language: userManifestJson.locale - }, - { + }, { plus: userManifestJson['app-plus'] } ) @@ -201,7 +211,10 @@ module.exports = function (pagesJson, userManifestJson, isAppView) { if (nvuePages && nvuePages.length) { const pages = {} - nvuePages.forEach(({ path, style }) => { + nvuePages.forEach(({ + path, + style + }) => { pages[path] = { window: parseStyle(style), nvue: true @@ -388,14 +401,20 @@ module.exports = function (pagesJson, userManifestJson, isAppView) { return ( path.replace(/\.html$/, '.nvue') === key || path.replace(/\.html$/, '.nvue') + '.nvue' === key || - subNVues.find(({ path }) => path === key.replace(/\.nvue$/, '')) + subNVues.find(({ + path + }) => path === key.replace(/\.nvue$/, '')) ) }) && - !pagesJson.pages.find(({ style = {} }) => { + !pagesJson.pages.find(({ + style = {} + }) => { style = Object.assign(style, style['app-plus']) const subNVues = style.subNVues || [] return subNVues.find( - ({ path }) => path === key.replace(/\.nvue$/, '') + ({ + path + }) => path === key.replace(/\.nvue$/, '') ) }) ) { @@ -491,8 +510,7 @@ module.exports = function (pagesJson, userManifestJson, isAppView) { pagesJson.tabBar.list && pagesJson.tabBar.list.length ) { - const tabBar = (manifestJson.plus.tabBar = Object.assign( - {}, + const tabBar = (manifestJson.plus.tabBar = Object.assign({}, pagesJson.tabBar )) const borderStyles = { @@ -518,9 +536,9 @@ module.exports = function (pagesJson, userManifestJson, isAppView) { const item = tabBar.list.find( page => page.pagePath === - (process.env.UNI_USING_NATIVE - ? appJson.entryPagePath - : entryPagePath) + (process.env.UNI_USING_NATIVE + ? appJson.entryPagePath + : entryPagePath) ) if (item) { tabBar.child = ['lauchwebview'] @@ -535,8 +553,8 @@ module.exports = function (pagesJson, userManifestJson, isAppView) { true ) if (i18nOptions) { - manifestJson.plus.tabBar = JSON.parse( - compileI18nJsonStr(JSON.stringify(tabBar), i18nOptions) + manifestJson = JSON.parse( + compileI18nJsonStr(JSON.stringify(manifestJson), i18nOptions) ) manifestJson.fallbackLocale = i18nOptions.locale } @@ -579,8 +597,7 @@ module.exports = function (pagesJson, userManifestJson, isAppView) { } return require('./index.v3')( appJson, - manifestJson, - { + manifestJson, { manifest, pagesJson, normalizeNetworkTimeout @@ -589,4 +606,4 @@ module.exports = function (pagesJson, userManifestJson, isAppView) { ) } return [app, manifest] -} +} diff --git a/packages/webpack-uni-pages-loader/lib/platforms/h5.js b/packages/webpack-uni-pages-loader/lib/platforms/h5.js index 81fb930a0edc9638c6d32000dbbcb3c544e6ae3f..af5131e8f6462a5fdd82d57f4f1528d146fce453 100644 --- a/packages/webpack-uni-pages-loader/lib/platforms/h5.js +++ b/packages/webpack-uni-pages-loader/lib/platforms/h5.js @@ -458,7 +458,7 @@ global.__uniConfig.sdkConfigs = ${JSON.stringify(sdkConfigs)}; global.__uniConfig.qqMapKey = ${JSON.stringify(qqMapKey)}; global.__uniConfig.locale = ${JSON.stringify(locale)}; global.__uniConfig.fallbackLocale = ${JSON.stringify(manifestJson.fallbackLocale)}; -global.__uniConfig.locales = locales.keys().reduce((res,key)=>{res[key.replace(/\\.\\/(.*).json/,'$1')]=locales(key);return res},{}); +global.__uniConfig.locales = locales.keys().reduce((res,key)=>{const locale=key.replace(/\\.\\/(uni-app.)?(.*).json/,'$2');const messages = locales(key);Object.assign(res[locale]||(res[locale]={}),messages.common||messages);return res},{}); global.__uniConfig.nvue = ${JSON.stringify({ 'flex-direction': getFlexDirection(manifestJson['app-plus']) })} global.__uniConfig.__webpack_chunk_load__ = __webpack_chunk_load__ ${genRegisterPageVueComponentsCode(pageComponents)}