0

I'm writing a Webpack plugin that, besides its main purpose, needs to add some global variables to the build. Normally, I would use definePlugin for declaring global variables, but I can't seem to easily replicate its behaviour inside my plugin. Is there any way to call definePlugin from inside of my plugin, or is there another way to declare global variables inside of a Webpack plugin?

I realise I can just copy the entirety of the definePlugin code, but I feel like there has to be a cleaner and more future-proof solution?

I could also pass a --env=… argument when running webpack, but this would require me to edit the command manually each time, and I'm not even sure I could reach it in the plugin that way.

Adriaan Marain
  • 294
  • 3
  • 10

1 Answers1

1

I found my answer in this question: webpack plugin that adds other plugins

I can run this to also call DefinePlugin during the build process:

apply(compiler: webpack.Compiler) {
    const versionId = '1234';

    new webpack.DefinePlugin({
        VERSION: JSON.stringify(versionId)
    }).apply(compiler);

   // other code
}
Adriaan Marain
  • 294
  • 3
  • 10