unpkg-bundler Client-Side ESBuild Bundler

0.0.12 · active · verified Tue Apr 21

unpkg-bundler is a client-side JavaScript library (current version 0.0.12) designed for in-browser code transpilation and bundling, with a unique capability to dynamically fetch and load npm packages directly from unpkg.com. It leverages esbuild internally for high-performance bundling and supports both ES modules (ESM) and CommonJS (CJS) syntax, as well as TypeScript and TSX with React code. The library's core differentiator is its ability to operate without filesystem access, making it ideal for browser-based development environments, interactive code editors, or learning platforms that need to execute user-provided code with npm dependencies on the fly. Its release cadence is currently irregular, typical for projects in early development.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates bundling a React component, including automatic fetching of `react` and `react-dom` from `unpkg.com`, and shows how to execute the resulting bundled code in a browser environment.

import bundle from 'unpkg-bundler';

// React component code to be bundled.
// unpkg-bundler will automatically fetch React and ReactDOM from unpkg.
const reactCode = `
import React from 'react';
import ReactDOM from 'react-dom';

const App = () => {
  return (
    <div style={{ fontFamily: 'sans-serif', textAlign: 'center' }}>
      <h1>Hello from unpkg-bundler!</h1>
      <h2>This code was bundled and transpiled on the client-side.</h2>
      <p>It automatically fetched React and ReactDOM from unpkg.com.</p>
    </div>
  );
};

// Typically, you'd render this into an existing DOM element.
// For this quickstart, we'll just demonstrate the bundling output.
const rootElement = document.createElement('div');
rootElement.id = 'root';
document.body.appendChild(rootElement);

ReactDOM.render(<App />, rootElement);
`;

const runBundle = async () => {
  console.log('Bundling React code...');
  const { code, err } = await bundle(reactCode);

  if (err) {
    console.error('Bundling error:', err);
    return;
  }

  console.log('Bundle successful. Executing code...');
  // To execute in a browser, you can create a <script> tag or use eval.
  // Be cautious with eval in production due to security implications.
  // For demonstration:
  try {
    // This will execute the bundled code directly in the current scope.
    // In a real application, you might inject it into an iframe for isolation.
    new Function(code)();
    console.log('Code executed successfully. Check the document body for the React app.');
  } catch (executionError) {
    console.error('Error executing bundled code:', executionError);
  }
};

runBundle();

view raw JSON →