0

So I know imports can be replaced with require.

import {x} from 'x' is equivalent to const {x} = require('x').

But what about import * from 'x' ? the only thing that I can imagine it being translated to is with(require('x')){... }.

Yet with statements are frown upon, aren't they? (because you don't know in compile time how a reference will be resolved)

caeus
  • 3,084
  • 1
  • 22
  • 36

2 Answers2

0
import * from "x";

is not valid JS.

import * as x from "x";

is.

How is that different from

import x from "x";

?

Well, the latter requires you to have a default export, while the former takes all the exports and groups them into one object.

RedGuy11
  • 344
  • 6
  • 14
0

I think you have the syntax confused. This:

import * from 'x'

is not valid. What you can do is:

import * as x from 'x';

This puts all the exports from the x file into the namespace (which is, for the most part, just a JavaScript object whose properties and default correspond to the names of the exports). The x identifier that gets imported refers to this namespace.

Also

import {x} from 'x' is equivalent to const {x} = require('x').

is not correct - require is CommonJS syntax, and import/export is ES6 module syntax. They're not interchangeable by default, though they do very similar things.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320