Search code examples
javascriptecmascript-6vue.jsvue-loader

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?


Solution

  • 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.