diff --git a/packages/fes-builder-webpack/src/plugins/commands/buildDevUtils.js b/packages/fes-builder-webpack/src/plugins/commands/buildDevUtils.js index 8087423b..b19c2ebd 100644 --- a/packages/fes-builder-webpack/src/plugins/commands/buildDevUtils.js +++ b/packages/fes-builder-webpack/src/plugins/commands/buildDevUtils.js @@ -51,7 +51,7 @@ export async function getBundleAndConfigs({ api }) { return api.applyPlugins({ key: 'addHTMLHeadScripts', type: api.ApplyPluginsType.add, - initialState: [], + initialValue: [], }); }, publicPath: await api.applyPlugins({ diff --git a/packages/fes-plugin-layout/build.config.js b/packages/fes-plugin-layout/build.config.js index 828db044..87b511e5 100644 --- a/packages/fes-plugin-layout/build.config.js +++ b/packages/fes-plugin-layout/build.config.js @@ -1,4 +1,3 @@ - module.exports = { - copy: ['runtime'] + copy: ['runtime'], }; diff --git a/packages/fes-plugin-layout/src/runtime/helpers/getRuntimeConfig.js b/packages/fes-plugin-layout/src/runtime/helpers/getRuntimeConfig.js index c44b2797..1ca1ef5c 100644 --- a/packages/fes-plugin-layout/src/runtime/helpers/getRuntimeConfig.js +++ b/packages/fes-plugin-layout/src/runtime/helpers/getRuntimeConfig.js @@ -1,7 +1,5 @@ - - import { plugin, ApplyPluginsType } from '@@/core/coreExports'; -import { inject } from 'vue'; +import { initialState } from '@@/initialState'; let runtimeConfig; @@ -11,11 +9,11 @@ export default () => { key: 'layout', type: ApplyPluginsType.modify, initialValue: { - initialState: inject('initialState'), + initialState, sidebar: true, header: true, - logo: true - } + logo: true, + }, }); } return runtimeConfig; diff --git a/packages/fes-plugin-model/src/runtime/models/initialState.js b/packages/fes-plugin-model/src/runtime/models/initialState.js index da7c6f64..34ab20c1 100644 --- a/packages/fes-plugin-model/src/runtime/models/initialState.js +++ b/packages/fes-plugin-model/src/runtime/models/initialState.js @@ -1,5 +1,5 @@ -import { inject } from 'vue'; +import { initialState } from '@@/initialState'; export default function initialStateModel() { - return inject('initialState'); + return initialState; } diff --git a/packages/fes-preset-built-in/package.json b/packages/fes-preset-built-in/package.json index 0d45a57b..576093d9 100644 --- a/packages/fes-preset-built-in/package.json +++ b/packages/fes-preset-built-in/package.json @@ -32,6 +32,7 @@ "express": "^4.17.3" }, "peerDependencies": { + "vue": "^3.0.5", "@vue/compiler-sfc": "^3.0.5" }, "typings": "./types.d.ts" diff --git a/packages/fes-preset-built-in/src/plugins/generateFiles/fes/fes.tpl b/packages/fes-preset-built-in/src/plugins/generateFiles/fes/fes.tpl index a062d20d..06d82368 100644 --- a/packages/fes-preset-built-in/src/plugins/generateFiles/fes/fes.tpl +++ b/packages/fes-preset-built-in/src/plugins/generateFiles/fes/fes.tpl @@ -7,15 +7,15 @@ import { plugin } from './core/plugin'; import './core/pluginRegister'; import { ApplyPluginsType } from '{{{ runtimePath }}}'; import { getRoutes } from './core/routes/routes'; - -import DefaultContainer from './defaultContainer'; +import { updateInitialState } from './initialState'; +import DefaultContainer from './defaultContainer.jsx'; {{{ imports }}} {{{ entryCodeAhead }}} const renderClient = (opts = {}) => { - const { plugin, routes, rootElement, initialState } = opts; + const { plugin, routes, rootElement } = opts; const rootContainer = plugin.applyPlugins({ type: ApplyPluginsType.modify, key: 'rootContainer', @@ -27,8 +27,6 @@ const renderClient = (opts = {}) => { }); const app = createApp(rootContainer); - // initialState是响应式的,后期可以更改 - app.provide("initialState", reactive(initialState ?? {})); plugin.applyPlugins({ key: 'onAppCreated', @@ -51,19 +49,18 @@ const beforeRender = async ({rootElement}) => { action: null }, }); - let initialState = {}; if (typeof beforeRenderConfig.action === "function") { const app = createApp(beforeRenderConfig.loading); app.mount(rootElement); try { - initialState = await beforeRenderConfig.action(); + const initialState = await beforeRenderConfig.action(); + updateInitialState(initialState || {}) } catch(e){ console.error(`[fes] beforeRender执行出现异常:`); console.error(e); } app.unmount(); } - return initialState; }; const getClientRender = (args = {}) => plugin.applyPlugins({ @@ -82,8 +79,8 @@ const getClientRender = (args = {}) => plugin.applyPlugins({ {{/enableTitle}} }, }); - const initialState = await beforeRender(opts); - return renderClient({...opts, initialState}); + await beforeRender(opts); + return renderClient(opts); }, args, }); diff --git a/packages/fes-preset-built-in/src/plugins/generateFiles/fes/index.js b/packages/fes-preset-built-in/src/plugins/generateFiles/fes/index.js index ad65a3c7..a15bae4e 100644 --- a/packages/fes-preset-built-in/src/plugins/generateFiles/fes/index.js +++ b/packages/fes-preset-built-in/src/plugins/generateFiles/fes/index.js @@ -65,5 +65,10 @@ export default function (api) { runtimePath, }), }); + + api.writeTmpFile({ + path: `initialState.js`, + content: Mustache.render(readFileSync(join(__dirname, `./initialState.tpl`), 'utf-8')), + }); }); } diff --git a/packages/fes-preset-built-in/src/plugins/generateFiles/fes/initialState.tpl b/packages/fes-preset-built-in/src/plugins/generateFiles/fes/initialState.tpl new file mode 100644 index 00000000..0d2f439a --- /dev/null +++ b/packages/fes-preset-built-in/src/plugins/generateFiles/fes/initialState.tpl @@ -0,0 +1,7 @@ +import { reactive } from 'vue'; + +export const initialState = reactive({}); + +export const updateInitialState = (obj) => { + Object.assign(initialState, obj); +}; diff --git a/packages/fes-template/src/components/UserCenter.vue b/packages/fes-template/src/components/UserCenter.vue index 8b0829a5..6bbb7563 100644 --- a/packages/fes-template/src/components/UserCenter.vue +++ b/packages/fes-template/src/components/UserCenter.vue @@ -1,5 +1,5 @@ - {{initialState.userName}} + {{ initialState.userName }}