├── .npmrc ├── cjs ├── package.json └── index.js ├── .gitignore ├── .npmignore ├── es.js ├── min.js ├── rollup ├── es.config.js └── babel.config.js ├── esm └── index.js ├── test └── index.html ├── index.js ├── LICENSE ├── README.md └── package.json /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /cjs/package.json: -------------------------------------------------------------------------------- 1 | {"type":"commonjs"} -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .nyc_output 3 | node_modules/ 4 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .nyc_output 3 | .travis.yml 4 | node_modules/ 5 | rollup/ 6 | test/ 7 | -------------------------------------------------------------------------------- /es.js: -------------------------------------------------------------------------------- 1 | self.onceDefined=function(e){"use strict";return e.default=e=>{const t=[].concat(e);return Promise.all(t.map(e=>customElements.whenDefined(e).then(t=>t||customElements.get(e)))).then(e=>t.length<2?e[0]:e)},e}({}).default; 2 | -------------------------------------------------------------------------------- /min.js: -------------------------------------------------------------------------------- 1 | self.onceDefined=function(n){"use strict";return n.default=function(n){var e=[].concat(n);return Promise.all(e.map((function(n){return customElements.whenDefined(n).then((function(e){return e||customElements.get(n)}))}))).then((function(n){return e.length<2?n[0]:n}))},n}({}).default; -------------------------------------------------------------------------------- /rollup/es.config.js: -------------------------------------------------------------------------------- 1 | import resolve from 'rollup-plugin-node-resolve'; 2 | import {terser} from 'rollup-plugin-terser'; 3 | 4 | export default { 5 | input: './esm/index.js', 6 | plugins: [ 7 | 8 | resolve({module: true}), 9 | terser() 10 | ], 11 | 12 | output: { 13 | exports: 'named', 14 | file: './es.js', 15 | format: 'iife', 16 | name: 'onceDefined' 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /rollup/babel.config.js: -------------------------------------------------------------------------------- 1 | import resolve from 'rollup-plugin-node-resolve'; 2 | import babel from 'rollup-plugin-babel'; 3 | 4 | export default { 5 | input: './esm/index.js', 6 | plugins: [ 7 | 8 | resolve({module: true}), 9 | babel({presets: ['@babel/preset-env']}) 10 | ], 11 | 12 | output: { 13 | exports: 'named', 14 | file: './index.js', 15 | format: 'iife', 16 | name: 'onceDefined' 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /esm/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {string|string[]} names one or more `customElements` names 3 | * @return {HTMLElement|HTMLElement[]} one or more classes defined through 4 | * the `customElements` registry 5 | */ 6 | export default names => { 7 | const all = [].concat(names); 8 | return Promise.all( 9 | all.map(name => customElements.whenDefined(name).then( 10 | Class => Class || customElements.get(name) 11 | )) 12 | ).then(result => all.length < 2 ? result[0] : result); 13 | }; 14 | -------------------------------------------------------------------------------- /cjs/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | /** 3 | * @param {string|string[]} names one or more `customElements` names 4 | * @return {HTMLElement|HTMLElement[]} one or more classes defined through 5 | * the `customElements` registry 6 | */ 7 | module.exports = names => { 8 | const all = [].concat(names); 9 | return Promise.all( 10 | all.map(name => customElements.whenDefined(name).then( 11 | Class => Class || customElements.get(name) 12 | )) 13 | ).then(result => all.length < 2 ? result[0] : result); 14 | }; 15 | -------------------------------------------------------------------------------- /test/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 |