Export external JavaScript libraries

Sébastien Bihan, modified 2 Years ago. New Member Posts: 17 Join Date: 11/7/12 Recent Posts

Hello, 

I would like to export some javascript functions from a module to use it in portlet. In 7.1 version, it wad possible do "addModule" as an AMD module and to use it in portlet by "require" aui for example. 

I am currently upgrading my portatl in 7.4 and "addModule" does not exist anymore :

Previously, in my config.js and my-library.js :

Liferay.Loader.addModule({
    dependencies: [],
    name: 'jquery',
    anonymous: true,
    path: MODULE_PATH + 'my-library.js'
});
// Assuming your "module" will be exported as "my-library"
(function (root, factory) {
    if (typeof Liferay.Loader.define === 'function' && Liferay.Loader.define.amd) {
        // AMD. Register as a "named" module.
        Liferay.Loader.define('my-library', [], factory);
    } else if (typeof module === 'object' && module.exports) {
        // Node. Does not work with strict CommonJS, but
        // only CommonJS-like environments that support module.exports,
        // like Node.
        module.exports = factory();
    } else {
        // Browser globals (root is window)
        root['my-library] = factory();
    }
}(this, function () {

    myFunction= function () {
        console.log('call my function');
    }
 
}));

 

Now, i've tried to do it with liferay-npm-bundler but i've not succeeded to expose my library with this method. If someone as already done a similar module, it could help me. 

Thanks !
 

thumbnail
Olaf Kock, modified 2 Years ago. Liferay Legend Posts: 6441 Join Date: 9/23/08 Recent Posts

As you're in a portlet, you can set com.liferay.portlet.footer-portal-javascript or com.liferay.portlet.header-portal-javascript in the @Component's properties (or in portlet.xml - check this mapping)