UNPKG

curl-amd

Version:

curl.js is small, fast, extensible module loader that handles AMD, CommonJS Modules/1.1, CSS, HTML/text, and legacy scripts.

79 lines (59 loc) 1.7 kB
<!DOCTYPE HTML> <html> <head> <title>undefined module loads once</title> <script> curl = { baseUrl: '', paths: { curl: '../src/curl' } }; </script> <script src="../src/curl.js" type="text/javascript"></script> <script type="text/javascript"> curl(['curl/_privileged', 'tdd/MethodSpy', 'domReady!'], function (priv, MethodSpy) { // override curl's insertScript function with an injected spy var core = priv.core, origLoadScript = core.loadScript, Promise = priv.Promise, module1 = 'stuff/undefined-module'; function runTest (moduleId, howMany) { var spy = new MethodSpy('loadScript', core), promise = new Promise(), count = 2; function countdown () { if (--count == 0) { write((spy.calledMany(howMany || 0) ? 'SUCCESS' : 'FAILED') + ': should load ' + moduleId + ' exactly once'); priv.core.loadScript = origLoadScript; promise.resolve(); } } curl([moduleId], function (whatevs) {}).then( countdown, function (ex) { write('FAILED: on ' + moduleId + ' exception:' + ex); } ); curl([moduleId], function (whatevs) {}).then( countdown, function (ex) { write('FAILED: on ' + moduleId + ' exception:' + ex); } ); return promise; } window.runTest = runTest; window.module1 = module1; runTest(module1, 1); }); function write (msg) { document.body.appendChild(document.createElement('div')).innerHTML = msg; } </script> </head> <body> <button onclick="runTest(window.module1);">press this button to load the local module again</button> <button onclick="curl('curl/_privileged',console.log.bind(console));">dump cache to console</button> </body> </html>