"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; function _react() { const data = _interopRequireDefault(require("react")); _react = function _react() { return data; }; return data; } function _path() { const data = require("path"); _path = function _path() { return data; }; return data; } function _fs() { const data = require("fs"); _fs = function _fs() { return data; }; return data; } var _utils = require("./utils"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } let hasAntd = false; try { hasAntd = !!require.resolve('antd'); } catch (_) { console.log('@umijs/plugin-locale WARNING: antd is not installed, unavailable.'); } var _default = api => { var _api$userConfig; const paths = api.paths, _api$utils = api.utils, Mustache = _api$utils.Mustache, winPath = _api$utils.winPath, lodash = _api$utils.lodash; api.describe({ key: 'locale', config: { default: { baseNavigator: true, useLocalStorage: true, baseSeparator: '-', antd: !!hasAntd }, schema(joi) { return joi.object({ default: joi.string(), useLocalStorage: joi.boolean(), baseNavigator: joi.boolean(), title: joi.boolean(), antd: joi.boolean(), baseSeparator: joi.string() }); } }, enableBy: api.EnableBy.config }); // polyfill if ((0, _utils.isNeedPolyfill)(((_api$userConfig = api.userConfig) === null || _api$userConfig === void 0 ? void 0 : _api$userConfig.targets) || {})) { api.addEntryImportsAhead(() => ({ source: require.resolve('intl') })); } const addAntdLocales = /*#__PURE__*/function () { var _ref = _asyncToGenerator(function* (args) { var _api$config; return yield api.applyPlugins({ key: 'addAntdLocales', type: api.ApplyPluginsType.add, initialValue: [`antd/${((_api$config = api.config) === null || _api$config === void 0 ? void 0 : _api$config.ssr) ? 'lib' : 'es'}/locale/${(0, _utils.getAntdLocale)(args.lang, args.country)}`], args }); }); return function addAntdLocales(_x) { return _ref.apply(this, arguments); }; }(); const getList = /*#__PURE__*/function () { var _ref2 = _asyncToGenerator(function* () { var _api$config2, _api$config$locale; return (0, _utils.getLocaleList)({ localeFolder: ((_api$config2 = api.config) === null || _api$config2 === void 0 ? void 0 : _api$config2.singular) ? 'locale' : 'locales', separator: (_api$config$locale = api.config.locale) === null || _api$config$locale === void 0 ? void 0 : _api$config$locale.baseSeparator, absSrcPath: paths.absSrcPath, absPagesPath: paths.absPagesPath, addAntdLocales }); }); return function getList() { return _ref2.apply(this, arguments); }; }(); // add runtime locale api.addRuntimePluginKey(() => 'locale'); // 生成临时文件 api.onGenerateFiles( /*#__PURE__*/_asyncToGenerator(function* () { var _api$config$locale2, _api$config$locale3, _api$config$locale4, _api$config3; const localeTpl = (0, _fs().readFileSync)((0, _path().join)(winPath(__dirname), 'templates', 'locale.tpl'), 'utf-8'); const _ref4 = api.config.locale, baseSeparator = _ref4.baseSeparator, baseNavigator = _ref4.baseNavigator, antd = _ref4.antd, title = _ref4.title, useLocalStorage = _ref4.useLocalStorage; const defaultLocale = ((_api$config$locale2 = api.config.locale) === null || _api$config$locale2 === void 0 ? void 0 : _api$config$locale2.default) || `zh${baseSeparator}CN`; const localeList = yield getList(); const momentLocales = localeList.map(({ momentLocale }) => momentLocale).filter(locale => locale); const antdLocales = localeList.map(({ antdLocale }) => antdLocale).filter(locale => locale); let MomentLocales = momentLocales; let DefaultMomentLocale = ''; // set moment default accounding to locale.default if (!MomentLocales.length && ((_api$config$locale3 = api.config.locale) === null || _api$config$locale3 === void 0 ? void 0 : _api$config$locale3.default)) { const _defaultLocale$split = defaultLocale.split(baseSeparator), _defaultLocale$split2 = _slicedToArray(_defaultLocale$split, 2), lang = _defaultLocale$split2[0], _defaultLocale$split3 = _defaultLocale$split2[1], country = _defaultLocale$split3 === void 0 ? '' : _defaultLocale$split3; const _getMomentLocale = (0, _utils.getMomentLocale)(lang, country), momentLocale = _getMomentLocale.momentLocale; if (momentLocale) { MomentLocales = [momentLocale]; DefaultMomentLocale = momentLocale; } } let DefaultAntdLocales = []; // set antd default locale if (!antdLocales.length && ((_api$config$locale4 = api.config.locale) === null || _api$config$locale4 === void 0 ? void 0 : _api$config$locale4.antd)) { const _defaultLocale$split4 = defaultLocale.split(baseSeparator), _defaultLocale$split5 = _slicedToArray(_defaultLocale$split4, 2), lang = _defaultLocale$split5[0], _defaultLocale$split6 = _defaultLocale$split5[1], country = _defaultLocale$split6 === void 0 ? '' : _defaultLocale$split6; DefaultAntdLocales = lodash.uniq((yield addAntdLocales({ lang, country }))); } api.writeTmpFile({ content: Mustache.render(localeTpl, { MomentLocales, DefaultMomentLocale, DefaultAntdLocales, Antd: !!antd, Title: title && api.config.title, BaseSeparator: baseSeparator, DefaultLocale: defaultLocale, DefaultLang: defaultLocale }), path: 'plugin-locale/locale.tsx' }); const localeExportsTpl = (0, _fs().readFileSync)((0, _path().join)(__dirname, 'templates', 'localeExports.tpl'), 'utf-8'); api.writeTmpFile({ path: 'plugin-locale/localeExports.ts', content: Mustache.render(localeExportsTpl, { BaseSeparator: baseSeparator, BaseNavigator: baseNavigator, UseLocalStorage: !!useLocalStorage, LocaleDir: api.config.singular ? 'locale' : 'locales', LocaleList: localeList, Antd: !!antd, DefaultLocale: JSON.stringify(defaultLocale), warningPkgPath: winPath(require.resolve('warning')), // react-intl main use `dist/index.js` // use dirname let webpack identify main or module reactIntlPkgPath: winPath((0, _path().dirname)(require.resolve('react-intl/package'))) }) }); // runtime.tsx const runtimeTpl = (0, _fs().readFileSync)((0, _path().join)(__dirname, 'templates', 'runtime.tpl'), 'utf-8'); api.writeTmpFile({ path: 'plugin-locale/runtime.tsx', content: Mustache.render(runtimeTpl, { Title: !!title }) }); // SelectLang.tsx const selectLang = (0, _fs().readFileSync)((0, _path().join)(__dirname, 'templates', 'SelectLang.tpl'), 'utf-8'); api.writeTmpFile({ path: 'plugin-locale/SelectLang.tsx', content: Mustache.render(selectLang, { Antd: !!antd, LocaleList: localeList, ShowSelectLang: localeList.length > 1 && !!antd, antdFiles: ((_api$config3 = api.config) === null || _api$config3 === void 0 ? void 0 : _api$config3.ssr) ? 'lib' : 'es' }) }); })); api.addRuntimePluginKey(() => 'locale'); // Runtime Plugin api.addRuntimePlugin(() => (0, _path().join)(paths.absTmpPath, 'plugin-locale/runtime.tsx')); // Modify entry js api.addEntryCodeAhead(() => `require('./plugin-locale/locale')._onCreate();`.trim()); // watch locale files api.addTmpGenerateWatcherPaths( /*#__PURE__*/_asyncToGenerator(function* () { const localeList = yield getList(); return (0, _utils.exactLocalePaths)(localeList); })); api.addUmiExports(() => { return { exportAll: true, source: `../plugin-locale/localeExports` }; }); api.addUmiExports(() => { return { exportAll: true, source: `../plugin-locale/SelectLang` }; }); }; exports.default = _default;