Javascript modules

Last updated on
6 October 2020

What is being done in Drupal 7

Current Drupal JS Workflow (2).png

AMD

What is AMD?

AMD stands for Asynchronous Module Definition, it specifies a mechanism for defining modules such that the module and its dependencies can be asynchronously loaded. To read more about this Javascript specification, go to https://github.com/amdjs/amdjs-api/wiki/AMD
AMD is the browser-friendly version of CommonJS. It will be possible to use both CommonJS and AMD format by the end, it's just a little easier to start with AMD.

Advantages of getting AMD into Drupal

  • Follow a battle-tried module definition and implementation
  • Dependency handling and resolution between scripts
  • Speed up page load: scripts can be loaded asynchronously
  • Reduce JS footprint: only load what you need
  • Make on-demand loading easy
  • Avoid the use of globals

How to get AMD into Drupal?

  • Modify Drupal API (hook_library_info) to handle enough meta data for AMD configuration
  • Add all the informations to library_info for core JS files
  • Use RequireJS as a first implementation of an AMD loader
  • Use AMD-style configuration to replace Drupal.settings
  • Make it pluggable so people don't get stuck with AMD or RequireJS
  • Make a basic PHP version of r.js (NodeJS/Rhino preprocessor/optimizer)

Drupal with AMD and Assetic - The AMD picture.png

Help improve this page

Page status: No known problems

You can: