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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@drtrt/inject-file-fragments

v3.1.1

Published

A simple NPM package that injects the contents of one or more files into another.

Downloads

24

Readme

Inject File Fragments

CI status NPM version License NPM bundle size analysis

@drtrt/inject-file-fragments is for those moments when all you want to do is inject the contents of one file into another, in the simplest and most framework-free manner possible.

| @drtrt/inject-file-fragments is used to build Fluent API Generator | | :----------------------------------------------------------------------------------------------------- |

Example

This example uses two fragment files, each of which will be injected into an HTML template.

Fragments

fragment_1.js:

console.log("Why, hello there.");

fragment_2.html:

<div>Hello, again.</div>

Template

index.html.template:

<!doctype html>
<html lang="en">
    <head>
        <title>Greetings to all</title>

        <script>
            /*% FRAGMENT_PATH: ./fragment_1.js %*/
        </script>
    </head>

    <body>
        <!--% FRAGMENT_PATH: ./fragment_2.html %-->
    </body>
</html>

Result

Executing the command:

$ inject-file-fragments index.html.template index.html

... gives us the result:

index.html:

<!doctype html>
<html lang="en">
    <head>
        <title>Greetings to all</title>

        <script>
            console.log("Why, hello there.");
        </script>
    </head>

    <body>
        <div>Hello, again.</div>
    </body>
</html>

Installation

NPM

npm install @drtrt/inject-file-fragments -D

Yarn

yarn add @drtrt/inject-file-fragments -D

Usage

The example above is self-explanatory. However, there are some points to bear in mind:

The path to a Fragment File should be specified in the placeholder

This makes for a straightforward configuration, as it is very clear to see which fragment will end up in which placeholder.

For example:

index.html.template:

<!doctype html>
<html lang="en">
    <head>
        <script>
            /*% FRAGMENT_PATH: ./fragment_1.js %*/
        </script>
    </head>
</html>

Here, we can see that it is the contents of fragment_1.js that will be injected, and we can see exactly where this will happen.

Note: Fragment File paths are relative to the template file. In the example above, fragment_1.js is in the same folder as index.html.template.

Any common comment type can be used

This allows placeholders to be used in many different template types, without causing linting complaints or formatting issues in Code Editors:

| Comment Type | Example | | --------------------------------------------- | ------------------------------------------- | | XML, HTML, etc. | <!--% FRAGMENT_PATH: ./fragment.html %--> | | C, C++, CSS, Java, JavaScript, etc. | /*% FRAGMENT_PATH: ./fragment.cs %*/ | | sh, bash, Python, Ruby, etc. | #% FRAGMENT_PATH: ./fragment.sh %# | | PowerShell | <#% FRAGMENT_PATH: ./fragment.ps1 %#> |

The Placeholder comment format must be precise

The examples in the table above give the general idea:

  • The comment opener & closer should each include a % symbol.
  • The comment opener should be followed by FRAGMENT_PATH:
  • This should then be followed by the path to the Fragment File.

Aside from this, the parser is quite permissive regarding use of whitespace.

Placeholder comments do not have to be on a separate line

This would work fine, for example:

<!doctype html>
<html lang="en">
    <head>
        <script>/*% FRAGMENT_PATH: ./fragment_1.js %*/</script>
    </head>
</html>

Note: The entire comment is removed as part of the injected content substitution.

Consumption from Node.js

The main use case for @drtrt/inject-file-fragments is from the command line during build processes. But, it can also be used in Node.js projects:

ECMAScript Modules (ESM)

import { injectFileFragments } from "@drtrt/inject-file-fragments";

injectFileFragments(templateFilePath, generatedFilePath);

CommonJS (CJS)

const { injectFileFragments } = require("@drtrt/inject-file-fragments");

injectFileFragments(templateFilePath, generatedFilePath);

Why?

Many JavaScript bundlers have features or plugins that allow you to inject content from one file into another. Parcel, Rollup, Vite and webpack, for example, can all be persuaded to do this.

However, this can mean finding the right plugin, of which there are sometimes many, and making sure it is suitable for the file type that you are working with; what works with an HTML file might not also work for CSS.

Additionally, for anything other than the most straightforward cases, the configuration for getting file injection to work can become involved. What is more, sometimes a development framework will include an injection-capable bundler, but the framework conventions prevent this functionality from being easily exposed.

In these situations, you want a tool which will do file injection, and only file injection, in as simple and straightforward a way possible.

Questions

What if I want to add content to a file directly from the command line?

For example, I'd like to write a Git Commit hash directly into a data-ver attribute on my index page's html element.

Answer: Write to an intermediary file fragment.

If you write the fragment to a file:

printf $(git rev-parse --short HEAD) > ./gitCommitShortHash.txt

... then you can use inject-file-fragments as normal:

index.html.template:

<!doctype html>
<html lang="en" data-ver="/*% FRAGMENT_PATH: ./gitCommitShortHash.txt %*/">
    ...
</html>

... and then:

$ inject-file-fragments index.html.template index.html

... gives the result:

index.html:

<!doctype html>
<html lang="en" data-ver="72fc5cb">
    ...
</html>

Release History

The Change Log for this package is available in the GitHub Repo, here.