用假名替换模块 ID [英] Replace module ids with fake names

查看:25
本文介绍了用假名替换模块 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一个 AMD Java-Script 应用程序由三个具有这些 ID 的模块组成:

  • common/core/api
  • common/data/foo
  • 通用/远程/条形

当我使用 r.js 构建优化文件时,这些 id 仍在编译文件中.IMO 这是不必要的.例如,当我搜索并替换以下内容时,优化的文件仍然可以工作:

  • common/core/api -> 一个
  • common/data/foo -> b
  • common/remote/bar -> c

替换的优点是:

  1. 较小的文件大小
  2. 更好的隐蔽性
    (是的,您永远不应该相信客户端.不过,替换模块名称需要逆向工程师进行猜测,而不仅仅是通过模块名称立即找到应用程序的重要部分)

是否有我想念为什么没有完成的原因,或者有一个额外的工具/命令可以用一些自动生成的 ID 替换这些 ID?

解决方案

替换的优点是:

  1. 较小的文件大小

勉强.我怀疑在实际项目中,您甚至能够缩小 1% 的尺寸.

<块引用>

  1. 更好的默默无闻

名义上的模糊性会更好,但我怀疑可以处理缩小代码的人会被这种额外的混淆所困扰.

<块引用>

是否有我想念为什么没有完成的原因

未完成的原因:

  1. RequireJS 仍然必须像目前那样运行,因为对于许多用途,将模块重命名为任意值是不可接受的.(在我使用 RequireJS 的任何地方都是这种情况.)所以这会向 r.js 添加额外的代码路径,这将需要进行测试.

  2. 有时 RequireJS 会完全无法执行重命名.考虑一下:

    define(function (require) {var deps = ['a', 'b'];如果(some_condition)deps.push('c');需要(deps,函数(){ ... });});

    RequireJS 无法跟踪 require 调用中的依赖关系,因为标记 deps 不是字符串的文字数组.然而,目前可能补偿传递给r.js的配置:a, b, c 可以作为显式包含列出.问题解决了.但是,如果 r.js 重命名模块,名称 abc 将必须自动改变了.要做到这一点,r.js 必须使用 JavaScript 解析器来解析和修改代码.不清楚在一般情况下它是否能够弄清楚它需要改变什么.

  3. 最终,如果 r.js 被改变以在可能的时候执行它,它会使 r.js 比它更复杂,以便处理相当特殊的需求,可以r.js 之外处理.

<块引用>

或者使用其他工具/命令将这些 ID 替换为一些自动生成的 ID?

没有工具或配置选项可以为您执行此操作.无论 r.js 为您做什么,理论上都可以在调用 r.js 之前作为构建阶段完成.在将文件传递给 r.js 之前,您必须编写自己的工具来执行转换.

Imagine an AMD Java-Script Application consisting of three modules with those IDs:

  • common/core/api
  • common/data/foo
  • common/remote/bar

When I'm building the optimized file with r.js, those ids are still inside compiled file. IMO this is unnecessary. For example, the optimized file would still work when I search-and-replace the following:

  • common/core/api -> a
  • common/data/foo -> b
  • common/remote/bar -> c

The advantages of the substitutions are:

  1. smaller filesize
  2. better obscurity
    (Yes, you should never trust the client. Still, substituting the module names would require a reverse engeneer to guess a lot more than finding imporant parts of the application right away through module names)

Is there a reason I'm missing why this isn't done or an additional tool/command to substitute those IDs with some auto generated id?

解决方案

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.

这篇关于用假名替换模块 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆