1 | # LWC Compiler
|
2 |
|
3 | @lwc/compiler is an open source project that enables developers to take full control of processing a single Lightning Web Components module for runtime consumption.
|
4 |
|
5 | ## Installation
|
6 |
|
7 | ```sh
|
8 | yarn add --dev @lwc/compiler
|
9 | ```
|
10 |
|
11 | ## APIs
|
12 |
|
13 | ### `compile`
|
14 |
|
15 | ```js
|
16 | import { compile } from '@lwc/compiler';
|
17 |
|
18 | const options = {
|
19 | name: 'foo',
|
20 | namespace: 'x',
|
21 | files: {
|
22 | 'foo.js': `
|
23 | import { LightningElement } from 'lwc';
|
24 | export default class Foo extends LightningElement {}
|
25 | `,
|
26 | 'foo.html': `<template><h1>Foo</h1></template>`,
|
27 | },
|
28 | };
|
29 |
|
30 | const {
|
31 | success,
|
32 | diagnostics,
|
33 | result: { code },
|
34 | } = await compile(options);
|
35 |
|
36 | if (!success) {
|
37 | for (let diagnostic of diagnostics) {
|
38 | console.log(diagnostic.level + ':' + diagnostic.message);
|
39 | }
|
40 | }
|
41 |
|
42 | return code;
|
43 | ```
|
44 |
|
45 | **Parameters:**
|
46 |
|
47 | - options (object) - the object which specifies compilation source and output shape
|
48 | - `name` (string, required) - component name.
|
49 | - `namespace` (string, required) - component namespace.
|
50 | - `files` (BundleFiles, required) - key value pairs where each key is one of the bundle files and its value is a string value of the file content.
|
51 | - `baseDir` (string, optional) - An optional directory prefix that contains the specified components. Only used when the component that is the compiler's entry point.
|
52 | - `stylesheetConfig` (StylesheetConfig, optional) - css configuration.
|
53 | - `outputConfig` (OutputConfig, optional) - compiler output configuration. Dictates the shape of the bundle output (ex: bundle compiled for production mode, minified).
|
54 |
|
55 | **Return**
|
56 |
|
57 | - output (object) - the object with the following fields:
|
58 | - `success` (boolean) - compilation results (true only if all compilation steps were successful).
|
59 | - `diagnostics` (Diagnostic[]) - an array of compilation `Diagnostic` objects (ex: warnings, errors)
|
60 | - `result` (BundleResult) - an object containing compiled code and configuration used during compilation;
|
61 | - `version` (string) - the version of compiler used for current compilation.
|
62 |
|
63 | ### `transform`
|
64 |
|
65 | Transform the content of individual file for manual bundling.
|
66 |
|
67 | ```js
|
68 | import { transform } from '@lwc/compiler';
|
69 |
|
70 | const source = `
|
71 | import { LightningElement } from 'lwc';
|
72 | export default class App extends LightningElement {}
|
73 | `;
|
74 |
|
75 | const filename = 'app.js';
|
76 |
|
77 | const options = {
|
78 | namespace: 'c',
|
79 | name: 'app',
|
80 | };
|
81 |
|
82 | const { code } = await transform(source, filename, options);
|
83 | ```
|
84 |
|
85 | **Parameters:**
|
86 |
|
87 | - `source` (string, required) - the source to be transformed can be the content of javascript, HTML, CSS.
|
88 | - `filename` (string, required) - the source filename with extension.
|
89 | - `options` (object, required) - the transformation options. The `name` and the `namespace` of the component is a minimum required for transformation.
|
90 |
|
91 | **Return**
|
92 |
|
93 | - `code` (string)- the compiled source code.
|
94 | - `map` (null) - not currently supported.
|
95 |
|
96 | ### `version`
|
97 |
|
98 | ```js
|
99 | import { version } from '@lwc/compiler';
|
100 |
|
101 | console.log(version);
|
102 | ```
|
103 |
|
104 | **Return**
|
105 |
|
106 | - `version` (string) - the current version of the compiler ex: `0.25.1`.
|
107 |
|
108 | ## Compiler Interface
|
109 |
|
110 | ### Configuration Interface
|
111 |
|
112 | Compiler can be configured to produce one bundle at a time with the following configurations:
|
113 |
|
114 | ```ts
|
115 | export interface CompilerOptions {
|
116 | name: string;
|
117 | namespace: string;
|
118 | files: BundleFiles;
|
119 | /**
|
120 | * An optional directory prefix that contains the specified components
|
121 | * files. Only used when the component that is the compiler's entry point.
|
122 | */
|
123 | baseDir?: string;
|
124 | stylesheetConfig?: StylesheetConfig;
|
125 | outputConfig?: OutputConfig;
|
126 | }
|
127 |
|
128 | export interface BundleFiles {
|
129 | [filename: string]: string;
|
130 | }
|
131 |
|
132 | export interface StylesheetConfig {
|
133 | customProperties?: CustomPropertiesConfig;
|
134 | }
|
135 |
|
136 | export interface OutputConfig {
|
137 | env?: { [name: string]: string };
|
138 | compat?: boolean;
|
139 | minify?: boolean;
|
140 | resolveProxyCompat?: OutputProxyCompatConfig;
|
141 | }
|
142 |
|
143 | export type OutputProxyCompatConfig =
|
144 | | { global: string }
|
145 | | { module: string }
|
146 | | { independent: string };
|
147 | ```
|
148 |
|
149 | ### Compiler Configuration Example
|
150 |
|
151 | ```js
|
152 | const config = {
|
153 | name: 'foo',
|
154 | namespace: 'x',
|
155 | files: {
|
156 | foo: `
|
157 | import { LightningElement, track } from 'lwc';
|
158 | export default class Foo extends from LightningElement {
|
159 | @track
|
160 | title = 'Foo Title';
|
161 | }
|
162 | `,
|
163 | 'foo.html': `<template><h1>{title}</h1></template>`,
|
164 | },
|
165 | outputConfig: {
|
166 | env: {},
|
167 | minify: false,
|
168 | compat: false,
|
169 | format: 'amd',
|
170 | },
|
171 | };
|
172 | ```
|
173 |
|
174 | The compiler configuration object is always normalized to apply necessary defaults. However, the bundle configuration is always a required parameter, which specifies a module name, namespace, type of the platform (used to determine linting rules), and a map of files to be compiled. Please note that the file value should be a string. If no outputConfig specified, compiler will produce a single result item with the following default output configuration:
|
175 |
|
176 | ```ts
|
177 | {
|
178 | outputConfig: {
|
179 | compat: false,
|
180 | minify: false,
|
181 | env: {
|
182 | NODE_ENV: 'development'
|
183 | },
|
184 | },
|
185 | stylesheeConfig: {
|
186 | customProperties: {
|
187 | allowDefinition: false,
|
188 | resolution: { type: 'native' }
|
189 | }
|
190 | }
|
191 | }
|
192 | ```
|
193 |
|
194 | ### Compiler Output Interface
|
195 |
|
196 | ```ts
|
197 | export interface CompilerOutput {
|
198 | success: boolean;
|
199 | diagnostics: Diagnostic[];
|
200 | result?: BundleResult;
|
201 | version: string;
|
202 | }
|
203 |
|
204 | export interface BundleResult {
|
205 | code: string;
|
206 | map: null;
|
207 | outputConfig: NormalizedOutputConfig;
|
208 | }
|
209 |
|
210 | export interface ModuleImportLocation {
|
211 | name: string;
|
212 | location: Location;
|
213 | }
|
214 |
|
215 | export interface NormalizedOutputConfig extends OutputConfig {
|
216 | compat: boolean;
|
217 | minify: boolean;
|
218 | env: {
|
219 | [name: string]: string;
|
220 | };
|
221 | }
|
222 | ```
|
223 |
|
224 | ## Compiler Transformation and Bundling:
|
225 |
|
226 | <img width="684" alt="screen shot 2018-08-23 at 4 14 28 pm" src="https://user-images.githubusercontent.com/7842674/44556674-b9d5ef00-a6ef-11e8-8225-6490329e10eb.png">
|
227 |
|
228 | ### Transformations
|
229 |
|
230 | There are several transformational phases that take place during compilation followed by a bundling phase. The Compiler utilizes [Rollup.js](https://rollupjs.org/guide/en) and its plugin system, which enables us to change its behaviour at key points in the bundling process. LWC compilation pipeline consists of the following plugins:
|
231 |
|
232 | _Replace_ - replace code (ex: process.env.NODE === ‘production’ will be replaced with true or false depending on the mode).
|
233 |
|
234 | _Module Resolution_ - ensure that each local import in the component can be resolved.
|
235 |
|
236 | _Transformation_ - apply LWC specific transformation to js, html, and css files.
|
237 |
|
238 | _Compat_ - apply the compatibility transformation (ex: polyfills for older browsers).
|
239 |
|
240 | _Minify_ - apply minification for ‘production’ mode code.
|
241 |
|
242 | These transformation may or may not be applied depending on the mode values specified in the compiler configuration object.
|
243 |
|
244 | ### Bundling
|
245 |
|
246 | The final phase of the compilation pipeline is bundling. During bundling, Rollup will ‘statically analyze and optimize the code you are importing, and will exclude anything that isn't actually used. This allows you to build on top of existing tools and modules without adding extra dependencies or bloating the size of your project’.
|
247 | The end result of the bundler is a single javascript file in a specified format - ‘amd’ by default.
|