Skip to content Skip to sidebar Skip to footer

Exporting Outside Webpack

This is just something I thought today and I didn't see a lot of information so I'm going to share this weird cases and how I personally solved them (if there's a better way pleas

Solution 1:

The solution is to create our own __non_webpack_module__ (as webpack does with __non_webpack_require__.

How I did it is using webpack.BannerPlugin to inject some code outside the bundle. This code is prepended to the build after the minification is done, so it's preserved safely.

In your webpack.config.js:

plugins: [
  newBannerPlugin({
    raw: true,
    banner: `const __non_webpack_module__ = module;`,
  }),
]

And again, if you are using TypeScript, in global.d.ts:

declareconst __non_webpack_module__: NodeModule;

And now, you can do something like this in your code:

__non_webpack_module__.exports = /* your class/function/data/whatever */

This will allow to import it as usual from other files

Tip: You might want to look at BannerPlugin to check other options, like include or exclude so this variable is only generated on the desired files, etc.

Post a Comment for "Exporting Outside Webpack"