code-split.macro
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

code-split.macro

Types Code coverage Build status NPM Version MIT License

npm i code-split.macro

A macro for assisting in client/server interop for code splitting so server side imports are not asynchronous

The Problem

We want our routes and other heavy components to be code split on the client side to reduce the size of our bundle, but we encounter issues when trying to render on the server because packages like react and preact don't handle promises when rendering apps to strings. Ideally, we would only include those asynchronous imports when building our client code and use regular require() statements when prerendering our app.

This macro solves the import/require interop part of that problem.

Requirements

Quick Start

import codeSplit from 'code-split.macro'

// __SERVER__ below would suggest you're using something like
// webpack.DefinePlugin() to create a special __SERVER__ constant
// that is `true` when you're building the SSR part of your app
// and `false` when building the client part
export const Home = codeSplit('./Home', __SERVER__)

////////////////////////////////////////////////////////////////////////////////
//                               ⬇ BECOMES ⬇                                //
///////////////////////////////////////////////////////////////////////////////

export const Home = __SERVER__
  ? () => require('./Home')
  : () => import(/* webpackChunkName: "src/pages/Home" */ './Home')

API

codeSplit(path: string, isServerBuild: boolean)

Argument Type Required? Description
path string Yes The relative path to the file you're code splitting
isServerBuild boolean Yes Should be true if this is your SSR build, false if this is your client build

LICENSE

MIT

Package Sidebar

Install

npm i code-split.macro

Weekly Downloads

1

Version

1.0.4

License

MIT

Unpacked Size

16.1 kB

Total Files

11

Last publish

Collaborators

  • jaredlunde