Skip to content

Commit

Permalink
feat: support runtime next
Browse files Browse the repository at this point in the history
  • Loading branch information
ClarkXia committed Feb 20, 2025
1 parent a7a885d commit d204b7e
Show file tree
Hide file tree
Showing 48 changed files with 1,197 additions and 379 deletions.
9 changes: 9 additions & 0 deletions examples/runtime-next-project/ice.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from '@ice/app';
import runtimeNext from '@ice/plugin-runtime-next';

export default defineConfig(() => ({
ssg: false,
plugins: [
runtimeNext(),
],
}));
22 changes: 22 additions & 0 deletions examples/runtime-next-project/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "runtime-next-project",
"private": true,
"version": "1.0.0",
"description": "Example for @ice/runtime-next usage",
"scripts": {
"start": "ice start",
"build": "ice build"
},
"dependencies": {
"@ice/runtime": "workspace:*",
"@ice/runtime-next": "workspace:*",
"react": "^19.0.0",
"react-dom": "^19.0.0"
},
"devDependencies": {
"@ice/app": "workspace:*",
"@ice/plugin-runtime-next": "workspace:*",
"@types/react": "^19.0.0",
"@types/react-dom": "^19.0.0"
}
}
5 changes: 5 additions & 0 deletions examples/runtime-next-project/src/app.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { defineAppConfig } from 'ice';

export default defineAppConfig(() => ({

}));
22 changes: 22 additions & 0 deletions examples/runtime-next-project/src/document.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Meta, Title, Links, Main, Scripts } from 'ice';

function Document() {
return (
<html>
<head>
<meta charSet="utf-8" />
<meta name="description" content="ICE Demo" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<Meta />
<Title />
<Links />
</head>
<body>
<Main />
<Scripts />
</body>
</html>
);
}

export default Document;
3 changes: 3 additions & 0 deletions examples/runtime-next-project/src/pages/home.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Home() {
return <div>Home</div>;
}
3 changes: 3 additions & 0 deletions examples/runtime-next-project/src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Index() {
return <div>Index</div>;
}
32 changes: 32 additions & 0 deletions examples/runtime-next-project/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"compileOnSave": false,
"buildOnSave": false,
"compilerOptions": {
"baseUrl": ".",
"outDir": "build",
"module": "esnext",
"target": "es6",
"jsx": "react-jsx",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"lib": ["es6", "dom"],
"sourceMap": true,
"allowJs": true,
"rootDir": "./",
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noImplicitAny": false,
"importHelpers": true,
"strictNullChecks": true,
"suppressImplicitAnyIndexErrors": true,
"noUnusedLocals": true,
"skipLibCheck": true,
"paths": {
"@/*": ["./src/*"],
"ice": [".ice"]
}
},
"include": ["src", ".ice", "ice.config.*"],
"exclude": ["build", "public"]
}
11 changes: 4 additions & 7 deletions packages/ice/src/createService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import ServerCompileTask from './utils/ServerCompileTask.js';
import { generateRoutesInfo } from './routes.js';
import GeneratorAPI from './service/generatorAPI.js';
import renderTemplate from './service/renderTemplate.js';
import createRouteConfig from './utils/createRouteConfig.js';

Check warning on line 42 in packages/ice/src/createService.ts

View workflow job for this annotation

GitHub Actions / build (18.x, windows-latest)

'createRouteConfig' is defined but never used. Allowed unused vars must match /[iI]gnored|createElement/u

Check warning on line 42 in packages/ice/src/createService.ts

View workflow job for this annotation

GitHub Actions / build (18.x, ubuntu-latest)

'createRouteConfig' is defined but never used. Allowed unused vars must match /[iI]gnored|createElement/u

const require = createRequire(import.meta.url);
const __dirname = path.dirname(fileURLToPath(import.meta.url));
Expand Down Expand Up @@ -209,16 +210,13 @@ async function createService({ rootDir, command, commandArgs }: CreateServiceOpt
// 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 runtimeRouter = runtimeConfig?.router;
const { routeImports, routeDefinition } = runtimeRouter?.routesDefinition?.({
const routeDefinition = runtimeRouter?.routesDefinition?.({
manifest: routesInfo.routes,
lazy,
}) || {
routeImports: [],
routeDefinition: '',
};
});

const routesFile = runtimeRouter?.source;

console.log('routesFile', routesFile);
const loaderExports = hasExportAppData || Boolean(routesInfo.loaders);
const hasDataLoader = Boolean(userConfig.dataLoader) && loaderExports;
const renderData = {
Expand All @@ -237,7 +235,6 @@ async function createService({ rootDir, command, commandArgs }: CreateServiceOpt
hasDocument: hasDocument(rootDir),
dataLoader: userConfig.dataLoader,
hasDataLoader,
routeImports,
routeDefinition,
routesFile: routesFile?.replace(/\.[^.]+$/, ''),
lazy,
Expand Down
6 changes: 3 additions & 3 deletions packages/ice/src/getWatchEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ const getWatchEvents = (options: Options): WatchEvent[] => {
async (eventName: string) => {
if (eventName === 'add' || eventName === 'unlink' || eventName === 'change') {
const routesRenderData = await generateRoutesInfo(rootDir, routesConfig);
const { routeImports, routeDefinition } = router?.routesDefinition?.({
const routeDefinition = router?.routesDefinition?.({
manifest: routesRenderData.routes,
lazy: lazyRoutes,
}) || {};
}) || '';
const stringifiedData = JSON.stringify(routesRenderData);
if (cache.get('routes') !== stringifiedData) {
cache.set('routes', stringifiedData);
Expand All @@ -49,7 +49,7 @@ const getWatchEvents = (options: Options): WatchEvent[] => {
generator.renderFile(
router.template,
router.source,
{ routeImports, routeDefinition },
{ routeDefinition },
);
}
// Keep generate route manifest for avoid breaking change.
Expand Down
6 changes: 5 additions & 1 deletion packages/ice/src/plugins/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createRequire } from 'module';
import type { Config } from '@ice/shared-config/types';
import { CACHE_DIR, RUNTIME_TMP_DIR, RUNTIME_EXPORTS } from '../constant.js';
import { getRoutesDefinition } from '../routes.js';
import createRouteConfig from '../utils/createRouteConfig.js';

const require = createRequire(import.meta.url);
const getDefaultTaskConfig = ({ rootDir, command }): Config => {
Expand Down Expand Up @@ -39,7 +40,10 @@ const getDefaultTaskConfig = ({ rootDir, command }): Config => {
source: '@ice/runtime',
server: '@ice/runtime/server',
router: {
routesDefinition: getRoutesDefinition,
routesDefinition: (args) => {
const { routeImports, routeDefinition } = getRoutesDefinition(args);
return createRouteConfig(routeImports, routeDefinition);
},
source: './routes.tsx',
template: 'core/routes.tsx.ejs',
},
Expand Down
16 changes: 16 additions & 0 deletions packages/ice/src/utils/createRouteConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const createRouteConfig = (routeImports: string[], routeDefinition: string) => {
return `
import { createRouteLoader, WrapRouteComponent, RouteErrorComponent } from '@ice/runtime';
import type { CreateRoutes } from '@ice/runtime';
${routeImports.length ? `${routeImports.join('\n')}\n\n` : ''}
const createRoutes: CreateRoutes = ({
requestContext,
renderMode,
}) => ([
${routeDefinition}
]);
export default createRoutes;
`;
};

export default createRouteConfig;
5 changes: 2 additions & 3 deletions packages/ice/src/utils/multipleEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,14 @@ export function renderMultiEntry(options: Options) {
);
// Generate route file for each route.
const matches = matchRoutes(routesManifest, route);
const { routeImports, routeDefinition } = routesDefinition?.({
const routeDefinition = routesDefinition?.({
manifest: routesManifest,
lazy,
matchRoute: (routeItem) => {
return matches.some((match) => match.route.id === routeItem.id);
},
}) || {};
}) || '';
generator.addRenderFile('core/routes.tsx.ejs', `routes.${routeId}.tsx`, {
routeImports,
routeDefinition,
});
});
Expand Down
11 changes: 1 addition & 10 deletions packages/ice/templates/core/routes.tsx.ejs
Original file line number Diff line number Diff line change
@@ -1,10 +1 @@
import { createRouteLoader, WrapRouteComponent, RouteErrorComponent } from '@ice/runtime';
import type { CreateRoutes } from '@ice/runtime';
<%- routeImports.length ? routeImports.join('\n') + '\n\n' : ''; -%>
const createRoutes: CreateRoutes = ({
requestContext,
renderMode,
}) => ([
<%- routeDefinition %>
]);
export default createRoutes;
<%- routeDefinition -%>
38 changes: 38 additions & 0 deletions packages/plugin-runtime-next/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@ice/plugin-runtime-next",
"version": "1.0.0",
"description": "runtime next plugin for ice.js.",
"files": [
"esm",
"!esm/**/*.map",
"*.d.ts",
"templates"
],
"type": "module",
"main": "esm/index.js",
"module": "esm/index.js",
"types": "esm/index.d.ts",
"exports": {
".": "./esm/index.js"
},
"sideEffects": false,
"scripts": {
"watch": "tsc -w --sourceMap",
"build": "tsc"
},
"dependencies": {
"@tanstack/router-generator": "^1.106.0"
},
"devDependencies": {
"@ice/app": "workspace:*",
"@types/react": "^19.0.0",
"@types/react-dom": "^19.0.0"
},
"repository": {
"type": "http",
"url": "https://github.com/alibaba/ice/tree/master/packages/plugin-runtime-next"
},
"publishConfig": {
"access": "public"
}
}
46 changes: 46 additions & 0 deletions packages/plugin-runtime-next/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import type { Plugin } from '@ice/app/types';

const plugin: Plugin = () => ({
name: 'plugin-runtime-next',
setup: ({ onGetConfig }) => {
console.log('plugin-runtime-next');
// Customize the runtime
onGetConfig((config) => {
config.swcOptions.compilationConfig = {
jsc: {
transform: {
react: {
// TODO: suport custom jsx runtime in react 19.
importSource: 'react',
},
},
},
};
// Override the runtime config
config.runtime = {
exports: [
{
specifier: ['Meta', 'Title', 'Links', 'Main', 'Scripts'],
source: '@ice/runtime-next/document',
},
{
specifier: ['defineAppConfig'],
source: '@ice/runtime-next',
},
],
server: '@ice/runtime-next/server',
source: '@ice/runtime-next',
router: {
routesDefinition: (args) => {
console.log('args', args);
return 'export default function createRoutes() { return []; }';
},
source: './routes.tsx',
template: 'core/routes.tsx.ejs',
},
};
});
},
});

export default plugin;
11 changes: 11 additions & 0 deletions packages/plugin-runtime-next/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"baseUrl": "./",
"rootDir": "src",
"outDir": "esm",
"module": "ES2020",
"moduleResolution": "NodeNext",
},
"include": ["src"]
}
1 change: 1 addition & 0 deletions packages/runtime-kit/document.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './esm/document';
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import type { AppContext } from '@ice/runtime-kit';
import type { AppContext } from './types.js';

const Context = React.createContext<AppContext | undefined>(undefined);

Expand Down
Loading

0 comments on commit d204b7e

Please sign in to comment.