webpackumd

Webpack: How to export a single class as a UMD library?


In my project semantic-version, I am exporting a UMD bundle with webpack.

When imported with <script src="bundles/semVersion.js"></script> in a HTML file, the interesting single class is then available under SemVersion.SemVersion() (<lib name>.<class name>()). How can I do to have it directly exposed as SimVersion()? (Actually like jQuery for example).

Thank you!


Solution

  • webpack has export option to export whatever in your library. It can be used in your case where a class as being exported as default.

    {
        output: {
            library: {
                name: 'SemVersion',
                type: 'umd',
                 // add this to export your class to the library
                export: "default"
            },
        },
    }