Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
@lwc/compiler
Advanced tools
lwc-compiler is an open source project that enables developers to take full control of processing a single Lightning Web Component module for runtime consumption.
yarn install --save-dev lwc-compiler
compile
import { compile } from 'lwc-compiler';
const options = {
name: "foo",
namespace: "x",
files: {
"foo.js": `
import { LightningElement } from 'lwc';
export default class Foo extends LightningElement {}
`,
"foo.html": `<template><h1>Foo</h1></template>`
}
}
const { success, diagnostics, result: { code } } = await compile(options);
if (!success) {
for ( let diagnostic of diagnostics ) {
console.log(diagnostic.level + ':' + diagnostic.message);
}
}
return code;
Parameters:
name
(string, required) - component name.namespace
(string, required) - component namespace.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.baseDir
(string, optional) - An optional directory prefix that contains the specified components. Only used when the component that is the compiler's entry point.stylesheetConfig
(StylesheetConfig, optional) - css configuration.outputConfig
(OutputConfig, optional) - compiler output configuratoin. Dictates the shape of the bunlde output (ex: bundle compiled for production mode, minified).Return
success
(boolean) - compilation results (true only if all compilation steps were successful).diagnostics
(Diagnostic[]) - an array of compilation Diagnostic
objects (ex: warnings, errors)result
(BundleResult) - an object containing compiled code, metadata, and configuration used during compilation;version
(string) - the version of compiler used for current compilation.transform
Transform the content of individual file for manual bundling.
import { transform } from 'lwc-compiler';
const source = `
import { LightningElement } from 'lwc';
export default class App extends LightningElement {}
`
const filename = 'app.js';
const options = {
namespace: 'c',
name: 'app',
}
const { code } = await transform(source, filename, options);
Parameters:
source
(string, required) - the source to be transformed can be the content of javascript, HTML, CSS.filename
(string, required) - the source filename with extension.options
(object, required) - the transformation options. The name
and the namespace
of the component is a minimum required for transformation.Return
code
(string)- the compiled source code.metadata
(object) - the metadata collected during transformation. Includes: declarationLoc
, impotLocations
, and experimentalTemplateDependencies
.map
(null) - not currenlty supported.version
import { version } from 'lwc-compiler';
console.log(version);
Return
version
(string) - the current version of the compiler ex: 0.25.1
.Compiler can be configured to produce one bundle at a time with the following configurations:
export interface CompilerOptions {
name: string;
namespace: string;
files: BundleFiles;
/**
* An optional directory prefix that contains the specified components
* files. Only used when the component that is the compiler's entry point.
*/
baseDir?: string;
stylesheetConfig?: StylesheetConfig;
outputConfig?: OutputConfig;
}
export interface BundleFiles {
[filename: string]: string;
}
export interface StylesheetConfig {
customProperties?: CustomPropertiesConfig;
}
export interface OutputConfig {
env?: { [name: string]: string };
compat?: boolean;
minify?: boolean;
resolveProxyCompat?: OutputProxyCompatConfig;
}
export type OutputProxyCompatConfig =
| { global: string }
| { module: string }
| { independent: string };
const config = {
name: "foo",
namespace: "x",
files: {
"foo": `
import { LightningElement, track } from 'lwc';
export default class Foo extends from LightningElement {
@track
title = 'Foo Title';
}
`,
"foo.html": `<template><h1>{title}</h1></template>`
},
outputConfig: {
env: {},
minify: false,
compat: false,
format: "amd"
}
}
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:
{
outputConfig: {
compat: false,
minify: false,
env: {
NODE_ENV: 'development'
},
},
stylesheeConfig: {
customProperties: {
allowDefinition: false,
resolution: { type: 'native' }
}
}
}
export interface CompilerOutput {
success: boolean;
diagnostics: Diagnostic[];
result?: BundleResult;
version: string;
}
export interface BundleResult {
code: string;
map: null;
metadata: BundleMetadata;
outputConfig: NormalizedOutputConfig;
}
export interface BundleMetadata {
importLocations: ModuleImportLocation[];
declarationLoc?: Location;
}
export interface ModuleImportLocation {
name: string;
location: Location;
experimentalTemplateDependencies: ModuleExports [];
}
export interface NormalizedOutputConfig extends OutputConfig {
compat: boolean;
minify: boolean;
env: {
[name: string]: string;
};
}
There are several transformational phases that take place during compilation followed by a bundling phase. The Compiler utilizes Rollup.js 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:
Replace - replace code (ex: process.env.NODE === ‘production’ will be replaced with true or false depending on the mode).
Module Resolution - ensure that each local import in the component can be resolved.
Transformation - apply LWC specific transformation to js, html, and css files.
Compat - apply the compatibility transformation (ex: polyfills for older browsers).
Minify - apply minification for ‘production’ mode code.
These transformation may or may not be applied depending on the mode values specified in the compiler configuration object.
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’. The end result of the bundler is a single javascript file in a specified format - ‘amd’ by default.
FAQs
LWC compiler
The npm package @lwc/compiler receives a total of 74,980 weekly downloads. As such, @lwc/compiler popularity was classified as popular.
We found that @lwc/compiler demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 15 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.