Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does import create a new copy of imported library?

I'm using webpack + vue-loader to create vuejs app. I have multiple .vue files for components. When I write something like this:

import _ from 'lodash'

inside the script part of ComponentA.vue and ComponentB.vue, does this create two separate copies of lodash or does it simply import a reference?

like image 534
King Julien Avatar asked Jun 22 '16 10:06

King Julien


People also ask

Does import copy the code in Python?

No. Importing is not #include . It doesn't dump another file into yours. I explained the basics of module loading in the answer: the module's code is executed in a new scope.

What happens when you import a module Python?

When a module is first imported, Python searches for the module and if found, it creates a module object 1, initializing it. If the named module cannot be found, a ModuleNotFoundError is raised. Python implements various strategies to search for the named module when the import machinery is invoked.

What is difference between require and import?

One of the major differences between require() and import() is that require() can be called from anywhere inside the program whereas import() cannot be called conditionally, it always runs at the beginning of the file. To use the require() statement, a module must be saved with .

What's the difference between export a product and import a product?

Exports describe selling products and solutions created in the home country to other markets. Imports are stemmed from the theoretical meaning of bringing in goods and services into the port of a country. An import in the obtaining country is an export to the sending nation.


1 Answers

Importing any part of an ES6 module (default or named exports) produces an immutable binding.

CommonJS modules export values, while ES6 modules export immutable bindings. This blog post explains what that means.

[ Source: ES6 Module Exports ]

So the answer is no, it does not create a copy of the exports. The module is initialised once and each import will receive a reference to the same value.

like image 99
sdgluck Avatar answered Sep 20 '22 23:09

sdgluck