Skip to content

Instantly share code, notes, and snippets.

@samthor
Forked from surma/importPolyfill.js
Last active November 11, 2024 07:10

Revisions

  1. samthor revised this gist Jul 25, 2017. 2 changed files with 2 additions and 1 deletion.
    2 changes: 2 additions & 0 deletions importPolyfill.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    // usage:
    // importScript('./path/to/script.js').then((allExports) => { .... }));
    function importScript(path) {
    let entry = window.importScript.__db[path];
    if (entry === undefined) {
    1 change: 0 additions & 1 deletion importPolyfill.min.js
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    function c(a){var b=c.a[a];if(void 0===b){var d=a.replace("'","\\'"),e=Object.assign(document.createElement("script"),{type:"module",textContent:"import * as x from '"+d+"'; importScript.a['"+d+"'].resolve(x);"}),b=c.a[a]={};b.b=new Promise(function(a,d){b.resolve=a;e.onerror=d});document.head.appendChild(e);e.remove()}return b.b}c.a={};window.importScript=c;
  2. samthor revised this gist Jun 16, 2017. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion importPolyfill.js
    Original file line number Diff line number Diff line change
    @@ -17,4 +17,4 @@ function importScript(path) {
    return entry.promise;
    }
    importScript.__db = {};
    window.importScript = importScript; // needed if we ourselves are in a module
    window['importScript'] = importScript; // needed if we ourselves are in a module
    2 changes: 1 addition & 1 deletion importPolyfill.min.js
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    function c(a){var b=c.a[a];if(void 0===b){var d=a.replace("'","\\'"),e=Object.assign(document.createElement("script"),{type:"module",textContent:"import * as x from '"+d+"'; importScript.__db['"+d+"'].resolve(x);"}),b=c.a[a]={};b.b=new Promise(function(a,d){b.resolve=a;e.onerror=d});document.head.appendChild(e);e.remove()}return b.b}c.a={};window.importScript=c;
    function c(a){var b=c.a[a];if(void 0===b){var d=a.replace("'","\\'"),e=Object.assign(document.createElement("script"),{type:"module",textContent:"import * as x from '"+d+"'; importScript.a['"+d+"'].resolve(x);"}),b=c.a[a]={};b.b=new Promise(function(a,d){b.resolve=a;e.onerror=d});document.head.appendChild(e);e.remove()}return b.b}c.a={};window.importScript=c;
  3. samthor revised this gist Jun 16, 2017. 2 changed files with 20 additions and 16 deletions.
    33 changes: 19 additions & 14 deletions importPolyfill.js
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,20 @@
    _registry = {};
    importPolyfill = path => {
    if(!(path in _registry)) {
    const entry = _registry[path] = {};
    entry.promise = new Promise(resolve => entry.resolve = resolve);
    document.head.appendChild(Object.assign(
    document.createElement('script'),
    {
    type: 'module',
    innerText: `import * as X from '${path}'; _registry['${path}'].resolve(X);`,
    }
    ));
    function importScript(path) {
    let entry = window.importScript.__db[path];
    if (entry === undefined) {
    const escape = path.replace(`'`, `\\'`);
    const script = Object.assign(document.createElement('script'), {
    type: 'module',
    textContent: `import * as x from '${escape}'; importScript.__db['${escape}'].resolve(x);`,
    });
    entry = importScript.__db[path] = {};
    entry.promise = new Promise((resolve, reject) => {
    entry.resolve = resolve;
    script.onerror = reject;
    });
    document.head.appendChild(script);
    script.remove();
    }
    return _registry[path].promise;
    }
    return entry.promise;
    }
    importScript.__db = {};
    window.importScript = importScript; // needed if we ourselves are in a module
    3 changes: 1 addition & 2 deletions importPolyfill.min.js
    Original file line number Diff line number Diff line change
    @@ -1,2 +1 @@
    r={},importPolyfill=(a)=>{if(!(a in r)){const b=r[a]={};b.p=new Promise((c)=>b.r=c),document.head.appendChild(Object.assign(document.createElement('script'),{type:'module',innerText:`import * as X from '${a}'; r['${a}'].r(X);`}))}return r[a].p};

    function c(a){var b=c.a[a];if(void 0===b){var d=a.replace("'","\\'"),e=Object.assign(document.createElement("script"),{type:"module",textContent:"import * as x from '"+d+"'; importScript.__db['"+d+"'].resolve(x);"}),b=c.a[a]={};b.b=new Promise(function(a,d){b.resolve=a;e.onerror=d});document.head.appendChild(e);e.remove()}return b.b}c.a={};window.importScript=c;
  4. @surma surma created this gist May 6, 2017.
    15 changes: 15 additions & 0 deletions importPolyfill.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    _registry = {};
    importPolyfill = path => {
    if(!(path in _registry)) {
    const entry = _registry[path] = {};
    entry.promise = new Promise(resolve => entry.resolve = resolve);
    document.head.appendChild(Object.assign(
    document.createElement('script'),
    {
    type: 'module',
    innerText: `import * as X from '${path}'; _registry['${path}'].resolve(X);`,
    }
    ));
    }
    return _registry[path].promise;
    }
    2 changes: 2 additions & 0 deletions importPolyfill.min.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    r={},importPolyfill=(a)=>{if(!(a in r)){const b=r[a]={};b.p=new Promise((c)=>b.r=c),document.head.appendChild(Object.assign(document.createElement('script'),{type:'module',innerText:`import * as X from '${a}'; r['${a}'].r(X);`}))}return r[a].p};