├── .gitignore ├── .npmignore ├── .npmrc ├── .travis.yml ├── README.md ├── cjs ├── index.js └── package.json ├── es.js ├── esm └── index.js ├── index.js ├── min.js ├── package.json ├── rollup ├── babel.config.js └── es.config.js └── test ├── index.html ├── index.js └── package.json /.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 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - stable 4 | git: 5 | depth: 1 6 | branches: 7 | only: 8 | - master 9 | after_success: 10 | - "npm run coveralls" 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # customLibraries 2 | 3 | [](https://travis-ci.com/WebReflection/custom-libraries) [](https://coveralls.io/github/WebReflection/custom-libraries?branch=master)  4 | 5 | **Social Media Photo by [Matteo Maretto](https://unsplash.com/@matmaphotos) on [Unsplash](https://unsplash.com/)** 6 | 7 | A [customElements](https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry) like registry for user-land libraries. 8 | 9 | Based on [Some Web Components Hints](https://medium.com/@WebReflection/some-web-components-hint-75dce339ac6b) post idea, but without polluting the `customElements` registry. 10 | 11 | Compatible down to IE10, or even IE9 (with `setTimeout` polyfill), and every other browser, for a total of 272 bytes once minified and gzipped. 12 | 13 | ```js 14 | import {define, get, whenDefined} from 'custom-libraries'; 15 | // const {define, get, whenDefined} = require('custom-libraries'); 16 | // 17 | 18 | whenDefined('my-lib').then(myLib => { 19 | // myLib is defined and passed along 20 | // get('my-lib') === myLib 21 | }); 22 | 23 | setTimeout(() => { 24 | define('my-lib', {any: 'value'}); 25 | }); 26 | ``` 27 | -------------------------------------------------------------------------------- /cjs/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const Lie = (m => m.__esModule ? /* istanbul ignore next */ m.default : /* istanbul ignore next */ m)(require('@webreflection/lie')); 3 | 4 | const {create} = Object; 5 | 6 | const defined = create(null); 7 | const registry = create(null); 8 | 9 | const define = (lib, value) => { 10 | if (lib in registry) 11 | throw new Error('duplicated ' + lib); 12 | whenDefined(lib); 13 | defined[lib]._(registry[lib] = value); 14 | }; 15 | exports.define = define; 16 | 17 | const get = lib => registry[lib]; 18 | exports.get = get; 19 | 20 | const whenDefined = lib => { 21 | if (!(lib in defined)) { 22 | let _, $ = new Lie($ => { _ = $; }); 23 | defined[lib] = {_, $}; 24 | } 25 | return defined[lib].$; 26 | }; 27 | exports.whenDefined = whenDefined; 28 | -------------------------------------------------------------------------------- /cjs/package.json: -------------------------------------------------------------------------------- 1 | {"type":"commonjs"} -------------------------------------------------------------------------------- /es.js: -------------------------------------------------------------------------------- 1 | self.customLibraries=function(e){"use strict";var t="function"==typeof Promise?Promise:function(e){let t,n=[],r=0;return e(e=>{t=e,r=1,n.splice(0).forEach(i)}),{then:i};function i(e){return r?setTimeout(e,0,t):n.push(e),this}};const{create:n}=Object,r=n(null),i=n(null),u=e=>{if(!(e in r)){let n,i=new t(e=>{n=e});r[e]={_:n,$:i}}return r[e].$};return e.define=(e,t)=>{if(e in i)throw new Error("duplicated "+e);u(e),r[e]._(i[e]=t)},e.get=e=>i[e],e.whenDefined=u,e}({}); 2 | -------------------------------------------------------------------------------- /esm/index.js: -------------------------------------------------------------------------------- 1 | import Lie from '@webreflection/lie'; 2 | 3 | const {create} = Object; 4 | 5 | const defined = create(null); 6 | const registry = create(null); 7 | 8 | export const define = (lib, value) => { 9 | if (lib in registry) 10 | throw new Error('duplicated ' + lib); 11 | whenDefined(lib); 12 | defined[lib]._(registry[lib] = value); 13 | }; 14 | 15 | export const get = lib => registry[lib]; 16 | 17 | export const whenDefined = lib => { 18 | if (!(lib in defined)) { 19 | let _, $ = new Lie($ => { _ = $; }); 20 | defined[lib] = {_, $}; 21 | } 22 | return defined[lib].$; 23 | }; 24 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | self.customLibraries = (function (exports) { 2 | 'use strict'; 3 | 4 | var Lie = typeof Promise === 'function' ? Promise : function (fn) { 5 | var queue = [], 6 | resolved = 0, 7 | value; 8 | fn(function ($) { 9 | value = $; 10 | resolved = 1; 11 | queue.splice(0).forEach(then); 12 | }); 13 | return { 14 | then: then 15 | }; 16 | 17 | function then(fn) { 18 | return resolved ? setTimeout(fn, 0, value) : queue.push(fn), this; 19 | } 20 | }; 21 | 22 | var create = Object.create; 23 | var defined = create(null); 24 | var registry = create(null); 25 | var define = function define(lib, value) { 26 | if (lib in registry) throw new Error('duplicated ' + lib); 27 | whenDefined(lib); 28 | 29 | defined[lib]._(registry[lib] = value); 30 | }; 31 | var get = function get(lib) { 32 | return registry[lib]; 33 | }; 34 | var whenDefined = function whenDefined(lib) { 35 | if (!(lib in defined)) { 36 | var _, 37 | $ = new Lie(function ($) { 38 | _ = $; 39 | }); 40 | 41 | defined[lib] = { 42 | _: _, 43 | $: $ 44 | }; 45 | } 46 | 47 | return defined[lib].$; 48 | }; 49 | 50 | exports.define = define; 51 | exports.get = get; 52 | exports.whenDefined = whenDefined; 53 | 54 | return exports; 55 | 56 | }({})); 57 | -------------------------------------------------------------------------------- /min.js: -------------------------------------------------------------------------------- 1 | self.customLibraries=function(n){"use strict";var e="function"==typeof Promise?Promise:function(n){var e,t=[],i=0;return n((function(n){e=n,i=1,t.splice(0).forEach(r)})),{then:r};function r(n){return i?setTimeout(n,0,e):t.push(n),this}},t=Object.create,i=t(null),r=t(null),u=function(n){if(!(n in i)){var t,r=new e((function(n){t=n}));i[n]={_:t,$:r}}return i[n].$};return n.define=function(n,e){if(n in r)throw new Error("duplicated "+n);u(n),i[n]._(r[n]=e)},n.get=function(n){return r[n]},n.whenDefined=u,n}({}); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "custom-libraries", 3 | "version": "0.1.0", 4 | "description": "A customElements like registry for user-land libraries", 5 | "main": "./cjs/index.js", 6 | "scripts": { 7 | "build": "npm run cjs && npm run rollup:es && npm run rollup:babel && npm run min && npm run test && npm run size", 8 | "cjs": "ascjs esm cjs", 9 | "rollup:es": "rollup --config rollup/es.config.js && sed -i.bck 's/^var /self./' es.js && rm -rf es.js.bck", 10 | "rollup:babel": "rollup --config rollup/babel.config.js && sed -i.bck 's/^var /self./' index.js && rm -rf index.js.bck", 11 | "min": "terser index.js --comments='/^!/' -c -m -o min.js", 12 | "coveralls": "nyc report --reporter=text-lcov | coveralls", 13 | "size": "cat min.js | brotli | wc -c && cat es.js | brotli | wc -c", 14 | "test": "nyc node test/index.js" 15 | }, 16 | "keywords": [ 17 | "customElements", 18 | "registry", 19 | "user", 20 | "libraries" 21 | ], 22 | "author": "Andrea Giammarchi", 23 | "license": "ISC", 24 | "devDependencies": { 25 | "@babel/core": "^7.11.0", 26 | "@babel/preset-env": "^7.11.0", 27 | "@rollup/plugin-babel": "^5.1.0", 28 | "@rollup/plugin-node-resolve": "^8.4.0", 29 | "ascjs": "^4.0.1", 30 | "coveralls": "^3.1.0", 31 | "nyc": "^15.1.0", 32 | "rollup": "^2.23.0", 33 | "rollup-plugin-terser": "^6.1.0", 34 | "terser": "^5.0.0" 35 | }, 36 | "module": "./esm/index.js", 37 | "type": "module", 38 | "exports": { 39 | "import": "./esm/index.js", 40 | "default": "./cjs/index.js" 41 | }, 42 | "unpkg": "min.js", 43 | "dependencies": { 44 | "@webreflection/lie": "^0.1.1" 45 | }, 46 | "repository": { 47 | "type": "git", 48 | "url": "git+https://github.com/WebReflection/custom-libraries.git" 49 | }, 50 | "bugs": { 51 | "url": "https://github.com/WebReflection/custom-libraries/issues" 52 | }, 53 | "homepage": "https://github.com/WebReflection/custom-libraries#readme" 54 | } 55 | -------------------------------------------------------------------------------- /rollup/babel.config.js: -------------------------------------------------------------------------------- 1 | import {nodeResolve} 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 | nodeResolve(), 9 | babel({ 10 | presets: ['@babel/preset-env'], 11 | babelHelpers: 'bundled' 12 | }) 13 | ], 14 | 15 | output: { 16 | exports: 'named', 17 | file: './index.js', 18 | format: 'iife', 19 | name: 'customLibraries' 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /rollup/es.config.js: -------------------------------------------------------------------------------- 1 | import {nodeResolve} 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 | nodeResolve(), 9 | terser() 10 | ], 11 | 12 | output: { 13 | exports: 'named', 14 | file: './es.js', 15 | format: 'iife', 16 | name: 'customLibraries' 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /test/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 |