1.61.4 • Published 3 years ago

@webruntime/compiler v1.61.4

Weekly downloads
1,523
License
MIT
Repository
-
Last release
3 years ago

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
0.61.2-234.9

3 years ago

0.61.2-234.8

3 years ago

0.61.2-234.6

3 years ago

0.61.2-234.7

3 years ago

0.61.2-234.5

3 years ago

0.61.2-234.2

3 years ago

0.61.2-234.1

3 years ago

0.61.2-234.4

3 years ago

0.61.2-234.3

3 years ago

1.61.4

3 years ago

0.61.2

3 years ago

0.61.1

3 years ago

0.61.3

3 years ago

0.61.0

3 years ago

0.61.2-234.0

3 years ago

0.60.3

3 years ago

0.60.4

3 years ago

0.60.2

3 years ago

0.59.2

3 years ago

0.59.3

3 years ago

0.59.4

3 years ago

0.60.1

3 years ago

0.60.0

3 years ago

0.59.1

3 years ago

0.59.0

3 years ago

0.58.3

3 years ago

0.58.2

3 years ago

0.58.1

3 years ago

0.55.2

3 years ago

0.55.3

3 years ago

0.55.0

3 years ago

0.55.1

3 years ago

0.56.0

3 years ago

0.54.0

3 years ago

0.57.0

3 years ago

0.58.0

3 years ago

0.50.10

3 years ago

0.53.2

3 years ago

0.53.0

3 years ago

0.53.1

3 years ago

0.51.0

3 years ago

0.51.1

3 years ago

0.52.0

3 years ago

0.49.6-232.2

3 years ago

0.50.9

3 years ago

0.50.8

3 years ago

0.50.7

3 years ago

0.49.6-232.0

3 years ago

0.50.6

3 years ago

0.50.5

3 years ago

0.50.3

3 years ago

0.50.4

3 years ago

0.50.2

3 years ago

0.50.1

3 years ago

0.50.0

3 years ago

0.49.7

3 years ago

0.49.6

3 years ago

0.49.5

3 years ago

0.49.4

3 years ago

0.49.1

3 years ago

0.49.0

3 years ago

0.49.3

3 years ago

0.48.6

3 years ago

0.48.2

3 years ago

0.48.3

3 years ago

0.48.0

3 years ago

0.48.1

3 years ago

0.48.4

3 years ago

0.48.5

3 years ago

0.47.4

3 years ago

0.47.3

3 years ago

0.47.2

3 years ago

0.47.1

3 years ago

0.45.10

3 years ago

0.45.11

3 years ago

0.46.0

3 years ago

0.47.0

3 years ago

0.45.9

3 years ago

0.45.6

3 years ago

0.45.7

3 years ago

0.45.8

3 years ago

0.45.5

3 years ago

0.45.4

3 years ago

0.45.3

3 years ago

0.42.6-230.3

3 years ago

0.45.1

3 years ago

0.45.2

3 years ago

0.44.2

3 years ago

0.45.0

3 years ago

0.44.1

3 years ago

0.44.0

3 years ago

0.42.6-230.2

3 years ago

0.43.8

3 years ago

0.43.7

3 years ago

0.43.6

3 years ago

0.43.5

3 years ago

0.43.4

3 years ago

0.43.3

3 years ago

0.42.6-230.1

3 years ago

0.42.6-230.0

3 years ago

0.43.2

3 years ago

0.43.1

3 years ago

0.43.0

3 years ago

0.42.6

3 years ago

0.42.5

3 years ago

0.42.4

3 years ago

0.42.3

3 years ago

0.42.2

3 years ago

0.42.0

3 years ago

0.42.1

3 years ago

0.41.1

3 years ago

0.41.0

3 years ago

0.40.0

3 years ago

0.39.2

3 years ago

0.39.1

3 years ago

0.38.0

3 years ago

0.39.0

3 years ago

0.37.10

3 years ago

0.37.7

3 years ago

0.37.6

4 years ago

0.37.5

4 years ago

0.37.4

4 years ago

0.37.3

4 years ago

0.37.2

4 years ago

0.37.1

4 years ago

0.37.0

4 years ago

0.36.2

4 years ago

0.36.1

4 years ago

0.36.0

4 years ago

0.35.9

4 years ago

0.35.8

4 years ago

0.35.7

4 years ago

0.35.6

4 years ago

0.35.5

4 years ago

0.35.4

4 years ago

0.35.3

4 years ago

0.35.2

4 years ago

0.35.1

4 years ago

0.34.9

4 years ago

0.35.0

4 years ago

0.34.10

4 years ago

0.34.8

4 years ago

0.34.7

4 years ago

0.34.6

4 years ago

0.34.5

4 years ago

0.34.4

4 years ago

0.34.3

4 years ago

0.34.2

4 years ago

0.34.1

4 years ago

0.33.29

4 years ago

0.34.0

4 years ago

0.33.28

4 years ago

0.33.27

4 years ago

0.33.26

4 years ago

0.33.24-228.1

4 years ago

0.33.24-228.0

4 years ago

0.33.25

4 years ago

0.33.24

4 years ago

0.33.23

4 years ago

0.33.21

4 years ago

0.33.22

4 years ago

0.33.20

4 years ago

0.33.19

4 years ago

0.33.18

4 years ago

0.33.17

4 years ago

0.33.16

4 years ago

0.33.15

4 years ago

0.33.14

4 years ago

0.33.13

4 years ago

0.33.12

4 years ago

0.33.11

4 years ago

0.33.10

4 years ago

0.33.9

4 years ago

0.33.8

4 years ago

0.33.7

4 years ago

0.33.6

4 years ago

0.33.5

4 years ago

0.33.4

4 years ago

0.33.3

4 years ago

0.33.2

4 years ago

0.33.1

4 years ago

0.33.0

4 years ago

0.32.3

4 years ago

0.32.2

4 years ago

0.32.1

4 years ago

0.32.0

4 years ago

0.31.1

4 years ago

0.31.0

4 years ago

0.30.14

4 years ago

0.30.13

4 years ago

0.30.11

4 years ago

0.30.12

4 years ago

0.30.10

4 years ago

0.30.9

4 years ago

0.30.8

4 years ago

0.30.7

4 years ago

0.30.6

4 years ago

0.30.5

4 years ago

0.30.4

4 years ago

0.30.3

4 years ago

0.30.2

4 years ago

0.30.1

4 years ago

0.30.0

4 years ago

0.29.34

4 years ago

0.29.33

4 years ago

0.29.32

4 years ago

0.29.31

4 years ago

0.29.30

4 years ago

0.29.29

4 years ago

0.29.28

4 years ago

0.29.27

4 years ago

0.29.26

4 years ago

0.29.24

4 years ago

0.29.25

4 years ago

0.29.23

4 years ago

0.29.22

4 years ago

0.29.21

4 years ago

0.29.20

4 years ago

0.29.19

4 years ago

0.29.13-226.1

4 years ago

0.29.18

4 years ago

0.29.16

4 years ago

0.29.17

4 years ago

0.29.15

4 years ago

0.29.13-226.0

4 years ago

0.29.13

4 years ago

0.29.14

4 years ago

0.29.12

4 years ago

0.29.10

4 years ago

0.29.11

4 years ago

0.29.9

4 years ago

0.29.8

4 years ago

0.29.7

4 years ago

0.29.6

4 years ago

0.29.5

4 years ago

0.29.4

4 years ago

0.29.3

4 years ago

0.29.2

4 years ago

0.29.1

4 years ago

0.29.0

4 years ago

0.28.5

4 years ago

0.28.4

4 years ago

0.28.3

4 years ago

0.28.2

4 years ago

0.28.1

4 years ago

0.28.0

4 years ago

0.27.1

4 years ago

0.27.0

4 years ago

0.26.1

4 years ago

0.26.0

4 years ago

0.25.3

4 years ago

0.25.2

4 years ago

0.25.1

4 years ago

0.25.0

4 years ago

0.24.0

4 years ago

0.23.1

4 years ago

0.22.4

4 years ago

0.23.0

4 years ago

0.22.3

4 years ago

0.22.2

4 years ago

0.22.1

4 years ago

0.22.0

4 years ago

0.21.9

4 years ago

0.21.8

4 years ago

0.21.7

4 years ago

0.21.6

4 years ago

0.21.5

4 years ago

0.21.4

4 years ago

0.21.3

4 years ago

0.21.2

4 years ago

0.21.1

4 years ago

0.21.0

4 years ago

0.20.18

4 years ago

0.20.17

4 years ago

0.20.15

4 years ago

0.20.13-226.0

4 years ago

0.20.14

4 years ago

0.20.13

4 years ago

0.20.12

4 years ago

0.20.11

4 years ago

0.20.10

4 years ago

0.20.9

4 years ago

0.20.8

4 years ago

0.20.6

4 years ago

0.20.5

4 years ago

0.20.3

4 years ago

0.20.1

4 years ago

0.20.2

4 years ago

0.19.5

4 years ago

0.19.4

4 years ago

0.19.3

4 years ago

0.19.2

4 years ago

0.18.51

4 years ago

0.18.47

4 years ago

0.18.49

4 years ago

0.18.46

4 years ago

0.18.45

4 years ago

0.18.39

4 years ago

0.18.23

5 years ago

0.18.21

5 years ago