microbundle

repository·master·Indexed 27 days ago

https://github.com/developit/microbundle

A zero-configuration bundler for tiny JavaScript libraries powered by Rollup. It automatically produces optimized CJS, ESM, and UMD bundles from a single source file, including a 'modern' bundle for browsers supporting <script type="module">. It supports TypeScript, CSS Modules, Module Workers, and can be used via CLI or as a programmatic asynchronous function.

Tokens
3.8K
Snippets
7
Records
26
Agent score
93%

What's inside microbundle

  1. Configure package.json for Microbundle

    master

    Microbundle is a zero-configuration bundler that uses your package.json to determine source files and output locations.

    To set up a standard build, define the following keys:

    • source: Your entry point file.
    • main: The CommonJS (CJS) bundle location.
    • module: The ES Modules (ESM) bundle location.
    • unpkg: The Universal Module Definition (UMD) bundle location.
    • exports: Defines how the package is exported, enabling modern ESM support and subpath exports.

    Add a build script to run the bundler and a dev script to watch for changes.

    {
      "name": "foo",
      "type": "module",
      "source": "src/foo.js",
      "exports": {
        "require": "./dist/foo.cjs",
        "default": "./dist/foo.modern.js"
      },
      "main": "./dist/foo.cjs",
      "module": "./dist/foo.module.js",
      "unpkg": "./dist/foo.umd.js",
      "scripts": {
        "build": "microbundle",
        "dev": "microbundle watch"
      }
    }
  2. Use Microbundle CLI commands

    master

    Microbundle provides two primary commands for bundling:

    • microbundle (or microbundle build): Bundles your code once and exits.
    • microbundle watch: Bundles your code and automatically re-bundles whenever files change.

    Microbundle automatically determines which dependencies to inline based on your package.json.

  3. Mangle object properties for smaller bundles

    master

    To reduce bundle size, you can rename internal object properties or class members. This is enabled by creating a mangle.json file or adding a "mangle" property to your package.json.

    You can use a regular expression to control which properties are mangled.

    {
      "mangle": {
        "regex": "^_"
      }
    }
  4. Configure CSS and CSS Modules

    master

    Microbundle supports importing CSS via import "./foo.css".

    • External CSS (Default): Generates a minified .css file in the output directory.
    • Inlined CSS: Use the --css inline flag to import CSS as a string: import css from './foo.css';.
    • CSS Modules:
      • Files ending in .module.css are treated as modules by default.
      • To treat all .css files as modules, use --css-modules true.
      • To disable CSS Modules, use --no-css-modules or --css-modules false.
      • Custom scope names can be set via --css-modules "[name]_[hash:base64:7]" using naming conventions.
  5. Use Microbundle with TypeScript

    master

    To use TypeScript, point the input to a .ts file via the CLI or the source key in package.json.

    Best Practices:

    • Set "module": "ESNext" and "target": "ESNext" in your tsconfig.json to match Microbundle's internal configuration.
    • If you want to include non-entry files (like ambient declarations), add them to the files or include array in tsconfig.json.
    • CSS Modules in TS: If using CSS Modules, add "include": ["node_modules/microbundle/index.d.ts"] to your tsconfig.json.
    • NodeNext Compatibility: To ensure .d.ts files are visible to projects using moduleResolution: 'NodeNext', add a types key to your package.json's exports mapping.
  6. Use Modern Mode for modern browsers

    master

    Modern Mode outputs a modern bundle designed for browsers that support <script type="module">. This bundle preserves modern JavaScript features (like async/await, arrow functions, and destructuring) using Babel's "bugfixes" mode. This results in smaller and faster bundles compared to the standard esm output.

    To enable Modern Mode, include an exports field in your package.json pointing to the modern bundle (typically with a .mjs or .modern.js extension).

    {
      "main": "./dist/foo.umd.js",
      "module": "./dist/foo.module.mjs",
      "exports": "./dist/foo.modern.mjs",
      "scripts": {
        "build": "microbundle src/foo.js"
      }
    }
  7. Configure entry points and output filenames in package.json

    master

    Microbundle uses package.json properties to define the bundling workflow.

    • Input: The source property defines the entry module.
    • Output: The main, umd:main, module, and exports properties define the paths for generated bundles.
    • UMD Name: For UMD builds, Microbundle uses the camelCase version of the name field. You can override this with amdName in package.json or the --name CLI flag.

    Note for {"type":"module"} packages: If your package is an ES Module package, you must change the extension for CommonJS bundles to .cjs.

    {
      "source": "src/index.js",             // input
      "main": "dist/foo.js",                // CommonJS output bundle
      "umd:main": "dist/foo.umd.js",        // UMD output bundle
      "module": "dist/foo.mjs",             // ES Modules output bundle
      "exports": {
        "types": "./dist/foo.d.ts",         // TypeScript typings for NodeNext modules
        "require": "./dist/foo.js",         // CommonJS output bundle
        "default": "./dist/foo.modern.mjs", // Modern ES Modules output bundle
      },
      "types": "dist/foo.d.ts"
    }
  8. Configure build settings via publishConfig

    master
    You can use the publishConfig property in package.json to override configuration specifically for production/publishing, allowing you to use different entry points or output paths for development (e.g., for Jest).