Skip to content
/ preland Public

Framework Agnostic utils to generate atomic islands for preact components

License

Notifications You must be signed in to change notification settings

dumbjs/preland

Repository files navigation

header

Preact Island

Framework Agnostic utils to generate atomic islands for preact components

What is this?

preland is a set of utilities that allow you to read, find , transform and generate island components for preact. None of the utlities in this set have any dependency on any bundler or framework and just statically analyse the given code to look for preact islands.

Highlights

  • Tiny
  • Supports Typescript
  • Handles individual named island exports from the same file
  • Provides utils to help parse and generate preact compatible ASTs and add imports to it

Usage

  • Installation as simple as adding @dumbjs/preland to your dependencies.

    $ npm i @dumbjs/preland

API

@dumbjs/preland

export function readSourceFile(file: any): any
export function findIslands(sourceCode: any): {
  id: any
  node: any
  nodeItem: any
}[]
export function islandNodeToTemplate(island: any): {
  server: any
  client: string
}
export function getExportedNodes(astBody: any): any
export function generateServerTemplate(name: any): string
export function generateClientTemplate(name: any): string
/**
 *
 * @param {import("acorn").Node} functionAST
 * @param {object} options
 * @param {string[]} options.transpiledIdentifiers , identifiers to look for when
 * searching the function ast. These are generally `_jsx` and `_jsxs` when working with
 * the `automatic` JSX Runtime in bundlers but might differ in your scenario
 * @returns
 */
export function isFunctionIsland(
  functionAST: import('acorn').Node,
  {
    transpiledIdentifiers,
  }?: {
    transpiledIdentifiers: string[]
  }
): boolean
export function getIslandName(name: any): string
export const DEFAULT_TRANSPILED_IDENTIFIERS: string[]

@dumbjs/preland/ast

export function walker(ast: any, visitors: any): void
export function astFromCode(code: any): import('acorn').Program
export function codeFromAST(ast: any): any
/**
 * Takes in an AST node and returns the name of the default
 * export for it
 * @param {*} ast
 * @returns
 */
export function getDefaultExportName(ast: any): any
export function getDefaultExport(ast: any): any[]
/**
 * NOT A PURE FUNCTION!
 * modifies the passed AST with the
 * requested import
 *
 * Note:
 * This function does not rename / or add a new identifier for the
 * requested import as that could add in a lot more complexity
 * and is easier handled in the user land. Changing and renaming
 * import specifier would also need proper tranformation to be handled
 * for cases where the imports might be responsible for things
 * like JSX.
 * @returns
 */
export function addImportToAST(ast: any): (
  name: string,
  from: string,
  {
    named,
  }: {
    named: boolean
  }
) => void
export function getNamedExport(ast: any, name: any): any
/**
 * NOT A PURE FUNCTION!
 * removes the export from the passed AST
 */
export function removeExportFromAST(ast: any): (
  name: string,
  options: {
    named: boolean
  }
) => void
export function isTopLevelFunction(parents: any): any

License

MIT