S
S
SomeHuman22021-12-30 19:17:03
webpack
SomeHuman2, 2021-12-30 19:17:03

How does webpack loader context work?

Hello, I wrote such a loader

const loaderUtils = require("loader-utils");

module.exports = function loader(content, options, map, meta) {
  if (!options) options = loaderUtils.getOptions(this);
  if (options.output) {
    const name = loaderUtils.interpolateName(this, options.output, options);
    this.emitFile(name, content);
  }
  content = "123loadertext";
  this.callback(null, content, map, meta);
};

the bottom line is that it should be able to be called as
require(`lite-file-loader?{"output":"sameName.txt"}!..someContentOfLoaderChain`)

so
const emitFile = require("lite-file-loader");
          emitFile(code, { output: "somename.txt" });

In the first case everything works but in the second
Template execution failed: TypeError: this.emitFile is not a function

Why is the context of the loader normal the first time, but not the second time))

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question