paopu

A CLI tool to manage your CDN script tags in files. Automatically generate a sub-resource integrity from a file, then add it to the matching script tag.

Usage no npm install needed!

<script type="module">
  import paopu from 'https://cdn.skypack.dev/paopu';
</script>

README

✩ Paopu ✩

A CLI tool to manage your CDN script tags in files. Automatically generate a sub-resource integrity from a file, then add it to the matching script tag.

Read the Why section for more details.

Paopu was created to work with Node 14.

Feel free to submit an issue or pull request. :)

TODO:

  • More hashing options (currently only SHA256 is supported).

Install & Run

  1. Install it in your project:
$ npm i -D paopu
  1. Create a configuration.
  2. Run it:
$ paopu

Why?

Scenario: Let's say you want to tell the world that your package's bundle can be accessed on a public CDN. You might include an example script in your README, like so:

<script
  type="text/javascript"
  src="https://cdn.jsdelivr.net/npm/my-package@0.3.2/dist/my-cool-package.min.js"
  integrity="sha256-waCWKicYMCJic4tBKdkV54qhuGsq8J9JWQY+QmFVjj8="
  crossorigin="anonymous"
></script>

Problem: Every time you release a new package version, you need to update that integrity hash. Sometimes in multiple files.

Solution: Install Paopu, add a config, then run the CLI tool. Done. ✨

Create a config

Create a paopu.config.json at the root of your project. Optional properties below have default values displayed.

// paopu.config.json
{
  // Each entry uses a package name as the key
  "package-name": {
  
    // Specifies the file paths used in your script tags. These paths must
    // partially match your path in the `src` attribute.
    //
    // REQUIRED
    
    "resources": ["some/path/bundle-name.min.js", "some-path/bundle-name.js"],

    // Specifies which files to search for script tags in.
    //
    // REQUIRED
    
    "targets": ["README.md", "test/index.html"],

    // Specifies if the resources are located in a modules folder.
    // Automatically prepends `node_modules/<CONFIG_KEY>` to paths
    // defined in `resources`, where `<CONFIG_KEY>` is the key
    // of the given paopu config entry.
    // This also causes the tool to ignore `resourceBasePath`
    //
    // OPTIONAL
    
    "module": false,

    // Specifies a common base path for paths in `resources`
    // Should resolve to a package root in all cases.
    // This is ignored if `module` is `true`.
    //
    // OPTIONAL
    
    "resourceBasePath": ".",

    // Specifies a common base path for paths in `targets`
    //
    // OPTIONAL
    
    "targetBasePath": ".",

    // A url matcher for your script tag. If its `src` contains
    // this pattern AND the matching resource path, then the 
    // script tag will be updated.
    // E.g., 'unpkg.com'
    //
    // OPTIONAL
    
    "urlPattern": 'cdn.jsdelivr.net'
  }
}

CLI options

Any CLI options given will override the defaults used by the tool.

Custom config

Flags: --config, -c

Default: paopu.config.json

Example usage: paopu -c path/to/paopu.config.json

Override the default configuration filename.

Debug mode

Flags: --debug, -d

Default: false

Example usage: paopu -d

If given, Paopu will spit out a .paopu-cache file so you can inspect the resolved paths, options, etc., being used by the tool.