Skip to content
This repository has been archived by the owner on Jan 4, 2020. It is now read-only.
/ hoast-convert Public archive

Hoast module used to convert the content of files using a specified function.

License

Notifications You must be signed in to change notification settings

hoast/hoast-convert

Repository files navigation

npm package version @latest npm package version @next

Travis-ci test status CodeCov test coverage

License agreement Open issues on GitHub

hoast-convert

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.

Usage

Install hoast-convert using npm.

$ npm install hoast-convert

Parameters

  • 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
  • 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 or Array of strings
      • Required: no
  • patternOptions: Options for the glob pattern matching. See planckmatch options for more details on the pattern options.
    • Type: Object
    • Default: {}
  • patternOptions.all: This options is added to patternOptions, and determines whether all patterns need to match instead of only one.
    • Type: Boolean
    • Default: false

Example

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!" }.