Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write a Webpack plugin to generate index.js files on demand?

In general, I want to know how to do code-generation/fabrication in a Webpack plugin on demand. I want to generate contents for files that do not exist when they are "required."

Specifically, I want a plugin that, when I require a directory, automatically requires all files in that directory (recursively).

For example, suppose we have the directory structure:

  • foo
    • bar.js
    • baz.js
  • main.js

And main.js has:

var foo = require("./foo");
// ...

I want webpack to automatically generate foo/index.js:

module.exports = {
  bar: require("./bar"),
  baz: require("./baz")
};

I've read most of the webpack docs. github.com/webpack/docs/wiki/How-to-write-a-plugin has an example of generating assets. However, I can't find an example of how to generate an asset on demand. It seems this should be a Resolver, but resolvers seem to only output file paths, not file contents.

like image 349
Shane Brinkman-Davis Avatar asked Dec 26 '15 18:12

Shane Brinkman-Davis


People also ask

How do I bundle a JavaScript file using webpack?

You can bundle your JavaScript using the CLI command by providing an entry file and output path. Webpack will automatically resolve all dependencies from import and require and bundle them into a single output together with your app's script.

Does webpack need Index JS?

Out of the box, webpack won't require you to use a configuration file. However, it will assume the entry point of your project is src/index. js and will output the result in dist/main. js minified and optimized for production.

What does html webpack plugin do?

The HtmlWebpackPlugin simplifies creation of HTML files to serve your webpack bundles. This is especially useful for webpack bundles that include a hash in the filename which changes every compilation.

What is a webpack plugin?

A webpack plugin is a JavaScript object that has an apply method. This apply method is called by the webpack compiler, giving access to the entire compilation lifecycle.


1 Answers

Actually for your use case:

Specifically, I want a plugin that, when I require a directory, automatically requires all files in that directory (recursively).

you don't need a plugin. See How to load all files in a subdirectories using webpack without require statements

Doing code-generation/fabrication on demand can be done in JavaScript quite easily, why would you restrict your code generation specifically to only applied, when "required" by WebPack?

As NodeJS itself will look for an index.js, if you require a directory, you can quite easily generate arbitrary exports:

//index.js generating dynamic exports
var time = new Date();

var dynamicExport = {
  staticFn : function() {
    console.log('Time is:', time);
  }
}

//dynamically create a function as a property in dynamicExport
//here you could add some file processing logic that is requiring stuff on demand and export it accordingly
dynamicExport['dyn' + time.getDay()] = function() {
  console.log('Take this Java!');
}

module.exports = dynamicExport;
like image 101
Christian Ulbrich Avatar answered Oct 02 '22 12:10

Christian Ulbrich