Transform Requests

The built-in transformModules target is a powerful way to customize the build process for a partiular file or set of files. Many common Targets are implemented using the transformModules target and a custom transformer module.

Interceptors of this target receive a single function as their first argument. This is the addTransform function documented below.

Typedefs

addTransform

Add a request to transform a file in the build. This function is passed as the first argument to an interceptor of the transformModules target.

TransformRequest : Object

Instruction for configuring Webpack to apply custom transformations to one particular file. The configureWebpack() function gathers TransformRequests from all interceptors of the transformModules target and turns them into a configuration of Webpack module rules.

TransformType : enum

Kind: global enum
Properties

Name Type Default Description
source string "source" Process the source code of fileToTransform through the transformModule as text. When applying a source TransformRequest, Buildpack will use the transformModule as a Webpack loader, so it must implement that interface. Any Webpack loader can be used as a transformModule for source TransformRequests. source transforms are fast and can run on source code of any language, but they aren’t as precise and safe as AST-type transforms when modifying code.
babel string "babel" Process the abstract syntax tree of the ES module specified by fileToTransform through the transformModule as a Babel AST. When applying a babel TransformRequest, Buildpack will use the transformModule as a Babel plugin, so it must implement that interface. Any Babel plugin can be used as a transformModule for babel TransformRequests. babel transforms are powerful and versatile, giving the transformer much more insight into the structure of the source code to modify. However, they are slower than source transforms, and they can only work on ES Modules.

addTransform ⇒

Add a request to transform a file in the build. This function is passed as the first argument to an interceptor of the transformModules target.

Kind: global typedef
Returns: null

Param Type Description
req TransformRequest Instruction object for the requested transform, including the transform to apply, the target source code, and other options.

TransformRequest : Object

Instruction for configuring Webpack to apply custom transformations to one particular file. The configureWebpack() function gathers TransformRequests from all interceptors of the transformModules target and turns them into a configuration of Webpack module rules.

Kind: global typedef
Properties

Name Type Description
type TransformType The type of transformation to apply.
requestor string Name of the module making this request. Used for debugging purposes.
fileToTransform string Resolvable path to the file to be transformed itself, the same path that you’d use in import or require().
transformModule string Absolute path to the Node module that will actually be doing the transforming. This path may be resolved using different rules at different times, so it’s best for this path to always be absolute.
[options] object Config values to send to the transform function. Note: Options should be serializable to JSON as Webpack loader options and/or Babel plugin options..

For implementation details View Source.