Skip to content

Fails to find BannerPlugin in Nuxt #394

@rchl

Description

@rchl

I've tried to implement support for @sentry/webpack-plugin@2 in the Nuxt module but run into an issue where this code crashes:

function webpackReleaseInjectionPlugin(injectionCode: string): UnpluginOptions {
return {
name: "sentry-webpack-release-injection-plugin",
webpack(compiler) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore webpack version compatibility shenanigans
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
const BannerPlugin = compiler?.webpack?.BannerPlugin || webback4or5?.BannerPlugin;
compiler.options.plugins = compiler.options.plugins || [];
compiler.options.plugins.push(
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-call
new BannerPlugin({
raw: true,
include: /\.(js|ts|jsx|tsx|mjs|cjs)$/,
banner: injectionCode,
})
);
},
};
}

with:

 FATAL  BannerPlugin is not a constructor
  at Object.webpack (node_modules/@sentry/webpack-plugin/dist/esm/index.mjs:20:7)
  at Object.apply (node_modules/unplugin/dist/index.mjs:1381:20)
  at webpack (node_modules/webpack/lib/webpack.js:51:13)
  at node_modules/@nuxt/webpack/dist/webpack.js:1821:24
  at Array.map (<anonymous>)
  at WebpackBundler.build (node_modules/@nuxt/webpack/dist/webpack.js:1820:37)
  at async Builder.build (node_modules/@nuxt/builder/dist/builder.js:249:5)
  at async Object.run (node_modules/@nuxt/cli/dist/cli-build.js:99:7)
  at async NuxtCommand.run (node_modules/@nuxt/cli/dist/cli-index.js:356:7)

It appears to be because of the import * as webback4or5 from "webpack" line where the relevant BannerPlugin import appears to be within the webback4or5.default object.

I think that's not unusual and the package should account for the import being potentially wrapped in default object since webpack is a CJS module and the behavior of how the exports will be transformed is not entirely well defined (when imported from ESM context at least).

The import webback4or5 has a structure like this here:

webback4or5: {
   debug: { ProfilingPlugin: [Getter] },
   default:
    { [Function: webpack]
      version: '4.46.0',
      WebpackOptionsDefaulter: [class WebpackOptionsDefaulter extends OptionsDefaulter],
      WebpackOptionsApply: [class WebpackOptionsApply extends OptionsApply],
      Compiler: [class Compiler extends Tapable],
      MultiCompiler: [class MultiCompiler extends Tapable],
      NodeEnvironmentPlugin: [class NodeEnvironmentPlugin],
      validate: [Function: bound validateSchema],
      validateSchema: [Function: validateSchema],
      WebpackOptionsValidationError: [class WebpackOptionsValidationError extends WebpackError],
      AutomaticPrefetchPlugin: [Getter],
      BannerPlugin: [Getter],
      CachePlugin: [Getter],
      ContextExclusionPlugin: [Getter],
      ContextReplacementPlugin: [Getter],
      DefinePlugin: [Getter],
      Dependency: [Getter],
      DllPlugin: [Getter],
      DllReferencePlugin: [Getter],
      EnvironmentPlugin: [Getter],
      EvalDevToolModulePlugin: [Getter],
      EvalSourceMapDevToolPlugin: [Getter],
      ExtendedAPIPlugin: [Getter],
      ExternalsPlugin: [Getter],
      HashedModuleIdsPlugin: [Getter],
      HotModuleReplacementPlugin: [Getter],
      IgnorePlugin: [Getter],
      LibraryTemplatePlugin: [Getter],
      LoaderOptionsPlugin: [Getter],
      LoaderTargetPlugin: [Getter],
      MemoryOutputFileSystem: [Getter],
      Module: [Getter],
      ModuleFilenameHelpers: [Getter],
      NamedChunksPlugin: [Getter],
      NamedModulesPlugin: [Getter],
      NoEmitOnErrorsPlugin: [Getter],
      NormalModuleReplacementPlugin: [Getter],
      PrefetchPlugin: [Getter],
      ProgressPlugin: [Getter],
      ProvidePlugin: [Getter],
      SetVarMainTemplatePlugin: [Getter],
      SingleEntryPlugin: [Getter],
      SourceMapDevToolPlugin: [Getter],
      Stats: [Getter],
      Template: [Getter],
      UmdMainTemplatePlugin: [Getter],
      WatchIgnorePlugin: [Getter],
      dependencies: [Object],
      optimize: [Object],
      web: [Object],
      webworker: [Object],
      node: [Object],
      debug: [Object],
      util: [Object] },
   dependencies: { DependencyReference: [Getter] },
   node: { NodeTemplatePlugin: [Getter], ReadFileCompileWasmTemplatePlugin: [Getter] },
   optimize:
    { AggressiveMergingPlugin: [Getter],
      AggressiveSplittingPlugin: [Getter],
      ChunkModuleIdRangePlugin: [Getter],
      LimitChunkCountPlugin: [Getter],
      MinChunkSizePlugin: [Getter],
      ModuleConcatenationPlugin: [Getter],
      OccurrenceOrderPlugin: [Getter],
      OccurrenceModuleOrderPlugin: [Getter],
      OccurrenceChunkOrderPlugin: [Getter],
      RuntimeChunkPlugin: [Getter],
      SideEffectsFlagPlugin: [Getter],
      SplitChunksPlugin: [Getter],
      UglifyJsPlugin: [Getter],
      CommonsChunkPlugin: [Getter] },
   util: { createHash: [Getter] },
   version: '4.46.0',
   web: { FetchCompileWasmTemplatePlugin: [Getter], JsonpTemplatePlugin: [Getter] },
   webworker: { WebWorkerTemplatePlugin: [Getter] }
}

Metadata

Metadata

Assignees

Labels

No labels
No labels
No fields configured for issues without a type.

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions