Last active
April 26, 2024 23:19
-
-
Save dy/ccbfe4ccc36efe172da5 to your computer and use it in GitHub Desktop.
require.js in browser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Require stub for browser. | |
* Prepend this script in head. | |
* Set `data-module="name"` attribute on script tag to define module name to register (or it will be parsed as src file name). | |
* Works only in browsers supporting Object.observe (Chrome with flags) | |
*/ | |
//module/exports changing observer to expose global variables | |
var module = {}; | |
var exports = {}; | |
var modules = {}; | |
var modulePaths = {}; | |
//stupid require stub | |
function require(name){ | |
// console.log('require', name+'.js', modulePaths[name+'.js']) | |
return modules[name] || modules[modulePaths[name]] || modules[modulePaths[name+'.js']]; | |
} | |
Object.observe(module, function(changes){ | |
for (var i = changes.length; i--;){ | |
var change = changes[i]; | |
if (change.name === "exports" && change.type === "add"){ | |
var exports = change.object.exports; | |
var script = document.currentScript; | |
//ignore inline scripts | |
if (!document.currentScript.src) return; | |
//parse module name | |
var moduleName = script.getAttribute('data-module'); | |
if (!moduleName) { | |
moduleName = script.getAttribute('src'); | |
//clear name | |
moduleName = moduleName.split(/[\\\/]/).pop().split('.').shift(); | |
} | |
//save module path | |
modulePaths[script.src] = moduleName; | |
modulePaths[script.getAttribute('src')] = moduleName; | |
//expose module to modules list (in order to get by require) | |
modules[moduleName] = exports; | |
delete module.exports; | |
} | |
} | |
Object.observe(exports, function(){ | |
//TODO: save exports changes to current module | |
}) | |
//TODO: listen to `onbeforescriptexecute` on Firefox. | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment