Extract content from html documents and replace by build result, analog to grunt-usemin, supports css and js out of the box.
Warning
I'm still prototyping on this package so expect changes to it's API! Also, I'm open to suggestions.
Usage
First install it
npm install --save-dev gulp-htmlbuild
then add it to your gulpfile:
gulp;
gulp build
will take index.html
:
Document <!-- htmlbuild:js --> <!-- endbuild -->
And turn it into:
Document
You can find more examples in the example
folder.
API
htmlbuild(options)
options
options
is an object which maps targets to build functions. The build functions take a single argument, a stream which represents the block. Read from this stream to get the content of the block and write to it to replace it.
The stream has the following properties:
- {String}
indent
: string containing the indentation of the block directive. - {Array}
args
: string array of arguments passed in via the block directive.
htmlbuild.preprocess.js(buildFn)
a preprocessor you can use to wrap your buildfunction in. It extracts script paths from the block. In this case you also write paths to the block. They will get templated into link elements.
buildFn
a function that has the same form as a normal buildfunction, only the argument here is a stream that contains script paths. You are expected to write script paths to this stream as well.
htmlbuild.preprocess.css(buildFn)
a preprocessor you can use to wrap your buildfunction in. It extracts stylesheet paths from the block. In this case you also write paths to the block. They will get templated into link elements.
buildFn
a function that has the same form as a normal buildfunction, only the argument here is a stream that contains stylesheet paths. You are expected to write paths to this stream as well.
Extended example
example gulp file:
var gulp = plugins = htmlbuild = es = ; // pipe a glob stream into this and receive a gulp file streamvar { var paths = es; var files = es; paths; return es;}; var jsBuild = es; var cssBuild = es; gulp;
it will take following html file
Document <!-- htmlbuild:css --> <!-- endbuild --> <!-- htmlbuild:template header --> <!-- endbuild --> <!-- htmlbuild:js --> <!-- endbuild --> <!-- htmlbuild:remove --> This will be removed in the build output <!-- endbuild --> <!-- htmlbuild:template footer --> <!-- endbuild -->
and turn it into:
Document <!-- processed by htmlbuild (header) --> <!-- processed by htmlbuild (footer) -->
While concatenating stylesheets and scripts on the fly.