https://github.com/gera2ld/gulp-assets-injector
A gulp plugin to collect and inject assets into HTMLs.
https://github.com/gera2ld/gulp-assets-injector
Last synced: about 2 months ago
JSON representation
A gulp plugin to collect and inject assets into HTMLs.
- Host: GitHub
- URL: https://github.com/gera2ld/gulp-assets-injector
- Owner: gera2ld
- Created: 2016-10-30T06:05:28.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-07-13T15:46:04.000Z (about 9 years ago)
- Last Synced: 2025-02-15T06:49:33.248Z (over 1 year ago)
- Language: JavaScript
- Size: 4.88 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
gulp-assets-injector
===



A plugin to inject assets into HTMLs.
Usage
---
First collect static files via `assetsInjector.collect()`, then inject them
via `assetsInjector.inject()`.
``` js
const gulp = require('gulp');
const assetsInjector = require('gulp-assets-injector')();
gulp.task('js', () => {
return gulp.src('src/**/*.js')
.pipe(assetsInjector.collect())
.pipe(gulp.dest('dist'));
});
gulp.task('css', () => {
return gulp.src('src/**/*.css')
.pipe(assetsInjector.collect())
.pipe(gulp.dest('dist'));
});
gulp.task('html', ['js', 'css'], () => {
return gulp.src('src/index.html')
.pipe(assetsInjector.inject())
.pipe(gulp.dest('dist'));
});
// and a more complexed example
const injectOptions = {
link: true,
filter: (htmlPath, assetPath) => assetPath.includes('/home/'),
}
gulp.task('html-complex', ['js', 'css'], () => {
return gulp.src('src/home.html')
.pipe(assetsInjector.inject(injectOptions))
.pipe(gulp.dest('dist'));
});
```
If `injectOptions.link` is set to false, HTMLs will be injected with asset contents directly:
``` html
/* css here */
...
//<![CDATA[
/* script here */
//]]>
```
Otherwise HTMLs will be injected with links:
``` html
...
```
If `injectOptions.link` is a function, the returned value will be used as asset paths.
Document
---
* AssetsInjector
The constructor takes no arguments.
* AssetsInjector::collect()
Collects all the static files piped to it.
* AssetsInjector::inject(*Optional* options)
Inject the collected static files to the piped HTMLs.
`options` is an object with following properties:
* link: *Any*
Whether the assets should be injected as a link. If set to false, the content will be injected into HTML directly. Default as `true`.
If set to a function, the injected link URL will be determined by the function, the parameters are `path/to/HTML` and `path/to/asset`.
* filter: *Function*
A function to decide whether an asset file should be injected into the current HTML. The parameters are `path/to/HTML` and `path/to/asset`.
If not provided, all assets within the same directory as the current HTML will be injected.