posthtml-rewrite-paths
A PostHTML plugin for rewriting paths in html attributes.
Before:
After:
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>`; ;
Options
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.