Absolute paths
Closed this issue · 2 comments
piuccio commented
It doesn't seem to work when assets have an absolute path.
index.html
<!doctype html>
<html lang="en">
<head>
<link rel="stylesheet" href="/styles.css">
</head>
<body></body>
</html>
Logs the error
[ { Error: ENOENT: no such file or directory, open '/styles.css'
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: '/styles.css' },
I tried passing from
but nothing seemed to work.
Eventually I managed to fix it with
const posthtml = require('posthtml');
const fs = require('fs');
const path = require('path');
const crass = require('crass');
const html = fs.readFileSync('./index.html').toString();
posthtml([
require('posthtml-inline-assets')({
inline: {
style: {
check(node) {
if (node.tag === 'link' && node.attrs && node.attrs.href && node.attrs.href.charAt(0) === '/') {
return path.join(process.cwd(), node.attrs.href);
}
},
then(node, data) {
node.tag = 'style';
node.attrs = {};
node.content = crass.parse(data.buffer.toString()).optimize().toString();
}
},
},
}),
])
.process(html)
.then((result) => console.log(result.html));
Would be nice if this plugin could handle absolute paths on its own, maybe adding a root
configuration option?
jonathantneal commented