Skip to content

dalcib/esbuild-plugin-cache

Repository files navigation

esbuild-plugin-cache

Esbuid plugin to cache http/https imports. It also allows to use import-maps .

This plugin uses a port of Deno cache to nodejs. It runs in nodejs and doesn't depend on Deno.

The plugin allows to use http/https imports in your webpage in development and bundle it in production without installing npm packages on node_modules.

It can provide a feature similar to Snowpack 3.0, the new Streaming NPM Imports, which allos to skip the NPM install and node_modules.

//index.js
import * as React from 'https://cdn.skypack.dev/react@17.0.1'
console.log(React.version)

Or, using import-maps:

//importmap.json
{"imports": {"react": "https://cdn.skypack.dev/react@17.0.1"}}

//index.js
import * as React from 'react'
console.log(React.version)

Build script:

import esbuild from 'esbuild'
import { cache } from './index.js'

const importap = {imports: { react: "https://cdn.skypack.dev/react@17.0.1" }}
//the use of import-maps is optional

esbuild
    .build({
      entryPoints: ['index.js'],
      bundle: true,
      outfile: 'bundle.js',
      plugins: [cache({importmap, directory:'./cache'})],
    })
    .catch(() => process.exit(1))

Config:

config: {importmap: {imports:{[key: string]: string}}, directory: string}

  • importmap: Import-map object. Default: {}

  • directory: Path or name for the directory of the cache. Default to Deno cache directory. Optionally the cache directory can be defined with DENO_DIR env variable: process.env.DENO_DIR = 'cache'.