├── .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 | when-defined 7 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | self.onceDefined = (function (exports) { 2 | 'use strict'; 3 | 4 | /** 5 | * @param {string|string[]} names one or more `customElements` names 6 | * @return {HTMLElement|HTMLElement[]} one or more classes defined through 7 | * the `customElements` registry 8 | */ 9 | var index = (function (names) { 10 | var all = [].concat(names); 11 | return Promise.all(all.map(function (name) { 12 | return customElements.whenDefined(name).then(function (Class) { 13 | return Class || customElements.get(name); 14 | }); 15 | })).then(function (result) { 16 | return all.length < 2 ? result[0] : result; 17 | }); 18 | }); 19 | 20 | exports.default = index; 21 | 22 | return exports; 23 | 24 | }({}).default); 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | ISC License 2 | 3 | Copyright (c) 2020, Andrea Giammarchi, @WebReflection 4 | 5 | Permission to use, copy, modify, and/or distribute this software for any 6 | purpose with or without fee is hereby granted, provided that the above 7 | copyright notice and this permission notice appear in all copies. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH 10 | REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 11 | AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, 12 | INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 13 | LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 14 | OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 15 | PERFORMANCE OF THIS SOFTWARE. 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # once-defined 2 | 3 | A minimalistic, ~140 bytes, boilerplate to wait for Custom Elements, or libraries, definition. 4 | 5 | ```js 6 | import when from 'once-defined'; 7 | // const when = require('once-defined'); 8 | //