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
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 importsWhat each call binds
| Call | Binds |
|---|---|
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:
| Order | Source |
|---|---|
| 1 | A function or variable the file declares |
| 2 | The last importClass with that name |
| 3 | The last importScript that brings a function of that name |
| 4 | The imported packages that have the class. Two packages make it ambiguous, as Rhino stops on it |
| 5 | The 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:
| Path | Looks in |
|---|---|
app_core:cart/CartUtils.ds | The app_core cartridge. The name matches in any case |
cart/CartUtils.ds | Only the calling script's cartridge |
/cart/CartUtils.ds, ./cart/CartUtils.ds | The 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:
| Item | Reads as | Order |
|---|---|---|
A class of an importClass call | BasketMgr (dw.order) | First |
A function of an importScript call | getTotal (scripts/cart/CartUtils.ds) with app_core on the right, inserted with its parentheses | Second |
A class of an importPackage call | HashMap (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
Navigation and usages
| Where | Shows | Click |
|---|---|---|
Gutter icon on an importScript call | Imports getTotal, unused +2 from cart/CartUtils.ds | Imported Functions, the functions the call brings, also those of the scripts it imports |
| Gutter icon on a function of an imported script | Used in Calc.ds, Other.ds +1 via importScript | Importers Using getTotal, the scripts and templates that call it |
| Code Vision above the same function | used in 2 scripts, 1 template via importScript | The 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 on | Shows |
|---|---|
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." |
importScript | The script below cartridge/scripts and the functions it brings, with the file of each one that comes from a script it imports |
| An imported name | The 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:
| Color | Paints | Default |
|---|---|---|
| Import call | importPackage, importClass and importScript | The global function color |
| Imported name | A name an import call binds | Empty, so imported names look like other names until you set it |
| importScript path | The path string | The 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:
| Fix | Writes |
|---|---|
| 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:
| Before | After |
|---|---|
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 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