-
-
Save pvdz/5128700 to your computer and use it in GitHub Desktop.
| // using requirejs 2.1.5 | |
| // see second example after http://requirejs.org/docs/plugins.html#apiload | |
| define({ | |
| load: function (name, req, onload, config) { | |
| if (name.slice(0,7) === 'js/user') { | |
| var parse = function(err, source){ | |
| if (err) return console.error(err), onload.error(err); // error ajax? | |
| // process source | |
| var par = toTree(source); | |
| translate(par.tok.btree); | |
| source = par.tok.tokens.map(function(t){ return t.value; }).join(''); | |
| // now load it. somehow. properly | |
| onload.fromText(source); | |
| //require(['foo'], onload); // would work, but not with `name`... ? also, examples say that's for older r.js version | |
| }; | |
| // use local storage is available, otherwise ajax it | |
| var source = localStorage.getItem(name); | |
| if (source) parse(null, source); | |
| else get(name+'.js', parse); | |
| } else { | |
| // let requirejs do it's normal thing | |
| req([name]); | |
| } | |
| } | |
| }); |
fromText is bound to 'foo' if orig dep is 'plug!foo'. So the text should not have a named module?
That's a little confusing to me.
Those user files define their own name. Additionally they are stored in a file, with the same name. So if there's a script in js/src/user/main.js it will be defined as
define('js/src/user/main.js', [], function(){ ... });
(and in my environment, it'll actually be defined as just main.js, I rewrite the paths in before passing them off to requirejs).
Is the problem I'm facing related to defined name vs file name?
Ah yes, that would be the problem, the named ID. Also, using IDs that end in '.js' are treated like URLs and are not given the normal module ID treatments and transforms so they are discouraged.
Actually, it may work if the module ID is the same value as the resourceId part of pluginId!resourceId that was used to load it (as long as it is not a .js URL), but it is really best to keep the anonymous if you can.
Ah, I didn't change the name in define(<name>, ...) before passing it back to requirejs. Seems that was indeed the issue. Thanks for your time! :)
Burke: I'm not sure whether
.loadis the better place.I basically need to do three things; localStorage override, path rewriting, and source rewriting. It seems to me like the plugin would/could allow me to do this ubiquitously (oh scrabble...).
The use case (for the global picture) is that of user scripts inside some engine, that run on the same require instance, but in their own namespace. Except I don't want to explicitly force namespaces on the user scripts, partially because I'm not quite sure that'll make my life (or that of the engine) easier.
The rewriting is simply for custom JS syntactic sugar.
The localStorage is (kind of obvious I guess) so the user can edit their scripts in their favorite IDE, or override them with a simple inline browser editor, stored in localStorage.
Basically, the above worked when I was messing wit hthat
requireline on #16, using 'foo', when there was a filejs/src/user/foo.js. But I can't get that stable. It also refuses to just acceptnameas the first parameter (in an array). No idea why. Surely I'm missing something here. But the docs say that thefromTextcall should suffice, so... yeah :/