jackocnr / intl-tel-input

A JavaScript plugin for entering and validating international telephone numbers. React and Vue components are also included.
https://intl-tel-input.com
MIT License
7.6k stars 1.94k forks source link

[Webpack] Compilation warning when initializing `intlTelInput` with `utilsScript` #1620

Closed carlssonemil closed 4 months ago

carlssonemil commented 4 months ago

Plugin version

v23.0.4

Steps to reproduce

  1. Have a webpack project (We use Vue)
  2. Import intlTelInput
  3. Initialise it with , e.g. intlTelInput(input, { utilsScript: <url_to_script> })

Expected behaviour

Input should be initialized and working.

Actual behaviour

The webpack compilation fails with the following error:

Critical dependency: the request of a dependency is an expression

Initialisation options

Here's how our initialisation looks:

const INTL_TEL_INPUT_UTILS_SCRIPT_URL = `https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/23.0.4/js/utils.min.js`;

this.telInput = intlTelInput(input, {
  utilsScript: INTL_TEL_INPUT_UTILS_SCRIPT_URL,
});

Additional information

I'm guessing the new way of handling the utilsScript is what's causing the issue. Right now we're doing what is outlined in the Initialisation options by providing an URL to the script. We're also using Vue so the React bundle with the utils script included won't work for us.

With the changes introduced in v23.0.0 that dynamically imports the utils script file rather than injecting a script tag, our Webpack setup seems to have an issue with the dynamic import. More specifically it has a problem with the loadUtils method:

var loadUtils = (path) => {
  if (!intlTelInput.utils && !intlTelInput.startedLoadingUtilsScript) {
    intlTelInput.startedLoadingUtilsScript = true;
    return new Promise((resolve, reject) => {
      import(path).then(({ default: utils2 }) => {
        intlTelInput.utils = utils2;
        forEachInstance("handleUtils");
        resolve(true);
      }).catch(() => {
        forEachInstance("rejectUtilsScriptPromise");
        reject();
      });
    });
  }
  return null;
};

And most likely the import(path).then() method is the real culprit, as @BRAiNCHiLD95 also confirmed in #1616.


We're using Webpack v.5 if it's of any help.

jackocnr commented 4 months ago

Thanks for the write-up. This should now be fixed in v23.0.5.

carlssonemil commented 4 months ago

@jackocnr thanks for the quick fix! We'll update and confirm if it works!

BRAiNCHiLD95 commented 4 months ago

@jackocnr the magic comment worked! No more webpack warnings!