Packager

raw JSON →
0.1.9 verified Sat Apr 25 auth: no javascript

A browser-based trans-bundler that runs entirely in the browser, enabling code transformation and bundling without a server. Current version 0.1.9. It leverages existing bundler concepts for client-side use, focusing on ease of integration and in-browser workflows. Differentiates from server-side bundlers by operating fully in the browser environment, making it suitable for online editors or prototyping tools.

error TypeError: packager is not a function
cause Using CommonJS require instead of ESM import.
fix
Use 'import packager from 'packager'' or 'import { bundle } from 'packager'' in an ESM context.
error Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'code')
cause The 'resolve' function did not return an object with 'code' property.
fix
Make sure 'resolve' returns an object with a 'code' property containing the module's source code as a string.
error Module not found: Error: Can't resolve 'packager'
cause Packager is not installed or not in node_modules.
fix
Run 'npm install packager' (or yarn/pnpm equivalent).
breaking The 'resolve' option is required; omitting it causes a runtime error.
fix Always provide a 'resolve' function that returns an object with a 'code' property for each imported module.
gotcha The 'bundle' function returns a Promise, not a synchronous result.
fix Use 'await' or '.then()' when calling 'bundle'.
deprecated Export 'transform' is deprecated; use 'bundle' instead.
fix Replace 'import { transform } from 'packager'' with 'import { bundle } from 'packager'' and adjust usage.
breaking In version 0.1.9, the 'resolve' callback must return an object with both 'code' and 'map' properties; returning only 'code' will be removed in future.
fix Ensure your resolve function returns an object with 'code' (string) and optionally 'map' (object). For now, 'code' is sufficient but prepare to add 'map' eventually.
npm install packager
yarn add packager
pnpm add packager

Shows how to bundle a simple ES module string with a custom resolver, outputting the transformed code.

import { bundle } from 'packager';

const code = `
import { foo } from './foo';
console.log(foo);
`;

async function run() {
  const result = await bundle(code, {
    resolve: (specifier) => {
      if (specifier === './foo') {
        return { code: 'export const foo = "bar";' };
      }
      throw new Error(`Cannot resolve ${specifier}`);
    }
  });
  console.log(result.code);
}
run();