0.19.2 • Published 1 year ago

distilt v0.19.2

Weekly downloads
219
License
MIT
Repository
github
Last release
1 year ago

distilt

bundle a project for node and browser using swc and rollup

MIT License Latest Release Github PRs Welcome


Usage

npm i -D distilt

Add to your package.json:

{
  "scripts": {
    "build": "distilt"
    "prepublishOnly": "npm run build"
  }
}

Add run:

npm build

This creates a dist/ folder which is ready to be published:

npm publish dist

Features

  • nodejs bundle (CommonJS and ESM wrapper)
  • browser bundles (ESM and IIFE)
  • shared state between all exports
  • typescript types
  • bundled dependencies
    • bundledDependencies are always bundled
    • for script exports all dependencies are bundled except they are listed in peerDependencies
  • dynamic-import-vars
  • @swc/helper are inlined (only once) if not defined as a `dependency
  • customize global name for script exports
    1. doc-block comment in entry file: /* @distilt-global-name useThisGlobalName */
    2. globalName or name from package.json appended with current entry point name
  • size-limit

Input/Output

package.json

  "exports": {
    // platform: neutral
    ".": "./src/index.ts",
    "./web": {
      // platform: browser
      browser: "./src/web.ts",
    },
    "./node": {
      // platform: node
      "node": "./src/node.ts",
    },
  },

-------------
dist/package.json

  "exports": {
    ".": {
      // platform: neutral
      // bundle "./src/index.ts", "./src/web.ts", "./src/node.ts"
      "esnext": "./pkg.esnext.js",
      "module": "./pkg.js",

      // platform: browser
      // bundle "./src/index.ts", "./src/web.ts"
      "script": "./pkg.global.js",

      "types": "./pkg.d.ts",

      // platform: node
      // bundle "./src/index.ts", "./src/node.ts"
      "node": {
        "module": "./pkg.js",
        "import": "./pkg.mjs",
        "require": "./pkg.cjs"
      },

      // platform: neutral
      // bundle "./src/index.ts", "./src/web.ts", "./src/node.ts"
      "default": "./pkg.js"
    },
    "./web": {
      // platform: neutral
      // bundle "./src/index.ts", "./src/web.ts", "./src/node.ts"
      "esnext": "./web.esnext.js",
      "module": "./web.js",

      // platform: browser
      // bundle "./src/index.ts" and "./src/web.ts"
      "script": "./web.global.js",

      "types": "./web.d.ts",

      // platform: neutral
      // bundle "./src/index.ts", "./src/web.ts", "./src/node.ts"
      "default": "./web.js"
    },
    "./node": {
      // platform: neutral
      // bundle "./src/index.ts", "./src/web.ts", "./src/node.ts"
      "esnext": "./node.esnext.js",
      "module": "./node.js",

      "types": "./node.d.ts",

      // platform: node
      // bundle "./src/index.ts" and "./src/node.ts"
      "node": {
        "module": "./node.js",
        "import": "./node.mjs",
        "require": "./node.cjs"
      },

      // platform: neutral
      // bundle "./src/index.ts", "./src/web.ts", "./src/node.ts"
      "default": "./node.js"
    },
  },

Use-cases

Providing platform-specific shims or polyfills

Suppose you're looking to provide a cross-runtime module that relies on a built-in object like AbortController. However:

  1. AbortController isn't ubiquitously available across LTS Node.js versions.
  2. All browsers relevant to your target audience support it natively.
  3. You do not want to penalize consumers of your module in browser use-cases with unnecessary bytes.

In this case, you could leverage export maps to use a special node-only entrypoint in which polyfills are used. In order to re-use code across platforms, a factory function is used to pass in any needed shims and the dependent logic is implemented in that closure.

./src/implementation.ts:

export function createImplementation(ctl = AbortController) {
  return myApiFunctionThatUsesAbortController(...args: any[]) {
    // TODO
  }
}

./src/index.ts

This is the generic entrypoint for any platform with native AbortController support

import { createImplementation } from './implementation'

// Uses the environment's AbortController
export default createImplementation()

./src/node.ts

This is the Node.js-specific entrypoint that relieas on the 'abort-controller' package

import { createImplementation } from './implementation'
import { AbortController } from 'abort-controller'

// Uses AbortController shim
export default createImplementation(AbortController)

./package.json

Here, we tell package consumers with the 'node' condition to use the Node.js-specific entrypoint so that the shim is used and available across versions.

  "exports": {
    // platform: neutral
    ".": {
      "node": "./src/node.ts",
      "default": "./src/index.ts"
    }
  },

License

MIT

0.19.0

1 year ago

0.19.1

1 year ago

0.19.2

1 year ago

0.18.3

1 year ago

0.17.0

2 years ago

0.17.1

2 years ago

0.18.1

1 year ago

0.18.2

1 year ago

0.18.0

1 year ago

0.16.9

2 years ago

0.16.3

2 years ago

0.16.4

2 years ago

0.16.5

2 years ago

0.16.6

2 years ago

0.16.7

2 years ago

0.16.8

2 years ago

0.11.0

2 years ago

0.12.0

2 years ago

0.13.0

2 years ago

0.14.0

2 years ago

0.13.1

2 years ago

0.15.0

2 years ago

0.14.1

2 years ago

0.16.0

2 years ago

0.15.1

2 years ago

0.16.1

2 years ago

0.16.2

2 years ago

0.12.1-0

2 years ago

0.10.6

3 years ago

0.10.7

3 years ago

0.10.5

3 years ago

0.10.4

3 years ago

0.10.2

3 years ago

0.10.3

3 years ago

0.10.1

3 years ago

0.10.0

3 years ago

0.9.6

3 years ago

0.9.5

3 years ago

0.9.4

3 years ago

0.9.3

3 years ago

0.9.2

3 years ago

0.9.1

3 years ago

0.9.0

3 years ago

0.8.1

3 years ago

0.8.0

3 years ago

0.7.0

3 years ago

0.6.2

3 years ago

0.6.1

3 years ago

0.6.0

3 years ago

0.5.0

3 years ago

0.4.0

3 years ago

0.3.0

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago