├── .eslintrc ├── .gitignore ├── .npmignore ├── LICENSE ├── README.md ├── importModule.js ├── package.json └── test ├── .nojekyll ├── a.js ├── abs.nested.js ├── b.js ├── c.js ├── index.html ├── link.js ├── native.html ├── polyfill.abs.html ├── polyfill.abs.nested.html ├── polyfill.rel.html ├── polyfill.rel.nested.html └── rel.nested.js /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "parserOptions": { 3 | "ecmaVersion": 6, 4 | "sourceType": "module" 5 | }, 6 | "env": { 7 | "browser": true, 8 | "node": true, 9 | "es6": true 10 | }, 11 | "extends": [ 12 | "eslint:recommended" 13 | ], 14 | "rules": { 15 | "strict": [0], 16 | "no-new": [0], 17 | "new-cap": [0], 18 | "no-empty": [0], 19 | "no-shadow": [0], 20 | "camelcase": [0], 21 | "key-spacing": [0], 22 | "dot-notation": [0], 23 | "comma-dangle": [0], 24 | "no-console": [0], 25 | "no-multi-str": [0], 26 | "no-multi-spaces": [0], 27 | "no-control-regex": [0], 28 | "no-unused-vars": [1, { "vars": "all", "args": "after-used"} ], 29 | "no-underscore-dangle": [0], 30 | "no-use-before-define": [0], 31 | "no-inner-declarations": [0] 32 | }, 33 | "globals": { 34 | "unescape": false, 35 | "escape": false, 36 | "google": false 37 | } 38 | } 39 | 40 | 41 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## ignore the dependency modules directory 2 | node_modules 3 | 4 | ## ignore the file that is automatically regenerated 5 | .DS_Store 6 | 7 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | ## ignore the dependency modules directory 2 | node_modules 3 | 4 | ## ignore the file that is automatically regenerated 5 | .DS_Store 6 | 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 uupaa 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Dynamic import() polyfill 2 | 3 | importModule() is a polyfill of dynamic import JavaScript function. 4 | 5 | ## Prepare 6 | 7 | `npm i -S @uupaa/dynamic-import-polyfill` 8 | 9 | ## Browser support 10 | 11 | | Browser | ` 106 | 107 | 108 | ``` 109 | 110 | -------------------------------------------------------------------------------- /importModule.js: -------------------------------------------------------------------------------- 1 | function toAbsoluteURL(url) { 2 | const a = document.createElement("a"); 3 | a.setAttribute("href", url); // 4 | return a.cloneNode(false).href; // -> "http://example.com/hoge.html" 5 | } 6 | 7 | export function importModule(url) { 8 | return new Promise((resolve, reject) => { 9 | const vector = "$importModule$" + Math.random().toString(32).slice(2); 10 | const script = document.createElement("script"); 11 | const destructor = () => { 12 | delete window[vector]; 13 | script.onerror = null; 14 | script.onload = null; 15 | script.remove(); 16 | URL.revokeObjectURL(script.src); 17 | script.src = ""; 18 | }; 19 | script.defer = "defer"; 20 | script.type = "module"; 21 | script.onerror = () => { 22 | reject(new Error(`Failed to import: ${url}`)); 23 | destructor(); 24 | }; 25 | script.onload = () => { 26 | resolve(window[vector]); 27 | destructor(); 28 | }; 29 | const absURL = toAbsoluteURL(url); 30 | const loader = `import * as m from "${absURL}"; window.${vector} = m;`; // export Module 31 | const blob = new Blob([loader], { type: "text/javascript" }); 32 | script.src = URL.createObjectURL(blob); 33 | 34 | document.head.appendChild(script); 35 | }); 36 | } 37 | 38 | export default importModule; 39 | 40 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@uupaa/dynamic-import-polyfill", 3 | "version": "1.0.2", 4 | "description": "importModule() is a polyfill of import().", 5 | "repository": { 6 | "type": "git", 7 | "url": "https://github.com/uupaa/dynamic-import-polyfill.git" 8 | }, 9 | "scripts": {}, 10 | "publishConfig": { 11 | "access": "public" 12 | }, 13 | "author": "uupaa.js@gmail.com", 14 | "license": "MIT", 15 | "main": "importModule.js", 16 | "keywords": [ 17 | "dynamic", 18 | "import", 19 | "polyfill", 20 | "module", 21 | "ESModule", 22 | "ESModules", 23 | "uupaa" 24 | ] 25 | } 26 | -------------------------------------------------------------------------------- /test/.nojekyll: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /test/a.js: -------------------------------------------------------------------------------- 1 | export function a() { 2 | return "a"; 3 | } 4 | 5 | export default a; 6 | 7 | -------------------------------------------------------------------------------- /test/abs.nested.js: -------------------------------------------------------------------------------- 1 | import "https://uupaa.github.io/dynamic-import-polyfill/test/b.js"; 2 | import "https://uupaa.github.io/dynamic-import-polyfill/test/c.js"; 3 | 4 | export function a() { 5 | return "a"; 6 | } 7 | 8 | export default a; 9 | -------------------------------------------------------------------------------- /test/b.js: -------------------------------------------------------------------------------- 1 | export function b() { 2 | return "b"; 3 | } 4 | 5 | export default b; 6 | 7 | -------------------------------------------------------------------------------- /test/c.js: -------------------------------------------------------------------------------- 1 | export function c() { 2 | return "c"; 3 | } 4 | 5 | document.body.style.backgroundColor = "lime"; 6 | 7 | -------------------------------------------------------------------------------- /test/index.html: -------------------------------------------------------------------------------- 1 | dynamic-import-polyfill 2 | 3 | 4 |
Back to dynamic-import-polyfill repository
5 | 6 | 7 | 13 | 16 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /test/link.js: -------------------------------------------------------------------------------- 1 | const LINKS = ` 2 | 10 | `; 11 | 12 | document.querySelector("div.link-contener").innerHTML = LINKS; 13 | 14 | -------------------------------------------------------------------------------- /test/native.html: -------------------------------------------------------------------------------- 1 | dynamic-import-polyfill 2 | 3 | 4 |
Back to dynamic-import-polyfill repository
5 | 6 | 7 | 13 | 16 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /test/polyfill.abs.html: -------------------------------------------------------------------------------- 1 | dynamic-import-polyfill 2 | 3 | 4 |
Back to dynamic-import-polyfill repository
5 | 6 | 7 | 13 | 16 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /test/polyfill.abs.nested.html: -------------------------------------------------------------------------------- 1 | dynamic-import-polyfill 2 | 3 | 4 |
Back to dynamic-import-polyfill repository
5 | 6 | 7 | 13 | 16 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /test/polyfill.rel.html: -------------------------------------------------------------------------------- 1 | dynamic-import-polyfill 2 | 3 | 4 |
Back to dynamic-import-polyfill repository
5 | 6 | 7 | 13 | 16 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /test/polyfill.rel.nested.html: -------------------------------------------------------------------------------- 1 | dynamic-import-polyfill 2 | 3 | 4 |
Back to dynamic-import-polyfill repository
5 | 6 | 7 | 13 | 16 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /test/rel.nested.js: -------------------------------------------------------------------------------- 1 | import "./b.js"; 2 | import "./c.js"; 3 | 4 | export function a() { 5 | return "a"; 6 | } 7 | 8 | export default a; 9 | --------------------------------------------------------------------------------