UNPKG

umi-plugin-keep-alive

Version:

<KeepAlive> for umi base on react-activation

94 lines (89 loc) 3.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _path = require("path"); var _fs = require("fs"); var _utils = require("@umijs/utils"); 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); }); }; } const Encoding = 'utf-8'; var _default = api => { // 通过新增方法判断umi4 const isUmi4 = typeof api.modifyAppData === 'function'; if (isUmi4) { // umi@4 自动生成插件目录 api.addRuntimePlugin({ fn: () => '@@/plugin-keepAlive/runtime', stage: -1 * Number.MAX_SAFE_INTEGER }); } else { api.addRuntimePlugin({ fn: () => '@@/plugin-keep-alive/runtime', stage: -1 * Number.MAX_SAFE_INTEGER }); } // // 因 keep-alive 的 runtime 部分选择不渲染其 children,可能会丢失默认的用户 rootContainer // // 此处修复自定义 rootContainer // // https://github.com/umijs/umi/blob/master/packages/preset-built-in/src/plugins/generateFiles/core/plugin.ts#L23-L27 // const customizedAppPath = getFile({ // base: api.paths.absSrcPath, // fileNameWithoutExt: 'app', // type: 'javascript', // })?.path // if (customizedAppPath) { // api.addRuntimePlugin({ // fn: () => '@@/plugin-keep-alive/fixCustomizedRuntime', // stage: -1 * Number.MAX_SAFE_INTEGER + 1, // }) // api.onGenerateFiles(async () => { // api.writeTmpFile({ // path: 'plugin-keep-alive/fixCustomizedRuntime.tsx', // content: Mustache.render( // readFileSync(join(__dirname, 'fixCustomizedRuntime.tsx.tpl'), Encoding), // {}, // ), // }) // }) // } // Babel Plugin for react-activation const babelOpt = [require.resolve('react-activation/babel')]; if (typeof api.modifyBabelOpts === 'function') { api.modifyBabelOpts(babelOpts => { babelOpts.plugins.push(babelOpt); return babelOpts; }); } if (typeof api.addExtraBabelPlugins === 'function') { api.addExtraBabelPlugins(() => [babelOpt]); } // 生成:export * from 'react-activation' // TODO: 指明支持的 api,使用上述方式存在 bug https://github.com/guybedford/es-module-lexer/issues/76 // 业务中可 import { KeepAlive } from 'umi' if (isUmi4) { // umi@4 需要通过插件文件进行自动导出 api.onGenerateFiles( /*#__PURE__*/_asyncToGenerator(function* () { api.writeTmpFile({ path: 'index.ts', content: _utils.Mustache.render((0, _fs.readFileSync)((0, _path.join)(__dirname, 'index.ts.tpl'), Encoding), {}) }); api.writeTmpFile({ path: 'runtime.tsx', content: _utils.Mustache.render((0, _fs.readFileSync)((0, _path.join)(__dirname, 'runtime.tsx.tpl'), Encoding), {}) }); })); } else { api.addUmiExports(() => [{ specifiers: ['KeepAlive', 'useActivate', 'autoFixContext', 'useUnactivate', 'withActivation', 'withAliveScope', 'useAliveController'], source: 'react-activation' }]); api.onGenerateFiles( /*#__PURE__*/_asyncToGenerator(function* () { api.writeTmpFile({ path: 'plugin-keep-alive/runtime.tsx', content: _utils.Mustache.render((0, _fs.readFileSync)((0, _path.join)(__dirname, 'runtime.tsx.tpl'), Encoding), {}) }); })); } }; exports.default = _default;