Modularized JavaScript code is a specification for the JavaScript language called Asynchronous Module Definition, or AMD. The Liferay AMD Module Loader is the native loader that you can use to load your AMD modules. This tutorial covers how to use the Liferay AMD Module Loader.
Configuring Your AMD Module for the Loader
Follow these steps to prepare your module:
-
Wrap your AMD module code with the
Liferay.Loader.define()
method, such as the one shown below:Liferay.Loader.define('my-dialog', ['my-node', 'my-plugin-base'], function(myNode, myPluginBase) { return { log: function(text) { console.log('module my-dialog: ' + text); } }; });
-
You can modify the configuration to load the module when another module is triggered or when a condition is met. The configuration below specifies that this module should be loaded if the developer requests the
my-test
module:Liferay.Loader.define('my-dialog', ['my-node', 'my-plugin-base'], function(myNode, myPluginBase) { return { log: function(text) { console.log('module my-dialog: ' + text); } }; }, { condition: { trigger: 'my-test', test: function() { var el = document.createElement('input'); return ('placeholder' in el); } }, path: 'my-dialog.js' });
The Liferay AMD Loader uses the definition, along with the listed dependencies, as well as any other configurations specified, to create a
config.json
file. This configuration object tells the loader which modules are available, where they are located, and what dependencies they require. Below is an example of a generatedconfig.json
file:{ "frontend-js-web@1.0.0/html/js/parser": { "dependencies": [] }, "frontend-js-web@1.0.0/html/js/list-display": { "dependencies": ["exports"] }, "frontend-js-web@1.0.0/html/js/autocomplete": { "dependencies": ["exports", "./parser", "./list-display"] } }
-
Load your module in your scripts. Pass the module name to the
Liferay.Loader.require
method. The example below loads a module calledmy-dialog
:Liferay.Loader.require('my-dialog', function(myDialog) { // your code here }, function(error) { console.error(error); });