muffel
muffel

Reputation: 7390

Webpack: is there a loader that just ignores require() calls?

Consider a JavaScript file with some

require("some-module")

I want Webpack to just ignore the require and output it "as-is" to the resulting JS. This can be achieved with the externals configuration parameter, but I would prefer some inline annotation like

require("emit-this-as-is-loader!some-module")

Background: I need to create a node.js-targeted output which requires() many auto-generated files (these are generated during and after the Webpack build phase, so they can't be injected using the corresponding content loaders). Specifying all these auto-generated files as 'externals' clutters my config and is error prone, as each require() to an auto-generated file only appears once.

Is there any way to do this without re-inventing the will and writing a custom loader?

Upvotes: 1

Views: 133

Answers (2)

Filip Dupanović
Filip Dupanović

Reputation: 33690

You can try using external-loader, which would give you the opportunity to define externals in place, at the point of reference:

const script = require('external-loader!./script');

Contrast this to specifying all your externals with configuration.externals in advance, before the compiler instance is created.

Upvotes: 1

Sebastian Sebald
Sebastian Sebald

Reputation: 16886

Does the target config parameter help here Here is the documentation for it: https://webpack.github.io/docs/configuration.html#target

Upvotes: 0

Related Questions