Overview
A gulp
plugin that converts arbitrary text files into JavaScript modules. Not limited to HTML.
Installation
npm i -E gulp-html-to-js# or yarn add -E gulp-html-to-js
Usage
In your gulpfile.js
:
const htmlToJs = // Without concatenationgulp // With concatenationgulp
Without the concat
option, each module exports a single string:
Hello world!
Becomes:
moduleexports = '<p>Hello world!</p>'
With concat
, files are grouped into one module, where strings are keyed by file paths:
moduleexports = Objectmoduleexports'index.html' = '<p>Hello world!</p>'
In your app, import the result like so (directory nesting depends on your build configuration):
// orconst html =
Options
See the concat
option above. You can also modify it with:
prefix
: Prepends a path prefix to all keys of the resulting module object.
For {prefix: 'templates'}
the resulting file from the above example is:
moduleexports = Objectmoduleexports'templates/index.html' = '<p>Hello world!</p>'
global
: Requiresconcat
. Assigns the resulting object to some global identifier other thanmodule.exports
(default).
For {global: 'window.templates', concat: 'templates.js'}
the example above would produce this:
windowtemplates = Objectwindowtemplates'index.html' = '<p>Hello world!</p>'