Replace module ids with fake names

白昼怎懂夜的黑 提交于 2019-12-02 11:22:52

The advantages of the substitutions are:

  1. smaller filesize

Barely. I doubt on real projects you'd be able to even get 1% size reduction.

  1. better obscurity

The obscurity would be nominally better but I doubt someone who would be okay dealing with minified code would be fazed by this additional obfuscation.

Is there a reason I'm missing why this isn't done

The reasons this isn't done:

  1. RequireJS would still have to operate as it currently does because for many uses, renaming modules to arbitrary values is not acceptable. (It is the case everywhere I use RequireJS.) So this would add additional code paths to r.js, that would have to be tested.

  2. There would be cases where RequireJS would be flat out incapable of performing the renaming. Consider this:

    define(function (require) {
    var deps = ['a', 'b'];
    if (some_condition)
        deps.push('c');
    require(deps, function () { ... });
    });
    

    RequireJS is unable to trace the dependencies in the require call because the token deps is not a literal array of strings. However, it is currently possible to compensate in the configuration passed to r.js: a, b, c can be listed as explicit inclusions. Problem solved. However, if r.js renames modules, the names a, b, and c would have to be automatically altered. To do this, r.js would have to use a JavaScript parser to parse the code and alter it. It is unclear that in the general case it would be able to figure out what it needs to alter.

  3. Ultimately, if r.js were altered to do it when it can, it would make r.js much more complex than it is in order to deal with a rather specialized need, which can be handled outside r.js.

or an additional tool/command to substitute those IDs with some auto generated id?

There is no tool or configuration option to do this for you. Whatever r.js would do for you could in theory be done as a build phase before r.js is invoked. You'd have to write your own tool to perform the transformation before passing the files to r.js.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!