From b930600a17564e8140208ab7f745d7f1c7141fb2 Mon Sep 17 00:00:00 2001 From: Homyee King Date: Tue, 8 Apr 2025 14:38:05 +0800 Subject: [PATCH 1/4] fix: config use esbuild (#7070) --- packages/ice/src/service/config.ts | 1 + packages/ice/src/service/serverCompiler.ts | 3 ++- packages/ice/src/types/plugin.ts | 2 ++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/ice/src/service/config.ts b/packages/ice/src/service/config.ts index 822ad4584..a18e1a4ca 100644 --- a/packages/ice/src/service/config.ts +++ b/packages/ice/src/service/config.ts @@ -64,6 +64,7 @@ class Config { }, }, redirectImports, + bundler: 'esbuild', }); if (!error) { this.status = 'RESOLVED'; diff --git a/packages/ice/src/service/serverCompiler.ts b/packages/ice/src/service/serverCompiler.ts index c8a058cd1..37e769ac1 100644 --- a/packages/ice/src/service/serverCompiler.ts +++ b/packages/ice/src/service/serverCompiler.ts @@ -116,6 +116,7 @@ export function createServerCompiler(options: Options) { enableEnv = false, transformEnv = true, isServer = true, + bundler, } = {}) => { let preBundleDepsMetadata: PreBundleDepsMetaData; let swcOptions = merge({}, { @@ -171,7 +172,7 @@ export function createServerCompiler(options: Options) { plugins, }); } - server.bundler = server.bundler ?? 'esbuild'; + server.bundler = bundler ?? server.bundler ?? 'esbuild'; const format = customBuildOptions?.format || 'esm'; let buildOptions: esbuild.BuildOptions = { diff --git a/packages/ice/src/types/plugin.ts b/packages/ice/src/types/plugin.ts index 174e1cbc8..6a9df6e09 100644 --- a/packages/ice/src/types/plugin.ts +++ b/packages/ice/src/types/plugin.ts @@ -60,6 +60,8 @@ export interface CompilerOptions { runtimeDefineVars?: Record; enableEnv?: boolean; isServer?: boolean; + /** @default esbuild */ + bundler?: 'webpack' | 'esbuild'; } export type ServerCompiler = ( From 2f73084d61c96d075f97ef74d2cf84d477df2142 Mon Sep 17 00:00:00 2001 From: ClarkXia Date: Wed, 9 Apr 2025 10:27:46 +0800 Subject: [PATCH 2/4] Feat: enhance plugin icestark and refactor webpack bundle exports (#7068) * feat: support custom AppRoute * feat: support framework provider * fix: failed to resolve context * fix: add export type * fix: add the library name to global * feat: add export of webpack * fix: require hooks * fix: export webpack module * feat: add export of webpack * fix: support resolve webpack * fix: revert bundles version * fix: update hook path * fix: render AppRouter * fix: init default value --- .changeset/healthy-rocks-fetch.md | 5 + .changeset/long-maps-search.md | 5 + .changeset/slow-jokes-speak.md | 5 + packages/bundles/webpack/bundle.js | 18 ++ .../bundles/webpack/packages/ChunkHelpers.js | 1 + .../bundles/webpack/packages/Compilation.js | 1 + .../webpack/packages/HotUpdateChunk.js | 1 + packages/bundles/webpack/packages/LazySet.js | 1 + .../webpack/packages/ModuleDependency.js | 1 + .../bundles/webpack/packages/ModuleFactory.js | 1 + .../webpack/packages/ModuleNotFoundError.js | 1 + .../bundles/webpack/packages/SetHelpers.js | 1 + .../bundles/webpack/packages/SortableSet.js | 1 + .../StartupEntrypointRuntimeModule.js | 1 + .../packages/StaticExportsDependency.js | 1 + .../bundles/webpack/packages/WebpackError.js | 1 + .../bundles/webpack/packages/comparators.js | 1 + .../packages/create-schema-validation.js | 1 + .../webpack/packages/extractUrlAndGlobal.js | 1 + packages/bundles/webpack/packages/fs.js | 1 + .../webpack/packages/makeSerializable.js | 1 + packages/bundles/webpack/packages/semver.js | 1 + packages/ice/src/requireHook.ts | 85 +++++---- packages/plugin-icestark/Context.d.ts | 1 + packages/plugin-icestark/package.json | 5 + packages/plugin-icestark/src/index.ts | 8 +- .../plugin-icestark/src/runtime/Context.tsx | 7 + .../plugin-icestark/src/runtime/child.tsx | 8 +- .../plugin-icestark/src/runtime/framework.tsx | 162 +++++++++--------- packages/plugin-icestark/src/types.ts | 4 +- 30 files changed, 211 insertions(+), 120 deletions(-) create mode 100644 .changeset/healthy-rocks-fetch.md create mode 100644 .changeset/long-maps-search.md create mode 100644 .changeset/slow-jokes-speak.md create mode 100644 packages/bundles/webpack/packages/ChunkHelpers.js create mode 100644 packages/bundles/webpack/packages/Compilation.js create mode 100644 packages/bundles/webpack/packages/HotUpdateChunk.js create mode 100644 packages/bundles/webpack/packages/LazySet.js create mode 100644 packages/bundles/webpack/packages/ModuleDependency.js create mode 100644 packages/bundles/webpack/packages/ModuleFactory.js create mode 100644 packages/bundles/webpack/packages/ModuleNotFoundError.js create mode 100644 packages/bundles/webpack/packages/SetHelpers.js create mode 100644 packages/bundles/webpack/packages/SortableSet.js create mode 100644 packages/bundles/webpack/packages/StartupEntrypointRuntimeModule.js create mode 100644 packages/bundles/webpack/packages/StaticExportsDependency.js create mode 100644 packages/bundles/webpack/packages/WebpackError.js create mode 100644 packages/bundles/webpack/packages/comparators.js create mode 100644 packages/bundles/webpack/packages/create-schema-validation.js create mode 100644 packages/bundles/webpack/packages/extractUrlAndGlobal.js create mode 100644 packages/bundles/webpack/packages/fs.js create mode 100644 packages/bundles/webpack/packages/makeSerializable.js create mode 100644 packages/bundles/webpack/packages/semver.js create mode 100644 packages/plugin-icestark/Context.d.ts create mode 100644 packages/plugin-icestark/src/runtime/Context.tsx diff --git a/.changeset/healthy-rocks-fetch.md b/.changeset/healthy-rocks-fetch.md new file mode 100644 index 000000000..8ce206b1b --- /dev/null +++ b/.changeset/healthy-rocks-fetch.md @@ -0,0 +1,5 @@ +--- +'@ice/plugin-icestark': minor +--- + +feat: support framework provider diff --git a/.changeset/long-maps-search.md b/.changeset/long-maps-search.md new file mode 100644 index 000000000..ef6984d9e --- /dev/null +++ b/.changeset/long-maps-search.md @@ -0,0 +1,5 @@ +--- +'@ice/bundles': patch +--- + +feat: export ModuleNotFoundError of webpack diff --git a/.changeset/slow-jokes-speak.md b/.changeset/slow-jokes-speak.md new file mode 100644 index 000000000..c7b9aa33e --- /dev/null +++ b/.changeset/slow-jokes-speak.md @@ -0,0 +1,5 @@ +--- +'@ice/plugin-icestark': minor +--- + +feat: support custom AppRoute diff --git a/packages/bundles/webpack/bundle.js b/packages/bundles/webpack/bundle.js index 27e0b3b14..919ca1e80 100644 --- a/packages/bundles/webpack/bundle.js +++ b/packages/bundles/webpack/bundle.js @@ -18,6 +18,24 @@ module.exports = { StringXor: require('webpack/lib/util/StringXor'), NormalModule: require('webpack/lib/NormalModule'), EntryDependency: require('webpack/lib/dependencies/EntryDependency'), + ModuleNotFoundError: require('webpack/lib/ModuleNotFoundError'), + LazySet: require('webpack/lib/util/LazySet'), + makeSerializable: require('webpack/lib/util/makeSerializable'), + SortableSet: require('webpack/lib/util/SortableSet'), + StaticExportsDependency: require('webpack/lib/dependencies/StaticExportsDependency'), + ModuleFactory: require('webpack/lib/ModuleFactory'), + ModuleDependency: require('webpack/lib/dependencies/ModuleDependency'), + createSchemaValidation: require('webpack/lib/util/create-schema-validation'), + extractUrlAndGlobal: require('webpack/lib/util/extractUrlAndGlobal'), + Compilation: require('webpack/lib/Compilation'), + semver: require('webpack/lib/util/semver'), + WebpackError: require('webpack/lib/WebpackError'), + comparators: require('webpack/lib/util/comparators'), + StartupEntrypointRuntimeModule: require('webpack/lib/runtime/StartupEntrypointRuntimeModule'), + SetHelpers: require('webpack/lib/util/SetHelpers'), + ChunkHelpers: require('webpack/lib/javascript/ChunkHelpers'), + HotUpdateChunk: require('webpack/lib/HotUpdateChunk'), + fs: require('webpack/lib/util/fs'), sources: require('webpack').sources, webpack: require('webpack'), package: { diff --git a/packages/bundles/webpack/packages/ChunkHelpers.js b/packages/bundles/webpack/packages/ChunkHelpers.js new file mode 100644 index 000000000..808451418 --- /dev/null +++ b/packages/bundles/webpack/packages/ChunkHelpers.js @@ -0,0 +1 @@ +module.exports = require('./bundle').ChunkHelpers; \ No newline at end of file diff --git a/packages/bundles/webpack/packages/Compilation.js b/packages/bundles/webpack/packages/Compilation.js new file mode 100644 index 000000000..86ec44745 --- /dev/null +++ b/packages/bundles/webpack/packages/Compilation.js @@ -0,0 +1 @@ +module.exports = require('./bundle').Compilation; \ No newline at end of file diff --git a/packages/bundles/webpack/packages/HotUpdateChunk.js b/packages/bundles/webpack/packages/HotUpdateChunk.js new file mode 100644 index 000000000..57353f3c9 --- /dev/null +++ b/packages/bundles/webpack/packages/HotUpdateChunk.js @@ -0,0 +1 @@ +module.exports = require('./bundle').HotUpdateChunk; \ No newline at end of file diff --git a/packages/bundles/webpack/packages/LazySet.js b/packages/bundles/webpack/packages/LazySet.js new file mode 100644 index 000000000..345f52c23 --- /dev/null +++ b/packages/bundles/webpack/packages/LazySet.js @@ -0,0 +1 @@ +module.exports = require('./bundle').LazySet; \ No newline at end of file diff --git a/packages/bundles/webpack/packages/ModuleDependency.js b/packages/bundles/webpack/packages/ModuleDependency.js new file mode 100644 index 000000000..fafe24be9 --- /dev/null +++ b/packages/bundles/webpack/packages/ModuleDependency.js @@ -0,0 +1 @@ +module.exports = require('./bundle').ModuleDependency; \ No newline at end of file diff --git a/packages/bundles/webpack/packages/ModuleFactory.js b/packages/bundles/webpack/packages/ModuleFactory.js new file mode 100644 index 000000000..2fc048401 --- /dev/null +++ b/packages/bundles/webpack/packages/ModuleFactory.js @@ -0,0 +1 @@ +module.exports = require('./bundle').ModuleFactory; \ No newline at end of file diff --git a/packages/bundles/webpack/packages/ModuleNotFoundError.js b/packages/bundles/webpack/packages/ModuleNotFoundError.js new file mode 100644 index 000000000..ab4d2d761 --- /dev/null +++ b/packages/bundles/webpack/packages/ModuleNotFoundError.js @@ -0,0 +1 @@ +module.exports = require('./bundle').ModuleNotFoundError; diff --git a/packages/bundles/webpack/packages/SetHelpers.js b/packages/bundles/webpack/packages/SetHelpers.js new file mode 100644 index 000000000..59e3639ae --- /dev/null +++ b/packages/bundles/webpack/packages/SetHelpers.js @@ -0,0 +1 @@ +module.exports = require('./bundle').SetHelpers; \ No newline at end of file diff --git a/packages/bundles/webpack/packages/SortableSet.js b/packages/bundles/webpack/packages/SortableSet.js new file mode 100644 index 000000000..e6f2688d6 --- /dev/null +++ b/packages/bundles/webpack/packages/SortableSet.js @@ -0,0 +1 @@ +module.exports = require('./bundle').SortableSet; diff --git a/packages/bundles/webpack/packages/StartupEntrypointRuntimeModule.js b/packages/bundles/webpack/packages/StartupEntrypointRuntimeModule.js new file mode 100644 index 000000000..249a832f2 --- /dev/null +++ b/packages/bundles/webpack/packages/StartupEntrypointRuntimeModule.js @@ -0,0 +1 @@ +module.exports = require('./bundle').StartupEntrypointRuntimeModule; \ No newline at end of file diff --git a/packages/bundles/webpack/packages/StaticExportsDependency.js b/packages/bundles/webpack/packages/StaticExportsDependency.js new file mode 100644 index 000000000..f27782b17 --- /dev/null +++ b/packages/bundles/webpack/packages/StaticExportsDependency.js @@ -0,0 +1 @@ +module.exports = require('./bundle').StaticExportsDependency; \ No newline at end of file diff --git a/packages/bundles/webpack/packages/WebpackError.js b/packages/bundles/webpack/packages/WebpackError.js new file mode 100644 index 000000000..80f2d5b42 --- /dev/null +++ b/packages/bundles/webpack/packages/WebpackError.js @@ -0,0 +1 @@ +module.exports = require('./bundle').WebpackError; \ No newline at end of file diff --git a/packages/bundles/webpack/packages/comparators.js b/packages/bundles/webpack/packages/comparators.js new file mode 100644 index 000000000..6df85b871 --- /dev/null +++ b/packages/bundles/webpack/packages/comparators.js @@ -0,0 +1 @@ +module.exports = require('./bundle').comparators; \ No newline at end of file diff --git a/packages/bundles/webpack/packages/create-schema-validation.js b/packages/bundles/webpack/packages/create-schema-validation.js new file mode 100644 index 000000000..19f4f1005 --- /dev/null +++ b/packages/bundles/webpack/packages/create-schema-validation.js @@ -0,0 +1 @@ +module.exports = require('./bundle').createSchemaValidation; \ No newline at end of file diff --git a/packages/bundles/webpack/packages/extractUrlAndGlobal.js b/packages/bundles/webpack/packages/extractUrlAndGlobal.js new file mode 100644 index 000000000..1ead8e30b --- /dev/null +++ b/packages/bundles/webpack/packages/extractUrlAndGlobal.js @@ -0,0 +1 @@ +module.exports = require('./bundle').extractUrlAndGlobal; \ No newline at end of file diff --git a/packages/bundles/webpack/packages/fs.js b/packages/bundles/webpack/packages/fs.js new file mode 100644 index 000000000..ec52150b4 --- /dev/null +++ b/packages/bundles/webpack/packages/fs.js @@ -0,0 +1 @@ +module.exports = require('./bundle').fs; diff --git a/packages/bundles/webpack/packages/makeSerializable.js b/packages/bundles/webpack/packages/makeSerializable.js new file mode 100644 index 000000000..d2ab18fe2 --- /dev/null +++ b/packages/bundles/webpack/packages/makeSerializable.js @@ -0,0 +1 @@ +module.exports = require('./bundle').makeSerializable; \ No newline at end of file diff --git a/packages/bundles/webpack/packages/semver.js b/packages/bundles/webpack/packages/semver.js new file mode 100644 index 000000000..d44274d97 --- /dev/null +++ b/packages/bundles/webpack/packages/semver.js @@ -0,0 +1 @@ +module.exports = require('./bundle').semver; \ No newline at end of file diff --git a/packages/ice/src/requireHook.ts b/packages/ice/src/requireHook.ts index cc4a891c5..d5e7be557 100644 --- a/packages/ice/src/requireHook.ts +++ b/packages/ice/src/requireHook.ts @@ -12,41 +12,56 @@ export function getFileName(filePath: string) { return filePath.split('/').slice(-1)[0]; } +const webpackPlugins = [ + // plugins require the same webpack instance + 'webpack/lib/LibraryTemplatePlugin', + 'webpack/lib/node/NodeTargetPlugin', + 'webpack/lib/node/NodeTemplatePlugin', + 'webpack/lib/NormalModule', + 'webpack/lib/optimize/LimitChunkCountPlugin', + 'webpack/lib/SingleEntryPlugin', + 'webpack/lib/webworker/WebWorkerTemplatePlugin', + 'webpack/lib/node/NodeEnvironmentPlugin', + 'webpack/lib/ModuleFilenameHelpers', + 'webpack/lib/GraphHelpers', + 'webpack/lib/ExternalsPlugin', + 'webpack/lib/web/FetchCompileAsyncWasmPlugin', + 'webpack/lib/web/FetchCompileWasmPlugin', + 'webpack/lib/runtime/StartupChunkDependenciesPlugin', + 'webpack/lib/javascript/JavascriptModulesPlugin', + 'webpack/lib/javascript/StartupHelpers', + 'webpack/lib/util/identifier', + 'webpack/lib/util/compileBooleanMatcher', + 'webpack/lib/ModuleNotFoundError', + 'webpack/lib/util/LazySet', + 'webpack/lib/util/fs', + 'webpack/lib/util/makeSerializable', + 'webpack/lib/util/SortableSet', + 'webpack/lib/dependencies/StaticExportsDependency', + 'webpack/lib/dependencies/EntryDependency', + 'webpack/lib/ModuleFactory', + 'webpack/lib/dependencies/ModuleDependency', + 'webpack/lib/util/create-schema-validation', + 'webpack/lib/util/extractUrlAndGlobal', + 'webpack/lib/Compilation', + 'webpack/lib/util/semver', + 'webpack/lib/WebpackError', + 'webpack/lib/util/comparators', + 'webpack/lib/runtime/StartupEntrypointRuntimeModule', + 'webpack/lib/util/SetHelpers', + 'webpack/lib/javascript/ChunkHelpers', + 'webpack/lib/HotUpdateChunk', +]; + export function getHookFiles() { - const webpackPlugins = [ - // plugins require the same webpack instance - 'webpack/lib/LibraryTemplatePlugin', - 'webpack/lib/node/NodeTargetPlugin', - 'webpack/lib/node/NodeTemplatePlugin', - 'webpack/lib/NormalModule', - 'webpack/lib/optimize/LimitChunkCountPlugin', - 'webpack/lib/SingleEntryPlugin', - 'webpack/lib/webworker/WebWorkerTemplatePlugin', - 'webpack/lib/node/NodeEnvironmentPlugin', - 'webpack/lib/ModuleFilenameHelpers', - 'webpack/lib/GraphHelpers', - 'webpack/lib/ExternalsPlugin', - 'webpack/lib/web/FetchCompileAsyncWasmPlugin', - 'webpack/lib/web/FetchCompileWasmPlugin', - 'webpack/lib/runtime/StartupChunkDependenciesPlugin', - 'webpack/lib/javascript/JavascriptModulesPlugin', - 'webpack/lib/javascript/StartupHelpers', - 'webpack/lib/util/identifier', - 'webpack/lib/util/compileBooleanMatcher', - ]; const webpackDir = path.join(require.resolve('@ice/bundles/compiled/webpack'), '../'); - const pluginMap = webpackPlugins.map((pluginPath) => { - return [ - pluginPath, - pluginPath.replace(/^webpack\/lib\/((web|node|optimize|webworker|runtime|javascript|util)\/)?/, webpackDir), - ]; - }); - const pluginMapWithJs = webpackPlugins.map((pluginPath) => { - return [ - `${pluginPath}.js`, - pluginPath.replace(/^webpack\/lib\/((web|node|optimize|webworker|runtime|javascript|util)\/)?/, webpackDir), - ]; - }); + const createPluginMapping = (pluginPath: string, withJsExtension = false) => [ + withJsExtension ? `${pluginPath}.js` : pluginPath, + pluginPath.replace(/^webpack\/lib\/((web|node|optimize|webworker|runtime|javascript|util|dependencies)\/)?/, webpackDir), + ]; + + const pluginMap = webpackPlugins.map(pluginPath => createPluginMapping(pluginPath)); + const pluginMapWithJs = webpackPlugins.map(pluginPath => createPluginMapping(pluginPath, true)); return [ ['webpack', `${webpackDir}webpack-lib`], @@ -69,7 +84,9 @@ function hijackWebpack() { const resolveFilename = mod._resolveFilename; mod._resolveFilename = function (request: string, parent: any, isMain: boolean, options: any) { const hookResolved = hookPropertyMap.get(request); - if (hookResolved) request = hookResolved; + if (hookResolved) { + request = hookResolved; + } return resolveFilename.call(mod, request, parent, isMain, options); }; } diff --git a/packages/plugin-icestark/Context.d.ts b/packages/plugin-icestark/Context.d.ts new file mode 100644 index 000000000..6a2482c98 --- /dev/null +++ b/packages/plugin-icestark/Context.d.ts @@ -0,0 +1 @@ +export * from './esm/runtime/Context'; \ No newline at end of file diff --git a/packages/plugin-icestark/package.json b/packages/plugin-icestark/package.json index 623c34dd1..384e0c5a0 100644 --- a/packages/plugin-icestark/package.json +++ b/packages/plugin-icestark/package.json @@ -18,6 +18,11 @@ "import": "./esm/index.js", "default": "./esm/index.js" }, + "./Context": { + "types": "./esm/runtime/Context.d.ts", + "import": "./esm/runtime/Context.js", + "default": "./esm/runtime/Context.js" + }, "./esm/runtime/child": { "types": "./esm/runtime/child.d.ts", "import": "./esm/runtime/child.js", diff --git a/packages/plugin-icestark/src/index.ts b/packages/plugin-icestark/src/index.ts index 048091f5d..10f5d1343 100644 --- a/packages/plugin-icestark/src/index.ts +++ b/packages/plugin-icestark/src/index.ts @@ -9,12 +9,12 @@ const PLUGIN_NAME = '@ice/plugin-icestark'; const plugin: Plugin = ({ type, library }) => ({ name: PLUGIN_NAME, setup: ({ onGetConfig, context, generator, modifyUserConfig }) => { + const libraryName = library || context.pkg?.name as string || 'microApp'; onGetConfig((config) => { config.configureWebpack ??= []; config.configureWebpack.push((webpackConfig) => { if (type === 'child') { - const { pkg } = context; - webpackConfig.output.library = library || pkg.name as string || 'microApp'; + webpackConfig.output.library = libraryName; webpackConfig.output.libraryTarget = 'umd'; } return webpackConfig; @@ -34,6 +34,10 @@ const plugin: Plugin = ({ type, library }) => ({ if (!window.ICESTARK?.root && !window.__POWERED_BY_QIANKUN__) { root = render(); } +// Set library name +if (typeof window !== 'undefined' && window.ICESTARK) { + window.ICESTARK.library = ${JSON.stringify(libraryName)}; +} // For qiankun lifecycle validation. export async function bootstrap(props) { diff --git a/packages/plugin-icestark/src/runtime/Context.tsx b/packages/plugin-icestark/src/runtime/Context.tsx new file mode 100644 index 000000000..7545be0df --- /dev/null +++ b/packages/plugin-icestark/src/runtime/Context.tsx @@ -0,0 +1,7 @@ +import { createContext, useContext } from 'react'; + +export const FrameworkContext = createContext({}); + +export const useFrameworkContext = (): T => { + return useContext(FrameworkContext) as T; +}; diff --git a/packages/plugin-icestark/src/runtime/child.tsx b/packages/plugin-icestark/src/runtime/child.tsx index 11dc16936..f1062b3d5 100644 --- a/packages/plugin-icestark/src/runtime/child.tsx +++ b/packages/plugin-icestark/src/runtime/child.tsx @@ -1,13 +1,19 @@ +import * as React from 'react'; import * as ReactDOM from 'react-dom/client'; import type { RuntimePlugin } from '@ice/runtime/types'; import type { LifecycleOptions } from '../types'; +import { FrameworkContext } from './Context.js'; const runtime: RuntimePlugin = ({ setRender }, runtimeOptions) => { if (runtimeOptions?.container) { setRender((_, element) => { // Replace render root when app rendered as a child app. const root = ReactDOM.createRoot(runtimeOptions.container); - root.render(element); + root.render( + + {element} + , + ); return root; }); } diff --git a/packages/plugin-icestark/src/runtime/framework.tsx b/packages/plugin-icestark/src/runtime/framework.tsx index e8743b990..fffc2484f 100644 --- a/packages/plugin-icestark/src/runtime/framework.tsx +++ b/packages/plugin-icestark/src/runtime/framework.tsx @@ -1,100 +1,98 @@ import * as React from 'react'; import { AppRouter, AppRoute } from '@ice/stark'; import type { RuntimePlugin, ClientAppRouterProps } from '@ice/runtime/types'; -import type { RouteInfo, AppConfig } from '../types'; +import type { AppRouterProps } from '@ice/stark/lib/AppRouter'; +import type { RouteInfo, AppConfig, FrameworkConfig } from '../types'; const { useState, useEffect } = React; const runtime: RuntimePlugin = ({ getAppRouter, setAppRouter, appContext }) => { const { appExport, appData } = appContext; const OriginalRouter = getAppRouter(); - const { layout, getApps, appRouter } = appExport?.icestark || {}; + const { layout, getApps, appRouter, AppRoute: CustomizeAppRoute } = (appExport?.icestark || {}) as FrameworkConfig; - if (getApps) { - const FrameworkRouter = (props: ClientAppRouterProps) => { - const [routeInfo, setRouteInfo] = useState({}); - const [appEnter, setAppEnter] = useState({}); - const [appLeave, setAppLeave] = useState({}); - const [apps, setApps] = useState([]); - const FrameworkLayout = layout || (({ children }) => (<>{children})); - const appInfo = { - pathname: routeInfo.pathname || - (typeof window !== 'undefined' && window.location.pathname), - routeInfo, - appEnter, - appLeave, - updateApps: setApps, - }; - useEffect(() => { - (async () => { - const appList = await getApps(appData); - setApps(appList); - })(); - }, []); - - function handleRouteChange(pathname: string, query: Record, hash: string, routeType: string) { - setRouteInfo({ pathname, query, hash, routeType }); - } - - function handleAppLeave(config: AppConfig) { - setAppLeave(config); - } - - function handleAppEnter(config: AppConfig) { - setAppEnter(config); - } - return ( - - {apps && ( - - {apps.map((item: AppConfig, idx: number) => { - return ( - - ); - })} - { - const { routerContext } = props; - routerContext.routes = [ - ...routerContext.routes, - { - path: '*', - Component: () => ( - process.env.NODE_ENV === 'development' - ?
Add $.tsx to folder pages as a 404 component
- : null - ), - }, - ]; - const routerProps = { - ...props, - routerContext, - }; - return ; - }} - /> -
- )} -
- ); - }; - setAppRouter(FrameworkRouter); - } else { + if (!getApps) { console.warn(` [plugin-icestark]: appConfig.icestark.getApps should be not empty if this is an framework app. see https://ice.work/docs/guide/advanced/icestark/ `); + return; } + + const FrameworkRouter = (props: ClientAppRouterProps) => { + const [routeInfo, setRouteInfo] = useState({}); + const [appEnter, setAppEnter] = useState({}); + const [appLeave, setAppLeave] = useState({}); + const [apps, setApps] = useState(null); + const FrameworkLayout = layout || React.Fragment; + const appInfo = { + pathname: routeInfo.pathname || (typeof window !== 'undefined' ? window.location.pathname : ''), + routeInfo, + appEnter, + appLeave, + updateApps: setApps, + }; + + useEffect(() => { + const fetchApps = async () => { + try { + const appList = await getApps(appData); + setApps(appList); + } catch (error) { + console.error('[plugin-icestark]: Failed to fetch apps', error); + } + }; + + fetchApps(); + }, []); + + const handleRouteChange: AppRouterProps['onRouteChange'] = (pathname, query, hash, routeType) => { + setRouteInfo({ pathname, query, hash, routeType }); + }; + + const handleAppLeave: AppRouterProps['onAppLeave'] = (config) => setAppLeave(config); + const handleAppEnter: AppRouterProps['onAppEnter'] = (config) => setAppEnter(config); + const AppRouteComponent = CustomizeAppRoute || AppRoute; + + const appRouterProps: AppRouterProps = { + ...appRouter, + onRouteChange: handleRouteChange, + onAppEnter: handleAppEnter, + onAppLeave: handleAppLeave, + }; + return ( + + {apps && ( + + {apps?.map((item: AppConfig, idx: number) => ( + + ))} + { + const { routerContext } = props; + routerContext.routes = [ + ...routerContext.routes, + { + path: '*', + Component: () => ( + process.env.NODE_ENV === 'development' + ?
Add $.tsx to folder pages as a 404 component
+ : null + ), + }, + ]; + return ; + }} + /> +
+ )} +
+ ); + }; + + setAppRouter(FrameworkRouter); }; export default runtime; diff --git a/packages/plugin-icestark/src/types.ts b/packages/plugin-icestark/src/types.ts index 905dc42ba..92acc6e6b 100644 --- a/packages/plugin-icestark/src/types.ts +++ b/packages/plugin-icestark/src/types.ts @@ -1,10 +1,11 @@ import type { ComponentType } from 'react'; import type { CompatibleAppConfig } from '@ice/stark/lib/AppRoute'; import type { AppRouterProps } from '@ice/stark/lib/AppRouter'; +import type { AppRoute } from '@ice/stark'; export interface RouteInfo { pathname?: string; - query?: Record; + query?: object; hash?: string; routeType?: string; } @@ -17,6 +18,7 @@ export interface FrameworkConfig { getApps?: (data?: any) => (AppConfig[] | Promise); appRouter?: Omit; layout?: ComponentType; + AppRoute?: typeof AppRoute; } export interface LifecycleOptions { From 478120d1df1584fbf4bb1e55ede0b3d7e0e9f511 Mon Sep 17 00:00:00 2001 From: ClarkXia Date: Wed, 9 Apr 2025 10:56:17 +0800 Subject: [PATCH 3/4] chore: changelog --- .changeset/short-eels-shave.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/short-eels-shave.md diff --git a/.changeset/short-eels-shave.md b/.changeset/short-eels-shave.md new file mode 100644 index 000000000..84a68336c --- /dev/null +++ b/.changeset/short-eels-shave.md @@ -0,0 +1,5 @@ +--- +'@ice/app': patch +--- + +fix: always use esbuild to compile server config From cfde6ccad0c65a223e6714d816cdeecc3e3ba255 Mon Sep 17 00:00:00 2001 From: ClarkXia Date: Wed, 9 Apr 2025 11:32:49 +0800 Subject: [PATCH 4/4] chore: update versions (#7075) --- .changeset/healthy-rocks-fetch.md | 5 ----- .changeset/long-maps-search.md | 5 ----- .changeset/short-eels-shave.md | 5 ----- .changeset/slow-jokes-speak.md | 5 ----- packages/bundles/CHANGELOG.md | 6 ++++++ packages/bundles/package.json | 2 +- packages/ice/CHANGELOG.md | 11 +++++++++++ packages/ice/package.json | 2 +- packages/miniapp-loader/CHANGELOG.md | 7 +++++++ packages/miniapp-loader/package.json | 2 +- packages/plugin-i18n/package.json | 2 +- packages/plugin-icestark/CHANGELOG.md | 7 +++++++ packages/plugin-icestark/package.json | 4 ++-- packages/plugin-miniapp/CHANGELOG.md | 8 ++++++++ packages/plugin-miniapp/package.json | 4 ++-- packages/rspack-config/CHANGELOG.md | 8 ++++++++ packages/rspack-config/package.json | 6 +++--- packages/shared-config/CHANGELOG.md | 7 +++++++ packages/shared-config/package.json | 4 ++-- packages/webpack-config/CHANGELOG.md | 8 ++++++++ packages/webpack-config/package.json | 6 +++--- pnpm-lock.yaml | 14 +++++++------- 22 files changed, 85 insertions(+), 43 deletions(-) delete mode 100644 .changeset/healthy-rocks-fetch.md delete mode 100644 .changeset/long-maps-search.md delete mode 100644 .changeset/short-eels-shave.md delete mode 100644 .changeset/slow-jokes-speak.md diff --git a/.changeset/healthy-rocks-fetch.md b/.changeset/healthy-rocks-fetch.md deleted file mode 100644 index 8ce206b1b..000000000 --- a/.changeset/healthy-rocks-fetch.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'@ice/plugin-icestark': minor ---- - -feat: support framework provider diff --git a/.changeset/long-maps-search.md b/.changeset/long-maps-search.md deleted file mode 100644 index ef6984d9e..000000000 --- a/.changeset/long-maps-search.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'@ice/bundles': patch ---- - -feat: export ModuleNotFoundError of webpack diff --git a/.changeset/short-eels-shave.md b/.changeset/short-eels-shave.md deleted file mode 100644 index 84a68336c..000000000 --- a/.changeset/short-eels-shave.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'@ice/app': patch ---- - -fix: always use esbuild to compile server config diff --git a/.changeset/slow-jokes-speak.md b/.changeset/slow-jokes-speak.md deleted file mode 100644 index c7b9aa33e..000000000 --- a/.changeset/slow-jokes-speak.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'@ice/plugin-icestark': minor ---- - -feat: support custom AppRoute diff --git a/packages/bundles/CHANGELOG.md b/packages/bundles/CHANGELOG.md index 14bee8fd0..cd1f4053d 100644 --- a/packages/bundles/CHANGELOG.md +++ b/packages/bundles/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 0.2.9 + +### Patch Changes + +- 2f73084d: feat: export ModuleNotFoundError of webpack + ## 0.2.8 ### Patch Changes diff --git a/packages/bundles/package.json b/packages/bundles/package.json index 0142b5d54..2b56957ba 100644 --- a/packages/bundles/package.json +++ b/packages/bundles/package.json @@ -1,6 +1,6 @@ { "name": "@ice/bundles", - "version": "0.2.8", + "version": "0.2.9", "license": "MIT", "author": "ICE", "description": "Basic dependencies for ice.", diff --git a/packages/ice/CHANGELOG.md b/packages/ice/CHANGELOG.md index df668b620..75bb41627 100644 --- a/packages/ice/CHANGELOG.md +++ b/packages/ice/CHANGELOG.md @@ -1,5 +1,16 @@ # Changelog +## 3.6.1 + +### Patch Changes + +- 478120d1: fix: always use esbuild to compile server config +- Updated dependencies [2f73084d] + - @ice/bundles@0.2.9 + - @ice/rspack-config@1.2.3 + - @ice/shared-config@1.3.2 + - @ice/webpack-config@1.2.2 + ## 3.6.0 ### Minor Changes diff --git a/packages/ice/package.json b/packages/ice/package.json index 789bf5d89..441526f26 100644 --- a/packages/ice/package.json +++ b/packages/ice/package.json @@ -1,6 +1,6 @@ { "name": "@ice/app", - "version": "3.6.0", + "version": "3.6.1", "description": "provide scripts and configuration used by web framework ice", "type": "module", "main": "./esm/index.js", diff --git a/packages/miniapp-loader/CHANGELOG.md b/packages/miniapp-loader/CHANGELOG.md index 4a289abfe..ffa34a163 100644 --- a/packages/miniapp-loader/CHANGELOG.md +++ b/packages/miniapp-loader/CHANGELOG.md @@ -1,5 +1,12 @@ # @ice/miniapp-loader +## 1.2.2 + +### Patch Changes + +- Updated dependencies [2f73084d] + - @ice/bundles@0.2.9 + ## 1.2.1 ### Patch Changes diff --git a/packages/miniapp-loader/package.json b/packages/miniapp-loader/package.json index ed7537acd..834ef15f6 100644 --- a/packages/miniapp-loader/package.json +++ b/packages/miniapp-loader/package.json @@ -1,6 +1,6 @@ { "name": "@ice/miniapp-loader", - "version": "1.2.1", + "version": "1.2.2", "description": "webpack loader for miniapps.", "main": "./lib/page.js", "files": [ diff --git a/packages/plugin-i18n/package.json b/packages/plugin-i18n/package.json index 6990831b3..9f75573e6 100644 --- a/packages/plugin-i18n/package.json +++ b/packages/plugin-i18n/package.json @@ -56,7 +56,7 @@ "webpack-dev-server": "4.15.0" }, "peerDependencies": { - "@ice/app": "^3.6.0", + "@ice/app": "^3.6.1", "@ice/runtime": "^1.5.2" }, "publishConfig": { diff --git a/packages/plugin-icestark/CHANGELOG.md b/packages/plugin-icestark/CHANGELOG.md index c50e241cc..c85b55a3c 100644 --- a/packages/plugin-icestark/CHANGELOG.md +++ b/packages/plugin-icestark/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## 1.2.0 + +### Minor Changes + +- 2f73084d: feat: support framework provider +- 2f73084d: feat: support custom AppRoute + ## 1.1.1 ### Patch Changes diff --git a/packages/plugin-icestark/package.json b/packages/plugin-icestark/package.json index 384e0c5a0..a03e33283 100644 --- a/packages/plugin-icestark/package.json +++ b/packages/plugin-icestark/package.json @@ -1,6 +1,6 @@ { "name": "@ice/plugin-icestark", - "version": "1.1.1", + "version": "1.2.0", "description": "Easy use `icestark` in icejs.", "author": "ice-admin@alibaba-inc.com", "homepage": "", @@ -49,7 +49,7 @@ "@ice/stark-app": "^1.2.0" }, "devDependencies": { - "@ice/app": "^3.3.2", + "@ice/app": "^3.6.1", "@ice/runtime": "^1.2.9", "@types/react": "^18.0.0", "@types/react-dom": "^18.0.0" diff --git a/packages/plugin-miniapp/CHANGELOG.md b/packages/plugin-miniapp/CHANGELOG.md index 993c9e60b..818aa7b94 100644 --- a/packages/plugin-miniapp/CHANGELOG.md +++ b/packages/plugin-miniapp/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## 1.2.3 + +### Patch Changes + +- Updated dependencies [2f73084d] + - @ice/bundles@0.2.9 + - @ice/miniapp-loader@1.2.2 + ## 1.2.2 ### Patch Changes diff --git a/packages/plugin-miniapp/package.json b/packages/plugin-miniapp/package.json index 24bb92a0e..857b38586 100644 --- a/packages/plugin-miniapp/package.json +++ b/packages/plugin-miniapp/package.json @@ -1,6 +1,6 @@ { "name": "@ice/plugin-miniapp", - "version": "1.2.2", + "version": "1.2.3", "description": "ice.js plugin for miniapp.", "license": "MIT", "type": "module", @@ -50,7 +50,7 @@ "sax": "^1.2.4" }, "devDependencies": { - "@ice/app": "^3.6.0", + "@ice/app": "^3.6.1", "@ice/runtime": "^1.5.2", "webpack": "^5.88.0" }, diff --git a/packages/rspack-config/CHANGELOG.md b/packages/rspack-config/CHANGELOG.md index 557efd48b..6fef4a7d9 100644 --- a/packages/rspack-config/CHANGELOG.md +++ b/packages/rspack-config/CHANGELOG.md @@ -1,5 +1,13 @@ # @ice/rspack-config +## 1.2.3 + +### Patch Changes + +- Updated dependencies [2f73084d] + - @ice/bundles@0.2.9 + - @ice/shared-config@1.3.2 + ## 1.2.2 ### Patch Changes diff --git a/packages/rspack-config/package.json b/packages/rspack-config/package.json index 8056ff51e..dff1347e9 100644 --- a/packages/rspack-config/package.json +++ b/packages/rspack-config/package.json @@ -1,6 +1,6 @@ { "name": "@ice/rspack-config", - "version": "1.2.2", + "version": "1.2.3", "repository": "alibaba/ice", "bugs": "https://github.com/alibaba/ice/issues", "homepage": "https://v3.ice.work", @@ -15,8 +15,8 @@ "*.d.ts" ], "dependencies": { - "@ice/bundles": "0.2.8", - "@ice/shared-config": "1.3.1" + "@ice/bundles": "0.2.9", + "@ice/shared-config": "1.3.2" }, "devDependencies": { "@rspack/core": "0.5.7" diff --git a/packages/shared-config/CHANGELOG.md b/packages/shared-config/CHANGELOG.md index 99db4f77c..32bb37875 100644 --- a/packages/shared-config/CHANGELOG.md +++ b/packages/shared-config/CHANGELOG.md @@ -1,5 +1,12 @@ # @ice/shared-config +## 1.3.2 + +### Patch Changes + +- Updated dependencies [2f73084d] + - @ice/bundles@0.2.9 + ## 1.3.1 ### Patch Changes diff --git a/packages/shared-config/package.json b/packages/shared-config/package.json index bf7aca5ff..8c1f5ebec 100644 --- a/packages/shared-config/package.json +++ b/packages/shared-config/package.json @@ -1,6 +1,6 @@ { "name": "@ice/shared-config", - "version": "1.3.1", + "version": "1.3.2", "repository": "alibaba/ice", "bugs": "https://github.com/alibaba/ice/issues", "homepage": "https://v3.ice.work", @@ -17,7 +17,7 @@ "*.d.ts" ], "dependencies": { - "@ice/bundles": "0.2.8", + "@ice/bundles": "0.2.9", "@rollup/pluginutils": "^4.2.0", "browserslist": "^4.22.1", "consola": "^2.15.3", diff --git a/packages/webpack-config/CHANGELOG.md b/packages/webpack-config/CHANGELOG.md index 810024477..c15f456de 100644 --- a/packages/webpack-config/CHANGELOG.md +++ b/packages/webpack-config/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## 1.2.2 + +### Patch Changes + +- Updated dependencies [2f73084d] + - @ice/bundles@0.2.9 + - @ice/shared-config@1.3.2 + ## 1.2.1 ### Patch Changes diff --git a/packages/webpack-config/package.json b/packages/webpack-config/package.json index 95ced9d7b..031b98d6d 100644 --- a/packages/webpack-config/package.json +++ b/packages/webpack-config/package.json @@ -1,6 +1,6 @@ { "name": "@ice/webpack-config", - "version": "1.2.1", + "version": "1.2.2", "repository": "alibaba/ice", "bugs": "https://github.com/alibaba/ice/issues", "homepage": "https://v3.ice.work", @@ -16,8 +16,8 @@ "*.d.ts" ], "dependencies": { - "@ice/shared-config": "1.3.1", - "@ice/bundles": "0.2.8", + "@ice/shared-config": "1.3.2", + "@ice/bundles": "0.2.9", "fast-glob": "^3.2.11", "process": "^0.11.10" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6bda785aa..f5cda6c05 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2040,7 +2040,7 @@ importers: version: 1.5.0 devDependencies: '@ice/app': - specifier: ^3.3.2 + specifier: ^3.6.1 version: link:../ice '@ice/runtime': specifier: ^1.2.9 @@ -2157,7 +2157,7 @@ importers: version: 1.2.4 devDependencies: '@ice/app': - specifier: ^3.6.0 + specifier: ^3.6.1 version: link:../ice '@ice/runtime': specifier: ^1.5.2 @@ -2388,10 +2388,10 @@ importers: packages/rspack-config: dependencies: '@ice/bundles': - specifier: 0.2.8 + specifier: 0.2.9 version: link:../bundles '@ice/shared-config': - specifier: 1.3.1 + specifier: 1.3.2 version: link:../shared-config devDependencies: '@rspack/core': @@ -2450,7 +2450,7 @@ importers: packages/shared-config: dependencies: '@ice/bundles': - specifier: 0.2.8 + specifier: 0.2.9 version: link:../bundles '@rollup/pluginutils': specifier: ^4.2.0 @@ -2493,10 +2493,10 @@ importers: packages/webpack-config: dependencies: '@ice/bundles': - specifier: 0.2.8 + specifier: 0.2.9 version: link:../bundles '@ice/shared-config': - specifier: 1.3.1 + specifier: 1.3.2 version: link:../shared-config fast-glob: specifier: ^3.2.11