npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

function-loader

v0.0.11

Published

A Webpack loader that executes tasks and transforms them into results at build time

Downloads

2

Readme

function-loader

A Webpack loader that executes tasks (modules exporting functions) and transforms them into results at build time.

You can see this loader as val-loader with APIs re-designed. We believe function-loader is much more useful when there're shared functions in both frontend and backend. Moreover, if you want to load assets but cannot find a proper loader, this loader allows you to quickly prototype a custom loader.

Requirements

  • Node v8.0.0 or above
  • Webpack v4.0.0 or above

Getting Started

Add a new rule in webpack.config.js.

// webpack.config.js
module.exports = {
  module: {
    rules: [{
      test: /\.task.js$/,
      loader: 'function-loader',
      exclude: /node_modules/
    }]
  }
}

Note that the rule /\.task.js$/ is a subset of /\.js$/. Be careful if you use babel or similar loaders to transform your source code.

Then, modules with name matching /\.task.js$/ will be loaded and transformed via function-loader.

// getMs.task.js
module.exports = ({ years }) => years * 365 * 24 * 60 * 60 * 1000
// app.js
import Ms from './getMs.task?years=2'  // Ms will be 63072000000

// ...

Yon can also export an array or a plain object from the task module. function-loader will respect the shape of the exported value and only transform functions into results in-place.

Loader Proxy API

The exported task functions will be executed under the context of loader proxies. The this variable has the following properties/functions

In order to allow this variable to work, don't use arrow function syntax.

this.isTask

only set to true when loaded by function-loader. Use this property to split frontend/backend logic.

this.webpack

only set to true when loaded by Webpack. See loader.webpack.

raw(value)

Calling this function with value=true will enable raw mode. In raw mode, your target function should return a string, which should be a valid module source passing down the loader chain.

this.emitError(obj)

Calling loader.emitError but accept strings as values.

this.emitWarning(obj)

Calling loader.emitWarning but accept strings as values.

this.cacheable(value)

This function directly call loader.cacheable.

this.addDependency(path)
this.addContextDependency(path)

Like loader.addDependency but these functions can work with relative paths.

this.clearDependencies()

This function directly call loader.clearDependencies.

this.run(task, query, callback)

This function allows you to run other tasks. Note that the target tasks will be automatically added to dependencies.

  • task: path of the target task, can be relative.
  • query: a plain object or null served as parameters.
  • callback(err, value): callback function that will be called when the task is succefully executed.
async this.runAsync(task, query)

The async version of the run method.

this.writeFile(path, cotent, sourceMap)

This functions directly call loader.emitFile.

Limitations

  • When or how much times task modules are loaded/executed depend on Webpack's caching mechanism. Make sure the side effects will not cause race conditions.
  • The query values and output results of the task modules should be able to be JSON-stringified.

TODO

  • Cache compiled functions

Similar Projects

License

MIT