diff --git a/readme.md b/readme.md index bc9f478..46d8b7b 100644 --- a/readme.md +++ b/readme.md @@ -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)); + +``` diff --git a/src/inlineEnvironmentVariables.js b/src/inlineEnvironmentVariables.js index ca4a923..f1dea4b 100644 --- a/src/inlineEnvironmentVariables.js +++ b/src/inlineEnvironmentVariables.js @@ -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) diff --git a/src/inlineImages.js b/src/inlineImages.js index 9b635fc..8c26cc1 100644 --- a/src/inlineImages.js +++ b/src/inlineImages.js @@ -3,9 +3,11 @@ const fs = require('fs').promises; const path = require('path'); -let inlineImages = async htmlPath => { +let inlineImages = async options => { const imgTagRegex = //; - 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; diff --git a/src/inlineScriptTags.js b/src/inlineScriptTags.js index d19699b..02a0986 100644 --- a/src/inlineScriptTags.js +++ b/src/inlineScriptTags.js @@ -3,9 +3,11 @@ const fs = require('fs').promises; const path = require('path'); -let inlineHtmlScripts = async htmlPath => { +let inlineHtmlScripts = async options => { const scriptTagRegex = / + +

line 2

+ + + +

line 3

+ + + +red dot + + \ No newline at end of file diff --git a/test/testApi/testfiles/main.js b/test/testApi/testfiles/main.js new file mode 100644 index 0000000..f986837 --- /dev/null +++ b/test/testApi/testfiles/main.js @@ -0,0 +1 @@ +console.log('port is' + process.env.PORT); \ No newline at end of file diff --git a/test/testApi/testfiles/myScript.js b/test/testApi/testfiles/myScript.js new file mode 100644 index 0000000..4f424e1 --- /dev/null +++ b/test/testApi/testfiles/myScript.js @@ -0,0 +1 @@ +console.log('hi from myScript '); diff --git a/test/testApi/testfiles/myStyle.css b/test/testApi/testfiles/myStyle.css new file mode 100644 index 0000000..da4dbaf --- /dev/null +++ b/test/testApi/testfiles/myStyle.css @@ -0,0 +1,3 @@ +p { + background: red; +} diff --git a/test/testApi/testfiles/nested/myNestedScript.js b/test/testApi/testfiles/nested/myNestedScript.js new file mode 100644 index 0000000..2ea5dd6 --- /dev/null +++ b/test/testApi/testfiles/nested/myNestedScript.js @@ -0,0 +1 @@ +console.log('hi from myNestedScript'); diff --git a/test/testApi/testfiles/nested/myNestedStylesheet.css b/test/testApi/testfiles/nested/myNestedStylesheet.css new file mode 100644 index 0000000..25190f6 --- /dev/null +++ b/test/testApi/testfiles/nested/myNestedStylesheet.css @@ -0,0 +1,3 @@ +p { + color: green; +} diff --git a/test/testApi/testfiles/red_dot.png b/test/testApi/testfiles/red_dot.png new file mode 100644 index 0000000..7a3361f Binary files /dev/null and b/test/testApi/testfiles/red_dot.png differ