posthtml-rewrite-paths
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

posthtml-rewrite-paths

A PostHTML plugin for rewriting paths in html attributes.

Before:

<script src="dist/index.js"></script>

After:

<script src="dist/index.hash.js"></script>

Install

npm i -D posthtml-rewrite-paths

Usage

const options = {
  // Map of tags and the attributes to check for paths. This is the
  // default if omitted.
  search: { "script": ["src"]},
  // Map of path names to replace or a path to a json file containing
  // the same. If a replacement value is nullish, the attribute is
  // removed.
  pathMap: { "dist/index.js": "index.hash.js" }
};
 
const html = `<body><script src="dist/index.js"></script></body>`;
 
posthtml([ plugin(options) ])
  .process(html)
  .then(result => {
    // write result.html;
  });

Options

{
  pathMap"path-to-json-file.json" | { "path/to/replace.js""replacement/path.js" };
  search{ "tag-name"["attribute-to-check", "attribute-to-check", ...]}
}

Paths are matched textually with some minor normalisation of path separators. Hence "src/a.js" will match "/src\a.js", but not "C:\project\src\s.js"; and leading and trailing whitespace in the path pattern is ignored.

There is a special case when pathMap is loaded from file so that { "path/to/replace.js": { "path": "replacement/path.js" } } is also accepted. This allows for simpler integration with tools that produce files in this format.

Package Sidebar

Install

npm i posthtml-rewrite-paths

Weekly Downloads

23

Version

1.0.1

License

ISC

Unpacked Size

16.4 kB

Total Files

8

Last publish

Collaborators

  • franklin-ross