optional dependencies when using es6 import/export

I am trying to define optional externals in an es6 project.
The feature exists – #339 , but there is no flag that I can set in the (externals) configuration to make a dependency optional. Digging into the code I found that optional is a flag checking for reasons, and if every reason’s dependency is marked as optional. But I can’t seem to find where it is defined.

What would be the best way to define an external dependency optional? Have I missed something and it is possible doing it using configuration?

Author: Fantashit

3 thoughts on “optional dependencies when using es6 import/export

  1. Thanks

    But 😄
    to keep an es6 (or esm) project consistent, I don’t want to use require when importing anything. I use the import keyword.
    As a fallback, we are also offering a UMD module, built with webpack. The UMD definition has the option to automatically define the require as optional, but, as i wrote, it is not a flag anywhere I can find.
    You can see it here – https://github.com/webpack/webpack/blob/master/lib/UmdMainTemplatePlugin.js#L162
    and here for AMD externals as global – https://github.com/webpack/webpack/blob/master/lib/UmdMainTemplatePlugin.js#L97

    All I actually want to know is how to get the (external) module to be defined as optional, without using the try-catch method.

  2. Hi everyone 👋

    I’ve read a few issues across GitHub of libraries using optional externals via a try/catch. None of them are fully resolved.

    The major issue is that you get a warning Module not found: Error: Can't resolve 'xxx'.

    If a library wants to offer optional adapters for third-party dependencies out-of-the-box, its users will receive a warning for each optional dependencies that this user doesn’t need. I’m not sure that this is an acceptable behaviour.

    To be more precise.

    Current context: wrap require('xxx') in a try/catch and register xxx as an external in webpack configurations.
    Current behaviour: library users get WARNING Module not found: Error: Can't resolve 'xxx'
    Expected behaviour: no warning

    Am I missing something?

    Here’s a couple of those issues I was talking about:

Comments are closed.