### Install and Run LWR Project Source: https://lwc.dev/guide/install After scaffolding your LWC project with LWR, navigate to the project directory, install dependencies, and start the development server. ```bash cd YourProjectName npm install npm run start ``` -------------------------------- ### Initialize LWC Project with LWR Source: https://lwc.dev/guide/install Use the npm init command to scaffold a new LWC project with the Lightning Web Runtime. This command guides you through the setup process. ```bash npm init lwr@latest ``` -------------------------------- ### Slot Forwarding Container Setup Source: https://lwc.dev/guide/light_dom Initial setup for passing content into a component for slot forwarding. ```html ``` -------------------------------- ### Create and Register a Custom Element with Shadow DOM Source: https://lwc.dev/guide/third_party_web_components This example shows how to create a custom element, attach a shadow root in open mode, and set its inner HTML. It also demonstrates the LWC component setup to render this custom element. ```javascript // main.js import { createElement } from "lwc"; import App from "x/app"; const elm = createElement("x-app", { is: App }); document.body.appendChild(elm); customElements.define('my-custom-element', class extends HTMLElement { constructor() { super(); this.attachShadow({ mode: 'open' }) .innerHTML = '
I am a third-party web component!
'; } }); ``` -------------------------------- ### Install Lightning Types Package Source: https://lwc.dev/guide/install Install the `@salesforce/lightning-types` package using npm or yarn. ```bash npm install @salesforce/lightning-types # OR yarn add @salesforce/lightning-types ``` -------------------------------- ### Install Jest and LWC Jest Packages Source: https://lwc.dev/guide/test Commands to install the core Jest framework and the LWC-specific presets or individual transformer, resolver, and serializer packages. ```bash npm install jest --save-dev ``` ```bash npm install @lwc/jest-preset --save-dev ``` ```bash npm install @lwc/jest-transformer --save-dev npm install @lwc/jest-resolver --save-dev npm install @lwc/jest-serializer --save-dev ``` -------------------------------- ### Example LWC Compiler Options Object Source: https://lwc.dev/guide/error_codes/lwc1023 This example shows the structure of a typical options object expected by the LWC compiler, including 'name' and 'namespace'. ```javascript const options = { name: 'componentName', // Component name (camelCase, no file extension) such as 'myButton', 'userCard' namespace: 'namespaceValue' // Namespace identifier such as 'c', 'x', or your org namespace }; ``` -------------------------------- ### Install and Create LWC App Source: https://lwc.dev/ Use the lwr tool to install Lightning Web Components and create a new application. Follow the terminal prompts to select project type and variant. ```bash $ npm init lwr $ npm install $ npm run start ``` -------------------------------- ### Corrected Component Folder Structure Source: https://lwc.dev/guide/error_codes/lwc1116 Example of a component folder structure after renaming the folder to start with a lowercase character, resolving the LWC1116 error. ```text contactCard/ ├── contactCard.js ├── contactCard.html ``` -------------------------------- ### Diagnostic Message Examples Source: https://lwc.dev/guide/error_codes/lwc1039 Examples of the LWC1039 error message format. ```text LWC1039: iterator:items directive is expected to be an expression ``` ```text LWC1039: iterator:products directive is expected to be an expression ``` ```text LWC1039: iterator:records directive is expected to be an expression ``` -------------------------------- ### Array of items for list Source: https://lwc.dev/guide/javascript An example array of items to be displayed in a list. ```javascript const items = [ {label : "item1"}, {label : "item2"}, {label : "item3"} ]; ``` -------------------------------- ### Reference Component in Markup Source: https://lwc.dev/guide/reference Example of how to reference a component named myComponent in the example namespace within an HTML template. ```html ``` -------------------------------- ### LWC1011 Error Message Example Source: https://lwc.dev/guide/error_codes/lwc1011 This is an example of the diagnostic message produced when an import path error occurs. ```text LWC1011: Failed to resolve import "./utils" from "myComponent.js". Please add "utils" file to the component folder. ``` -------------------------------- ### Example Diagnostic Message for LWC1102 Source: https://lwc.dev/guide/error_codes/lwc1102 An example of the error message produced by LWC1102, showing the decorator name and the expected import source. ```text LWC1102: Invalid 'api' decorator usage. Supported decorators (api, wire, track) should be imported from "lwc" ``` -------------------------------- ### LWC1011 Error Message Example with Relative Path Source: https://lwc.dev/guide/error_codes/lwc1011 Another example of the LWC1011 error message, demonstrating an import path that navigates up the directory structure. ```text LWC1011: Failed to resolve import "../sharedUtils" from "myComponent.js". Please add "../sharedUtils" file to the component folder. ``` -------------------------------- ### Valid CSS Syntax Example Source: https://lwc.dev/guide/error_codes/lwc1009 This is a corrected version of the CSS syntax error examples, demonstrating valid CSS syntax with proper braces and semicolons. ```css .container { color: red; background: blue; } ``` -------------------------------- ### Example Diagnostic Messages for LWC1024 Source: https://lwc.dev/guide/error_codes/lwc1024 These are examples of how the LWC1024 error message might appear, indicating unexpected file content for specific files. ```text LWC1024: Unexpected file content for "foo.css". Expected a string, received "null". ``` ```text LWC1024: Unexpected file content for "foo.js". Expected a string, received "undefined". ``` ```text LWC1024: Unexpected file content for "foo.html". Expected a string, received "[object Object]". ``` -------------------------------- ### Example Diagnostic Message for LWC1052 Source: https://lwc.dev/guide/error_codes/lwc1052 An example of a diagnostic message that might be logged when LWC1052 occurs. The {0} placeholder is filled with the specific compilation error. ```text Unexpected compilation error: LWC1052: Error parsing attribute: {0} ``` -------------------------------- ### Corrected for:each and for:item usage Source: https://lwc.dev/guide/error_codes/lwc1044 These examples resolve LWC1044 by ensuring both directives are present. ```html ``` ```html ``` ```html ``` -------------------------------- ### Event Handler Starting with Underscore (Error) Source: https://lwc.dev/guide/error_codes/lwc1056 This example shows an LWC template with an event handler attribute that starts with an underscore after the 'on' prefix, causing LWC1056. ```html ``` -------------------------------- ### Event Handler Starting with Number (Error) Source: https://lwc.dev/guide/error_codes/lwc1056 This example shows an LWC template with an event handler attribute that starts with a number after the 'on' prefix, causing LWC1056. ```html ``` -------------------------------- ### Install LWC Rollup Dependencies Source: https://lwc.dev/guide/install Install the necessary npm packages for building LWC applications using Rollup. This includes LWC, Rollup, and related plugins. ```bash npm install --save-dev lwc rollup @lwc/rollup-plugin @rollup/plugin-replace ``` -------------------------------- ### Implement the server abstraction Source: https://lwc.dev/guide/wire_adapter A mock server implementation using a Map to store book data and provide CRUD operations. ```javascript // server.js // A server abstraction class BookEndpoint { bookStore = new Map(); nextBookId = 0; getAll() { return this.bookStore.values() } getById(id) { return this.bookStore.get(parseInt(id)); } create(title) { const book = { id: this.nextBookId++, title, }; this.bookStore.set(book.id, book); } update(id, title) { const book = { id: parseInt(id), title }; this.bookStore.set(book.id, book); } remove(id) { this.bookStore.delete(parseInt(id)); } } export const bookEndpoint = new BookEndpoint(); bookEndpoint.create('The Way of Kings'); ``` -------------------------------- ### Event Handler Starting with Number (Resolution) Source: https://lwc.dev/guide/error_codes/lwc1056 This example demonstrates the correct way to name an event handler attribute in LWC, starting with a lowercase letter after the 'on' prefix. ```html ``` -------------------------------- ### Install LWC Webpack Dependencies Source: https://lwc.dev/guide/install Install the necessary packages for integrating LWC with Webpack. This includes Webpack itself, the LWC Webpack plugin, and LWC core packages. ```bash npm install --save-dev webpack webpack-cli lwc-webpack-plugin lwc @lwc/module-resolver ``` -------------------------------- ### Event Handler Starting with Underscore (Resolution) Source: https://lwc.dev/guide/error_codes/lwc1056 This example demonstrates the correct way to name an event handler attribute in LWC when using underscores, ensuring it starts with a lowercase letter after 'on'. ```html ``` -------------------------------- ### Invalid Public Property Declarations Source: https://lwc.dev/guide/error_codes/lwc1108 Example of a component class triggering LWC1108 due to @api properties starting with 'on'. ```javascript import { api, LightningElement } from 'lwc'; export default class MyComponent extends LightningElement { @api onChange; @api onClick; @api get onLoad() { return this._value; } } ``` -------------------------------- ### Importing an SCSS file for component styles (Error Example) Source: https://lwc.dev/guide/error_codes/lwc1005 This example demonstrates an LWC component attempting to import an SCSS file directly, which causes LWC1005 because the LWC compiler only transforms plain .css files. ```javascript import { LightningElement } from 'lwc'; import styles from './card.scss'; export default class Card extends LightningElement {} ``` ```css /* card.scss */ $primary-color: #0070d2; $border-radius: 8px; .card { border-radius: $border-radius; &__header { background: $primary-color; color: white; } } ``` -------------------------------- ### Shadow DOM Structure Example Source: https://lwc.dev/guide/composition Illustrates the encapsulated structure of components using Shadow DOM, showing the shadow root and its contents. ```text #shadow-root | | #shadow-root | |
Some content in child component
|
``` -------------------------------- ### Invalid Component Folder Structure Source: https://lwc.dev/guide/error_codes/lwc1116 Example of a component folder structure where the folder name starts with an uppercase letter, causing the LWC1116 error. ```text ContactCard/ ├── contactCard.js ├── contactCard.html ``` -------------------------------- ### Unsupported Syntax Errors Source: https://lwc.dev/guide/error_codes/lwc1007 Examples of unsupported JSX syntax and the standard LWC approach. ```javascript import { LightningElement } from 'lwc'; export default class MyComponent extends LightningElement { renderContent() { return
Content
; } } ``` ```javascript import { LightningElement } from 'lwc'; export default class MyComponent extends LightningElement { } ``` -------------------------------- ### Using Dynamic Imports for Internal Components Source: https://lwc.dev/guide/error_codes/lwc1704 This example demonstrates using dynamic imports for custom components, which is a recommended alternative to importing reserved modules. ```javascript // Use standard JavaScript features like dynamic imports if required await import('c/myComponent'); ``` -------------------------------- ### connectStore Wire Adapter Example Source: https://lwc.dev/guide/wire_adapter The `connectStore` wire adapter provisions data to components by subscribing to a store and notifying on state changes. It handles connecting, disconnecting, and updating the store subscription. ```javascript export class connectStore { dataCallback; store; subscription; connected = false; constructor(dataCallback) { this.dataCallback = dataCallback; } connect() { this.connected = true; this.subscribeToStore(); } disconnect() { this.unsubscribeFromStore(); this.connected = false; } update(config) { this.unsubscribeFromStore(); this.store = config.store; this.subscribeToStore(); } subscribeToStore() { if (this.connected && this.store) { const notifyStateChange = () => { const state = this.store.getState(); this.dataCallback(state); }; this.subscription = this.store.subscribe(notifyStateChange); notifyStateChange(); } } unsubscribeFromStore() { if (this.subscription) { this.subscription(); this.subscription = undefined; } } } ``` -------------------------------- ### Diagnostic Message Examples Source: https://lwc.dev/guide/error_codes/lwc1703 Examples of the LWC1703 error message format. ```text LWC1703: Invalid module identifier "@salesforce/user/'-alert(window)-'". ``` ```text LWC1703: Invalid module identifier "@salesforce/user/Name". ``` ```text LWC1703: Invalid module identifier "". ``` -------------------------------- ### Passing an unsupported file to transformSync (Error Example) Source: https://lwc.dev/guide/error_codes/lwc1005 This example shows an error LWC1005 occurring when the `@lwc/compiler`'s `transformSync` function is called with an unsupported file extension, such as '.json'. ```javascript import { transformSync } from '@lwc/compiler'; // Attempting to transform a JSON file const result = transformSync( '{"apiEndpoint": "/api/v1"}', 'config.json', { name: 'config', namespace: 'c' } ); ``` -------------------------------- ### Instantiating a Component for Testing Source: https://lwc.dev/guide/test Use createElement to create an instance of the component under test. ```javascript const element = createElement('example-hello', { is: Hello }); ``` -------------------------------- ### Diagnostic Message Examples Source: https://lwc.dev/guide/error_codes/lwc1095 Examples of the LWC1095 error message format. ```text LWC1095: @wire method or property cannot be used with @api ``` ```text LWC1095: @wire method or property cannot be used with @track ``` -------------------------------- ### Migrate to lwc:is (Recommended) Source: https://lwc.dev/guide/error_codes/lwc1128 This code demonstrates the recommended solution by replacing `lwc:dynamic` with `lwc:is` on a `` element. This requires `enableDynamicComponents: true` in the compiler configuration. ```html ``` ```javascript transformSync(html, filename, { enableDynamicComponents: true, // ... other options }); ``` -------------------------------- ### Diagnostic Message Examples Source: https://lwc.dev/guide/error_codes/lwc1074 Examples of the diagnostic messages produced by LWC1074. ```text Invalid expression {exp1;exp2} - LWC1074: Multiple expressions found ``` ```text Invalid expression {exp;} - LWC1074: Multiple expressions found ``` -------------------------------- ### Resolve LWC1016: Provide multiple component files Source: https://lwc.dev/guide/error_codes/lwc1016 This example demonstrates how to resolve LWC1016 by providing multiple component files (JS, HTML, CSS) within the 'files' object. Each file should have its name as the key and its content as the value. ```javascript import { compile } from '@lwc/sfdc-lwc-compiler'; const jsFilename = 'myComponent.js'; const jsFileContent = ` import { LightningElement } from 'lwc'; export default class MyComponent extends LightningElement {} `; const htmlFilename = 'myComponent.html'; const htmlFileContent = ''; const cssFilename = 'myComponent.css'; const cssFileContent = '.container { padding: 10px; }'; const config = { bundle: { type: 'platform', name: 'myComponent', namespace: 'c', files: { [jsFilename]: jsFileContent, [htmlFilename]: htmlFileContent, [cssFilename]: cssFileContent, }, }, }; const result = await compile(config); ``` -------------------------------- ### Invalid for:item usage examples Source: https://lwc.dev/guide/error_codes/lwc1044 These examples trigger LWC1044 because the for:each directive is missing. ```html ``` ```html ``` -------------------------------- ### Async/Await Errors Source: https://lwc.dev/guide/error_codes/lwc1007 Examples of await usage outside of async functions and their resolution. ```javascript import { LightningElement } from 'lwc'; export default class MyComponent extends LightningElement { fetchData() { const data = await fetch('/api/data'); } } ``` ```javascript import { LightningElement } from 'lwc'; export default class MyComponent extends LightningElement { async fetchData() { const data = await fetch('/api/data'); } } ``` -------------------------------- ### Control Flow Syntax Errors Source: https://lwc.dev/guide/error_codes/lwc1007 Examples of incomplete try-catch blocks and their resolution. ```javascript import { LightningElement } from 'lwc'; export default class MyComponent extends LightningElement { throwError() { try { throw new Error('test'); } } } ``` ```javascript import { LightningElement } from 'lwc'; export default class MyComponent extends LightningElement { handleError() { try { throw new Error('test'); } catch (error) { console.error(error); } } } ``` -------------------------------- ### Invalid for:each usage examples Source: https://lwc.dev/guide/error_codes/lwc1044 These examples trigger LWC1044 because the for:item directive is missing. ```html ``` ```html ``` ```html ``` -------------------------------- ### Light DOM vs Shadow DOM Example Source: https://lwc.dev/guide/composition Demonstrates the distinction between elements in the light DOM and those encapsulated within a component's shadow DOM. ```html

I'm a paragraph element, part of the light DOM

#shadow-root |

| In c-child, I'm light DOM. | To everyone else, I'm shadow DOM. |

``` -------------------------------- ### Component Directory Structure Source: https://lwc.dev/guide/test Recommended file structure for organizing LWC components and their corresponding test files. ```text src ├── modules │ └── foo │ └── todoList │ ├── __tests__ │ │ └── todoList.test.js │ │ └── todoList.ssr-test.js │ ├── todoList.html │ └── todoList.js └── main.js ``` -------------------------------- ### LWC1130 Diagnostic Message Example Source: https://lwc.dev/guide/error_codes/lwc1130 An example of the diagnostic message produced by the LWC1130 error. ```text LWC1130: Invalid lwc:dynamic usage on element "". The directive binding must be an expression. ``` -------------------------------- ### Define the main application template Source: https://lwc.dev/guide/wire_adapter The main entry point for the Book List app, orchestrating child components for creating, listing, and editing books. ```html ``` -------------------------------- ### Nested Iterator Error Examples Source: https://lwc.dev/guide/error_codes/lwc1039 Examples showing LWC1039 in nested iterator structures. ```html ``` ```html ``` -------------------------------- ### LWC1049 Diagnostic Message Example Source: https://lwc.dev/guide/error_codes/lwc1049 Example of the error message format generated by the LWC compiler. ```text LWC1049: Forbidden svg namespace tag found in template: ' ``` ```html ``` ```html ``` ```html ``` ```html ``` -------------------------------- ### Diagnostic Message Examples Source: https://lwc.dev/guide/error_codes/lwc1035 Examples of the LWC1035 error message format showing the raw attribute and the suggested escaped string literal. ```text LWC1035: Ambiguous attribute value title={myValue}checked. If you want to make it a string you should escape it title="\{myValue}checked" ``` ```text LWC1035: Ambiguous attribute value class={value}extra. If you want to make it a string you should escape it class="\{value}extra" ``` ```text LWC1035: Ambiguous attribute value aria-label={label}text. If you want to make it a string you should escape it aria-label="\{label}text" ``` -------------------------------- ### Invalid Attribute Name: Starts with a Digit Source: https://lwc.dev/guide/error_codes/lwc1124 Demonstrates an LWC template with an attribute name starting with a digit, which triggers the LWC1124 error. ```html ``` -------------------------------- ### Using Valid Imports for UI Components Source: https://lwc.dev/guide/error_codes/lwc1704 This example shows a correct import statement for a standard UI API module, which can be used to resolve issues with unrecognized modules. ```javascript // Check for typos in @salesforce module names import { getRecord } from 'lightning/uiRecordApi'; ``` -------------------------------- ### Directory Structure for Missing File Resolution Source: https://lwc.dev/guide/error_codes/lwc1011 Illustrates the directory structure required to resolve a missing file import by adding the necessary utility file. ```text myComponent/ ├── myComponent.js ├── myComponent.html └── utils.js ← Add utils.js or utils.html or utils.css, etc. ``` -------------------------------- ### Error Message Example with lwc:component Source: https://lwc.dev/guide/error_codes/lwc1140 An example of the LWC1140 error message when lwc:inner-html is used on the special element. ```text LWC1140: Invalid lwc:inner-html usage on element "". The directive can't be used on a custom element or special LWC managed elements denoted with lwc:*. ``` -------------------------------- ### LWC1134 Error Message Example Source: https://lwc.dev/guide/error_codes/lwc1134 An example of the LWC1134 error message, indicating invalid attributes on a slot element in a Light DOM template. ```text LWC1134: Invalid attribute(s) 'style' on slot. Slots in Light DOM templates (which have 'lwc:render-mode' directive) can only have [key, lwc:slot-bind, name, slot] attributes ``` -------------------------------- ### LWC1201 Error Message Example Source: https://lwc.dev/guide/error_codes/lwc1201 This is an example of the diagnostic message generated for LWC1201. It indicates that a slot attribute will be ignored due to an ancestor element. ```text LWC1201: The slot attribute in will be ignored due to its ancestor
. It must be a direct child of the containing component. ``` -------------------------------- ### Resolved: Root template with closing tag Source: https://lwc.dev/guide/error_codes/lwc1078 This is the corrected version of the root template example, now including the closing `` tag. ```html ```