1
import `moment/locale/fr`;

I've just noticed that we can't use string interpolation backtick with import statement? In general Js file backtick to be used to support variable but even if we don't provide a variable, it will be working fine. But apparently not in this case with import statement? Is that done on purpose or something that the JS community missed out?

Isaac
  • 12,042
  • 16
  • 52
  • 116
  • 1
    Must have been removed from a newer version of webpack, I remember it used to work but just tested and doesn't work anymore. I'm pretty sure using backticks with a variable would not work anyway. – HMR Jan 30 '19 at 08:28
  • @vlaz, this question is not asking how to dynamic import. This question is asking why backtick isn't working but single quote working with the import statement. – Isaac Jan 30 '19 at 08:30
  • @Isaac "*In addition to Felix's answer, I'll note explicitly that this is not currently allowed by the ECMAScript 6 grammar*" - the second most voted answer. There are also more practical explanations: "*`import` and `export` are defined in such a way that they are statically analyzable, so they cannot depend on runtime information.*" A template string *by definition* is evaluated at runtime. So, everything you are asking is already covered, it's only the title that doesn't really match. – VLAZ Jan 30 '19 at 08:34
  • @HMR: Yes you're right as dynamic import is not supported now. Just out of curiousity that backtick isn't supporting with `import` statement. Is `import` statement keyword belongs to Webpack or JS? – Isaac Jan 30 '19 at 08:34
  • @vlaz: Thank you very much for the explicit explanation which definitely gives me a better picture. – Isaac Jan 30 '19 at 08:38
  • Import can be [natively supported](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) but I'm not sure how to set webpack/babel not to compile all resources and leave the import as is. – HMR Jan 30 '19 at 08:43

1 Answers1

2

Because linking, where modules are loaded, is a pre-runtime process.

From what I'm guessing, since template literals signal an intent for interpolation which would be executed at runtime, only vanilla string literals are allowed.

FWIW there's a Stage 3 Proposal for Dynamic Imports using a function-like import() statement which like it's name states, allows dynamic imports.

nicholaswmin
  • 21,686
  • 15
  • 91
  • 167
  • Hi Nik I do aware that as of now dynamic import is not supporting. This question is merely asking why backtick can't be used with import statement but single quote working fine – Isaac Jan 30 '19 at 08:31
  • Yes I get that, I've explained why in the very first line. Linking using "standard" `imports` is a *pre-runtime* process so interpolation doesn't work. – nicholaswmin Jan 30 '19 at 08:32