gulp-inject
HELP WANTED
Contributors are welcomed!
I don't have enough time to maintain this plugin as I would want to, so I'm looking for people who want to help out and be contributors/repository admins.
Interested?
Contact me! See package.json
for contact information.
A stylesheet, javascript and webcomponent reference injection plugin for gulp. No more manual editing of your index.html!
Contents
- Introduction
- Installation
- Basic usage
- More examples
- Injecting files relative to target files
- Injecting files from multiple source streams
- Injecting some files into
<head>
and some into<body>
- Injecting all files for development
- Injecting AngularJS scripts for development
- Injecting into a json-file
- Injecting with custom
transform
function with default fallback - Injecting dist files into bower.json's main section
- Injecting all javascript files into a karma config file
- Injecting files contents
- Injecting files contents based on file path
- API
- License
Introduction
gulp-inject
takes a stream of source files, transforms each file to a string and injects each transformed string into placeholders in the target stream files. See Basic usage and More examples below.
Default transforms and placeholders exists for injecting files into html
, jade
, pug
, jsx
, less
, slm
, haml
and sass
/ scss
files.
Installation
Install gulp-inject
as a development dependency:
npm install --save-dev gulp-inject
Basic usage
The target file src/index.html
:
Each pair of comments are the injection placeholders (aka. tags, see options.starttag
and options.endtag
).
My index <!-- inject:css --> <!-- endinject --> <!-- inject:js --> <!-- endinject -->
The gulpfile.js
:
var gulp = ;var inject = ; gulp;
src/index.html
after running gulp index
:
My index <!-- inject:css --> <!-- endinject --> <!-- inject:js --> <!-- endinject -->
More examples
Injecting files relative to target files
By default the injected file paths are relative to each source file's cwd
(see options.ignorePath
). If options.relative
is set to true
each injected path will be relative to each target file's directory instead.
Project structure:
└── src
├── module
│ ├── module.js
│ └── module.html
└── app
├── main.js
└── index.html
src/app/index.html
:
My Index Home <!-- inject:js --> <!-- endinject -->
src/module/module.html
:
Module Module <!-- inject:js --> <!-- endinject -->
gulpfile.js
:
var inject = ; gulp ;
Resulting src/app/index.html
:
My Index Home <!-- inject:js --> <!-- endinject -->
Resulting src/module/module.html
:
Module Home <!-- inject:js --> <!-- endinject -->
Injecting files from multiple source streams
This example demonstrates how to inject files from multiple different streams into the same injection placeholder.
Install event-stream
with: npm install --save-dev event-stream
and use its merge
function.
Code:
var es = inject = concat = uglify = ; // Concatenate vendor scriptsvar vendorStream = gulp ; // Concatenate AND minify app sourcesvar appStream = gulp ; gulp ;
Multiple sources when order is important
Use stream-series
.
Code:
var series = inject = ; var vendorStream = gulp; var appStream = gulp; gulp // This will always inject vendor files before app files ;
<head>
and some into <body>
Injecting some files into gulp-inject
's starttag
option.
Method 1: Use gulpfile.js
:
var inject = ; gulp ;
And in your ./src/index.html
:
My index <!-- inject:head:js --> <!-- only importantFile.js will be injected here --> <!-- endinject --> <!-- inject:js --> <!-- the rest of the *.js files will be injected here --> <!-- endinject -->
gulp-inject
's name
option.
Method 2: Use gulpfile.js
:
var inject = ; gulp ;
And in your ./src/index.html
:
My index <!-- head:js --> <!-- only importantFile.js will be injected here --> <!-- endinject --> <!-- inject:js --> <!-- the rest of the *.js files will be injected here --> <!-- endinject -->
Injecting all files for development
If you use Bower for frontend dependencies I recommend using main-bower-files
and injecting them as well.
gulpfile.js
:
var bowerFiles = inject = stylus = es = ; var cssFiles = gulp ; gulp ;
src/index.html
:
My index <!-- bower:css --> <!-- bower installed css files will go here... --> <!-- endinject --> <!-- inject:css --> <!-- built css files will go here... --> <!-- endinject --> <!-- bower:js --> <!-- bower installed scripts will go here... --> <!-- endinject --> <!-- inject:js --> <!-- app scripts will go here... --> <!-- endinject -->
Note remember to mount ./bower_components
, ./build
and ./src/app
as static resources in your server to make this work.
Injecting AngularJS scripts for development
If you're writing an AngularJS application and follow Google's Angular APP Structure Recommendations, which I think you should, it's important that the script files are injected in the correct order to avoid module instantiation problems like Uncaught Error: [$injector:modulerr]
.
To do this you can use gulp-angular-filesort
together with gulp-inject
like so:
var angularFilesort = inject = ; gulp ;
Injecting into a json-file
You can customize gulp-inject
further by using the transform
function option, e.g. by injecting files into a json-file.
Code:
gulp ;
Initial contents of files.json
:
transform
function with default fallback
Injecting with custom The default transform
function is available to use e.g. as a default fallback.
Used here to inject Word documents as <a>
tags below:
index.html
:
My documents Documents <!-- inject:docx --> <!-- endinject --> <!-- inject:js --> <!-- endinject -->
gulpfile.js
:
var inject = ; gulp ;
Resulting index.html
:
My documents Documents <!-- inject:docx --> <!-- endinject --> <!-- inject:js --> <!-- endinject -->
Injecting dist files into bower.json's main section
Code:
gulp ;
Injecting all javascript files into a karma config file
Code:
gulp ;
Injecting files contents
In order to inject files contents you have to provide custom transform
function, that will return file contents as string. You also have to omit {read: false}
option of gulp.src
in this case. Example below shows how to inject contents of html partials into head of index.html
:
Code:
gulp ;
And in your ./src/index.html
:
My index <!-- inject:head:html --> <!-- contents of html partials will be injected here --> <!-- endinject -->
Injecting files contents based on file path
In order to inject files based on file path you have to provide custom starttag
which includes {{path}}
. Additionally, in order to inject file contents include transform
function, that will return file contents as string. You also have to omit {read: false}
option of gulp.src
in this case. Path can be either absolute, or relative in which case you should set [options.relative
] to true. Example below shows how to inject contents of html partials into index.html
:
Code:
gulp ;
And in your ./src/index.html
:
My index <!-- inject:path/to/your/file.ext --> <!-- contents of html partials will be injected here --> <!-- endinject -->
API
inject(sources, options)
Parameter: sources
Type: Stream
Provide a Vinyl File Stream as input to inject
, see examples above.
Parameter: options
Type: Object
For available options see Options
Options
options.ignorePath
Type: String
or Array
Default: NULL
A path or paths that should be removed from each injected file path.
This could also be solved by setting the cwd
option for your gulp.src
streams, each source file's cwd
is automatically removed from its path before injection (if not options.relative
is set to true
, see below).
options.relative
Type: Boolean
Default: false
If set to true
paths for the injected files will be relative to each target file, this also means that each source file's cwd
is not necessary to remove from its path.
options.addPrefix
Type: String
Default: NULL
A path that should be prefixed to each injected file path.
options.addSuffix
Type: String
Default: NULL
A path that should be suffixed to each injected file path.
options.addRootSlash
Type: Boolean
Default: !options.relative
The root slash is automatically added at the beginning of the path ('/'), or removed if set to false
.
options.name
Type: String
Default: "inject"
Used in the default start and end tags below.
options.removeTags
Type: Boolean
Default: false
When true
the start and end tags will be removed when injecting files.
options.empty
Type: Boolean
Default: false
When true
all tags without corresponding files will be emptied.
Warning this has the potential issue of emptying more than expected.
options.starttag
Type: String
|Function(targetExt, sourceExt)
Params (if function):
targetExt
- The file extension of the target filesourceExt
- The file extension of source file
Purpose:
Used to dynamically set starting placeholder tag depending on file extensions.
In the provided string, or the string returned from the given function, the string {{ext}}
is replaced with the source file extension name, e.g. "css", "js" or "html". {{name}}
will be replaced by options.name
. {{path}}
will be replaced by path to source file (when used together with [options.relative
] it will allow relative path to source file.
Default:
A function dependent on target file type and source file type that returns:
- html as target:
<!-- {{name}}:{{ext}} -->
- haml as target:
-# {{name}}:{{ext}}
- jade as target:
//- {{name}}:{{ext}}
- pug as target:
//- {{name}}:{{ext}}
- jsx as target:
{/* {{name}}:{{ext}} */}
- slm as target:
/ {{name}}:{{ext}}
- less as target:
/* {{name}}:{{ext}} */
- sass, scss as target:
/* {{name}}:{{ext}} */
options.endtag
Type: String
|Function(targetExt, sourceExt)
Params (if function):
targetExt
- The file extension of the target filesourceExt
- The file extension of source file
Purpose:
Used to dynamically set ending placeholder tag depending on file extensions.
In the provided string, or the string returned from the given function, the string {{ext}}
is replaced with the source file extension name, e.g. "css", "js" or "html". {{name}}
will be replaced by options.name
. {{path}}
will be replaced by path to source file.
Default:
A function dependent on target file type and source file type that returns:
- html as target:
<!-- endinject -->
- haml as target:
-# endinject
- jade as target:
//- endinject
- pug as target:
//- endinject
- jsx as target:
{/* endinject */}
- slm as target:
/ endinject
- less as target:
/* endinject */
- sass, scss as target:
/* endinject */
options.transform
Type: Function(filepath, file, index, length, targetFile)
Params:
filepath
- The "unixified" path to the file with anyignorePath
's removed,addPrefix
andaddSuffix
addedfile
- The File object to inject given fromgulp.src
index
- 0-based file indexlength
- Total number of files to inject for the current file extensiontargetFile
- The target file to inject into
Purpose:
Used to generate the content to inject for each file.
Default:
A function dependent on target file type and source file type that returns:
Injecting into html
- css files:
<link rel="stylesheet" href="<filename>.css">
- js files:
<script src="<filename>.js"></script>
- coffee files:
<script type="text/coffeescript" src="<filename>.coffee"></script>
- html files:
<link rel="import" href="<filename>.html">
- png files:
<img src="<filename>.png">
- gif files:
<img src="<filename>.gif">
- jpg files:
<img src="<filename>.jpg">
- jpeg files:
<img src="<filename>.jpeg">
If options.selfClosingTag
is true
the default transformer above will make the <link>
and <img>
tags self close, i.e: <link ... />
and <img ... />
respectively.
Injecting into jsx
The same as for injecting into html
above with options.selfClosingTag
set to true
.
Injecting into jade
- css files:
link(rel="stylesheet", href="<filename>.css")
- js files:
script(src="<filename>.js")
- coffee files:
script(type="text/coffeescript", src="<filename>.coffee")
- html files:
link(rel="import", href="<filename>.html")
- png files:
img(src="<filename>.png")
- gif files:
img(src="<filename>.gif")
- jpg files:
img(src="<filename>.jpg")
- jpeg files:
img(src="<filename>.jpeg")
Injecting into pug
- css files:
link(rel="stylesheet", href="<filename>.css")
- js files:
script(src="<filename>.js")
- coffee files:
script(type="text/coffeescript", src="<filename>.coffee")
- html files:
link(rel="import", href="<filename>.html")
- png files:
img(src="<filename>.png")
- gif files:
img(src="<filename>.gif")
- jpg files:
img(src="<filename>.jpg")
- jpeg files:
img(src="<filename>.jpeg")
Injecting into slm
- css files:
link rel="stylesheet" href="<filename>.css"
- js files:
script src="<filename>.js"
- coffee files:
script type="text/coffeescript" src="<filename>.coffee"
- html files:
link rel="import" href="<filename>.html"
- png files:
img src="<filename>.png"
- gif files:
img src="<filename>.gif"
- jpg files:
img src="<filename>.jpg"
- jpeg files:
img src="<filename>.jpeg"
Injecting into haml
- css files:
%link{rel:"stylesheet", href:"<filename>.css"}
- js files:
%script{src:"<filename>.js"}
- coffee files:
%script{type:"text/coffeescript", src:"<filename>.coffee"}
- html files:
%link{rel:"import", href:"<filename>.html"}
- png files:
%img{src:"<filename>.png"}
- gif files:
%img{src:"<filename>.gif"}
- jpg files:
%img{src:"<filename>.jpg"}
- jpeg files:
%img{src:"<filename>.jpeg"}
Injecting into less
- css files:
@import "<filename>.css";
- less files:
@import "<filename>.less";
Injecting into scss
- css files:
@import "<filename>.css";
- scss files:
@import "<filename>.scss";
- sass files:
@import "<filename>.sass";
Injecting into sass
- css files:
@import "<filename>.css"
- sass files:
@import "<filename>.sass"
- scss files:
@import "<filename>.scss"
options.selfClosingTag
Type: Boolean
Default: false
Affects the default options.transform
function, see above.
options.quiet
Type: Boolean
Default: false
Lower the verbosity by setting this to true, suppressing the logging of successful injections.
options.templateString
DEPRECATED!
Deprecated since v.1.0
. Use gulp-file
instead:
var gulp = ;var file = ;var inject = ; ;
options.sort
DEPRECATED!
Deprecated since v.1.0
. Use sort-stream
instead:
var gulp = ;var sort = ;var inject = ; gulp ;
inject.transform
The default transform function is exposed in the public API.
For more details see the code with tests.
inject.transform.html
The default transform function for files into html
, or other file types not jade
, pug
, jsx
, slm
, less
, scss
, sass
or haml
.
inject.transform.jade
The default transform function for files into jade
.
inject.transform.pug
The default transform function for files into pug
.
inject.transform.jsx
The default transform function for files into jsx
.
inject.transform.slm
The default transform function for files into slm
.
inject.transform.haml
The default transform function for files into haml
.
inject.transform.less
The default transform function for files into less
.
inject.transform.sass
The default transform function for files into sass
.
inject.transform.scss
The default transform function for files into scss
.