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

inline-scripts

v1.7.4

Published

Inline <script> tags in HTML files.

Downloads

129

Readme

Inline Scripts

This package contains scripts that help build client code:

  • inline-script-tags inlines files referenced by <script> tags into an output HTML.

  • inline-stylesheets inlines stylesheets referenced by <link> tags into an output HTML.

  • inline-requires bundles require dependencies inside a single output JS.

  • inline-environment-variables replaces references to process.env.<envVarName> with their values in a JS file.

Installation

npm i --save-dev inline-scripts

CLI Examples

inline-script-tags

<!-- src/index.html -->
<p>Welcome</p>
<script src="main.js"></script>
// src/main.js
console.log('Welcome');

$ inline-scripts src/index.html out/index.html

<!-- out/index.html -->
<p>Welcome</p>
<script>console.log('Welcome');</script>

inline-stylesheets

<!-- src/index.html -->
<p>Welcome</p>
<link rel="stylesheet" href="style.css">
/* src/style.css */
body {
	color: red;
}

$ inline-stylesheets src/index.html out/index.html

<!-- out/index.html -->
<p>Welcome</p>
<style>body {
	color: red;
}</style>

inline-images

<!-- src/index.html -->
<p>Welcome</p>
<img src="red_dot.png">

$ inline-images src/index.html out/index.html

<!-- out/index.html -->
<p>Welcome</p>
<img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==">

inline-requires

// src/main.js
let math = require('./mathHelper');
console.log('10 ^ 2 =', math.square(10));
// src/mathHelper.js
module.exports = {
	square: a => a * a,
	add: (a, b) => a + b,
	double: a => a * 2,
	increment: a => a++,
};

$ inline-requires src/main.js out/main.js

// out/main.js
let dependencies = {
	'main': (require, module) => {
		let math = require('./mathHelper');
		console.log('10 ^ 2 =', math.square(10));
	},
	'mathHelper': (require, module) => {
		module.exports = {
			square: a => a * a,
			add: (a, b) => a + b,
			double: a => a * 2,
			increment: a => a++,
		};
	}
};

let fakeRequire = (currentPath, dependencyPath) => {
	currentPath.pop();
	dependencyPath
		.replace(/.js$/, '')
		.split('/')
		.filter(a => a !== '.')
		.forEach(pathFragment => {
			if (pathFragment === '..')
				currentPath.pop();
			else
				currentPath.push(pathFragment);
		});

	let module = {};
	dependencies[currentPath.toString()](fakeRequire.bind(null, currentPath), module);
	return module.exports;
};

dependencies['main'](fakeRequire.bind(null, ['main']));

inline-environment-variables

// src/main.js
console.log('server URL is' + process.env.SERVER_URL);

$ inline-environment-variables src/main.js out/main.js

// out/main.js
console.log('server URL is' + 'https://api.github.com');

Note on parameters

All scripts usually take 2 parameters: the input and output files.

$ inline-scripts src/index.html out/index.html .

For convenience, if the 2nd parameter is ., the output will replace the input file.

$ inline-scripts out/index.html .

JS API

const {inlineEnvironmentVariables, inlineRequires, inlineScriptTags} = require(inline-scripts);

Each of the functions take a string path to the entry file as their single parameter and return a promise that resolves to the computed output.