用假名称替换模块ID [英] Replace module ids with fake names
问题描述
想象一个由三个具有这些ID的模块组成的AMD Java脚本应用程序:
Imagine an AMD Java-Script Application consisting of three modules with those IDs:
- common/core/api
- common/data/foo
- 普通/远程/酒吧
当我使用r.js构建优化文件时,这些ID仍位于编译文件中.海事组织这是不必要的.例如,当我搜索并替换以下内容时,优化后的文件仍然可以使用:
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
- 公共/远程/酒吧-> c
替换的优点是:
- 较小的文件大小
- 晦涩难懂
(是的,您永远不应该信任客户端.但是,替换模块名称将需要反向生成器进行猜测,而不是立即通过模块名称查找应用程序的重要部分.)
- smaller filesize
- 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)
我是否有理由不这样做或没有其他工具/命令将这些ID替换为一些自动生成的ID?
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%.
Barely. I doubt on real projects you'd be able to even get 1% size reduction.
- 更好地掩盖
从本质上讲,这种模糊处理会更好,但是我怀疑一个可以处理最小化代码的人会因为这种额外的模糊处理而感到困惑.
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
未完成的原因:
-
RequireJS仍必须按当前的方式进行操作,因为对于许多用途而言,将模块重命名为任意值是不可接受的. (在所有使用RequireJS的地方都是这种情况.)因此,这将向
r.js
添加其他代码路径,必须对其进行测试.
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.
在某些情况下,RequireJS将无法执行重命名.考虑一下:
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无法跟踪require
调用中的依赖项,因为令牌deps
不是字符串的文字数组.但是,目前可能可以补偿传递给r.js
的配置:a
,b
,c
可以列为显式包含.问题解决了.但是,如果r.js
重命名模块,则必须自动更改名称a
,b
和c
.为此,r.js
必须使用JavaScript解析器来解析代码并对其进行更改.目前尚不清楚在一般情况下能否确定需要更改的内容.
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.
最终,如果更改了r.js
以便在可能的情况下执行此操作,则r.js
会比处理可以满足的相当专业化的需求复杂得多. em>在r.js
之外处理.
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
.
还是用其他工具/命令将这些ID替换为一些自动生成的ID?
or an additional tool/command to substitute those IDs with some auto generated id?
没有工具或配置选项可帮助您执行此操作.理论上,无论r.js
为您做什么,都可以在调用r.js
之前作为构建阶段完成.在将文件传递到r.js
之前,您必须编写自己的工具来执行转换.
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屋!