GithubHelp home page GithubHelp logo

anikethsaha / val-loader Goto Github PK

View Code? Open in Web Editor NEW

This project forked from webpack-contrib/val-loader

0.0 1.0 0.0 223 KB

val loader module for webpack

License: MIT License

JavaScript 100.00%

val-loader's Introduction

npm node deps tests chat

val-loader

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:

$ npm install val-loader --save-dev

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

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

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /target-file.js$/,
        use: [
          {
            loader: `val-loader`
          }
        ]
      }
    ]
  }
}
// src/entry.js
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:

// years-in-ms.js
module.exports = function yearsInMs({ years }) {
  const value = years * 365 * 24 * 60 * 60 * 1000;
  // NOTE: this return value will replace the module in the bundle
  return { code: 'module.exports = ' + value };
}
// webpack.config.js
module.exports = {
  ...
  module: {
    rules: [
      {
        test: require.resolve('src/years-in-ms.js'),
        use: [
          {
            loader: 'val-loader',
            options: {
              years: 10
            }
          }
        ]
      }
    ]
  }
};

In the bundle, requiring the module then returns:

// ... bundle code ...

  const tenYearsMs = require('years-in-ms'); // 315360000000

// ... bundle code ...

require("val-loader!tenyearsinms") == 315360000000

License

val-loader's People

Contributors

jhnns avatar joshwiens avatar michael-ciniawsky avatar sokra avatar raleksandar avatar agundermann avatar shellscape avatar evilebottnawi avatar spacek33z avatar kevinzwhuang avatar thesebas avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.