Comparing version 0.1.1 to 0.2.0
@@ -10,4 +10,5 @@ /*! (C) 2017 by Andrea Giammarchi */ | ||
} else { | ||
(function CommonJS(filename) { | ||
(function CommonJS(info, el) { | ||
var | ||
filename = info._ || el.getAttribute('data-main'), | ||
exports = {}, | ||
@@ -44,7 +45,13 @@ module = { | ||
function (resolve, reject) { | ||
var xhr = new XMLHttpRequest(); | ||
var | ||
script, | ||
xhr = new XMLHttpRequest(), | ||
html = document.documentElement | ||
; | ||
xhr.open('GET', path, true); | ||
xhr.onerror = reject; | ||
xhr.onload = function () { | ||
resolve(Function( | ||
script = document.createElement('script'); | ||
script.setAttribute('nonce', window.module._nonce); | ||
script.textContent = 'module.$(function(){' + | ||
'var module=' + CommonJS + '(arguments[0]),' + | ||
@@ -54,3 +61,9 @@ 'exports=module.exports;(function(){"use strict";' + | ||
'}.call(exports));return module.exports;' | ||
)(path)); | ||
+ '}(module));'; | ||
window.module._ = path; | ||
window.module.$ = resolve; | ||
setTimeout(function () { | ||
html.removeChild(script); | ||
},1); | ||
html.appendChild(script); | ||
}; | ||
@@ -65,6 +78,7 @@ xhr.send(null); | ||
module._cache = Object.create(null); | ||
module._nonce = el.getAttribute('nonce'); | ||
module.import(filename); | ||
} | ||
return module; | ||
}(document.getElementById('common-js').getAttribute('data-main'))); | ||
}({_:null}, document.getElementById('common-js'))); | ||
} |
/*! (C) 2017 by Andrea Giammarchi */ | ||
"object"==typeof module?module.constructor.prototype.import=function(e){var t=this;return new Promise(function(o){o(t.require(e))})}:!function e(t){var o={},n={filename:t,exports:o,import:function(o){for(var n,r=/^(?:[a-z]+:)?\/\//.test(o),s=r?o:t.slice(0,t.lastIndexOf("/")),c=r?0:o.length,i=0,u=0;i<c;u=i+1)if(i=o.indexOf("/",u),i<0)i=c,s+="/"+o.slice(u),/\.js$/i.test(s)||(s+=".js");else if(0===i)s="";else{for(n=u,u=i;u&&"."===o.charAt(u-1);)--u;switch(i-u){case 0:s+="/"+o.slice(n,i);break;case 1:break;case 2:s=s.slice(0,s.lastIndexOf("/"))}}return window.module._cache[s]||(window.module._cache[s]=new Promise(function(t,o){var n=new XMLHttpRequest;n.open("GET",s,!0),n.onerror=o,n.onload=function(){t(Function("var module="+e+'(arguments[0]),exports=module.exports;(function(){"use strict";'+n.responseText+"}.call(exports));return module.exports;")(s))},n.send(null)}))}};return window.module||(window.module=n,n._cache=Object.create(null),n.import(t)),n}(document.getElementById("common-js").getAttribute("data-main")); | ||
"object"==typeof module?module.constructor.prototype.import=function(e){var t=this;return new Promise(function(n){n(t.require(e))})}:!function e(t,n){var o=t._||n.getAttribute("data-main"),r={},u={filename:o,exports:r,import:function(t){for(var n,r=/^(?:[a-z]+:)?\/\//.test(t),u=r?t:o.slice(0,o.lastIndexOf("/")),c=r?0:t.length,i=0,l=0;i<c;l=i+1)if(i=t.indexOf("/",l),i<0)i=c,u+="/"+t.slice(l),/\.js$/i.test(u)||(u+=".js");else if(0===i)u="";else{for(n=l,l=i;l&&"."===t.charAt(l-1);)--l;switch(i-l){case 0:u+="/"+t.slice(n,i);break;case 1:break;case 2:u=u.slice(0,u.lastIndexOf("/"))}}return window.module._cache[u]||(window.module._cache[u]=new Promise(function(t,n){var o,r=new XMLHttpRequest,c=document.documentElement;r.open("GET",u,!0),r.onerror=n,r.onload=function(){o=document.createElement("script"),o.setAttribute("nonce",window.module._nonce),o.textContent="module.$(function(){var module="+e+'(arguments[0]),exports=module.exports;(function(){"use strict";'+r.responseText+"}.call(exports));return module.exports;}(module));",window.module._=u,window.module.$=t,setTimeout(function(){c.removeChild(o)},1),c.appendChild(o)},r.send(null)}))}};return window.module||(window.module=u,u._cache=Object.create(null),u._nonce=n.getAttribute("nonce"),u.import(o)),u}({_:null},document.getElementById("common-js")); |
{ | ||
"name": "common-js", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "module.exports and module.import for browsers too", | ||
@@ -8,4 +8,6 @@ "main": "common.js", | ||
"test": "node test.js", | ||
"build": "npm run minify; npm run nonce; npm run size;", | ||
"size": "cat common.js | wc -c;cat common.min.js | wc -c;gzip -c common.min.js | wc -c", | ||
"minify": "uglifyjs common.js --comments=all --compress --mangle -o common.min.js" | ||
"minify": "uglifyjs common.js --comments=all --compress --mangle -o common.min.js", | ||
"nonce": "cat common.js | openssl dgst -sha256 -binary | base64 > nonce.txt;cat common.min.js | openssl dgst -sha256 -binary | base64 > nonce.min.txt" | ||
}, | ||
@@ -12,0 +14,0 @@ "keywords": [ |
@@ -8,4 +8,6 @@ # CommonJS + module.import() [![build status](https://secure.travis-ci.org/WebReflection/common-js.svg)](http://travis-ci.org/WebReflection/common-js) | ||
<sub>Yes, it is secure too, check the [CSP enabled page](https://webreflection.github.io/common-js/)!<sub> | ||
### Browser Example | ||
@@ -60,3 +62,19 @@ ```html | ||
### Exporting modules asynchronously | ||
```js | ||
// an async example of /js-browser/test.js content | ||
// for the /js-browser/main.js file nothing changes | ||
module.exports = new Promise(function (resolve) { | ||
setTimeout( | ||
resolve, | ||
1000, | ||
function (message) { | ||
alert(message + '\nfrom ' + module.filename); | ||
} | ||
); | ||
}); | ||
``` | ||
### F.A.Q | ||
@@ -71,5 +89,5 @@ | ||
* **Is there a CDN I can use to test?** | ||
There is always one for npm modules. [https://unpkg.com/common-js@latest](https://unpkg.com/common-js@latest) would do. | ||
There is always one for npm modules. [https://unpkg.com/common-js@latest](https://unpkg.com/common-js@0.2.0/common.min.js) should be already OK. | ||
* **Is this using eval?** | ||
No, it's using `Function`, which is not the same, and it does exactly what a script loading your code would do. Actually, it does something better. It makes the module run on strict by default and it avoids global scope pollution, like a proper module system. | ||
It's using a technique that is compatible with highest security measurements such [Content Security Policy](https://w3c.github.io/webappsec-csp/) | ||
@@ -76,0 +94,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9438
8
80
94