val-loader

[![npm][npm]][npm-url] [![node][node]][node-url] [![deps][deps]][deps-url] [![tests][tests]][tests-url] [![chat][chat]][chat-url]

A webpack loader which executes a given module, and returns the result of the execution at build-time, when the module is required in the bundle. In this way, the loader changes a module from code to a result.

Another way to view val-loader, is that it allows a user a way to make their own custom loader logic, without having to write a custom loader.

Requirements

This module requires a minimum of Node v6.9.0 and Webpack v4.0.0.

Getting Started

To begin, you’ll need to install val-loader:

  1. $ npm install val-loader --save-dev

Then add the loader to your webpack config. For example:

  1. // target-file.js
  2. module.exports = () => {
  3. return { code: 'module.exports = 42;' }
  4. };

webpack.config.js

  1. module.exports = {
  2. module: {
  3. rules: [
  4. {
  5. test: /target-file.js$/,
  6. use: [
  7. {
  8. loader: `val-loader`
  9. }
  10. ]
  11. }
  12. ]
  13. }
  14. }
  1. // src/entry.js
  2. const answer = require('test-file');

And run webpack via your preferred method.

Return Object Properties

Targeted modules of this loader must export either a Function or Promise that returns an object containing a code property at a minimum, but can contain any number of additional properties.

code

Type: String|Buffer Default: undefined Required

Code passed along to webpack or the next loader that will replace the module.

sourceMap

Type: Object Default: undefined

A source map passed along to webpack or the next loader.

ast

Type: Array[Object] Default: undefined

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

Type: Array[String] Default: []

An array of absolute, native paths to file dependencies that should be watched by webpack for changes.

contextDependencies

Type: Array[String] Default: []

An array of absolute, native paths to directory dependencies that should be watched by webpack for changes.

cacheable

Type: Boolean Default: false

If true, specifies that the code can be re-used in watch mode if none of the dependencies have changed.

Examples

In this example the loader is configured to operator on a file name of years-in-ms.js, execute the code, and store the result in the bundle as the result of the execution. This example passes years as an option, which corresponds to the years parameter in the target module exported function:

  1. // years-in-ms.js
  2. module.exports = function yearsInMs({ years }) {
  3. const value = years * 365 * 24 * 60 * 60 * 1000;
  4. // NOTE: this return value will replace the module in the bundle
  5. return { code: 'module.exports = ' + value };
  6. }
  1. // webpack.config.js
  2. module.exports = {
  3. ...
  4. module: {
  5. rules: [
  6. {
  7. test: require.resolve('src/years-in-ms.js'),
  8. use: [
  9. {
  10. loader: 'val-loader',
  11. options: {
  12. years: 10
  13. }
  14. }
  15. ]
  16. }
  17. ]
  18. }
  19. };

In the bundle, requiring the module then returns:

  1. // ... bundle code ...
  2. const tenYearsMs = require('years-in-ms'); // 315360000000
  3. // ... bundle code ...

require(“val-loader!tenyearsinms”) == 315360000000

License

MIT MIT” class=”icon-link” href=”#mit”>