npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@mfonseca/gulp-file-injector

v1.0.0-alpha.4

Published

Gulp plugin that injects file contents into another files (much like an include directive).

Downloads

1

Readme

gulp-file-injector

Gulp plugin that injects file contents into another files (much like an include directive).

NOTE: This plugin still as Work in Progress!!!

Usage

First, install the plugin as a dev dependency:

npm install gulp-file-injector -D

Say you have these files:

src/file1.js

function func1() {
    return "hello 1";
}

src/file2.js

function func2() {
    return "hello 2";
}

src/bundle.js

//! $file(src/file1.js)
//! $file(src/file2.js)
console.log(func1());
console.log(func2());

Then you can start using it, as follows:

gulpfile.js

const gulp = require("gulp");
const del = require("del");
const sourcemaps = require("gulp-sourcemaps");
const injectFiles = require("gulp-file-injector");

const clean = () => {
    return del([`${outputDir}/**`]);
};
const build = () => {
    return gulp
        .src([`src/**/*.js`])
        .pipe(sourcemaps.init())
        .pipe(injectFiles())
        // do other stuff...
        .pipe(sourcemaps.write("."))
        .pipe(gulp.dest(`dist`));
};

exports.default = gulp.series(clean, build);

Assuming you have this on your package.json:

{
    ...
    "scripts": {
        "build": "gulp",
        ...
    },
    ...
}

Just run: npm run-script build.

This example will inject the contents of "file1.js" and "file2.js" into "bundle.js":

dest/bundle.js

function func1() {
    return "hello 1";
}
function func2() {
    return "hello 2";
}
console.log(func1());
console.log(func2());

Controlling file absence

Every time the plugin identifies a injection-point, it tries to do the injection.

By default, if said file doesn't exists, the plugin fails (throws exception).

You can change this behavior by signaling what to do in case of file not found.

Just right to the path, use a comma and the directive: ifAbsent=[fail|keep|empty].

src/bundle.js

//! $file(src/file1.js, ifAbsent=keep)
//! $file(src/file2.js, ifAbsent=empty)
if (func1) {
    console.log(func1());
}
if (func2) {
    console.log(func2());
}
  • fail: is the default behavior.
  • keep: if the file is not found, the expression is kept as is on the output.
  • empty: if the file is not found, assume an empty file, thus removing the expression on the output.

Options

The default patterns recognized are:

  • $file(path/to/file.ext)
  • //! $file(path/to/file.ext)
  • /*! $file(path/to/file.ext) */
  • <!--! $file(path/to/file.ext) -->;
  • <file path="path/to/file.ext" />;
  • //! <file path="path/to/file.ext" />;
  • /*! <file path="path/to/file.ext" />; */
  • <!--! <file path="path/to/file.ext" />; -->;

Replacing default patterns

If you need or want to replace the default patterns with your own, just pass a plain object to the plugin, containing the array of delimiters you need.

Each delimiter must have a start regex and an end regex. Anything between these delimiters will be treated as the file path being included.

    ...
    .pipe(injectFiles(
        {
            delimiters: [
                // will replace default patterns with these:
                // #file{ path/to/file }
                { start: /\#file\{\s*/i, end: /\s*\}/i }
            ]
        }
    ))
    ...

Adding extra patterns

If you want to keep the default patterns and just add new ones, just pass a plain object to the plugin, containing the array of extraDelimiters you need.

    ...
    .pipe(injectFiles(
        {
            extraDelimiters: [
                // will keep the default patterns and add these extra ones:
                // #file{ path/to/file }
                { start: /\#file\{\s*/i, end: /\s*\}/i }
            ]
        }
    ))
    ...

**NOTE: ** Keep in mind that the injection-point expression must be on a single line of code!

this_works.js

/*! $file( src/file1.js ) */
console.log("this file injection works!");

this_works_too.js

// inline injection
function myObject() {
    return /*! $file( src/myObject.json ) */;
}
console.log("this file injection works!");

this_doesnt_work.js

/*! $file(
    src/file1.js
) */
console.log("this file injection doesn't work!");

TODO

As noted previowsly, this is a WIP.

The project's testing is lacking at the moment and it can contain some edge-cases bugs.

Also, I aim to integrate my other plugin gulp-replacer into this one, so it will be possible to inject properties and files with the same plugin, but for now I will leave this integration for another ocasion.