Electrode Subapp for Preact and Redux Bundler
This module, `subapp-pbundle`, version 1.1.1, serves as an integration layer for Electrode subapps utilizing the Preact UI framework and `redux-bundler` for state management. It essentially re-exports core functionalities from `subapp-web` and adapts them for Preact, including re-exporting Preact's `h`, `Component`, and `render` APIs for convenience. A key differentiator is its `reduxBundlerLoadSubApp` API, specifically designed for loading `redux-bundler`-based subapps within the Electrode ecosystem. The package is primarily distributed as ES modules, advocating for modern bundling practices like tree-shaking. While it provides server-side rendering (SSR) context support via `AppContext`, further support for advanced features like React Router and Preact Suspense remains "TBD," suggesting a slower release cadence or a maintenance-focused development cycle since its initial release around 2016 by WalmartLabs.
Common errors
-
SyntaxError: Cannot use import statement outside a module
cause Attempting to use `import` or `export` syntax in a CommonJS (`.js` without `"type": "module"` in `package.json` or a `.cjs` file) Node.js environment.fixRename your file to `.mjs`, add `"type": "module"` to your `package.json`, or configure your build system (e.g., Webpack) to correctly handle ESM modules. -
Error: Cannot find module 'preact' or 'Error: Cannot find module 'preact-render-to-string''
cause The peer dependencies `preact` or `preact-render-to-string` are not installed in the consuming project's `node_modules`.fixRun `npm install preact preact-render-to-string` or `yarn add preact preact-render-to-string` in your project's root directory. -
ReferenceError: Promise is not defined
cause The runtime environment (e.g., older browser, specific Node.js version) is missing global ES6+ APIs like `Promise` or array iteration methods.fixInclude a polyfill for ES6+ features in your application. Common solutions include `core-js` or a service like `polyfill.io`.
Warnings
- breaking This package is distributed as ES Modules (ESM) only. Attempting to use CommonJS `require()` syntax will result in errors.
- gotcha `preact` and `preact-render-to-string` are declared as peer dependencies and must be installed explicitly in your project's `package.json`.
- gotcha The package assumes the presence of polyfills for modern ES6+ APIs (e.g., `Promise`, Array methods) in the runtime environment. These are not bundled with `subapp-pbundle`.
Install
-
npm install subapp-pbundle -
yarn add subapp-pbundle -
pnpm add subapp-pbundle
Imports
- h, reduxBundlerLoadSubApp
const { h, reduxBundlerLoadSubApp } = require('subapp-pbundle');import { h, reduxBundlerLoadSubApp } from 'subapp-pbundle'; - AppContext
const { AppContext } = require('subapp-pbundle');import { AppContext } from 'subapp-pbundle'; - Component, render
import { Component, render } from 'preact';import { Component, render } from 'subapp-pbundle';
Quickstart
/** @jsx h */
import { h, reduxBundlerLoadSubApp, AppContext } from "subapp-pbundle";
import { useState } from 'preact/hooks'; // Assuming preact/hooks is also available
const MyPreactComponent = () => {
const [count, setCount] = useState(0);
return (
<AppContext.Consumer>
{({ isSsr, ssr, subApp }) => (
<div>
<h1>My Electrode Subapp</h1>
<p>Current count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
<p>Running on SSR: {`${Boolean(isSsr)}`}</p>
{isSsr && ssr && ssr.request && <p>Request URL: {ssr.request.url ?? 'N/A'}</p>}
<p>Subapp name: {subApp?.name ?? 'Unknown'}</p>
</div>
)}
</AppContext.Consumer>
);
};
export default reduxBundlerLoadSubApp({ name: "MyComponent", Component: MyPreactComponent });