npm i -D posthtml-modules
<!-- index.html -->
<html>
<body>
<module href="./module.html">
title
</module>
</body>
</html>
<!-- module.html -->
<header>
<h1>
Test <content></content>
</h1>
</header>
const { readFileSync } = require('fs')
const posthtml = require('posthtml')
const options = { /* see available options below */ }
posthtml()
.use(require('posthtml-modules')(options))
.process(readFileSync('index.html', 'utf8'))
.then((result) => result)
});
<html>
<body>
<header>
<h1>Test title</h1>
</header>
</body>
</html>
Type: string
Default: ./
Root path for modules lookup.
Type: array | function
Default: []
PostHTML plugins to apply for every parsed module.
If a function provided, it will be called with module's file path.
Type: string
Default: ''
Root filename for processing apply, needed for path resolving (it's better to always provide it).
Type: boolean
Default: false
Apply plugins to root file after modules processing.
Type: string
Default: module
Use a custom tag name.
Type: string
Default: href
Use a custom attribute name.
Type: object
Default: {}
Pass data to the module.
If present, the JSON object from the locals=""
attribute will be merged on top of this, overwriting any existing values.
Type: boolean
Default: false
All attributes on <module></module>
will be added to locals
Type: object
Default: {}
Options for the PostHTML parser.
By default, posthtml-parser
is used.
You can pass data to a module using a locals=""
attribute.
Must be a valid JSON object.
Example:
<p>The foo is strong in this one.</p>
<p>Or so they say...</p>
All attributes on <module></module>
will be added to locals
Example:
<div class="text-center uppercase" id="example" style="display: flex; gap: 2;">
Module content
</div>