Home Reference Source Test Repository
import CompilerHost from 'electron-compile/src/compiler-host.js'
public class | source

CompilerHost

This class is the top-level class that encapsulates all of the logic of compiling and caching application code. If you're looking for a "Main class", this is it.

This class can be created directly but it is usually created via the methods in config-parser, which will among other things, set up the compiler options given a project root.

CompilerHost is also the top-level class that knows how to serialize all of the information necessary to recreate itself, either as a development host (i.e. will allow cache misses and actual compilation), or as a read-only version of itself for production.

Static Method Summary

Static Public Methods
public static

createFromConfiguration(rootCacheDir: string, appRoot: string, compilersByMimeType: Object, fallbackCompiler: CompilerBase): CompilerHost

Creates a development-mode CompilerHost from the previously saved configuration.

public static

createFromConfigurationSync(rootCacheDir: string, appRoot: string, compilersByMimeType: Object, fallbackCompiler: CompilerBase): CompilerHost

Creates a development-mode CompilerHost from the previously saved configuration.

public static

createReadonlyFromConfiguration(rootCacheDir: string, appRoot: string, fallbackCompiler: CompilerBase): CompilerHost

Creates a production-mode CompilerHost from the previously saved configuration

public static

createReadonlyFromConfigurationSync(rootCacheDir: string, appRoot: string, fallbackCompiler: CompilerBase): CompilerHost

Creates a production-mode CompilerHost from the previously saved configuration

Constructor Summary

Public Constructor
public

constructor(rootCacheDir: string, compilers: Object, fileChangeCache: FileChangedCache, readOnlyMode: boolean, fallbackCompiler: CompilerBase)

Creates an instance of CompilerHost.

Member Summary

Public Members
public

appRoot: *

public

Method Summary

Public Methods
public

compile(filePath: string): object

Compiles a file and returns the compiled result.

public

compileAll(rootDirectory: string, shouldCompile: Function): Promise

Pre-caches an entire directory of files recursively.

public

compileAllSync(rootDirectory: string, shouldCompile: Function): Promise

Pre-caches an entire directory of files recursively.

public

compileSync(filePath: *): *

public

Saves the current compiler configuration to a file that createReadonlyFromConfiguration can use to recreate the current compiler environment

public

Saves the current compiler configuration to a file that createReadonlyFromConfiguration can use to recreate the current compiler environment

Static Public Methods

public static createFromConfiguration(rootCacheDir: string, appRoot: string, compilersByMimeType: Object, fallbackCompiler: CompilerBase): CompilerHost source

Creates a development-mode CompilerHost from the previously saved configuration.

Params:

NameTypeAttributeDescription
rootCacheDir string

The root directory to use for the cache. This cache must have cache information saved via saveConfiguration

appRoot string

The top-level directory for your application (i.e. the one which has your package.json).

compilersByMimeType Object

an Object whose keys are input MIME types and whose values are instances of CompilerBase. Create this via the createCompilers method in config-parser.

fallbackCompiler CompilerBase

(optional) When a file is compiled which doesn't have a matching compiler, this compiler will be used instead. If null, will fail compilation. A good alternate fallback is the compiler for 'text/plain', which is guaranteed to be present.

Return:

CompilerHost

A read-only CompilerHost

public static createFromConfigurationSync(rootCacheDir: string, appRoot: string, compilersByMimeType: Object, fallbackCompiler: CompilerBase): CompilerHost source

Creates a development-mode CompilerHost from the previously saved configuration.

Params:

NameTypeAttributeDescription
rootCacheDir string

The root directory to use for the cache. This cache must have cache information saved via saveConfiguration

appRoot string

The top-level directory for your application (i.e. the one which has your package.json).

compilersByMimeType Object

an Object whose keys are input MIME types and whose values are instances of CompilerBase. Create this via the createCompilers method in config-parser.

fallbackCompiler CompilerBase

(optional) When a file is compiled which doesn't have a matching compiler, this compiler will be used instead. If null, will fail compilation. A good alternate fallback is the compiler for 'text/plain', which is guaranteed to be present.

Return:

CompilerHost

A read-only CompilerHost

public static createReadonlyFromConfiguration(rootCacheDir: string, appRoot: string, fallbackCompiler: CompilerBase): CompilerHost source

Creates a production-mode CompilerHost from the previously saved configuration

Params:

NameTypeAttributeDescription
rootCacheDir string

The root directory to use for the cache. This cache must have cache information saved via saveConfiguration

appRoot string

The top-level directory for your application (i.e. the one which has your package.json).

fallbackCompiler CompilerBase

(optional) When a file is compiled which doesn't have a matching compiler, this compiler will be used instead. If null, will fail compilation. A good alternate fallback is the compiler for 'text/plain', which is guaranteed to be present.

Return:

CompilerHost

A read-only CompilerHost

public static createReadonlyFromConfigurationSync(rootCacheDir: string, appRoot: string, fallbackCompiler: CompilerBase): CompilerHost source

Creates a production-mode CompilerHost from the previously saved configuration

Params:

NameTypeAttributeDescription
rootCacheDir string

The root directory to use for the cache. This cache must have cache information saved via saveConfiguration

appRoot string

The top-level directory for your application (i.e. the one which has your package.json).

fallbackCompiler CompilerBase

(optional) When a file is compiled which doesn't have a matching compiler, this compiler will be used instead. If null, will fail compilation. A good alternate fallback is the compiler for 'text/plain', which is guaranteed to be present.

Return:

CompilerHost

A read-only CompilerHost

Public Constructors

public constructor(rootCacheDir: string, compilers: Object, fileChangeCache: FileChangedCache, readOnlyMode: boolean, fallbackCompiler: CompilerBase) source

Creates an instance of CompilerHost. You probably want to use the methods in config-parser for development, or createReadonlyFromConfiguration for production instead.

Params:

NameTypeAttributeDescription
rootCacheDir string

The root directory to use for the cache

compilers Object

an Object whose keys are input MIME types and whose values are instances of CompilerBase. Create this via the createCompilers method in config-parser.

fileChangeCache FileChangedCache

A file-change cache that is optionally pre-loaded.

readOnlyMode boolean

If True, cache misses will fail and compilation will not be attempted.

fallbackCompiler CompilerBase

(optional) When a file is compiled which doesn't have a matching compiler, this compiler will be used instead. If null, will fail compilation. A good alternate fallback is the compiler for 'text/plain', which is guaranteed to be present.

Public Members

public appRoot: * source

public cachesForCompilers: * source

Public Methods

public compile(filePath: string): object source

Compiles a file and returns the compiled result.

Params:

NameTypeAttributeDescription
filePath string

The path to the file to compile

Return:

object

An Object with the compiled result

Return Properties:

NameTypeAttributeDescription
hashInfo Object

The hash information returned from getHashForPath

code string

The source code if the file was a text file

binaryData Buffer

The file if it was a binary file

mimeType string

The MIME type saved in the cache.

dependentFiles string[]

The dependent files returned from compiling the file, if any.

public compileAll(rootDirectory: string, shouldCompile: Function): Promise source

Pre-caches an entire directory of files recursively. Usually used for building custom compiler tooling.

Params:

NameTypeAttributeDescription
rootDirectory string

The top-level directory to compile

shouldCompile Function

(optional) A Function which allows the caller to disable compiling certain files. It takes a fully-qualified path to a file, and should return a Boolean.

Return:

Promise

Completion.

public compileAllSync(rootDirectory: string, shouldCompile: Function): Promise source

Pre-caches an entire directory of files recursively. Usually used for building custom compiler tooling.

Params:

NameTypeAttributeDescription
rootDirectory string

The top-level directory to compile

shouldCompile Function

(optional) A Function which allows the caller to disable compiling certain files. It takes a fully-qualified path to a file, and should return a Boolean.

Return:

Promise

Completion.

public compileSync(filePath: *): * source

Params:

NameTypeAttributeDescription
filePath *

Return:

*

public saveConfiguration(): Promise source

Saves the current compiler configuration to a file that createReadonlyFromConfiguration can use to recreate the current compiler environment

Return:

Promise

Completion

public saveConfigurationSync(): Promise source

Saves the current compiler configuration to a file that createReadonlyFromConfiguration can use to recreate the current compiler environment

Return:

Promise

Completion