返回介绍

val-loader

发布于 2019-05-27 04:54:30 字数 6033 浏览 875 评论 0 收藏 0

Executes the given module to generate source code on build time

安装

npm i -D val-loader

用法

此 loader 所加载的模块必须符合以下接口

加载的模块必须使用以下函数接口,将 default export 导出为一个函数。

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

Modules transpiled by Babel are also supported

export default function () {...};

Function Interface

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

{Object}

Following the Object Interface

{Promise}

Resolving to an {Object} following the Object Interface

Object Interface

NameTypeDefaultDescription
code`{String\Buffer}`undefined(Required) The code that is passed to the next loader or to webpack
sourceMap{Object}undefined(Optional) Will be passed to the next loader or to webpack
ast{Array<{Object}>}undefined(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}>}[]An array of absolute, native paths to file dependencies that need to be watched for changes
contextDependencies{Array<{String}>}[]An array of absolute, native paths to directory dependencies that need to be watched for changes
cacheable{Boolean}falseFlag whether the code can be re-used in watch mode if none of the dependencies have changed

Options

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

Examples

If you have a module like this

answer.js

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

module.exports = answer;

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/answer.js'),
        use: [
          {
            loader: 'val-loader'
          }
        ]
      }
    ]
  }
};

Complete

A complete example of all available features looks like this

answer.js

const ask = require('./ask.js');
const generate = require('./generate.js');

function answer(options) {
  return ask(options.question)
    .then(generate)
    .then(result => ({
      ast: result.abstractSyntaxTree,
      code: result.code,
      // Mark dependencies of answer().
      // 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('./generate.js')
      ],
      // Flag the generated code as cacheable.
      // If none of the dependencies have changed,
      // the function won't be executed again.
      cacheable: true
      sourceMap: result.sourceMap,
    })
  );
}

module.exports = answer;

webpack.config.js

module.exports = {
  ...
  module: {
    rules: [
      {
        test: require.resolve('path/to/answer.js'),
        use: [
          {
            loader: 'val-loader',
            options: {
              question: 'What is the meaning of life?'
            }
          }
        ]
      }
    ]
  }
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文