fetch-inject
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -6,2 +6,41 @@ /** | ||
var fetchInject=function(){"use strict";const e=function(e,t,n,o,c,r,i){r=t.createElement(n),i=t.getElementsByTagName(n)[0],r.appendChild(t.createTextNode(o)),r.onload=c,i.parentNode.insertBefore(r,i)},t=function(e,t,n,o,c,r,i){r=t.createElement(n),i=t.getElementsByTagName(n)[0],r.type="text/css",r.appendChild(t.createTextNode(o)),i.parentNode.insertBefore(r,i)};return function(n){const o=[],c=[],r=[];return n.forEach(e=>c.push(window.fetch(e).then(e=>{return[e.clone().text(),e.blob()]}).then(e=>{return Promise.all(e).then(e=>{o.push({text:e[0],type:e[1].type})})}))),Promise.all(c).then(()=>{return o.forEach(n=>{r.push({then:o=>{"application/javascript"===n.type?e(window,document,"script",n.text,o()):"text/css"===n.type&&(t(window,document,"style",n.text),o())}})}),Promise.all(r)})}}(); | ||
var fetchInject = (function () { | ||
'use strict'; | ||
const script = (function(i,s,o,g,r,a,m){a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.appendChild(s.createTextNode(g));a.onload=r;m.parentNode.insertBefore(a,m);}); // eslint-disable-line | ||
const style = (function(i,s,o,g,r,a,m){a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.type='text/css';a.appendChild(s.createTextNode(g));m.parentNode.insertBefore(a,m);}); // eslint-disable-line | ||
var fetchInject = function (urls) { | ||
const resources = []; | ||
const deferreds = []; | ||
const thenables = []; | ||
urls.forEach((url) => deferreds.push( | ||
window.fetch(url).then(res => { | ||
return [res.clone().text(), res.blob()] | ||
}).then(kvArrays => { | ||
return Promise.all(kvArrays).then((kvArray) => { | ||
resources.push({ text: kvArray[0], type: kvArray[1].type }); | ||
}) | ||
}) | ||
)); | ||
return Promise.all(deferreds).then(() => { | ||
resources.forEach((resource) => { | ||
thenables.push({ then: (resolve) => { | ||
if (resource.type === 'application/javascript') { | ||
script(window, document, 'script', resource.text, resolve()); | ||
} else if (resource.type === 'text/css') { | ||
style(window, document, 'style', resource.text); | ||
resolve(); | ||
} | ||
}}); | ||
}); | ||
return Promise.all(thenables) | ||
}) | ||
}; | ||
return fetchInject; | ||
}()); | ||
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZmV0Y2gtaW5qZWN0LmpzIiwic291cmNlcyI6WyIuLi9zcmMvaW5qZWN0b3JzLmpzIiwiLi4vc3JjL2ZldGNoLWluamVjdC5qcyJdLCJzb3VyY2VzQ29udGVudCI6WyJleHBvcnQgY29uc3Qgc2NyaXB0ID0gKGZ1bmN0aW9uKGkscyxvLGcscixhLG0pe2E9cy5jcmVhdGVFbGVtZW50KG8pLG09cy5nZXRFbGVtZW50c0J5VGFnTmFtZShvKVswXTthLmFwcGVuZENoaWxkKHMuY3JlYXRlVGV4dE5vZGUoZykpO2Eub25sb2FkPXI7bS5wYXJlbnROb2RlLmluc2VydEJlZm9yZShhLG0pfSkgLy8gZXNsaW50LWRpc2FibGUtbGluZVxuZXhwb3J0IGNvbnN0IHN0eWxlID0gKGZ1bmN0aW9uKGkscyxvLGcscixhLG0pe2E9cy5jcmVhdGVFbGVtZW50KG8pLG09cy5nZXRFbGVtZW50c0J5VGFnTmFtZShvKVswXTthLnR5cGU9J3RleHQvY3NzJzthLmFwcGVuZENoaWxkKHMuY3JlYXRlVGV4dE5vZGUoZykpO20ucGFyZW50Tm9kZS5pbnNlcnRCZWZvcmUoYSxtKX0pIC8vIGVzbGludC1kaXNhYmxlLWxpbmVcbiIsImltcG9ydCB7XG4gIHNjcmlwdCBhcyBpbmplY3RTY3JpcHQsXG4gIHN0eWxlIGFzIGluamVjdFN0eWxlXG59IGZyb20gJy4vaW5qZWN0b3JzJ1xuXG5leHBvcnQgZGVmYXVsdCBmdW5jdGlvbiAodXJscykge1xuICBjb25zdCByZXNvdXJjZXMgPSBbXVxuICBjb25zdCBkZWZlcnJlZHMgPSBbXVxuICBjb25zdCB0aGVuYWJsZXMgPSBbXVxuXG4gI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VksQ0FBQyxNQUFNLEVBQUUsUUFBUSxFQUFFLFFBQVEsRUFBRSxRQUFRLENBQUMsSUFBSSxFQUFFLE9BQU8sRUFBRSxDQUFDLENBQUE7U0FDbkUsTUFBTSxJQUFJLFFBQVEsQ0FBQyxJQUFJLEtBQUssVUFBVSxFQUFFO1VBQ3ZDQyxLQUFXLENBQUMsTUFBTSxFQUFFLFFBQVEsRUFBRSxPQUFPLEVBQUUsUUFBUSxDQUFDLElBQUksQ0FBQyxDQUFBO1VBQ3JELE9BQU8sRUFBRSxDQUFBO1NBQ1Y7T0FDRixDQUFDLENBQUMsQ0FBQTtLQUNKLENBQUMsQ0FBQTtJQUNGLE9BQU8sT0FBTyxDQUFDLEdBQUcsQ0FBQyxTQUFTLENBQUM7R0FDOUIsQ0FBQztDQUNILENBQUE7Ozs7In0= |
{ | ||
"name": "fetch-inject", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Inline assets into the DOM using Fetch Injection.", | ||
"main": "lib/fetch-inject.js", | ||
"scripts": { | ||
"build": "rollup -c", | ||
"build": "npm run lint && npm run clean && rollup -c && BUILD_TARGET=minified rollup -c", | ||
"build-min": "BUILD_TARGET=minified rollup -c", | ||
"clean": "rimraf ./dist/*", | ||
"test": "npm run lint", | ||
@@ -27,8 +29,9 @@ "lint": "standard", | ||
"devDependencies": { | ||
"rimraf": "^2.6.1", | ||
"rollup": "^0.41.4", | ||
"rollup-plugin-license": "^0.3.0", | ||
"rollup-plugin-uglify": "^1.0.1", | ||
"semantic-release": "^6.3.2", | ||
"standard": "^9.0.0", | ||
"uglify-js-harmony": "^2.7.5", | ||
"semantic-release": "^6.3.2" | ||
"uglify-js-harmony": "^2.7.5" | ||
}, | ||
@@ -35,0 +38,0 @@ "standard": { |
# Fetch Inject | ||
[![Join the chat at https://gitter.im/fetch-inject/Lobby](https://badges.gitter.im/fetch-inject/Lobby.svg)](https://gitter.im/fetch-inject/Lobby) | ||
[![Build Status](https://travis-ci.org/jhabdas/fetch-inject.svg?branch=master)](https://travis-ci.org/jhabdas/fetch-inject) | ||
@@ -18,4 +17,8 @@ [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release) | ||
Read about [why I created this](https://hackcabin.com/post/introducing-fetch-injection/) on **_Hack Cabin_**. | ||
Read about [why I created this](https://hackcabin.com/post/managing-asynchronous-dependencies-javascript/) on **Hack Cabin**. | ||
## Installation | ||
Install the library from NPM with `npm i fetch-inject` or Bower with `bower install fetch-inject`. | ||
## Usage | ||
@@ -22,0 +25,0 @@ |
import uglify from 'rollup-plugin-uglify' | ||
import license from 'rollup-plugin-license' | ||
import { minify } from 'uglify-js-harmony' | ||
export default { | ||
const config = { | ||
entry: 'src/fetch-inject.js', | ||
@@ -9,9 +10,16 @@ format: 'iife', | ||
plugins: [ | ||
uglify(), | ||
license({ | ||
banner: `Copyright (c) <%= moment().format('YYYY') %> Josh Habdas\n@licence MIT` | ||
}) | ||
], | ||
sourceMap: false, | ||
dest: 'dist/fetch-inject.js' | ||
] | ||
} | ||
if (process.env.BUILD_TARGET === 'minified') { | ||
config.plugins.unshift(uglify({}, minify)) | ||
config.dest = 'dist/fetch-inject.min.js' | ||
} else { | ||
config.sourceMap = 'inline' | ||
config.dest = 'dist/fetch-inject.js' | ||
} | ||
export default config |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
18308
12
143
200
7
1