Rafael Xavier
Rafael Xavier

Reputation: 2899

Is there a webpack dependency object that takes content instead of filepath? (developing plugin)

A webpack plugin can transform require("something") into something like __webpack_require__(165) using CommonJsRequireDependency as in lib/dependencies/CommonJsRequireDependencyParserPlugin.js#L74-L82. CommonJsRequireDependency takes request (a filepath) and a range.

Is there any dependency object that instead of passing the filepath, takes the file content itself? (I want to generate the contents dynamically).

PS: I had this question while implementing a plugin that injects a dependency dynamically generated on the fly.

Upvotes: 3

Views: 183

Answers (1)

Adam Sax
Adam Sax

Reputation: 127

I don't know if you can make the context of the require() statement itself dynamic beyond the dynamic context stuff included in webpack (http://webpack.github.io/docs/context.html)

However, I ran into a similar problem while dealing with creating config json based off of environment variables. I ended up creating a custom loader that would create the dynamic content. It ended up looking something like this:

var config = require('config!.)

and then my custom loader was something like:

module.exports = function(source) {
    this.cacheable();
    var callback = this.async();

    myLib.getConfig()
        .then(function(config) {
            callback(null, config)
        });
}

Upvotes: 1

Related Questions