Import Calls

importPackage, importClass and importScript in Digital Script, what each call binds, script paths across cartridges, completion, navigation and require

importPackage, importClass and importScript are how Rhino scripts bring in names. The plugin binds them as the server does, in .ds files, server .js files and templates, and follows every name back to its class or script

Completion in an importScript path lists the scripts of this cartridge as written and those of every linked cartridge with a cartridge: prefix. Once the path resolves, its functions are bound and the gutter shows what the call imports

What each call binds

CallBinds
importPackage(dw.util)Every class of the package by its simple name, such as HashMap
importClass(dw.order.BasketMgr)One class, BasketMgr
importScript('app_core:cart/CartUtils.ds')The top-level functions of the script, and those of the scripts it imports. Its variables, classes, nested functions and module.exports members aren't imported

An import call binds for the whole file, wherever it stands, also inside a function. It runs where it stands, though, so top-level code that uses a name before the call fails at runtime, see Inspections

Each file is its own scope. A script's own import calls don't reach the scripts that import it, so every script imports the packages it uses

Which name wins

An unqualified name resolves in this order:

OrderSource
1A function or variable the file declares
2The last importClass with that name
3The last importScript that brings a function of that name
4The imported packages that have the class. Two packages make it ambiguous, as Rhino stops on it
5The ES library and other globals

So Iterator after importPackage(dw.util) is dw.util.Iterator, not the ES iterator. Qualified names such as dw.util.HashMap, and files without import calls, resolve as plain JavaScript. this.importPackage(dw.util) counts as an import call too

Script paths

importScript looks for the script under cartridge/scripts:

PathLooks in
app_core:cart/CartUtils.dsThe app_core cartridge. The name matches in any case
cart/CartUtils.dsOnly the calling script's cartridge
/cart/CartUtils.ds, ./cart/CartUtils.dsThe same as cart/CartUtils.ds. A \ counts as /

B2C Commerce needs the file name with its extension. A path that resolves only without it is a warning with an Add '.ds' fix, and a path that names no script is an error with a fix that creates it, see Inspections

Completion inside the quotes lists the scripts of the calling cartridge as written, and the scripts of every linked cartridge with their cartridge: prefix. A .ts source shows under the .js name it deploys as, and node_modules is skipped

Renaming a script updates the last segment of every path that names it. Moving it keeps the prefix, and adds one when the script leaves the caller's cartridge. Pipeline ScriptFile values follow the same way, see Pipelets & Script Nodes

Completion

Completion offers the names the import calls bind. Names the file declares come from the IDE's own completion:

ItemReads asOrder
A class of an importClass callBasketMgr (dw.order)First
A function of an importScript callgetTotal (scripts/cart/CartUtils.ds) with app_core on the right, inserted with its parenthesesSecond
A class of an importPackage callHashMap (dw.util)Third

Any item for a function, variable or member declared in another file of a linked cartridge shows that cartridge on the right, with the icon of its kind: a cartridge, a B2C module or the global modules folder. Its type follows the parameters, appendQs(url: string, key: string, value: string): string, and the path in the cartridge, (scripts/util/urls.js), replaces the shortened file path. This needs a license or trial

WhereShowsClick
Gutter icon on an importScript callImports getTotal, unused +2 from cart/CartUtils.dsImported Functions, the functions the call brings, also those of the scripts it imports
Gutter icon on a function of an imported scriptUsed in Calc.ds, Other.ds +1 via importScriptImporters Using getTotal, the scripts and templates that call it
Code Vision above the same functionused in 2 scripts, 1 template via importScriptThe same list. With one importer, its file opens
  • Cmd+Click (Ctrl+Click) on a path opens the script, and on an imported name the class or function
  • Find Usages (Alt+F7) and Rename (Shift+F6) of an imported function reach every script and template that imports its file, also through other scripts
  • An imported function isn't reported as unused while its file has importers
  • Call Hierarchy lists the callers in importing scripts and in template functions

Lenses and gutter icons show for scripts under cartridge/scripts of a linked cartridge, and need a license. Turn the lens off in Settings › Editor › Inlay Hints › Code vision › Digital Script, and the icons in Settings › Editor › General › Gutter Icons › importScript imported functions and Function used through importScript

Quick documentation

Quick documentation (F1, Ctrl+Q) explains each call. Every page ends with "An import call binds for the whole file, wherever it stands."

Caret onShows
importPackage"Binds every class of dw.util by its simple name.", and the package's classes, up to 12
importClass"Binds BasketMgr to dw.order.BasketMgr."
importScriptThe script below cartridge/scripts and the functions it brings, with the file of each one that comes from a script it imports
An imported nameThe class or function page with a line such as "via importPackage(dw.util)". Hovering shows getTotal via importScript('cart/CartUtils.ds')

Colors

Settings › Editor › Color Scheme › Digital Script › Imports:

ColorPaintsDefault
Import callimportPackage, importClass and importScriptThe global function color
Imported nameA name an import call bindsEmpty, so imported names look like other names until you set it
importScript pathThe path stringThe string color

Missing imports

A Script API class used without an import is an error, 'HashMap' is a class of dw.util that this script does not import. Its fixes go after the last top-level import call, or before the first statement:

FixWrites
Add importPackage(dw.util)importPackage(dw.util);
Add importClass(dw.util.HashMap)importClass(dw.util.HashMap);
Use require('dw/util/HashMap')var HashMap = require('dw/util/HashMap');

Replace with require

Replace the import call with require (Alt+Enter on a top-level import call) moves a script to modules:

BeforeAfter
importClass(dw.util.HashMap);var HashMap = require('dw/util/HashMap');
importPackage(dw.util);One require per class the file uses and no other call binds, such as var ArrayList = require('dw/util/ArrayList');
importScript('app_core:cart/CartUtils.ds');var CartUtils = require('~/cartridge/scripts/cart/CartUtils');, and each getTotal(...) becomes CartUtils.getTotal(...)

The library gets the module.exports.getTotal = getTotal; lines it lacks. While other scripts still import it with importScript, those lines are wrapped in if (typeof module !== 'undefined') { ... }. A script in another cartridge is required as app_custom/cartridge/scripts/lib/Tax

It's not offered for a package the file doesn't use, a name the file declares, a call inside a function, a .ts library, a script that imports itself, a file that other scripts import, or a script whose used functions come from the scripts it imports. In templates, ${getTotal(basket)} becomes ${CartUtils.getTotal(basket)}

Templates

In <isscript>, import calls at the top level of the block bind their names from the call to the end of the template. importScript functions resolve as in scripts, and the path and argument checks run in templates too, see Scripting in ISML

TypeScript sources

Import calls bind nothing in a server .ts file. importPackage, importClass or importScript in a server .ts file reports them, and the fix writes ES imports:

Import the classes of dw.util the file uses
import ArrayList from 'dw/util/ArrayList';
import HashMap from 'dw/util/HashMap';

importScript has no fix: move the functions into a module and import it. Client .ts and .d.ts files aren't checked

On this page