node package manager

val-loader

npm node npm-stats deps travis appveyor coverage chat

Val Loader

Executes the given module to generate source code on build time.

Install

npm install val-loader --save-dev

Examples

If you have a module findAnswer.js like this:

function findAnswer() {
    return {
        code: 'module.exports = 42;'
    };
}
 
module.exports = findAnswer;

you can use the val-loader to generate source code on build time:

// webpack.config.js 
module.exports = {
    ...
    module: {
        rules: [{
            test: require.resolve('path/to/findAnswer.js'),
            use: [{
                loader: 'val-loader'
            }]
        }]
    }
};

A complete example of all available features looks like this:

const ask = require('./ask.js');
const generateResult = require('./generateResult.js');
 
function findAnswer(options) {
    return ask(options.question)
        .then(generateResult)
        .then(result => ({
 
            code: result.code,
            sourceMap: result.sourceMap,
            ast: result.abstractSyntaxTree,
 
            // Mark dependencies of findAnswer(). 
            // The function will be re-executed if one of these 
            // dependencies has changed in watch mode. 
            dependencies: [
                // Array of absolute native paths! 
                require.resolve('./ask.js'),
                require.resolve('./generateResult.js')
            ],
 
            // Flag the generated code as cacheable. 
            // If none of the dependencies have changed, 
            // the function won't be executed again. 
            cacheable: true
 
        }));
}
 
module.exports = findAnswer;
// webpack.config.js 
module.exports = {
    ...
    module: {
        rules: [{
            test: require.resolve('path/to/findAnswer.js'),
            use: [{
                loader: 'val-loader',
                options: {
                    question: 'What is the meaning of life?'
                }
            }]
        }]
    }
};

Usage

The module that is loaded with this loader must stick to the following interfaces:

Expected module interface

The loaded module must export a function as default export with the following function interface.

module.exports = function (...) { ... };

Modules transpiled by Babel are also supported.

export default function (...) { ... }

Expected function interface

The function will be called with the loader options and must either return:

  • an object with the following object interface or
  • a promise that resolves to the following object interface

Expected object interface

Property Type Description
code string|Buffer Required. The code that is passed to the next loader or to webpack.
sourceMap SourceMap Optional. Will be passed to the next loader or to webpack.
ast any Optional. An Abstract Syntax Tree that will be passed to the next loader. Useful to speed up the build time if the next loader uses the same AST.
dependencies Array<string> Default: []. An array of absolute, native paths to file dependencies that need to be watched for changes.
cacheable boolean Default: false. Flag whether the code can be re-used in watch mode if none of the dependencies have changed.

Loader Options

The val-loader itself has no options. The options are passed as they are (without cloning them) to the exported function.

Maintainers


Juho Vepsäläinen

Joshua Wiens

Kees Kluskens

Johannes Ewald