简体   繁体   English

在运行时使用require js退出优化代码

[英]Switch out of optimized code with require js at run time

We are considering integrating our (large) web app with require js. 我们正在考虑将我们的(大型)Web应用程序与require js集成在一起。

Currently, we optimize (concatenate and minify) our js files for production. 当前,我们优化(连接和缩小)我们的js文件进行生产。 In our environment at production runtime, we allow for a session level "debug" flag that can be secretly entered via the browser. 在生产运行时的环境中,我们允许可以通过浏览器秘密输入的会话级别“调试”标志。 When that flag is set, the browser loads the non-minified versions of the files (by use of a if/else statement in razor code) for debugging. 设置该标志后,浏览器将加载文件的非最小版本(通过在razor代码中使用if / else语句)进行调试。

Is there a equivalent possible mechanism when working with require js? 使用require js时是否存在等效的可能机制?

In summary; 综上所述; we would like to use optimized js files at runtime using require js. 我们想在运行时使用require js使用优化的js文件。 We would also like the ability to load non-minified files at runtime based on a session state debug flag. 我们还希望能够基于会话状态调试标志在运行时加载未缩小的文件。

thanks in advance, -Marc 在此先感谢,-马克

we would like to use optimized js files at runtime using require js. 我们想在运行时使用require js使用优化的js文件。 We would also like the ability to load non-minified files at runtime 我们还希望能够在运行时加载未缩小的文件

This is best done by using source maps instead. 最好通过使用源映射来完成。 If a source map is present for a minified file, the browser can use it to translate your minified code back into the raw version on the fly without having to have the actual raw code present. 如果存在缩小文件的源映射,则浏览器可以使用它将您的缩小代码即时转换回原始版本,而无需提供实际的原始代码。

Minifiers have the option to generate source maps along with minified code. 缩小器可以选择生成源地图以及缩小的代码。 RequireJS optimizer, afaik, uses Uglify2 which can generate source maps. RequireJS优化器afaik使用Uglify2可以生成源映射。

Source maps are good especially in pre-production environments. 源地图很好,尤其是在预生产环境中。 Should you want to disable this ability, like in production, just don't deploy the source map files. 如果要禁用此功能(例如在生产中),请不要部署源地图文件。

In our environment at production runtime, we allow for a session level "debug" flag that can be secretly entered via the browser. 在生产运行时的环境中,我们允许可以通过浏览器秘密输入的会话级别“调试”标志。

By the way, this is a terrible idea. 顺便说一句,这是一个可怕的想法。 This setup is just overhead and code cruft. 这种设置只是开销和代码繁琐。


If you really insist on loading the raw source for debugging purposes and the compiled version for production, you can simply swap out the compiled script with the entrypoint script of the uncompiled code. 如果您确实要出于调试目的而坚持加载原始源代码,并为生产而坚持使用已编译的版本,则可以简单地将已编译的脚本替换为未编译的代码的入口点脚本。 This assumes you have both versions, raw and compiled, on the server. 假定服务器上同时具有原始版本和编译版本。

In the following example (PHP), assuming main.js is the entrypoint script of the raw source, main.min.js is the compiled version, and $debug is some value telling the server what mode you're using, you can simply do: 在下面的示例(PHP)中,假设main.js是原始源代码的入口点脚本, main.min.js是已编译版本,并且$debug是一些告诉服务器正在使用哪种模式的值,您可以简单地做:

<script src="path/to/require.js"></script>

<? if($debug): ?>
<script src="path/to/main.js"></script>
<? else: ?>
<script src="path/to/main.min.js"></script>
<? endif; ?>

Do note, however, that raw source may act differently from one that's compiled. 但是请注意,原始源的行为可能与已编译的源不同。 Minifiers have the tendency to take unexpected shortcuts and make assumptions on your code. 缩小器倾向于采取意想不到的捷径,并对代码进行假设。 This may cause raw and compiled versions to differ in behavior. 这可能会导致原始版本和编译版本的行为不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM