@visulima/packem-share

Shared types, utilities, and constants

@visulima/packem-share

Shared types, utilities, and constants used across the Packem packages. It centralizes the build-context contracts, Rollup helper utilities, and the constants that drive module resolution and export handling.

This package is ESM-only ("type": "module" with no CommonJS exports) and requires Node.js ^22.14.0 || >= 24.10.0. It has a rollup peer dependency.

Installation

npm install @visulima/packem-share

Subpath exports

The root re-exports everything from the three subpaths, which are also importable directly:

import { /* constants, utils, types */ } from '@visulima/packem-share'
import { /* utils */ } from '@visulima/packem-share/utils'
import type { /* types */ } from '@visulima/packem-share/types'
import { /* constants */ } from '@visulima/packem-share/constants'

Constants — @visulima/packem-share/constants

import {
  VALID_EXPORT_EXTENSIONS,
  DEFAULT_EXTENSIONS,
  DEFAULT_LOADERS,
  PRODUCTION_ENV,
  DEVELOPMENT_ENV,
  RUNTIME_EXPORT_CONVENTIONS,
  SPECIAL_EXPORT_CONVENTIONS,
  EXCLUDE_REGEXP,
  ENDING_REGEX,
  CHUNKS_PACKEM_FOLDER,
  SHARED_PACKEM_FOLDER,
  ALLOWED_TRANSFORM_EXTENSIONS_REGEX,
} from '@visulima/packem-share/constants'
ConstantTypeDescription
VALID_EXPORT_EXTENSIONSReadonlyArray<string>Valid file extensions for the package.json exports field.
DEFAULT_EXTENSIONSstring[]Default extensions Packem processes (adds .ctsx/.mtsx).
DEFAULT_LOADERSRecord<string, Loader>Maps file extensions to esbuild loader types.
PRODUCTION_ENVstring"production".
DEVELOPMENT_ENVstring"development".
RUNTIME_EXPORT_CONVENTIONSSet<string>edge-light, react-native, react-server.
SPECIAL_EXPORT_CONVENTIONSSet<string>Runtime conventions plus the environment names.
EXCLUDE_REGEXPRegExpMatches node_modules for skipping processing.
ENDING_REGEXRegExpMatches file extensions, including .d.ts/.d.mts/.d.cts.
CHUNKS_PACKEM_FOLDERstring"packem_chunks" — directory for generated chunks.
SHARED_PACKEM_FOLDERstring"packem_shared" — directory for shared resources.
ALLOWED_TRANSFORM_EXTENSIONS_REGEXRegExpMatches JS/TS files (and module variants) eligible for transform.

Utilities — @visulima/packem-share/utils

import {
  arrayify,
  createRollupLogger,
  enhanceRollupError,
  FileCache,
  getCacheHash,
  getChunkFilename,
  getEntryFileNames,
  getDtsExtension,
  getOutputExtension,
  getHash,
  getPackageName,
  getRegexMatches,
  isBareSpecifier,
  isFromNodeModules,
  isOutsideProject,
  parseSpecifier,
  memoize,
  memoizeByKey,
  replaceContentWithinMarker,
  sortUserPlugins,
  svgToCssDataUri,
  svgToTinyDataUri,
  svgEncoder,
  warn,
} from '@visulima/packem-share/utils'

import type { RollupLogger, Memoized } from '@visulima/packem-share/utils'
  • arrayify — Normalize a value into an array.
  • createRollupLogger / RollupLogger — Build a Rollup-compatible logger.
  • enhanceRollupError — Enrich Rollup errors with extra context.
  • FileCache — Filesystem-backed cache class.
  • getCacheHash, getHash — Hashing helpers.
  • getChunkFilename, getEntryFileNames — Output filename helpers.
  • getDtsExtension, getOutputExtension — Resolve declaration/output extensions.
  • getPackageName — Extract a package name from a specifier.
  • getRegexMatches — Collect all matches for a pattern.
  • isBareSpecifier, isFromNodeModules, isOutsideProject, parseSpecifier — Import-specifier analysis.
  • memoize, memoizeByKey / Memoized — Memoization helpers.
  • replaceContentWithinMarker — Replace content between markers.
  • sortUserPlugins — Order user-supplied plugins.
  • svgToCssDataUri, svgToTinyDataUri, svgEncoder — SVG data-URI encoding.
  • warn — Emit a warning.

Types — @visulima/packem-share/types

import type {
  BuildContext,
  BuildContextBuildAssetAndChunk,
  BuildContextBuildEntry,
  BuildHooks,
  Environment,
  Format,
  Mode,
  Runtime,
} from '@visulima/packem-share/types'

Core type aliases

type Environment = 'production' | 'development' | undefined
type Mode = 'build' | 'jit' | 'watch'
type Format = 'cjs' | 'esm'
type Runtime =
  | 'browser' | 'bun' | 'deno' | 'edge-light' | 'electron'
  | 'node' | 'react-native' | 'react-server' | 'workerd' | undefined

BuildContext<T>

The context object passed to build hooks. Carries the resolved options, package manifest, logger, hookable instance, dependency tracking sets, and the collected build entries.

type BuildContext<T = unknown> = {
  buildEntries: (BuildContextBuildAssetAndChunk | BuildContextBuildEntry)[]
  dependencyGraphMap: Map<string, Set<[string, string]>>
  environment: Environment
  hoistedDependencies: Set<string>
  hooks: Hookable<BuildHooks<T>>
  implicitDependencies: Set<string>
  jiti: Jiti
  logger: Pail
  mode: Mode
  options: T
  pkg: PackageJson
  tsconfig?: TsConfigResult
  usedDependencies: Set<string>
  warnings: Set<string>
}

BuildHooks<T>

The lifecycle hooks contract. Each hook receives the BuildContext (some also receive a Rollup build/options/watcher object):

interface BuildHooks<T> {
  'build:before': (context: BuildContext<T>) => Promise<void> | void
  'build:prepare': (context: BuildContext<T>) => Promise<void> | void
  'build:done': (context: BuildContext<T>) => Promise<void> | void
  'builder:before': (name: string, context: BuildContext<T>) => Promise<void> | void
  'builder:done': (name: string, context: BuildContext<T>) => Promise<void> | void
  'rollup:options': (context: BuildContext<T>, options: RollupOptions) => Promise<void> | void
  'rollup:build': (context: BuildContext<T>, build: RollupBuild) => Promise<void> | void
  'rollup:done': (context: BuildContext<T>) => Promise<void> | void
  'rollup:watch': (context: BuildContext<T>, watcher: RollupWatcher) => Promise<void> | void
  'rollup:dts:options': (context: BuildContext<T>, options: RollupOptions) => Promise<void> | void
  'rollup:dts:build': (context: BuildContext<T>, build: RollupBuild) => Promise<void> | void
  'rollup:dts:done': (context: BuildContext<T>) => Promise<void> | void
  /** @deprecated Use "builder:before" instead */
  'typedoc:before': (context: BuildContext<T>) => Promise<void> | void
  /** @deprecated Use "builder:done" instead */
  'typedoc:done': (context: BuildContext<T>) => Promise<void> | void
  'validate:before': (context: BuildContext<T>) => Promise<void> | void
  'validate:done': (context: BuildContext<T>) => Promise<void> | void
}

BuildContextBuildEntry / BuildContextBuildAssetAndChunk

Describe a single emitted entry or asset/chunk, including its path, size breakdown (bytes/gzip/brotli), modules, chunks, exports, and dynamic imports.

Support

Contribute to our work and keep us going

Community is the heart of open source. The success of our packages wouldn't be possible without the incredible contributions of users, testers, and developers who collaborate with us every day.Want to get involved? Here are some tips on how you can make a meaningful impact on our open source projects.

Ready to help us out?

Be sure to check out the package's contribution guidelines first. They'll walk you through the process on how to properly submit an issue or pull request to our repositories.

Submit a pull request

Found something to improve? Fork the repo, make your changes, and open a PR. We review every contribution and provide feedback to help you get merged.

Good first issues

Simple issues suited for people new to open source development, and often a good place to start working on a package.
View good first issues