npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rollup-plugin-dep-inject

v0.2.6

Published

A rollup plugin that uses your externally defined modules and injects their unpkg cdn equivalent into an index entry file.

Downloads

22

Readme

npm

rollup-plugin-dep-inject

A rollup plugin that will use your external defined modules and inject their unpkg CDN equivalents as script tags into a custom defined entry index file. The benifits here are that Rollup will skip bundling heavy dependencies that you might be using, which in turn will speed up your bundle times.

Why not just code split?

You can, but even with code splitting, Rollup is still processing your dependencies. This plugin will be exceptionally helpful when you're working on a web project that uses a large amount of external depenedencies.

Install

yarn add rollup-plugin-dep-inject --dev

Example


import depInject from  'rollup-plugin-dep-inject'

export default {
  input: 'src/app.js',
  external: ['lodash', 'bss', 'mithril', 'turbolinks'],
  output: [
    {
      file: 'dist/app.bundle.js',
      format: 'iife',
      name: 'App',
      sourcemap: true,
      globals: {
        lodash: '_',
        bss: 'b',
        mithril: 'm',
        turbolinks: 'Turbolinks'
      }
    }
  ],
  plugins: [
    depInject({
      index: 'dist/index.html'
    })
  ]
}

Options

| Option | Type | Description | |--|--|--| | index | String | Relative path to the projects entry index file. | | attrs | String | Attributes apply to <script> tags. | | ignore | Array | External listed module IDs to ignore. | | remove | Boolean | When set to true will remove any injected dependencies in file | | unpkg | Object | Retrieve specific reference using a unpkg-uri pattern. | | custom | Object | Use a custom url reference to a module. |

Both the unpkg and custom options accept a comma-separate list of moduleID:Reference pairs.

unpkg-uri

The plugin leverages unpkg-uri when generating the unpkg CDN module equivalents. The unpkg option accepts unpkg-uri pattern values which allows you to retrieve specified references within a modules package.

| Value | Returns | |--|--| | [min.js] | https://unpkg.com/[email protected]/lib/index.min.js | |file.json | https://unpkg.com/[email protected]/lib/file.json | /package.json | https://unpkg.com/[email protected]/package.json | @1.2.3 | https://unpkg.com/[email protected]/lib/index.js | @prefix/ | https://unpkg.com/@prefix/[email protected]/lib/index.js

Usage

By default all external modules listed in rollup.config.js files will be injected and use the version numbers sourced from your projects package.json file. The unpkg uri will be generated automatically.

Below is a usage example with custom configuration:


depInject({
  index: 'dist/index.html', // inject modules into this file
  attrs: 'defer', // add a 'defer' attribute to each generated script tag
  ignore: ['lodash'], // do not inject the 'lodash' module
  unpkg: {
    // leverage unpkg-uri pattern and retrive the minified version of bss
    bss: '[min.js]'
  },
  custom: {
    // use a custom CDN reference for the 'mithril' module external
    mithril:
      'https://cdnjs.cloudflare.com/ajax/libs/mithril/1.1.6/mithril.min.js'
  }
})

The above configuration would write the following to the entry index defined file:


<!-- dist/index.html -->

<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Document</title>

    <!--dep-inject-->
    <script src="https://unpkg.com/[email protected]/bss.min.js" defer></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/mithril/1.1.6/mithril.min.js" defer></script>
    <script src="https://unpkg.com/[email protected]/dist/turbolinks.js" defer></script>
    <!--dep-inject-->

    <script src="bundle.test.js"></script>
  </head>
  <body>
    <div class="test">
      <h1>Hello World</h1>
    </div>
  </body>
</html>

The injectected dependencies in this example reflect the options defined within the plugin configuration:

  • The script tags contain defer attributes
  • Lodash was ignored
  • BSS uses the minified version
  • Mithril is using the custom CDN address.

Removing injections

You can remove injections by passing a truthy to the remove option which will detect and remove any injected <script> dependencies from the index file. This is helpful when you are building in different environments and may want development or production version scripts, where in development you inject but in production you bundle.

How it works?

The plugin uses your bundles external: [] modules and cross-references them with your projects package.json dependencies. The name and version number is used to generate <script src=""> tags that reference the modules unpkg uri equivalent cdn address. The generated script tag modules are then injected into the entry index file that was defined in the plugin options.

Changelog

Please see changelog for more information what has changed recently.

License

The MIT License (MIT). Please see License File for more information.