Skip to content

Commit

Permalink
Feat: unnecessary to compile routes config for server render (#6856)
Browse files Browse the repository at this point in the history
* fix: unnecessary to compile routes config for server render

* chore: format ejs template

* feat: support build fallback entry

* chore: add test case for fallback entry

* chore: update lock

* chore: changeset

* chore: lint

* chore: lint

* chore: remove console log

* fix: merge conflict

* fix: undefined assign
  • Loading branch information
ClarkXia authored Jun 17, 2024
1 parent d073ee5 commit 15c8200
Show file tree
Hide file tree
Showing 45 changed files with 429 additions and 504 deletions.
7 changes: 7 additions & 0 deletions .changeset/green-files-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@ice/shared-config': patch
'@ice/runtime': patch
'@ice/app': patch
---

feat: support build additional server entry for fallback
2 changes: 1 addition & 1 deletion examples/basic-project/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@
},
"include": ["src", ".ice", "ice.config.*"],
"exclude": ["build", "public"]
}
}
18 changes: 0 additions & 18 deletions examples/with-entry-type/ice.config.mts

This file was deleted.

42 changes: 0 additions & 42 deletions examples/with-entry-type/src/document.tsx

This file was deleted.

19 changes: 0 additions & 19 deletions examples/with-entry-type/src/pages/Custom/index.tsx

This file was deleted.

30 changes: 0 additions & 30 deletions examples/with-entry-type/src/pages/about.tsx

This file was deleted.

21 changes: 0 additions & 21 deletions examples/with-entry-type/src/pages/blog.tsx

This file was deleted.

20 changes: 0 additions & 20 deletions examples/with-entry-type/src/pages/home.tsx

This file was deleted.

25 changes: 0 additions & 25 deletions examples/with-entry-type/src/pages/index.module.css

This file was deleted.

22 changes: 0 additions & 22 deletions examples/with-entry-type/src/pages/layout.tsx

This file was deleted.

File renamed without changes.
10 changes: 10 additions & 0 deletions examples/with-fallback-entry/ice.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from '@ice/app';
import plugin from './plugin';

export default defineConfig(() => ({
plugins: [plugin()],
ssr: true,
server: {
format: 'cjs',
}
}));
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@examples/with-entry-type",
"private": true,
"name": "@examples/with-fallback-entry",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "ice start",
"build": "ice build"
Expand All @@ -12,11 +12,10 @@
"dependencies": {
"@ice/app": "workspace:*",
"@ice/runtime": "workspace:*",
"react": "^18.0.0",
"react-dom": "^18.0.0"
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"fs-extra": "^10.0.0",
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.2",
"webpack": "^5.88.0"
Expand Down
12 changes: 12 additions & 0 deletions examples/with-fallback-entry/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default function createPlugin() {
return {
name: 'custom-plugin',
setup({ onGetConfig }) {
onGetConfig((config) => {
config.server = {
fallbackEntry: true,
};
});
},
};
}
File renamed without changes.
File renamed without changes.
22 changes: 22 additions & 0 deletions examples/with-fallback-entry/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/with-fallback-entry/src/global.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
font-size: 14px;
}
3 changes: 3 additions & 0 deletions examples/with-fallback-entry/src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Home() {
return <h1>home</h1>;
}
File renamed without changes.
File renamed without changes.
19 changes: 18 additions & 1 deletion packages/ice/src/bundler/config/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import path from 'path';
import type { CommandName } from 'build-scripts';
import ServerRunnerPlugin from '../../webpack/ServerRunnerPlugin.js';
import { IMPORT_META_RENDERER, IMPORT_META_TARGET, WEB } from '../../constant.js';
import { IMPORT_META_RENDERER, IMPORT_META_TARGET, WEB, FALLBACK_ENTRY, RUNTIME_TMP_DIR } from '../../constant.js';
import getServerCompilerPlugin from '../../utils/getServerCompilerPlugin.js';
import ReCompilePlugin from '../../webpack/ReCompilePlugin.js';
import getEntryPoints from '../../utils/getEntryPoints.js';
Expand All @@ -20,6 +22,18 @@ export const getSpinnerPlugin = (spinner, name?: string) => {
};
};

export const getFallbackEntry = (options: {
rootDir: string;
command: CommandName;
fallbackEntry: boolean;
}): string => {
const { command, fallbackEntry, rootDir } = options;
if (command === 'build' && fallbackEntry) {
return path.join(rootDir, RUNTIME_TMP_DIR, FALLBACK_ENTRY);
}
return '';
};

interface ServerPluginOptions {
serverRunner?: ServerRunner;
serverCompiler?: ServerCompiler;
Expand All @@ -30,6 +44,7 @@ interface ServerPluginOptions {
serverEntry?: string;
ensureRoutesConfig: () => Promise<void>;
userConfig?: UserConfig;
fallbackEntry?: string;
getFlattenRoutes?: () => string[];
command?: string;
}
Expand All @@ -43,6 +58,7 @@ export const getServerPlugin = ({
outputDir,
serverCompileTask,
userConfig,
fallbackEntry,
getFlattenRoutes,
command,
}: ServerPluginOptions) => {
Expand All @@ -53,6 +69,7 @@ export const getServerPlugin = ({
return getServerCompilerPlugin(serverCompiler, {
rootDir,
serverEntry,
fallbackEntry,
outputDir,
serverCompileTask,
userConfig,
Expand Down
9 changes: 7 additions & 2 deletions packages/ice/src/bundler/rspack/getConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
CSS_MODULES_LOCAL_IDENT_NAME,
CSS_MODULES_LOCAL_IDENT_NAME_DEV,
} from '../../constant.js';
import { getReCompilePlugin, getServerPlugin, getSpinnerPlugin } from '../config/plugins.js';
import { getFallbackEntry, getReCompilePlugin, getServerPlugin, getSpinnerPlugin } from '../config/plugins.js';
import { getExpandedEnvs } from '../../utils/runtimeEnv.js';
import type { BundlerOptions, Context } from '../types.js';
import type { PluginData } from '../../types/plugin.js';
Expand All @@ -34,8 +34,8 @@ const getConfig: GetConfig = async (context, options, rspack) => {
} = options;
const {
rootDir,
userConfig,
command,
userConfig,
extendsPluginAPI: {
serverCompileTask,
getRoutesFile,
Expand All @@ -50,6 +50,11 @@ const getConfig: GetConfig = async (context, options, rspack) => {
getSpinnerPlugin(spinner),
// Add Server runner plugin.
getServerPlugin({
fallbackEntry: getFallbackEntry({
rootDir,
command,
fallbackEntry: server?.fallbackEntry,
}),
serverRunner,
ensureRoutesConfig,
serverCompiler,
Expand Down
Loading

0 comments on commit 15c8200

Please sign in to comment.