@lwc/compiler
LWC compiler
Last updated a month ago by diervo .
MIT · Original npm · Tarball · package.json
$ cnpm install @lwc/compiler 
SYNC missed versions from official npm registry.

LWC Compiler

@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.

Installation

yarn add --dev @lwc/compiler

APIs

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:

  • options (object) - the object which specifies compilation source and output shape
    • 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 configuration. Dictates the shape of the bundle output (ex: bundle compiled for production mode, minified).

Return

  • output (object) - the object with the following fields:
    • 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 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.
  • map (null) - not currently supported.

version

import { version } from '@lwc/compiler';

console.log(version);

Return

  • version (string) - the current version of the compiler ex: 0.25.1.

Compiler Interface

Configuration Interface

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 };

Compiler Configuration Example

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' }
        }
    }
}

Compiler Output Interface

export interface CompilerOutput {
    success: boolean;
    diagnostics: Diagnostic[];
    result?: BundleResult;
    version: string;
}

export interface BundleResult {
    code: string;
    map: null;
    outputConfig: NormalizedOutputConfig;
}

export interface ModuleImportLocation {
    name: string;
    location: Location;
}

export interface NormalizedOutputConfig extends OutputConfig {
    compat: boolean;
    minify: boolean;
    env: {
        [name: string]: string;
    };
}

Compiler Transformation and Bundling:

screen shot 2018-08-23 at 4 14 28 pm

Transformations

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.

Bundling

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.

Current Tags

  • 0.34.7                                ...           218-patch (9 months ago)
  • 0.37.4-220.2                                ...           220-patch (6 months ago)
  • 1.1.12-alpha2                                ...           canary (9 days ago)
  • 1.1.1                                ...           latest (a month ago)
  • 0.35.1                                ...           master (10 months ago)
  • 1.1.13                                ...           next (4 days ago)
  • 0.34.7                                ...           spring-19 (9 months ago)
  • 0.37.4-220.2                                ...           summer-19 (6 months ago)
  • 0.40.1-222.1                                ...           winter-19 (6 months ago)

243 Versions

  • 1.1.13                                ...           4 days ago
  • 1.1.12                                ...           6 days ago
  • 1.1.12-alpha2                                ...           9 days ago
  • 1.1.12-alpha1                                ...           12 days ago
  • 1.1.11                                ...           16 days ago
  • 1.1.11-alpha2                                ...           16 days ago
  • 1.1.11-alpha1                                ...           16 days ago
  • 1.1.11-alpha                                ...           17 days ago
  • 1.1.10                                ...           17 days ago
  • 1.1.10-alpha2                                ...           17 days ago
  • 1.1.10-alpha1                                ...           17 days ago
  • 1.1.9                                ...           17 days ago
  • 1.1.9-alpha1                                ...           20 days ago
  • 1.1.8                                ...           25 days ago
  • 1.1.8-alpha1                                ...           25 days ago
  • 1.1.7                                ...           a month ago
  • 1.1.7-alpha.0                                ...           a month ago
  • 1.1.6                                ...           a month ago
  • 1.1.5                                ...           a month ago
  • 1.1.4-alpha7                                ...           a month ago
  • 1.1.4-alpha6                                ...           a month ago
  • 1.1.4-alpha5                                ...           a month ago
  • 1.1.4-alpha4                                ...           a month ago
  • 1.2.0-alpha4                                ...           a month ago
  • 1.1.4-alpha891                                ...           a month ago
  • 1.1.4-alpha890                                ...           a month ago
  • 1.1.4-alpha889                                ...           a month ago
  • 1.1.4-alpha888                                ...           a month ago
  • 1.1.4-alpha3                                ...           a month ago
  • 1.2.0-alpha3                                ...           a month ago
  • 1.1.4-alpha2                                ...           a month ago
  • 1.1.4-alpha1                                ...           a month ago
  • 1.1.4                                ...           a month ago
  • 1.1.3                                ...           a month ago
  • 1.1.2-alpha1                                ...           a month ago
  • 1.1.1                                ...           a month ago
  • 1.1.1-alpha6                                ...           a month ago
  • 1.1.1-alpha5                                ...           a month ago
  • 1.1.1-alpha4                                ...           a month ago
  • 1.2.0-alpha2                                ...           a month ago
  • 1.2.0-alpha1                                ...           a month ago
  • 1.1.1-alpha3                                ...           a month ago
  • 1.1.1-alpha2                                ...           a month ago
  • 1.1.1-alpha1                                ...           a month ago
  • 1.1.0                                ...           a month ago
  • 1.1.0-beta8                                ...           a month ago
  • 1.0.2-222.24                                ...           a month ago
  • 1.0.2-222.23                                ...           2 months ago
  • 1.1.0-beta7                                ...           2 months ago
  • 1.1.0-beta6                                ...           2 months ago
  • 1.1.0-beta5                                ...           2 months ago
  • 1.1.0-beta4                                ...           2 months ago
  • 1.1.0-beta3                                ...           2 months ago
  • 1.1.0-beta2                                ...           2 months ago
  • 1.1.0-beta1                                ...           2 months ago
  • 1.0.3-alpha.74                                ...           2 months ago
  • 1.0.2-222.1000                                ...           2 months ago
  • 1.1.0-alpha1000                                ...           2 months ago
  • 1.0.3-beta.67                                ...           2 months ago
  • 1.0.2-test6                                ...           2 months ago
  • 1.0.2-test5                                ...           2 months ago
  • 1.0.2-test4                                ...           2 months ago
  • 1.0.2-test3                                ...           2 months ago
  • 1.0.2-test2                                ...           2 months ago
  • 1.0.2-222.22                                ...           2 months ago
  • 1.1.0-alpha19                                ...           2 months ago
  • 1.1.0-alpha18                                ...           2 months ago
  • 1.1.0-alpha17                                ...           2 months ago
  • 1.1.0-alpha16                                ...           2 months ago
  • 1.1.0-alpha15                                ...           2 months ago
  • 1.1.0-alpha14                                ...           2 months ago
  • 1.1.0-alpha13                                ...           2 months ago
  • 1.0.2-222.21-revert-test                                ...           2 months ago
  • 1.0.2-222.21                                ...           2 months ago
  • 1.0.2-222.20                                ...           2 months ago
  • 1.0.2-222.19                                ...           2 months ago
  • 1.1.0-alpha12                                ...           2 months ago
  • 1.1.0-alpha11                                ...           2 months ago
  • 1.1.0-alpha10                                ...           2 months ago
  • 1.1.0-alpha9                                ...           2 months ago
  • 1.0.2-222.18                                ...           3 months ago
  • 1.1.0-alpha8                                ...           3 months ago
  • 1.1.0-alpha7                                ...           3 months ago
  • 1.1.0-alpha6                                ...           3 months ago
  • 1.1.0-alpha5                                ...           3 months ago
  • 1.1.0-alpha4                                ...           3 months ago
  • 1.1.0-alpha3                                ...           3 months ago
  • 1.1.0-alpha2                                ...           3 months ago
  • 1.1.0-alpha1                                ...           3 months ago
  • 1.1.0-alpha0                                ...           3 months ago
  • 1.0.2-222.17                                ...           3 months ago
  • 1.0.2-222.16                                ...           3 months ago
  • 1.0.2-222.15                                ...           3 months ago
  • 1.0.2-222.14                                ...           3 months ago
  • 1.0.2-222.13                                ...           3 months ago
  • 1.0.2-222.13-alpha1                                ...           4 months ago
  • 1.0.2-222.13-alpha0                                ...           4 months ago
  • 1.0.2-222.12                                ...           4 months ago
  • 1.0.2-222.12-alpha2                                ...           4 months ago
  • 1.0.2-222.12-alpha1                                ...           4 months ago
  • 1.0.2-222.11                                ...           4 months ago
  • 1.0.2-222.10                                ...           4 months ago
  • 1.0.2-222.9                                ...           4 months ago
  • 1.0.2-222.9-alpha1                                ...           4 months ago
  • 1.0.2-222.9-canary                                ...           4 months ago
  • 1.0.2-222.8                                ...           4 months ago
  • 1.0.2-222.7                                ...           4 months ago
  • 1.0.2-222.6                                ...           4 months ago
  • 1.0.2-222.5                                ...           5 months ago
  • 1.0.2-222.4                                ...           5 months ago
  • 1.0.2-222.3                                ...           5 months ago
  • 1.0.2-222.2                                ...           5 months ago
  • 1.0.2-222.1                                ...           5 months ago
  • 1.0.3                                ...           5 months ago
  • 1.0.2                                ...           5 months ago
  • 1.0.1                                ...           5 months ago
  • 1.0.0-alpha5                                ...           5 months ago
  • 1.0.1-alpha03                                ...           5 months ago
  • 1.0.0-alpha2                                ...           5 months ago
  • 1.0.0-alpha.2                                ...           5 months ago
  • 0.40.1-222.8                                ...           5 months ago
  • 1.0.1-alpha0                                ...           5 months ago
  • 0.40.1-222.7                                ...           5 months ago
  • 0.40.1-222.6                                ...           5 months ago
  • 0.40.1-222.5                                ...           5 months ago
  • 0.40.1-222.4                                ...           5 months ago
  • 0.40.1-222.3                                ...           5 months ago
  • 0.40.1-222.2                                ...           5 months ago
  • 1.0.0                                ...           6 months ago
  • 1.0.0-alpha.0                                ...           6 months ago
  • 0.41.0                                ...           6 months ago
  • 0.40.2-alpha1                                ...           6 months ago
  • 0.40.1-ekashida1                                ...           6 months ago
  • 0.40.1-222.1                                ...           6 months ago
  • 0.40.1                                ...           6 months ago
  • 0.40.1-alpha2                                ...           6 months ago
  • 0.40.1-alpha1                                ...           6 months ago
  • 0.40.0                                ...           6 months ago
  • 0.39.1-222.4                                ...           6 months ago
  • 0.37.4-220.2                                ...           6 months ago
  • 0.39.1-222.3                                ...           6 months ago
  • 0.37.4-220.1                                ...           6 months ago
  • 0.39.2-alpha6                                ...           6 months ago
  • 0.39.1-222.2                                ...           6 months ago
  • 0.39.1-222.1                                ...           6 months ago
  • 0.39.2-alpha2                                ...           6 months ago
  • 0.39.2-alpha5                                ...           6 months ago
  • 0.39.2-alpha1                                ...           6 months ago
  • 0.37.4-alpha2                                ...           6 months ago
  • 0.37.4-alpha1                                ...           6 months ago
  • 0.37.4                                ...           6 months ago
  • 0.37.3                                ...           6 months ago
  • 0.39.1                                ...           6 months ago
  • 0.38.2                                ...           7 months ago
  • 0.38.2-alpha1                                ...           7 months ago
  • 0.39.0                                ...           7 months ago
  • 0.38.1-alpha1                                ...           7 months ago
  • 0.38.1                                ...           7 months ago
  • 0.38.0                                ...           7 months ago
  • 0.38.0-alpha3                                ...           7 months ago
  • 0.38.0-alpha2                                ...           7 months ago
  • 0.38.0-alpha1                                ...           7 months ago
  • 0.37.3-alpha10                                ...           7 months ago
  • 0.37.3-alpha9                                ...           7 months ago
  • 0.37.3-alpha8                                ...           7 months ago
  • 0.37.3-alpha7                                ...           7 months ago
  • 0.37.3-alpha6                                ...           7 months ago
  • 0.37.3-alpha5                                ...           7 months ago
  • 0.37.3-alpha4                                ...           7 months ago
  • 0.37.3-alpha3                                ...           7 months ago
  • 0.37.3-alpha2                                ...           7 months ago
  • 0.37.3-alpha                                ...           7 months ago
  • 0.37.2                                ...           8 months ago
  • 0.34.8                                ...           8 months ago
  • 0.37.1                                ...           8 months ago
  • 0.37.0                                ...           8 months ago
  • 0.36.1                                ...           8 months ago
  • 0.36.0                                ...           8 months ago
  • 0.35.12                                ...           8 months ago
  • 0.36.0-beta4                                ...           8 months ago
  • 0.36.0-beta3                                ...           8 months ago
  • 0.36.0-beta2                                ...           8 months ago
  • 0.36.0-beta1                                ...           8 months ago
  • 0.35.11-test1                                ...           9 months ago
  • 0.34.7                                ...           9 months ago
  • 0.35.11                                ...           9 months ago
  • 0.36.0-alpha6                                ...           9 months ago
  • 0.36.0-alpha5                                ...           9 months ago
  • 0.35.10                                ...           9 months ago
  • 0.36.0-alpha4                                ...           9 months ago
  • 0.36.0-alpha3                                ...           9 months ago
  • 0.36.0-alpha2                                ...           9 months ago
  • 0.35.7-test1                                ...           9 months ago
  • 0.36.0-alpha1                                ...           9 months ago
  • 0.35.9                                ...           9 months ago
  • 0.34.6                                ...           9 months ago
  • 0.35.7                                ...           9 months ago
  • 0.35.7-alpha4                                ...           9 months ago
  • 0.35.7-alpha3                                ...           9 months ago
  • 0.34.5                                ...           9 months ago
  • 0.35.7-alpha2                                ...           9 months ago
  • 0.34.4                                ...           9 months ago
  • 0.35.6                                ...           9 months ago
  • 0.35.6-alpha1                                ...           10 months ago
  • 0.34.4-alpha1                                ...           10 months ago
  • 0.34.3                                ...           10 months ago
  • 0.35.5                                ...           10 months ago
  • 0.35.5-alpha-1                                ...           10 months ago
  • 0.34.2                                ...           10 months ago
  • 0.34.1                                ...           10 months ago
  • 0.35.4                                ...           10 months ago
  • 0.35.3                                ...           10 months ago
  • 0.35.2                                ...           10 months ago
  • 0.33.28-5                                ...           10 months ago
  • 0.33.28-4                                ...           10 months ago
  • 0.33.28-3                                ...           10 months ago
  • 0.33.28-2                                ...           10 months ago
  • 0.35.1                                ...           10 months ago
  • 0.34.0-alpha1                                ...           10 months ago
  • 0.35.0                                ...           10 months ago
  • 0.34.0                                ...           10 months ago
  • 0.33.28-alpha5                                ...           10 months ago
  • 0.33.28-alpha4                                ...           10 months ago
  • 0.33.28-1                                ...           10 months ago
  • 0.33.28-alpha3                                ...           10 months ago
  • 0.33.28-alpha2                                ...           10 months ago
  • 0.33.28-alpha1                                ...           10 months ago
  • 0.33.27-alpha2                                ...           a year ago
  • 0.33.27-alpha1                                ...           a year ago
  • 0.33.26                                ...           a year ago
  • 0.33.25                                ...           a year ago
  • 0.33.24                                ...           a year ago
  • 0.33.23                                ...           a year ago
  • 0.33.22                                ...           a year ago
  • 0.33.21                                ...           a year ago
  • 0.33.20                                ...           a year ago
  • 0.33.19                                ...           a year ago
  • 0.33.18                                ...           a year ago
  • 0.33.10                                ...           a year ago
  • 0.33.14                                ...           a year ago
  • 0.33.13                                ...           a year ago
  • 0.33.17                                ...           a year ago
  • 0.33.17-alpha04                                ...           a year ago

Copyright 2014 - 2016 © taobao.org |