Convert the content of files using a specified function.
As the name suggest this is a hoast module.
This module is meant to be used for simple task that do not require a whole new module to be made. As a result a little more knowledge on how to making modules is recommended.
Install hoast-convert using npm.
$ npm install hoast-convert
engine
: The file processing function which gets given two parameters, the file data and the hoast metadata. The return can be an object, which gets merged with the pre-existing file, or an array of objects, whereby each item in the array becomes a new file and gets merged with the pre-existing file.- Type:
Function
- Required:
yes
- Required:
- Type:
patterns
: Glob patterns to match file paths with. If the engine function is set it will only give the function any files matching the pattern.- Type:
String
orArray of strings
- Required:
no
- Required:
- Type:
patternOptions
: Options for the glob pattern matching. See planckmatch options for more details on the pattern options.- Type:
Object
- Default:
{}
- Type:
patternOptions.all
: This options is added topatternOptions
, and determines whether all patterns need to match instead of only one.- Type:
Boolean
- Default:
false
- Type:
CLI
Not compatible with the CLI tool as it requires a reference to a self specified function.
Script
const Hoast = require(`hoast`);
const read = Hoast.read,
convert = require(`hoast-convert`);
const minifyHTML = require(`html-minifier`).minify;
Hoast(__dirname)
.use(read())
.use(convert({
engine: function(file, metadata) {
return {
content: {
data: minifyHTML(file.content.data)
}
};
},
patterns: `*.html`
}))
.process();
In the example above the HTML files are minified using html-minifier.
const Hoast = require(`hoast`);
const read = Hoast.read,
convert = require(`hoast-convert`);
const babel = require(`@babel/core`);
Hoast(__dirname)
.use(read())
.use(convert({
engine: async function(file, metadata) {
const result = await babel.transformAsync(file.content.data, { code: true, map: true });
return [{
content: {
data: result.code
}
}, {
path: file.path.substring(0, file.lastIndexOf(`.`)).concat(`.map.js`);
content: {
data: result.map
}
}];
},
patterns: `*.js`
}))
.process();
In the example above the JavaScript files are transformed using Babel, and an additional
.map.js
file is created. Do note Babel requires more setup than is shown in the example.
const Hoast = require(`hoast`);
const read = Hoast.read,
convert = require(`hoast-convert`);
Hoast(__dirname, {
metadata: {
hello: `World!`
}
})
.use(read())
.use(convert({
engine: function(file, metadata) {
console.log(JSON.Stringify(metadata));
}
}))
.process();
In the example above the metadata will be printed to the console as
{ hello: "World!" }
.