@appigram/react-code-split-ssr 1.3.8 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/bundle.js CHANGED
@@ -1,26 +1,15 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
1
  import { jsx as _jsx } from "react/jsx-runtime";
11
- import { useState, useEffect } from 'react';
2
+ import React, { useState, useEffect } from 'react';
12
3
  const Bundle = ({ mod, loading }) => {
13
4
  const [state, setState] = useState({ mod: null });
14
5
  useEffect(() => {
15
- (function () {
16
- return __awaiter(this, void 0, void 0, function* () {
17
- const Mod = yield mod;
18
- setState({ mod: Mod.default });
19
- });
6
+ (async function () {
7
+ const Mod = await mod;
8
+ setState({ mod: Mod.default });
20
9
  })();
21
10
  }, []);
22
11
  const Mod = state.mod;
23
12
  const Loading = loading || (() => _jsx("div", {}));
24
- return state.mod ? _jsx(Mod, {}) : _jsx(Loading, {});
13
+ return Mod ? _jsx(Mod, {}) : _jsx(Loading, {});
25
14
  };
26
15
  export default Bundle;
@@ -1,4 +1,3 @@
1
- import React from "react";
2
1
  export interface IJSXModule {
3
2
  default: React.FC | React.ComponentClass;
4
3
  }
@@ -1,37 +1,25 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import {} from "react";
11
3
  import { matchPath, Route, Routes } from "react-router";
12
- const generateRoutes = (...args_1) => __awaiter(void 0, [...args_1], void 0, function* (options = {
4
+ const generateRoutes = async (options = {
13
5
  pathname: "/",
14
6
  routes: [],
15
- }) {
7
+ }) => {
16
8
  if (!Array.isArray(options.routes) || options.routes.length === 0) {
17
9
  throw new Error("options.routes must be an non-empty array");
18
10
  }
19
- const preload = options.routes.find((route) => !!matchPath(route.path, options.pathname));
11
+ const preload = options.routes.find((route) => !!route.path && !!matchPath(route.path, options.pathname));
20
12
  const preloadedElement = preload === undefined ? options.notFoundComp : preload.element;
21
- // fallback to previous version
22
- const preloadedComp = typeof preloadedElement === 'function' ?
23
- yield preloadedElement().props.mod
24
- :
25
- yield preloadedElement.props.mod;
13
+ const preloadedComp = typeof preloadedElement === 'function'
14
+ ? await preloadedElement().props.mod
15
+ : await preloadedElement?.props?.mod ?? null;
26
16
  const renderElement = (path, bundle) => {
27
17
  if (!preloadedComp)
28
18
  return bundle;
29
19
  const isRouteMatched = (preload && preload.path === path) || (!preload && !path);
30
- const Element = isRouteMatched ? preloadedComp.default : bundle;
31
- return isRouteMatched ? _jsx(Element, {}) : Element;
20
+ const Element = isRouteMatched ? preloadedComp.default : null;
21
+ return isRouteMatched && Element ? _jsx(Element, {}) : bundle;
32
22
  };
33
- return (_jsxs(Routes, { children: [options.routes.map((props, i) => {
34
- return (_jsx(Route, { path: props.path, element: renderElement(props.path, props.element) }, i));
35
- }), _jsx(Route, { path: '*', element: renderElement(null, options.notFoundComp) }, 'nf')] }));
36
- });
23
+ return (_jsxs(Routes, { children: [options.routes.map((props, i) => (_jsx(Route, { path: props.path, element: renderElement(props.path ?? null, props.element) }, i))), _jsx(Route, { path: '*', element: renderElement(null, options.notFoundComp) }, 'nf')] }));
24
+ };
37
25
  export default generateRoutes;
package/lib/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- import Bundle from './bundle';
2
- import generateRoutes from './generate-routes';
1
+ import Bundle from './bundle.js';
2
+ import generateRoutes from './generate-routes.js';
3
3
  export { Bundle, generateRoutes };
package/lib/index.js CHANGED
@@ -1,3 +1,3 @@
1
- import Bundle from './bundle';
2
- import generateRoutes from './generate-routes';
1
+ import Bundle from './bundle.js';
2
+ import generateRoutes from './generate-routes.js';
3
3
  export { Bundle, generateRoutes };
@@ -0,0 +1,7 @@
1
+ import React from 'react';
2
+ export interface IProps {
3
+ mod: Promise<any>;
4
+ loading?: React.FC;
5
+ }
6
+ declare const Bundle: ({ mod, loading }: IProps) => import("react/jsx-runtime").JSX.Element;
7
+ export default Bundle;
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ const jsx_runtime_1 = require("react/jsx-runtime");
37
+ const react_1 = __importStar(require("react"));
38
+ const Bundle = ({ mod, loading }) => {
39
+ const [state, setState] = (0, react_1.useState)({ mod: null });
40
+ (0, react_1.useEffect)(() => {
41
+ (async function () {
42
+ const Mod = await mod;
43
+ setState({ mod: Mod.default });
44
+ })();
45
+ }, []);
46
+ const Mod = state.mod;
47
+ const Loading = loading || (() => (0, jsx_runtime_1.jsx)("div", {}));
48
+ return state.mod ? (0, jsx_runtime_1.jsx)(Mod, {}) : (0, jsx_runtime_1.jsx)(Loading, {});
49
+ };
50
+ exports.default = Bundle;
@@ -0,0 +1,18 @@
1
+ import React from "react";
2
+ export interface IJSXModule {
3
+ default: React.FC | React.ComponentClass;
4
+ }
5
+ export interface ISSRRoute {
6
+ caseSensitive?: boolean;
7
+ children?: React.ReactNode;
8
+ element?: any;
9
+ index?: boolean;
10
+ path?: string;
11
+ }
12
+ export interface IOptions {
13
+ pathname: string;
14
+ routes: ISSRRoute[];
15
+ notFoundComp?: any;
16
+ }
17
+ declare const generateRoutes: (options?: IOptions) => Promise<import("react/jsx-runtime").JSX.Element>;
18
+ export default generateRoutes;
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ const jsx_runtime_1 = require("react/jsx-runtime");
37
+ const react_1 = __importStar(require("react"));
38
+ const react_router_1 = require("react-router");
39
+ const generateRoutes = async (options = {
40
+ pathname: "/",
41
+ routes: [],
42
+ }) => {
43
+ if (!Array.isArray(options.routes) || options.routes.length === 0) {
44
+ throw new Error("options.routes must be an non-empty array");
45
+ }
46
+ const preload = options.routes.find((route) => !!(0, react_router_1.matchPath)(route.path, options.pathname));
47
+ const preloadedElement = preload === undefined ? options.notFoundComp : preload.element;
48
+ // fallback to previous version
49
+ const preloadedComp = typeof preloadedElement === 'function' ?
50
+ await preloadedElement().props.mod
51
+ :
52
+ await preloadedElement.props.mod;
53
+ const renderElement = (path, bundle) => {
54
+ if (!preloadedComp)
55
+ return bundle;
56
+ const isRouteMatched = (preload && preload.path === path) || (!preload && !path);
57
+ const Element = isRouteMatched ? preloadedComp.default : bundle;
58
+ return isRouteMatched ? (0, jsx_runtime_1.jsx)(Element, {}) : Element;
59
+ };
60
+ return ((0, jsx_runtime_1.jsxs)(react_router_1.Routes, { children: [options.routes.map((props, i) => {
61
+ return ((0, jsx_runtime_1.jsx)(react_router_1.Route, { path: props.path, element: renderElement(props.path, props.element) }, i));
62
+ }), (0, jsx_runtime_1.jsx)(react_router_1.Route, { path: '*', element: renderElement(null, options.notFoundComp) }, 'nf')] }));
63
+ };
64
+ exports.default = generateRoutes;
@@ -0,0 +1,3 @@
1
+ import Bundle from './bundle';
2
+ import generateRoutes from './generate-routes';
3
+ export { Bundle, generateRoutes };
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.generateRoutes = exports.Bundle = void 0;
7
+ const bundle_1 = __importDefault(require("./bundle"));
8
+ exports.Bundle = bundle_1.default;
9
+ const generate_routes_1 = __importDefault(require("./generate-routes"));
10
+ exports.generateRoutes = generate_routes_1.default;
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "@appigram/react-code-split-ssr",
3
- "version": "1.3.8",
3
+ "version": "2.0.0",
4
4
  "description": "React code splitting with SSR",
5
+ "type": "module",
5
6
  "main": "./lib/index.js",
6
7
  "scripts": {
7
8
  "build": "tsc",
@@ -29,8 +30,8 @@
29
30
  "typescript": "6.0.3"
30
31
  },
31
32
  "dependencies": {
32
- "react": "19.2.5",
33
- "react-dom": "19.2.5",
34
- "react-router": "7.14.2"
33
+ "react": "19.2.6",
34
+ "react-dom": "19.2.6",
35
+ "react-router": "7.15.0"
35
36
  }
36
37
  }
package/src/bundle.tsx CHANGED
@@ -5,8 +5,12 @@ export interface IProps {
5
5
  loading?: React.FC;
6
6
  }
7
7
 
8
+ type BundleState = {
9
+ mod: React.FC | React.ComponentClass | null;
10
+ };
11
+
8
12
  const Bundle = ({ mod, loading }: IProps) => {
9
- const [state, setState] = useState({ mod: null });
13
+ const [state, setState] = useState<BundleState>({ mod: null });
10
14
 
11
15
  useEffect(() => {
12
16
  (async function () {
@@ -17,7 +21,8 @@ const Bundle = ({ mod, loading }: IProps) => {
17
21
 
18
22
  const Mod = state.mod;
19
23
  const Loading = loading || (() => <div />);
20
- return state.mod ? <Mod /> : <Loading />;
24
+
25
+ return Mod ? <Mod /> : <Loading />;
21
26
  };
22
27
 
23
- export default Bundle;
28
+ export default Bundle;
@@ -1,4 +1,4 @@
1
- import React, { ReactElement } from "react";
1
+ import { type ReactElement } from "react";
2
2
  import { matchPath, Route, Routes } from "react-router";
3
3
 
4
4
  export interface IJSXModule {
@@ -8,7 +8,7 @@ export interface IJSXModule {
8
8
  export interface ISSRRoute {
9
9
  caseSensitive?: boolean;
10
10
  children?: React.ReactNode;
11
- element?: any; // () => React.FunctionComponentElement<{ mod: Promise<IJSXModule> }>;
11
+ element?: any;
12
12
  index?: boolean;
13
13
  path?: string;
14
14
  }
@@ -16,7 +16,7 @@ export interface ISSRRoute {
16
16
  export interface IOptions {
17
17
  pathname: string;
18
18
  routes: ISSRRoute[];
19
- notFoundComp?: any; // () => React.FunctionComponentElement<{ mod: Promise<IJSXModule> }>;
19
+ notFoundComp?: any;
20
20
  }
21
21
 
22
22
  const generateRoutes = async (
@@ -30,38 +30,34 @@ const generateRoutes = async (
30
30
  }
31
31
 
32
32
  const preload = options.routes.find(
33
- (route) => !!matchPath(route.path, options.pathname)
33
+ (route): route is ISSRRoute & { path: string; } => !!route.path && !!matchPath(route.path, options.pathname)
34
34
  );
35
35
 
36
36
  const preloadedElement = preload === undefined ? options.notFoundComp : preload.element;
37
37
 
38
- // fallback to previous version
39
- const preloadedComp: any = typeof preloadedElement === 'function' ?
40
- await preloadedElement().props.mod
41
- :
42
- await preloadedElement.props.mod;
38
+ const preloadedComp: IJSXModule | null = typeof preloadedElement === 'function'
39
+ ? await preloadedElement().props.mod
40
+ : await preloadedElement?.props?.mod ?? null;
43
41
 
44
- const renderElement = (path: string, bundle: ReactElement) => {
42
+ const renderElement = (path: string | null, bundle: ReactElement) => {
45
43
  if (!preloadedComp) return bundle;
46
44
  const isRouteMatched = (preload && preload.path === path) || (!preload && !path);
47
- const Element = isRouteMatched ? preloadedComp.default : bundle;
48
- return isRouteMatched ? <Element /> : Element;
45
+ const Element = isRouteMatched ? preloadedComp.default : null;
46
+ return isRouteMatched && Element ? <Element /> : bundle;
49
47
  };
50
48
 
51
49
  return (
52
50
  <Routes>
53
- {options.routes.map((props, i) => {
54
- return (
55
- <Route
56
- key={i}
57
- path={props.path}
58
- element={renderElement(props.path, props.element)}
59
- />
60
- );
61
- })}
51
+ {options.routes.map((props, i) => (
52
+ <Route
53
+ key={i}
54
+ path={props.path}
55
+ element={renderElement(props.path ?? null, props.element)}
56
+ />
57
+ ))}
62
58
  <Route key='nf' path='*' element={renderElement(null, options.notFoundComp)} />
63
59
  </Routes>
64
60
  );
65
61
  };
66
62
 
67
- export default generateRoutes;
63
+ export default generateRoutes;
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- import Bundle from './bundle';
2
- import generateRoutes from './generate-routes';
1
+ import Bundle from './bundle.js';
2
+ import generateRoutes from './generate-routes.js';
3
3
 
4
- export { Bundle, generateRoutes };
4
+ export { Bundle, generateRoutes };
package/tsconfig.json CHANGED
@@ -1,20 +1,19 @@
1
1
  {
2
2
  "compilerOptions": {
3
3
  "outDir": "lib",
4
- "target": "ES6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */,
5
- "module": "esnext" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
6
- "lib": [
7
- "esnext",
8
- "dom"
9
- ] /* Specify library files to be included in the compilation. */,
10
- "allowJs": true /* Allow javascript files to be compiled. */,
11
- "jsx": "react-jsx" /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */,
12
- "noImplicitAny": false /* Raise error on expressions and declarations with an implied 'any' type. */,
13
- "moduleResolution": "node",
14
- "allowSyntheticDefaultImports": true /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */,
15
- "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
4
+ "rootDir": "src",
5
+ "target": "ES2022",
6
+ "module": "NodeNext",
7
+ "moduleResolution": "NodeNext",
8
+ "lib": ["ES2022", "dom"],
9
+ "allowJs": true,
10
+ "jsx": "react-jsx",
11
+ "noImplicitAny": false,
12
+ "allowSyntheticDefaultImports": true,
13
+ "esModuleInterop": true,
16
14
  "skipLibCheck": true,
17
- "declaration": true
15
+ "declaration": true,
16
+ "verbatimModuleSyntax": true
18
17
  },
19
18
  "include": ["src/**/*"],
20
19
  "exclude": ["node_modules", "build", "scripts", "webpack", "jest"]