2.0.1 • Published 5 years ago

hoast-convert v2.0.1

Weekly downloads
1
License
ISC
Repository
github
Last release
5 years ago

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

2.0.1

5 years ago

2.0.0

5 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

1.0.0-beta.2

6 years ago

1.0.0-beta.1

6 years ago

1.0.0-beta.0

6 years ago

0.1.0

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago

0.0.0

6 years ago