Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,29 @@ For convenience, if the 2nd parameter is `.`, the output will replace the input

# JS API

`const {inlineEnvironmentVariables, inlineRequires, inlineScriptTags} = require(inline-scripts);`
`const {inlineEnvironmentVariables, inlineRequires, inlineScriptTags, inlineStylesheets, inlineImages} = 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.
Each of the functions take a string path to the entry file as their single parameter and returns a promise that resolves to the computed output.

To make it easy to perform multiple operations you can pass the output of each to the input for the next operation. Rather than pass a string path to `inlineScriptTag`, `inlineStylesheets` and `inlineImage` you can pass a hash with the file path and an html string. Both the path and string must be supplied. Rather than read the file it uses the supplied string as input. The path is required to resolve relative references to the css, js or image files.
```js
{
htmlPath: '/path-to-file.html',
htmlString: 'string-containing-html'
}
```
`inlineEnvironmentVariables` also takes either a string with the file path or a hash with either a path or a string (if both are present then the string is used and the path is ignored):
```js
{
jsPath: '/path-to-file.js',
jsString: 'string-containing-js'
}
```
An example of inlining scripts, stylesheets and images:
```js
inlineScriptTags('./index.html')
.then (htmlString => inlineStylesheets({ htmlPath: './index.html', htmlString }))
.then (htmlString => inlineImages({ htmlPath: './index.html', htmlString }))
.then (htmlString => fs.writeFileSync('./dist/index.html', htmlString));

```
6 changes: 4 additions & 2 deletions src/inlineEnvironmentVariables.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const fs = require('fs').promises;
const path = require('path');

let inlineJsEnvVars = async jsPath => {
let file = await fs.readFile(jsPath, 'utf8');
let inlineJsEnvVars = async options => {
let jsString, jsPath = (typeof options === 'string') ? options : '';
if (typeof options === 'object') ({ jsString, jsPath } = options);
const file = jsString || await fs.readFile(jsPath, 'utf8');
const envVarRegex = /process\.env\.(\w+)/;
let envVarMatches = file.match(new RegExp(envVarRegex, 'g'));
if (!envVarMatches)
Expand Down
6 changes: 4 additions & 2 deletions src/inlineImages.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
const fs = require('fs').promises;
const path = require('path');

let inlineImages = async htmlPath => {
let inlineImages = async options => {
const imgTagRegex = /<img (.* )?src="([\w.\-\/]+)"(.*)>/;
let html = await fs.readFile(htmlPath, 'utf8');
let htmlString, htmlPath = (typeof options === 'string') ? options : '';
if (typeof options === 'object') ({ htmlString, htmlPath } = options);
const html = htmlString || await fs.readFile(htmlPath, 'utf8');
let matches = html.match(new RegExp(imgTagRegex, 'g'));
if (!matches)
return html;
Expand Down
6 changes: 4 additions & 2 deletions src/inlineScriptTags.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
const fs = require('fs').promises;
const path = require('path');

let inlineHtmlScripts = async htmlPath => {
let inlineHtmlScripts = async options => {
const scriptTagRegex = /<script (?:.* )?src="([\w.\-\/]+)".*><\/script>/;
let html = await fs.readFile(htmlPath, 'utf8');
let htmlString, htmlPath = (typeof options === 'string') ? options : '';
if (typeof options === 'object') ({ htmlString, htmlPath } = options);
const html = htmlString || await fs.readFile(htmlPath, 'utf8');
let matches = html.match(new RegExp(scriptTagRegex, 'g'));
if (!matches)
return html;
Expand Down
6 changes: 4 additions & 2 deletions src/inlineStylesheets.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
const fs = require('fs').promises;
const path = require('path');

let inlineHtmlStyles = async htmlPath => {
let inlineHtmlStyles = async options => {
const linkTagRegex = /<link (?:.* )?rel="stylesheet"(?:.* )?href="([\w.\-\/]+)".*>|<link (?:.* )?href="([\w.\-\/]+)"(?:.* )?rel="stylesheet".*>/;
let html = await fs.readFile(htmlPath, 'utf8');
let htmlString, htmlPath = (typeof options === 'string') ? options : '';
if (typeof options === 'object') ({ htmlString, htmlPath } = options);
const html = htmlString || await fs.readFile(htmlPath, 'utf8');
let matches = html.match(new RegExp(linkTagRegex, 'g'));
if (!matches)
return html;
Expand Down
11 changes: 11 additions & 0 deletions test/testApi/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const fs = require('fs/promises');
const {inlineEnvironmentVariables, inlineScriptTags, inlineStylesheets, inlineImages} = require('../../src/index');

fs.readFile('./testfiles/main.js', { encoding: 'utf8' })
.then (jsString => inlineEnvironmentVariables({ jsPath: './testfiles/myScript.js', jsString }))
.then (jsString => console.log('process.env.PORT should have been replaced:', jsString));

inlineScriptTags('./testfiles/index.html')
.then (htmlString => inlineStylesheets({ htmlPath: './testfiles/index.html', htmlString }))
.then (htmlString => inlineImages({ htmlPath: './testfiles/index.html', htmlString }))
.then (htmlString => console.log(htmlString))
3 changes: 3 additions & 0 deletions test/testApi/testfiles/icons/rss-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions test/testApi/testfiles/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Link tags without `rel="stylesheet"` should not be replaced.

Not a stylesheet
<link href="/author.html" rel="author">

Missing rel attribute
<link href="myStyle.css">

Incorrect syntax
<linkrel="stylesheet" href="myStyle.css">


Link tags with `rel="stylesheet"` should be replaced.

<link href="myStyle.css" rel="stylesheet">

Space between attributes is optional
<link rel="stylesheet"href="myStyle.css">

<link href="nested/myNestedStylesheet.css" rel="stylesheet" />

<!------------------------------------------->

<p>hi from index.html</p>

<script src="myScript.js"></script>

<p>line 2</p>

<script src="nested/myNestedScript.js" type="application/javascript"></script>

<p>line 3</p>

<!------------------------------------------->

<img src="red_dot.png" alt="red dot" />

<img alt="" src="icons/rss-icon.svg"width="18">
1 change: 1 addition & 0 deletions test/testApi/testfiles/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('port is' + process.env.PORT);
1 change: 1 addition & 0 deletions test/testApi/testfiles/myScript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('hi from myScript </script></script>');
3 changes: 3 additions & 0 deletions test/testApi/testfiles/myStyle.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
p {
background: red;
}
1 change: 1 addition & 0 deletions test/testApi/testfiles/nested/myNestedScript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('hi from myNestedScript');
3 changes: 3 additions & 0 deletions test/testApi/testfiles/nested/myNestedStylesheet.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
p {
color: green;
}
Binary file added test/testApi/testfiles/red_dot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.