What is the most reliable way to get security like closure compiler with AMD (requirejs)?

Although JavaScript and its many libraries (jQuery, RequireJS ) allow you to create many great sites, I find its lack of security to be tiring when planning to create a larger website.

Google has a great closure compiler that lets you annotate your JavaScript with JSDoc and check its type. Having experimented with its rich type system, I expect this to greatly improve the maintainability of a longer-running JavaScript project.

The only problem is that it does not work very well with AMD libraries like RequireJS. There is an experimental flag - transform_amd_modules , which combines your JavaScript files and processes the scope, eliminating it. However, this is similar to an anti-pattern that eliminates most of the benefits of RequireJS (but preserves the modular file structure). There is also the question of what future support will receive.

For the ultimate goal, is security like RequireJS's low cost benefits, what will be my best bet?

PS: Although I used RequireJS as an AMD library of my choice, I would not mind a solution that worked with another AMD library.

+8
javascript type-safety requirejs google-closure-compiler
source share
1 answer

Although the Closure Compiler can be used with various JavaScript libraries, the greatest advantage is achieved when used with the Closure Library , which has its own module and dependency system. Web applications that use the Closure library are usually compiled into a single JavaScript file, which does not require loading an asynchronous module. However, Closure provides support for modules that can be loaded asynchronously. Chad Killingsworth served as a good example of how to configure modules here . In addition, the plovr tool provides add -on module support to simplify their use.

If you decide to use the Closure compiler (and you are not tied to a large AMD codebase), you are likely to maximize performance by adopting the Closure library and its module / dependency system in your new code. If you plan to use the AMD codebase, then as you mentioned, the new Compiler experimental flags --transform_amd_modules and --process_common_js_modules can help, but if you use human libraries, you will miss most of the compiler features.

Looking to the future, if ECMAScript Harmony Modules become an official standard, then it is likely that libraries such as Closure, Dojo, and YUI will eventually conform to this standard. Ultimately, this will make it easy to integrate JavaScript code from disparate libraries. At the same time, if you want to write JavaScript applications and use type checking, eliminating dead codes, minimizing, advanced memory management, browser agnosticism and a phenomenal standard library, I highly recommend using the Closure compiler in combination with the Closure Library.

+10
source share

All Articles