Skip to content

Commit

Permalink
feat: refactor ice runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
ClarkXia committed Jan 16, 2025
1 parent cd2b97e commit 2c6900e
Show file tree
Hide file tree
Showing 15 changed files with 1,430 additions and 40 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,8 @@
"@rspack/[email protected]": "patches/@[email protected]",
"[email protected]": "patches/[email protected]"
}
}
},
"workspaces": [
"packages/runtime-kit"
]
}
2 changes: 1 addition & 1 deletion packages/ice/src/createService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ async function createService({ rootDir, command, commandArgs }: CreateServiceOpt
// Get first task config as default platform config.
const platformTaskConfig = taskConfigs[0];

const iceRuntimePath = '@ice/runtime';
const iceRuntimePath = platformTaskConfig.config.runtimeSource || '@ice/runtime';
// Only when code splitting use the default strategy or set to `router`, the router will be lazy loaded.
const lazy = [true, 'chunks', 'page', 'page-vendors'].includes(userConfig.codeSplitting);
const { routeImports, routeDefinition } = getRoutesDefinition({
Expand Down
1 change: 1 addition & 0 deletions packages/ice/src/plugins/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const getDefaultTaskConfig = ({ rootDir, command }): Config => {
logging: process.env.WEBPACK_LOGGING || defaultLogging,
minify: command === 'build',
useDevServer: true,
runtimeSource: '@ice/runtime',
};
};

Expand Down
52 changes: 17 additions & 35 deletions packages/ice/templates/core/entry.client.tsx.ejs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<% if (importCoreJs) { -%>import 'core-js';<% } %>
<%- entry.imports %>
import { createElement, Fragment } from 'react';
import { runClientApp, getAppConfig } from '<%- iceRuntimePath %>';
import { commons, statics } from './runtime-modules';
import * as app from '@/app';
Expand All @@ -15,19 +14,6 @@ const getRouterBasename = () => {
const appConfig = getAppConfig(app);
return appConfig?.router?.basename ?? <%- basename %> ?? '';
}
// Add react fragment for split chunks of app.
// Otherwise chunk of route component will pack @ice/jsx-runtime and depend on framework bundle.
const App = <></>;

<% if (!dataLoaderImport.imports && hasDataLoader) {-%>
let dataLoaderFetcher = (options) => {
return window.fetch(options.url, options);
}
let dataLoaderDecorator = (dataLoader) => {
return dataLoader;
}
<% } -%>

const renderOptions: RunClientAppOptions = {
app,
Expand All @@ -39,7 +25,7 @@ const renderOptions: RunClientAppOptions = {
basename: getRouterBasename(),
hydrate: <%- hydrate %>,
memoryRouter: <%- memoryRouter || false %>,
<% if (hasDataLoader) { -%>
<% if (dataLoaderImport.imports && hasDataLoader) { -%>
dataLoaderFetcher,
dataLoaderDecorator,<% } -%>
runtimeOptions: {
Expand All @@ -50,27 +36,23 @@ const renderOptions: RunClientAppOptions = {
},
};

const defaultRender = (customOptions: Partial<RunClientAppOptions> = {}) => {
return runClientApp({
...renderOptions,
...customOptions,
runtimeOptions: {
...(renderOptions.runtimeOptions || {}),
...customOptions.runtimeOptions,
},
});
};

const renderApp = (appExport: any, customOptions: Partial<RunClientAppOptions>) => {
if (appExport.runApp) {
return appExport.runApp(defaultRender, renderOptions);
} else {
return defaultRender(customOptions);
}
};
const mergeOptions = (customOptions: Partial<RunClientAppOptions> = {}): RunClientAppOptions => ({
...renderOptions,
...customOptions,
runtimeOptions: {
...renderOptions.runtimeOptions,
...customOptions.runtimeOptions,
},
});

const render = (customOptions: Partial<RunClientAppOptions> = {}) => {
return renderApp(app, customOptions);
const render = () => {
return app.runApp?.(
(customOptions: Partial<RunClientAppOptions> = {}) => {
const options = mergeOptions(customOptions);
return runClientApp(options);
},
renderOptions
) ?? runClientApp(renderOptions);
};

<%- entryCode %>
Expand Down
37 changes: 37 additions & 0 deletions packages/runtime-kit/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "@ice/runtime-kit",
"version": "0.1.0",
"description": "Runtime utilities and tools for ICE framework",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"files": [
"dist"
],
"sideEffects": false,
"scripts": {
"build": "tsup",
"watch": "tsup --watch"
},
"dependencies": {},
"devDependencies": {
"tsup": "^8.0.0",
"typescript": "^5.0.0",
"@types/react": "^18.0.8",
"@types/react-dom": "^18.0.3",
"react": "^18.0.0",
"react-dom": "^18.0.0"
},
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/alibaba/ice.git"
},
"bugs": {
"url": "https://github.com/alibaba/ice/issues"
},
"homepage": "https://ice.work",
"license": "MIT"
}
29 changes: 29 additions & 0 deletions packages/runtime-kit/src/appConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { AppConfig, AppExport } from './types.js';

const defaultAppConfig: AppConfig = {
app: {
strict: false,
rootId: 'ice-container',
},
router: {
type: 'browser',
},
} as const;

export default function getAppConfig(appExport: AppExport): AppConfig {
const { default: appConfig = {} } = appExport || {};
const { app, router, ...others } = appConfig;

return {
app: { ...defaultAppConfig.app, ...app },
router: { ...defaultAppConfig.router, ...router },
...others,
};
}

export const defineAppConfig = (
appConfigOrDefineAppConfig: AppConfig | (() => AppConfig),
): AppConfig =>
(typeof appConfigOrDefineAppConfig === 'function'
? appConfigOrDefineAppConfig()
: appConfigOrDefineAppConfig);
Loading

0 comments on commit 2c6900e

Please sign in to comment.