Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optional dependencies when using es6 import/export #7713

Closed
RaananW opened this issue Jul 12, 2018 · 8 comments
Closed

optional dependencies when using es6 import/export #7713

RaananW opened this issue Jul 12, 2018 · 8 comments
Labels

Comments

@RaananW
Copy link
Contributor

RaananW commented Jul 12, 2018

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?

@sokra
Copy link
Member

sokra commented Jul 12, 2018

try {
  require("external");
} catch(e) {
  // fallback
}

@RaananW
Copy link
Contributor Author

RaananW commented Jul 13, 2018

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.

@webpack-bot
Copy link
Contributor

This issue had no activity for at least half a year.

It's subject to automatic issue closing if there is no activity in the next 15 days.

@alexander-akait
Copy link
Member

Answer above #7713 (comment)

@RaananW
Copy link
Contributor Author

RaananW commented Jan 12, 2019

I know closing the ticket is automated, but this ticket is not resolved.
The issue is the fact that this has to be done manually, so es6 projects can't use this feature. Unless there is something I'm missing, of course.
The feature is already there, there is just a need to expose it using configuration.

@lorisleiva
Copy link

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:

@ozyman42
Copy link
Contributor

ozyman42 commented Aug 4, 2019

@lorisleiva here's another example: websockets/ws#1220
What would recommend in this case and what is the best fix? I dislike the idea that I need to know about every transitive dependency which has optional dependencies and I need to go explicitly externalize them in the webpack config. While this could lead to a can of worms implementation, it would be nice if webpack could analyze the code to see if the require is within a try catch, then auto externalize if so.

Vinnl added a commit to inrupt/solid-client-js that referenced this issue May 7, 2020
Vinnl added a commit to inrupt/solid-client-js that referenced this issue May 7, 2020
Webpack issues a warning when trying to import() a module that is
not present, whereas it does not do so with import(). Since we're
requiring a bundler anyway (since we're using npm package names
rather than import paths), we're not any worse off using a non-
standard module system for these.

For more info, see: webpack/webpack#7713
Vinnl added a commit to inrupt/solid-client-js that referenced this issue May 8, 2020
Webpack issues a warning when trying to import() a module that is
not present, whereas it does not do so with import(). Since we're
requiring a bundler anyway (since we're using npm package names
rather than import paths), we're not any worse off using a non-
standard module system for these.

For more info, see: webpack/webpack#7713
Vinnl added a commit to inrupt/solid-client-js that referenced this issue May 8, 2020
Webpack issues a warning when trying to import() a module that is
not present, whereas it does not do so with require(). Since we're
requiring a bundler anyway (since we're using npm package names
rather than import paths), we're not any worse off using a non-
standard module system for these.

For more info, see: webpack/webpack#7713
Vinnl added a commit to inrupt/solid-client-js that referenced this issue May 8, 2020
Webpack issues a warning when trying to import() a module that is
not present, whereas it does not do so — or at least, not in a way
that blocks Next.js from building the app — with require(). Since
we're requiring a bundler anyway (since we're using npm package
names rather than import paths), we're not any worse off using a
non-standard module system for these.

For more info, see: webpack/webpack#7713
Vinnl added a commit to inrupt/solid-client-js that referenced this issue May 11, 2020
Webpack issues a warning when trying to import() a module that is
not present, whereas it does not do so — or at least, not in a way
that blocks Next.js from building the app — with require(). Since
we're requiring a bundler anyway (since we're using npm package
names rather than import paths), we're not any worse off using a
non-standard module system for these.

For more info, see: webpack/webpack#7713
Vinnl added a commit to inrupt/solid-client-js that referenced this issue May 11, 2020
Webpack issues a warning when trying to import() a module that is
not present, whereas it does not do so — or at least, not in a way
that blocks Next.js from building the app — with require(). Since
we're requiring a bundler anyway (since we're using npm package
names rather than import paths), we're not any worse off using a
non-standard module system for these.

For more info, see: webpack/webpack#7713
@slorber
Copy link

slorber commented Sep 1, 2020

Hey,

If there is a good solution to this problem, please let me know.

In the meantime, you can probably disable the warning with warningsFilter option: https://webpack.js.org/configuration/stats/#statswarningsfilter

        stats: {
          warningsFilter: [
            (warning) =>
              warning.includes("Can't resolve 'xyz"),
          ],
        },

Not sure it will work for you because our setup is not very usual (we actually duplicate some code from Webpack), but this worked for me.

Edit, for Webpack 5 I believe this has changed a bit and now it became:

        ignoreWarnings: [
          (e) => e.message.includes("Can't resolve 'xyz'"),
        ],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants