RequireJS load string

后端 未结 3 1411
隐瞒了意图╮
隐瞒了意图╮ 2021-02-10 01:47

In my app there are dynamic parts that are loaded from database as string that looks like:

\"define([\'dependency1\', \'dependency2\'], function(){\"+
\"   // fu         


        
3条回答
  •  被撕碎了的回忆
    2021-02-10 02:22

    This is quite late, but I just post my solution here in case anyone needs.

    So I ended up asking in requireJS forum and examining the source of text! plugin and json! plugin. The cleanest way to load module from String in RequireJS is by making your own plugin to load the String, and then use onLoad.fromText() that will eval your String and resolve all dependencies.

    Example of my plugin (let's call it db! plugin):

    define([], function(){
        var db = new Database(); // string is loaded from LocalStorage
        return {
            load: function(name, req, onLoad, reqConfig){
                db.get(name, function(err, scriptString){
                    if (err) onLoad(err);
                    else onLoad.fromText(scriptString);
                });  
             }
        }
    });
    

    You can then use the plugin like:

    require(["jquery", "db!myScript"], function($, myScript){        
        // jQuery, myScript and its dependencies are loaded from database
    });
    

    Note:

    1. There's no way to require() from String without eval. This is what onLoad.fromText() does internally. Since eval is evil, you should only use it if you know what String you're going to eval(). If you're using it in browser extension, you might want to relax the CSP policy.
    2. To name your String module, you can use explicit naming syntax. This way, your module will always have the same absolute name.

提交回复
热议问题