Skip to content

Commit

Permalink
fix: lazyWithPreload is not defined
Browse files Browse the repository at this point in the history
  • Loading branch information
sanyuan0704 committed Sep 17, 2022
1 parent 6768e7e commit 0602a57
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 37 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -23,7 +23,7 @@
},
"scripts": {
"dev": "tsup --watch",
"build": "tsup --minify && tsx scripts/pre-bundle.cts",
"build": "NODE_ENV=production tsup && tsx scripts/pre-bundle.cts",
"start:client": "cd src/client && tsc -w",
"start:node": "cd src/node && tsc -w",
"dev:docs": "node ./bin/island docs",
Expand Down
Expand Up @@ -5,7 +5,6 @@ export type PreloadableComponent<T extends ComponentType<any>> = T & {
preload: () => Promise<PageModule<T>>;
};

// Runtime code
// Inspired by https://github.com/ianschmitz/react-lazy-with-preload/blob/master/src/index.ts
export function lazyWithPreload<T extends ComponentType<any>>(
factory: () => Promise<{ default: T }>
Expand Down
2 changes: 1 addition & 1 deletion src/client/theme-default/components/Aside/index.tsx
Expand Up @@ -31,7 +31,7 @@ export function Aside(

useAsideAnchor(prevActiveLinkRef, headers, asideRef, markerRef);

const renderHeader = (header: any, index: number) => {
const renderHeader = (header: Header) => {
const isNested = header.depth > 2;
return (
<li key={header.text}>
Expand Down
3 changes: 1 addition & 2 deletions src/node/build.ts
Expand Up @@ -273,7 +273,6 @@ class SSGBuilder {
}
</body>
</html>`.trim();
console.log('route:', routePath);
const fileName =
routePath === '/' ? 'index.html' : `${routePath.slice(1)}.html`;
await fs.ensureDir(join(this.#root, DIST_PATH, dirname(fileName)));
Expand Down Expand Up @@ -323,7 +322,7 @@ class SSGBuilder {
noExternal: ['lodash-es', 'react-router-dom']
},
build: {
minify: false,
minify: !process.env.DEBUG,
ssr: isServer,
outDir: isServer
? join(this.#root, TEMP_PATH, 'ssr')
Expand Down
4 changes: 3 additions & 1 deletion src/node/constants/index.ts
Expand Up @@ -45,7 +45,9 @@ export const DEFAULT_EXTERNALS: string[] = [
'react/jsx-runtime'
];

export const ISLAND_JSX_RUNTIME_PATH = join(PACKAGE_ROOT_PATH, 'dist');
export const CLI_BUNDLE_OUTDIR = join(PACKAGE_ROOT_PATH, 'dist');

export const ISLAND_JSX_RUNTIME_PATH = CLI_BUNDLE_OUTDIR;

export const ISLAND_CLI_PATH = join(PACKAGE_ROOT_PATH, DIST_PATH, 'cli.js');

Expand Down
21 changes: 10 additions & 11 deletions src/node/plugin-routes/RouteService.ts
@@ -1,6 +1,7 @@
import fastGlob from 'fast-glob';
import fs from 'fs-extra';
import { CLI_BUNDLE_OUTDIR } from '../constants';
import path from 'path';
import { lazyWithPreload } from './lazyWithPreload';

export interface RouteMeta {
routePath: string;
Expand All @@ -17,6 +18,11 @@ export const normalizeRoutePath = (routePath: string) => {
return addLeadingSlash(routePath);
};

const lazyWithPreloadRuntimeCode = fs.readFileSync(
path.join(CLI_BUNDLE_OUTDIR, 'lazyWithPreload.js'),
'utf-8'
);

export class RouteService {
#routeData: RouteMeta[] = [];
constructor(
Expand Down Expand Up @@ -59,14 +65,7 @@ export class RouteService {

generateRoutesCode(ssr?: boolean) {
return `
${
ssr
? ''
: `import loadable from '@loadable/component';
import { ComponentType, forwardRef, lazy, useRef } from 'react';
import { jsx } from 'react/jsx-runtime';
${lazyWithPreload.toString()};`
};
${lazyWithPreloadRuntimeCode.toString()};
import React from 'react';
${this.#routeData
.map((route, index) => {
Expand All @@ -85,15 +84,15 @@ ${this.#routeData
* For SSR, example:
* {
* route: '/',
* element: React.createElement(Route0),
* element: jsx(Route0),
* preload: Route0.preload,
* filePath: '/Users/xxx/xxx/index.md'
* }
*
* For client render, example:
* {
* route: '/',
* element: React.createElement(Route0.default),
* element: jsx(Route0.default),
* preload: Route0.preload,
* filePath: '/Users/xxx/xxx/index.md'
* }
Expand Down
1 change: 1 addition & 0 deletions src/shared/types/index.ts
Expand Up @@ -7,6 +7,7 @@ export { DefaultTheme } from './default-theme';
export interface Header {
id: string;
text: string;
depth: number;
}
export interface SiteSiteData {
title: string;
Expand Down
49 changes: 29 additions & 20 deletions tsup.config.ts
@@ -1,22 +1,31 @@
import { defineConfig } from 'tsup';
import { defineConfig, Options } from 'tsup';

export default defineConfig({
entry: {
'jsx-runtime': 'src/client/runtime/island-jsx-runtime.js',
cli: 'src/node/cli.ts',
index: 'src/node/index.ts'
export default defineConfig([
{
entry: {
'jsx-runtime': 'src/client/runtime/island-jsx-runtime.js',
cli: 'src/node/cli.ts',
index: 'src/node/index.ts'
},
minifyIdentifiers: false,
bundle: true,
platform: 'node',
format: 'esm',
dts: true,
sourcemap: true,
splitting: false,
keepNames: true,
clean: true,
minify: process.env.NODE_ENV === 'production',
skipNodeModulesBundle: true
},
bundle: true,
platform: 'node',
format: 'esm',
dts: true,
sourcemap: true,
splitting: false,
skipNodeModulesBundle: true
// https://github.com/evanw/esbuild/issues/1921
// banner() {
// return {
// // js: `import { createRequire } from 'module';const require = createRequire(import.meta.url);`
// };
// }
});
{
entry: {
lazyWithPreload: 'src/client/runtime/lazyWithPreload.tsx'
},
format: 'esm',
clean: true,
dts: false,
minify: false
}
]);

0 comments on commit 0602a57

Please sign in to comment.