You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@font-face { src:url('./files/noto-sans-all-400-normal.woff') format('woff'); }
div { background-image:url('something.png'); }
currently posthtml-inline-assets does not inline these files
here is a snippet i use to inline woff binary font files
// based on posthtml-inline-assets/lib/default-transforms.jsconstinlineAssetsOptions={transforms: {style: {transform: (node,data)=>{deletenode.attrs.href;deletenode.attrs.rel;node.tag='style';letcontent=data.buffer.toString('utf8');constfromDir=require('path').dirname(data.from);content=content.replace(/url\(('.+?'|".+?")\)(?:\s+format\(('.+?'|".+?"))?\)/g,(fullMatch,url,format)=>{constsrcPath=fromDir+'/'+url.slice(1,-1);constsrcFormat=format.slice(1,-1);console.log(`inline style url: ${srcPath} format ${srcFormat}`);if(!fs.existsSync(srcPath))returnfullMatch;// no change// TODO detect mimetype of src// type == woff: binary fileconstmimeType='application/octet-stream;charset=binary';constb64=require('fs').readFileSync(srcPath).toString('base64');return`url('data:${mimeType};base64,${b64}')`;// TODO recurse? find `url(...)` in src// TODO use a proper css parser to avoid false positives// text file://const src = require('fs').readFileSync(srcPath, 'utf8');//const b64 = Buffer.from(src).toString('base64');//return `url('data:text/css;charset=utf-8;base64,${b64}')`;});node.content=[content];}}}};constposthtml=require('posthtml')constinlineAssets=require('posthtml-inline-assets')posthtml().use(inlineAssets(inlineAssetsOptions)).process('<html>....</html>').then(res=> ....)
it works, but its ugly ... ideally we would use some css loader to do this recursively for all file types
but maybe i should use a proper bundler like rollup or webpack or 11ty
my goal is to bundle many small html pages + styles + scripts into one single html file as shown here
The text was updated successfully, but these errors were encountered:
I had the same issue and tried a few approaches. First idea I had was to use postcss to inline fonts from the CSS within <style elements. However postcss-url didn't support font inlining either. Eventually I settled to write my own simple posthtml plugin:
importfsfrom'fs'import_from'lodash'importmimeTypesfrom'mime-types'importpathfrom'path'import{Node}from'posthtml'constposthtmlInlineStyleCssImports=()=>{return(tree: Node): Node=>{tree.match({tag: 'style'},(node)=>{constcss=node.content?.[0]if(!_.isString(css)){returnnode}// Regex is good enough hereconstprocessedCss=css.replace(/url\('(.*?)'\)/gi,(match,contents)=>{constdataMime=`data:${mimeTypes.lookup(contents)}`constfileContent=fs.readFileSync(path.join(__dirname,'templates/',contents))return`url(${dataMime};base64,${fileContent.toString('base64')})`})node.content=[processedCss]returnnode})returntree}}exportdefaultposthtmlInlineStyleCssImports
css stylesheets can include other files like
currently
posthtml-inline-assets
does not inline these fileshere is a snippet i use to inline
woff
binary font filesit works, but its ugly ... ideally we would use some css loader to do this recursively for all file types
but maybe i should use a proper bundler like rollup or webpack or 11ty
my goal is to bundle many small html pages + styles + scripts into one single html file as shown here
The text was updated successfully, but these errors were encountered: