Skip to content

Commit

Permalink
feat: migrate state plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
caohuilin committed Dec 12, 2024
1 parent 0e1b9d9 commit 950a386
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 36 deletions.
4 changes: 2 additions & 2 deletions packages/runtime/plugin-runtime/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export const runtimePlugin = (params?: {
usePlugins: params?.plugins || [
ssrPlugin(),
routerPlugin(),
statePlugin() as any,
documentPlugin(),
statePlugin(),
documentPlugin() as any,
],
setup: api => {
api.checkEntryPoint(({ path, entry }) => {
Expand Down
66 changes: 32 additions & 34 deletions packages/runtime/plugin-runtime/src/state/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,46 @@
import type { AppTools, CliPlugin } from '@modern-js/app-tools';
import type { AppTools, CliPluginFuture } from '@modern-js/app-tools';
import { createRuntimeExportsUtils, getEntryOptions } from '@modern-js/utils';

const PLUGIN_IDENTIFIER = 'state';

export const statePlugin = (): CliPlugin<AppTools> => ({
export const statePlugin = (): CliPluginFuture<AppTools> => ({
name: '@modern-js/plugin-state',

required: ['@modern-js/runtime'],

setup: api => {
return {
_internalRuntimePlugins({ entrypoint, plugins }) {
const { entryName, isMainEntry } = entrypoint;
const userConfig = api.useResolvedConfigContext();
const { packageName, metaName } = api.useAppContext();
api._internalRuntimePlugins(({ entrypoint, plugins }) => {
const { entryName, isMainEntry } = entrypoint;
const userConfig = api.useResolvedConfigContext();
const { packageName, metaName } = api.useAppContext();

const stateConfig = getEntryOptions(
entryName,
isMainEntry,
userConfig.runtime,
userConfig.runtimeByEntries,
packageName,
)?.state;
if (stateConfig) {
plugins.push({
name: PLUGIN_IDENTIFIER,
path: `@${metaName}/runtime/model`,
config: typeof stateConfig === 'boolean' ? {} : stateConfig,
});
}
return { entrypoint, plugins };
},
addRuntimeExports() {
const { internalDirectory, metaName } = api.useAppContext();
const stateConfig = getEntryOptions(
entryName,
isMainEntry,
userConfig.runtime,
userConfig.runtimeByEntries,
packageName,
)?.state;
if (stateConfig) {
plugins.push({
name: PLUGIN_IDENTIFIER,
path: `@${metaName}/runtime/model`,
config: typeof stateConfig === 'boolean' ? {} : stateConfig,
});
}
return { entrypoint, plugins };
});
api.addRuntimeExports(() => {
const { internalDirectory, metaName } = api.useAppContext();

const pluginsExportsUtils = createRuntimeExportsUtils(
internalDirectory,
'plugins',
);
pluginsExportsUtils.addExport(
`export { default as state } from '@${metaName}/runtime/model'`,
);
},
};
const pluginsExportsUtils = createRuntimeExportsUtils(
internalDirectory,
'plugins',
);
pluginsExportsUtils.addExport(
`export { default as state } from '@${metaName}/runtime/model'`,
);
});
},
});

Expand Down

0 comments on commit 950a386

Please sign in to comment.