@webruntime/compiler
## Table of contents
Last updated 2 days ago by tconn .
MIT · Original npm · Tarball · package.json
$ cnpm install @webruntime/compiler 
SYNC missed versions from official npm registry.

Lightning Web Runtime :: Compiler

Table of contents

Introduction

The Lightning Web Runtime Compiler is a pluggable Rollup-based bundler for off-core LWC-based applications.

The Web Runtime Compiler extends the LWC/Rollup bundling functionality with the ability to inject container services, which can then participate in the compilation lifecycle phase.

Background

Definitions

Lightning Web Runtime: A LWC application runtime.

container: Initializes and builds the set of container services which make up an application.

container service: A service plugin, which can be (1) addressed and (2) hook into various container lifecycle phases.

specifier: A string import value (eg: import('app/home'), import('lodash')).

pivot: A way to specify when a particular component/module may be loaded relative to some context (eg: 'mobile' vs 'desktop', locale, etc).

Rollup plugins

Along with custom plugins, these Rollup plugins are automatically included and used by the Compiler:

APIs

compile()

The Web Runtime Compiler exposes one main function:

async function compile(input):Promise<output>

It takes in a configuration object, and returns a Promise to its output.

Compiler input

  • LWC compiler options:

    • name (string, required): component name.
    • namespace (string, required): component namespace.
    • files ({ string: string, ...}, required): key/value pairs where each key is a file path and its value is a string containing the file contents.
    • baseDir (string, optional, default = ""): a directory prefix which contains contains the components being compiled.
    • lwcOptions (object, optional): pass in a customized LWC Rollup plugin config object, which gets merged with a default object. The following options are available (all are optional): rootDir, modules, exclude, stylesheetConfig, and experimentalDynamicComponent.
    • outputConfig (object[], optional):
      • compat (boolean, optional, default = false): true indicates browser compatibility mode.
      • minify (boolean, optional, default = false): true if the bundle should be minified.
      • sourcemap (boolean, optional, default = false): true if a source map should be generated.
      • env (object, optional, default = { NODE_ENV: 'development' }):
        • NODE_ENV (string, required) = node environment string (eg: 'production', 'development', etc).
  • Rollup options:

    • input (string, optional, default = ${namespace}/${name}): the input path for the bundle, similar to Rollup's input.
    • external (string[], optional, default =['lwc']): list of dependencies that should not be included in the bundled output, similar to Rollup's external.
    • format (string, optional, default = 'amd'): type of output ('amd', 'cjs', 'esm', 'iife', 'umd') similar to Rollup's output.format.
    • formatConfig (object, optional, default = { id: namespace + '/' + name, define: undefined }):
      • amd (object):
        • define (string): function to use in place of define, similar to Rollup's output.amd.define
  • Web Runtime options:

    • plugins (Plugin[], optional, default = []): array of Rollup plugins to use.
    • inlineConfig (object[], optional, default = []): describes the exclusion patterns for each module descriptor; array of objects with the following properites:
      • descriptor (string | glob, required): specifier which should have all dependencies inlined, EXCEPT for those in the exclude array.
      • exclude (string[] | RegExp | glob, optional, default = []): array of specifiers which should be excluded from being inlined.

Compiler output

The Compiler's output uses the same format as the LWC compiler.

  • output (object):
    • success (boolean): true if the Compiler encountered no fatal errors.
    • diagnostics (CompilerDiagnostic[]): array of error and warning information.
    • result:
      • code (string): the resulting bundled code.
      • map (* | null): the generated source map, if requested.
      • outputConfig (object | undefined): same object passed in as [Compiler input](#compiler-input).outputConfig.
    • version (string): the version of the Compiler which bundled this code.

Examples

Simple application

Creating a bundle for an LWC application. Components are stored in ./src.

import { compile } from '@webruntime/compiler';
import * as path from 'path';

const options = {
    name: 'main',
    namespace: 'any',
    external: ['lwc'],
    baseDir: path.join(__dirname, 'src'),
    files: {
        'any/main': `
            import { createElement } from 'lwc';
            import App from 'x/app';
            const element = createElement('x-app', { is: App });
            document.body.appendChild(element);
        `,
    },
};

const { success, diagnostics } = await compile(options);

if (!success) {
    for (let diagnostic of diagnostics) {
        console.error(`${diagnostic.level}: ${diagnostic.message}`);
    }
}

LWC

Creating a bundle for a LWC stored in the files object:

import { compile } from '@webruntime/compiler';

const options = {
    name: 'lwc',
    namespace: 'my',
    external: ['lwc'],
    files: {
        'my/lwc.js': `
            import { LightningElement } from 'lwc';
            export default class Lwc extends LightningElement {}
        `,
        'my/lwc.html': '<template>lwc</template>',
        'my/lwc.css': ':host { font-size: 16px; }',
    },
};

const { success, code } = await compile(options);

console.log('The bundle is: ', success ? code : 'a failure');

// Output:
// The bundle is: define('my/lwc', ['lwc'], function (lwc) { ... });

JavaScript library

Create a non-LWC bundle. The JavaScript entry point must live in the files object:

import { compile } from '@webruntime/compiler';
import * as fs from 'fs';
import * as path from 'path';

const filePath = path.join(__dirname, 'src', 'x', 'file.js');
const fileContents = fs.readFileSync(filePath).toString();
export const options = {
    name: 'file',
    namespace: '',
    files: {
        'file.js': fileContents,
    },
};

const { code = '' } = await compile(options);
export default code;

Full Compiler config object

{
    // LWC
    name: 'cmp',
    input: 'x',
    files: {
        'logger.js': 'export const logger = console.log;'
    },
    baseDir: path.join(__dirname, 'apps'),
    lwcOptions: {
        stylesheetConfig: {
            customProperties: {
                resolution: {
                    type: 'module',
                    name: '@salesforce/cssvars/customProperties',
                },
            },
        },
        experimentalDynamicComponent: {
            loader: '',
            strictSpecifier: true,
        },
    },
    outputConfig: {
        compat: true,
        minify: true,
        sourcemap: true,
        env: { NODE_ENV: 'production' }
    },

    // Rollup
    external: [
        'lwc',
        'force/lds',
        'wire-service'
    ],
    format: 'amd',
    formatConfig: {
        amd: { define: 'loader.addModule' }
    },

    // Web Runtime
    plugins: [
        {
            plugin: 'src/rollup/global-rollup.js',
            config: {
                globals: {
                    'lodash': '_',
                },
            },
        },
    ],
    inlineConfig: [
       { descriptor: 'view/*', exclude: ['view/faq', 'wire-service'] },
       { descriptor: 'x/foo', exclude: ['@salesforce/**'] }
    ],
}

Container config mappings

Compiler configuration options are gathered from different areas, including:

  • the calling service, possibly via a Rollup plugin (service)
  • the Container config object (containerConfig)
name: service
namespace: service
files: service
baseDir: containerConfig.moduleDir
stylesheetConfig: service or containerConfig.compilerConfig.lwcOptions
experimentalDynamicComponent: service or containerConfig.compilerConfig.lwcOptions
outputConfig: containerConfig.compilerConfig.outputConfig[mode]
external: containerConfig.externals, containerConfig.preloadModules, compilerConfig.external and exclusionsFrom(config.bundle)
format: service or container defaults
formatConfig: containerConfig.compilerConfig.formatConfig
plugins: service or container defaults
inlineConfig: containerConfig.bundle

Current Tags

  • 0.31.1                                ...           latest (2 days ago)
  • 0.29.13-226.1                                ...           summer20 (2 months ago)

114 Versions

  • 0.31.1                                ...           2 days ago
  • 0.31.0                                ...           3 days ago
  • 0.30.14                                ...           4 days ago
  • 0.30.13                                ...           5 days ago
  • 0.30.12                                ...           8 days ago
  • 0.30.11                                ...           8 days ago
  • 0.30.10                                ...           11 days ago
  • 0.30.9                                ...           15 days ago
  • 0.30.8                                ...           17 days ago
  • 0.30.7                                ...           19 days ago
  • 0.30.6                                ...           23 days ago
  • 0.30.5                                ...           24 days ago
  • 0.30.4                                ...           24 days ago
  • 0.30.3                                ...           25 days ago
  • 0.30.2                                ...           a month ago
  • 0.30.1                                ...           a month ago
  • 0.30.0                                ...           a month ago
  • 0.29.34                                ...           a month ago
  • 0.29.33                                ...           a month ago
  • 0.29.32                                ...           a month ago
  • 0.29.31                                ...           a month ago
  • 0.29.30                                ...           a month ago
  • 0.29.29                                ...           a month ago
  • 0.29.28                                ...           a month ago
  • 0.29.27                                ...           a month ago
  • 0.29.26                                ...           2 months ago
  • 0.29.25                                ...           2 months ago
  • 0.29.24                                ...           2 months ago
  • 0.29.23                                ...           2 months ago
  • 0.29.22                                ...           2 months ago
  • 0.29.21                                ...           2 months ago
  • 0.29.20                                ...           2 months ago
  • 0.29.13-226.1                                ...           2 months ago
  • 0.29.19                                ...           2 months ago
  • 0.29.18                                ...           2 months ago
  • 0.29.17                                ...           2 months ago
  • 0.29.16                                ...           2 months ago
  • 0.29.15                                ...           2 months ago
  • 0.29.13-226.0                                ...           2 months ago
  • 0.29.14                                ...           2 months ago
  • 0.29.13                                ...           2 months ago
  • 0.29.12                                ...           2 months ago
  • 0.29.11                                ...           2 months ago
  • 0.29.10                                ...           2 months ago
  • 0.29.9                                ...           2 months ago
  • 0.29.8                                ...           2 months ago
  • 0.29.7                                ...           2 months ago
  • 0.29.6                                ...           2 months ago
  • 0.29.5                                ...           2 months ago
  • 0.29.4                                ...           2 months ago
  • 0.29.3                                ...           2 months ago
  • 0.29.2                                ...           2 months ago
  • 0.29.1                                ...           2 months ago
  • 0.29.0                                ...           2 months ago
  • 0.28.5                                ...           2 months ago
  • 0.28.4                                ...           2 months ago
  • 0.28.3                                ...           2 months ago
  • 0.28.2                                ...           2 months ago
  • 0.28.1                                ...           2 months ago
  • 0.28.0                                ...           2 months ago
  • 0.27.1                                ...           3 months ago
  • 0.27.0                                ...           3 months ago
  • 0.26.1                                ...           3 months ago
  • 0.26.0                                ...           3 months ago
  • 0.25.3                                ...           3 months ago
  • 0.25.2                                ...           3 months ago
  • 0.25.1                                ...           3 months ago
  • 0.25.0                                ...           3 months ago
  • 0.24.0                                ...           3 months ago
  • 0.23.1                                ...           3 months ago
  • 0.23.0                                ...           3 months ago
  • 0.22.4                                ...           3 months ago
  • 0.22.3                                ...           3 months ago
  • 0.22.2                                ...           3 months ago
  • 0.22.1                                ...           3 months ago
  • 0.22.0                                ...           3 months ago
  • 0.21.9                                ...           3 months ago
  • 0.21.8                                ...           3 months ago
  • 0.21.7                                ...           3 months ago
  • 0.21.6                                ...           3 months ago
  • 0.21.5                                ...           4 months ago
  • 0.21.4                                ...           4 months ago
  • 0.21.3                                ...           4 months ago
  • 0.21.2                                ...           4 months ago
  • 0.21.1                                ...           4 months ago
  • 0.21.0                                ...           4 months ago
  • 0.20.18                                ...           4 months ago
  • 0.20.17                                ...           4 months ago
  • 0.20.15                                ...           4 months ago
  • 0.20.13-226.0                                ...           4 months ago
  • 0.20.14                                ...           4 months ago
  • 0.20.13                                ...           4 months ago
  • 0.20.12                                ...           4 months ago
  • 0.20.11                                ...           4 months ago
  • 0.20.10                                ...           4 months ago
  • 0.20.9                                ...           4 months ago
  • 0.20.8                                ...           4 months ago
  • 0.20.6                                ...           4 months ago
  • 0.20.5                                ...           4 months ago
  • 0.20.3                                ...           4 months ago
  • 0.20.2                                ...           4 months ago
  • 0.20.1                                ...           4 months ago
  • 0.19.5                                ...           4 months ago
  • 0.19.4                                ...           4 months ago
  • 0.19.3                                ...           4 months ago
  • 0.19.2                                ...           4 months ago
  • 0.18.51                                ...           4 months ago
  • 0.18.49                                ...           5 months ago
  • 0.18.47                                ...           5 months ago
  • 0.18.46                                ...           5 months ago
  • 0.18.45                                ...           5 months ago
  • 0.18.39                                ...           5 months ago
  • 0.18.23                                ...           8 months ago
  • 0.18.21                                ...           8 months ago

Copyright 2014 - 2016 © taobao.org |