What qualifies as being a dynamic export in ES6
Asked Answered
D

4

16

I hear that dynamic exports/imports are not allowed in es6.

This website Uses the example export default 5 * 7; as if it were a legal, static export. This seems reasonable since it clearly evaluates to the static value of 35, but I'm wondering what exactly qualifies as a static export now.

This Code uses export default Backbone.Router.extend({...}); as if it were a legal, static, export. This seems fishy to me as it seems like a dynamic export to me (exporting the result of a function call).

Deterioration answered 27/1, 2016 at 10:52 Comment(1)
you can export static promises resolving to dynamic content, or less cleanly simply references to objects.Jebel
C
13

The second example only exports the result of the function call, which is static. The function is only called once, thus the result will always be the same on every import.

An Example to illustrate:

f.js

function f() {
    return 2 * Math.random();
}

export default f(); // Is called, before the export is defined. Result: 1.23543

i1.js

import f from 'f';

console.log(f); // 1.23543

i2.js

import f from 'f';

console.log(f); // 1.23543 as well
Cilka answered 27/1, 2016 at 10:58 Comment(0)
D
11

All exports are static in ES6, that is, their exported name resolves to exactly one variable binding in the exporting module and this can be determined by a single look prior to evaluating of the module code.

A module cannot dynamically add or remove exports through execution of code, the list of exported names is fixed by the declaration.

Whether this variable holds a constant or the result of a function call doesn't matter, neither does whether it holds a primitive value or an object. It doesn't even need to be a constant, the content of the variable may change over time (see an example here).


All imports from import statements are static as well, which means that you can explore the dependency graph without executing any module code.

A dynamic import is done by an explicit call to the module loader. Such loads can depend on the control flow of the module, and may differ from run to run. The code needs to manually handle the asynchrony of the process and potential errors.

Damocles answered 27/1, 2016 at 10:59 Comment(0)
G
2

You can actually have "dynamic" exports through named exports.

If you do something like this

let awesome = 42;
export { awesome };

you're exporting a binding to the variable and not the value

you can later do this

import { awesome } from './awesome';
awesome = 100;

and any place awesome has been imported will now get the updated value regardless of when awesome was imported

reference: https://github.com/getify/You-Dont-Know-JS/blob/1st-ed/es6%20%26%20beyond/ch3.md#exporting-api-members

you can also have dynamic imports

import('/modules/my-module.js')
  .then((module) => {
    // Do something with the module.
  });

reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import

Gaal answered 9/12, 2019 at 15:31 Comment(0)
T
0

Dynamic export refers to the ability to export different things depending on some condition. The following is not allowed:

if (process.env.DEBUG) {
    export function log (txt) {
        console.log(txt)
    }
}

Everything else are considered static exports even if the thing exported is the result of function calls.

Note that this ability was possible in CommonJs:

module.exports = {};

if (process.env.DEBUG) {
    module.exports.log = (txt) => console.log(txt);
}
Tees answered 15/4 at 6:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.