Skip to main content

denopack logo

Bundling and minification toolset for Deno


Preface

🦕📦 denopack is a CLI tool and a collection of plugins that for bundling Typescript code to be used with Deno or in the browser. This library uses the browser versions of Rollup (bundling) and Terser (compression/minification).

This library is made for Deno and is thus fully usable in Deno as a package or as a CLI app with deno install. No node_modules, npm/yarn scripts, etc are needed.

Contents

Goals

There is absolutely nothing wrong with deno bundle, but in its current state it is missing several features that are present in the NodeJS ecosystem. The goal is to provide an alternative bundling method that can - together with a plugin system, extending functionality - provide several key wishlist features without turning to Node:

  • Tree shaking comes built-in with Rollup
  • Minification by the usage of the Terser plugin
  • Source Maps
  • Lock file support, checking checksums from the lockfile against loaded code
  • File watching (currently not supported in conjunction with pluginTypescriptCompile)
  • Redirects on main module are checked and handled (circumvents denoland/deno/issues/3082)

More to come, also see the deno bundle roadmap/wishlist over at denoland/deno/issues/4549

General note: make sure you are running Deno v1.2.0 or later

CLI

Installation

deno install --unstable --allow-read --allow-write --allow-env --allow-net -n denopack https://deno.land/x/denopack@0.5.0/cli.ts
eggs install --unstable --allow-read --allow-write --allow-env --allow-net -n denopack https://x.nest.land/denopack@0.5.0/cli.ts

NOTE: denopack uses unstable Deno APIs. These APIs are not final and may break, but this does mean --unstable is mandatory!

Usage

Usage:
  $ denopack -i mod.ts

Options:
  -v, --version                Display version number
  -i, --input <pathToFile>     The input file (most likely mod.ts)
  -o, --output [pathToFile]    The output file name
  -d, --dir [pathToDir]        The output directory
  -c, --config [pathToConfig]  The config file. Use --defaultConfig for default values
  --cache <cacheLocation>      Persist build cache
  --watch <dirOrFile>          Watch a file or directory and rebuild on changes
  --defaultConfig              Prints the default config to stdout
  --print                      Prints the generated bundle to stdout
  -h, --help                   Display this message

Examples:
denopack -i mod.ts
denopack -i mod.ts -o bundle.js
denopack -i mod.ts --dir dist
denopack -c denopack.config.ts
denopack -i mod.ts -o out.js -d dist -c denopack.config.ts

Usage with script runners

In case you don’t want to globally install denopack, but want to use it locally with script runners: this is absolutely, totally possible since denopack uses 0 NodeJS specific code!

Script runners that should work out-of-the-box:

# example

scripts:
  start: deno run --unstable --allow-read --allow-write https://deno.land/x/denopack@0.5.0/cli.ts
vr run start -i mod.ts -o bundle.js
#example

scripts:
  start:
    file: https://deno.land/x/denopack@0.5.0/cli.ts
    deno_options:
      allow-read: true
      allow-write: true
      unstable: true
denox run start -i mod.ts -o bundle.js
{
  "config": {
    "start": "--unstable --allow-read --allow-write https://deno.land/x/denopack@0.5.0/cli.ts -i mod.ts -o bundle.js"
  }
}
Commands start

Permissions

NOTE: both denopack and its plugins use unstable Deno APIs. These APIs are not final and may break, but this does mean --unstable is mandatory!

The CLI itself can run with a base permission of --allow-read, printing to stdout with the -p flag. Writing to file naturally requires --allow-write.

Additionally, the various built-in plugins can require extra permissions like --allow-net and --allow-env. More info can be found here.

Config file

Importing a Rollup config file is supported using the -c <path/to/config> flag and follow the same conventions as Rollup:

  • use default export for your config
  • ideally call it rollup.config.ts or denopack.config.ts

Plugins

Since the bundling logic - aside file system/network access - is handled by Rollup, the remaining core functionality of denopack is based around plugins that use Deno APIs for key features.

A list of included plugins and a collection of strategies are included in the plugin directory. Documentation from Rollup regarding plugins is available on their docs site.

Usage

If you only need plugins or hooks - for example to create a config file - you can import straight from the mod.ts in the plugin directory.

import /* whatever plugins/hooks are needed */ "https://deno.land/x/denopack@0.5.0/plugin/mod.ts";

export default {
  file: "mod.ts",
  plugins: [
    /* whatever plugins or hooks were imported */
  ],
};

Usage without CLI

If you want to handle the building/bundling yourself, the toplevel mod.ts also includes rollup that exposes the Rollup Javascript API and several typings.

import {
  rollup /* whatever plugins/hooks are needed */,
} from "https://deno.land/x/denopack@0.5.0/mod.ts";
import type { RollupOptions } from "https://deno.land/x/denopack@0.5.0/mod.ts";

Just like the CLI, you can use it out-of-the-box with deno run or script runners like the ones mentioned up above.

Contributing

See CONTRIBUTING.md

Acknowledgements

  • Reddit user u/HarmonicAscendant - unrelated to this library - who coined the name name Denopack
  • The sauropod and package emoji’s courtesy of Twemoji