Lynx
Lynx

Reputation: 51

External library in DW

I'm new to demandware. What I'm trying to do is add an external library to use in DW Controllers.

in short: How to use an external library as lodash in demandware controller?

Upvotes: -1

Views: 325

Answers (3)

Antonio Marin
Antonio Marin

Reputation: 11

You can use cartridge_lib_lodash based on lodash npm node module version 3.10.1.

This cartridge library is a babel transpilation to ES5 with some minor changes in order to be usable as a standard SFRA cartridge.

It can be installed as a standard SFRA cartridge cloning the repository and running npm script uploadCartridge


$ git clone [email protected]:pikamachu/pika-cartridge-lib-lodash.git
$ cd pika-cartridge-lib-lodash
$ npm run uploadCartridge

Or can be added as a node module dependency to an existing SFRA cartridges project using


$ npm i cartridge_lib_lodash

Lodash modules can be loaded using require cartridge as a standard SFRA script.


// lodash modules can be loaded separately using
var _array = require('*/cartridge/scripts/lib/lodash/array');
var _chain = require('*/cartridge/scripts/lib/lodash/chain');
var _collection = require('*/cartridge/scripts/lib/lodash/collection');
var _date = require('*/cartridge/scripts/lib/lodash/date');
var _function = require('*/cartridge/scripts/lib/lodash/function');
var _lang = require('*/cartridge/scripts/lib/lodash/lang');
var _math = require('*/cartridge/scripts/lib/lodash/math');
var _number = require('*/cartridge/scripts/lib/lodash/number');
var _object = require('*/cartridge/scripts/lib/lodash/object');
var _string = require('*/cartridge/scripts/lib/lodash/string');
var _support = require('*/cartridge/scripts/lib/lodash/support');
var _utility = require('*/cartridge/scripts/lib/lodash/utility');

See lodash docs documentation for module usage.

Upvotes: 0

Nikolaos Georgiou
Nikolaos Georgiou

Reputation: 2874

Demandware is using the Rhino engine under the hood, which typically isn't compatible with the latest libraries. To use a third party library in Demandware, try these steps:

  1. Create a new blank cartridge
  2. Copy paste the library's code
  3. Try to use it. If it is a really simple library it might work out of the box. Most likely however, you'll have to make code adjustments. Dive into the error logs to see what goes wrong.
  4. If the library has dependencies, you'll need to fix those as well.

Upvotes: 0

Oleg Sapishchuk
Oleg Sapishchuk

Reputation: 657

To able to use lodash in demandware scripts(controller for example) you need to make some adjustments. For reference please check how underscore was adapted and used like another cartridge in your project. Example of usage for underscore will be:

const map = require('underscore/map');
...
return map(basket.productLineItems, function (productLineItem) {
   return productLineItem.getQuantity().getValue();
});
...

So you will need to create cartridge from lodash sources and use it with module require approach.

Upvotes: 0

Related Questions