How to pass metadata from a loader to webpack

The loader returns the sourcecode and optional a sourcemap by calling
this.callback(null, source, map); But how can I return additional metadata which should be retrieved in a webpack plugin when iterating over all modules?

The real problem is when generating messages for translation from modules:

  • there is the babel-react-intl plugin, which returns the messages it finds in the module in the metadata[react-int] property. (babel returns not only source code and source map but also the ast and the metadata property )
  • babel loader returns only source code and source map but nothing else so I would like to modify it to return the metadata too, so that I can access it in a webpack plugin via compilation.modules[moduleId].metadataor via the already found compilation.modules[moduleId].meta property

How can this be done?
Thanks
Ognian

Author: Fantashit

1 thought on “How to pass metadata from a loader to webpack

  1. Plugins can add properties to the loader context.

    Add a function to the loader context (i. e. choose a property the __dirname of your plugin). The loader call this method to pass metadata to the plugin.

    https://github.com/webpack/webpack/blob/master/lib/NormalModule.js#L121

    compilation.plugin("normal-module-loader", function(context, module) {
      context[__dirname] = function(metadata) {
        // do something with metadata and module
      };
    });

    Keep in mind that if the loader in cacheable, it’s not called in incremental compilation if nothing was changed.

Comments are closed.