Home Manual Reference Source
import {BuildTranspiler} from 'projext/src/services/building/buildTranspiler.js'
public class | source

BuildTranspiler

Manages the transpilation of target files using Babel.

Constructor Summary

Public Constructor
public

constructor(babelConfiguration: BabelConfiguration, appLogger: Logger, targets: Targets, utils: Utils)

Class constructor.

Member Summary

Public Members
public

A local reference for the appLogger service.

public

A local reference for the babelConfiguration service.

public

A local reference for the targets service.

public

A local reference for the utils service.

Method Summary

Public Methods
public

findFiles(directory: string, pattern: string): Promise<Array, Error>

Find files of a given type on a directory.

public

Get a target Babel configuration based on a filepath.

public

transpileFile(filepath: string | Object, buildType: string, options: Object, writeFile: boolean): Promise<Object|string, Error>

Transpile a file.

public

transpileFileSync(filepath: string | Object, buildType: string, options: Object, writeFile: boolean): Object | string

Synchronous version of transpileFile.

public

transpileTargetFiles(target: Target, buildType: string): Promise<undefined,Error

Transpile a target files for a given build type.

Public Constructors

public constructor(babelConfiguration: BabelConfiguration, appLogger: Logger, targets: Targets, utils: Utils) source

Class constructor.

Params:

NameTypeAttributeDescription
babelConfiguration BabelConfiguration

To get a target Babel configuration.

appLogger Logger

To print information messages after transpiling files.

targets Targets

To access targets information.

utils Utils

To normalize file extensions.

Public Members

public appLogger: Logger source

A local reference for the appLogger service.

public babelConfiguration: BabelConfiguration source

A local reference for the babelConfiguration service.

public targets: Targets source

A local reference for the targets service.

public utils: Utils source

A local reference for the utils service.

Public Methods

public findFiles(directory: string, pattern: string): Promise<Array, Error> source

Find files of a given type on a directory.

Params:

NameTypeAttributeDescription
directory string

The directory where the files will be searched for.

pattern string
  • optional
  • default: '**\/*.{js,jsx,ts,tsx}'

A glob pattern to match the files.

Return:

Promise<Array, Error>

If everything goes well, the promise will resolve on the list of files found.

public getTargetConfigurationForFile(file: string, buildType: string): Object source

Get a target Babel configuration based on a filepath.

Params:

NameTypeAttributeDescription
file string

The file that will be used to obtain the target and then the Babel configuration.

buildType string
  • optional
  • default: 'development'

The build type for which the configuration is needed for. This allows the method to check if the target has source map enabled for the build type, and if this happens, it will also enable it on the configuration it returns.

Return:

Object

public transpileFile(filepath: string | Object, buildType: string, options: Object, writeFile: boolean): Promise<Object|string, Error> source

Transpile a file.

Params:

NameTypeAttributeDescription
filepath string | Object

If used as a string, it's the path to the file to transpile; if used as an object, it should have source and output properties to define from where to where the file is transpiled.

buildType string
  • optional
  • default: 'development'

The build type for which the file is being transpiled for. If options is not specified, the method will try to load the target configuration based on the file path, and if the target has source maps enabled for the build type, it will tell Babel to create them.

options Object
  • optional
  • default: null
  • nullable: true

The Babel configuration to use. If not defined, the method will try to find a target configuration using the path of the file.

writeFile boolean
  • optional
  • default: true

If true, it will write the transpile code, otherwise, it will return it on the promise.

Return:

Promise<Object|string, Error>

If writeFile is true, the promise will resolve on an object with the keys filepath (the path where it was transpiled) and code; but if the parameter is false, the promise will resolve on a string with the path to the file.

public transpileFileSync(filepath: string | Object, buildType: string, options: Object, writeFile: boolean): Object | string source

Synchronous version of transpileFile.

Params:

NameTypeAttributeDescription
filepath string | Object

If used as a string, it's the path to the file to transpile; if used as an object, it should have source and output properties to define from where to where the file is transpiled.

buildType string
  • optional
  • default: 'development'

The build type for which the file is being transpiled for. If options is not specified, the method will try to load the target configuration based on the file path, and if the target has source maps enabled for the build type, it will tell Babel to create them.

options Object
  • optional
  • default: null
  • nullable: true

The Babel configuration to use. If not defined, the method will try to find a target configuration using the path of the file.

writeFile boolean
  • optional
  • default: true

If true, it will write the transpile code, otherwise, it will return it.

Return:

Object | string

If writeFile is true, it will return an object with the keys filepath (the path where it was transpiled) and code; but if the parameter is false, it will return a string with the path to the file.

public transpileTargetFiles(target: Target, buildType: string): Promise<undefined,Error source

Transpile a target files for a given build type. This requires the target files to have been previously copied to the distribution directory.

Params:

NameTypeAttributeDescription
target Target

The target information.

buildType string
  • optional
  • default: 'development'

The build type for which the target is being transpiled for. This will be used to read the source map settings of the target and tell Babel if it needs to create them.

Return:

Promise<undefined,Error