Custom Modules Frequently Asked Questions
When should I use require versus define?
Always use the define Object in entry point scripts and when creating new modules. Use the require Function for loading and using existing modules.
There is a performance advantage to using the require Function. Generally, give preference to using the require Function whenever you can. The define Object will imports all dependencies. The require Function loads dependencies only as they are needed.
Here is a summary of when to use or not use the define Object and require Function:
Object/Function |
When to use |
Caveat |
define Object |
|
If you use the define Object, you do not receive the performance benefit of progressive loading. |
require Function |
|
If you use the require Function, you may need to import a define that provides correct context. For example, to import a custom module with a relative path:
|
Are third-party libraries supported?
Yes. You can define the library as a custom module, set up a global identifier to reference it, and configure it as a dependency. See Import a third-party library and Import a third-party JavaScript library.