Sub topic 5
ES modules as the unit of code sharing
This chapter assumes you know JavaScript classes and arrow functions, and that you have used <script> tags in a browser. It covers the fourth web standard LWC builds on: ES modules.
What an ES module is
An ES module is a JavaScript file that declares what it exports and what it imports. Each module has its own scope. A top-level variable in one module is invisible to other modules unless the module exports it.
Before modules, scripts shared one global scope. Two scripts that both defined name would overwrite each other. Modules remove that problem.
Exporting and importing
A module can have named exports and one default export.
// formatters.js
export function shout(text) {
return text.toUpperCase() + '!';
}
export const DEFAULT_NAME = 'World';
export default function greet(name = DEFAULT_NAME) {
return `Hello, ${name}`;
}Another module imports from it by path.
// main.js
import greet, { shout, DEFAULT_NAME } from './formatters.js';
console.log(greet()); // Hello, World
console.log(shout(DEFAULT_NAME)); // WORLD!Default imports take any local name, and named imports must match the exported name. A browser loads a module graph only when the page uses <script type="module">.
<script type="module" src="./main.js"></script>Module code runs once, in strict mode. Every importer shares the same module instance.
How LWC uses modules
Every LWC component's JavaScript file is an ES module. Its default export is the component class. The framework imports LightningElement from the special lwc module.
// helloGreeting.js
import { LightningElement } from 'lwc';
export default class HelloGreeting extends LightningElement {
name = 'World';
}The lwc import is not a file you write. The compiler and runtime resolve it for you. Decorators such as api also come from this import.
You can share code between components by putting it in a plain module and importing it.
// utils.js
export function capitalize(text) {
return text.charAt(0).toUpperCase() + text.slice(1);
}// profileCard.js
import { LightningElement } from 'lwc';
import { capitalize } from 'c/utils';
export default class ProfileCard extends LightningElement {
name = capitalize('ada');
}On Salesforce, c/utils names the utils module in the default c namespace. This is an LWC convention. A plain browser needs a file path such as ./utils.js.
Common errors
- Forgetting the file extension in a plain browser import.
./formattersfails, and./formatters.jsworks. - Importing a named export without braces, or a default export with braces.
- Opening a module page from
file://. Browsers block module loading there, so serve the files over HTTP. - Creating a circular import where two modules need each other at load time. One of them sees an uninitialized value.
Exercise
A module exports export default class Timer {} and export const LIMIT = 10;. Write the single import line that gets both into another module.
Show answer
import Timer, { LIMIT } from './timer.js'; The default export has no braces, and the named export goes inside braces.