ES6 - Export module with getter

I would like to export a module that receives a module definition from some global object.

This is something like:

export {
  get DynamicModule() {
    return __globalFluxStorage.state.property.property.property.property
  }
}

...

import {DynamicModule} from 'dynamic-module'

We have a complex thread store, and DynamicModule is just a means of accessing __globalFluxStorage.state.property.property.property.property without having to enter a long property accessory. Is it possible? Thanks.

Edit:

Since I am using babel, try something like this:

Object.defineProperty(module.exports, "Forms", {
  get: function() {
    return __globalFluxStorage.state.property.property.property.property
  }
});

But does not work, i.e. {DynamicModule} isundefined

+4
source share
2 answers

No, it is not possible to create a getter to export a module — these are binding variables, not properties.

However, you can simply do the default export:

export default __globalFluxStorage.state.property.property.property.property;

import DynamicModule from 'dynamic-module';

, :

export var DynamicModule = __globalFluxStorage.state.property.property.property.property;

import {DynamicModule} from 'dynamic-module';

, :

export var DynamicModule;
DynamicModule = __globalFluxStorage.state.property.property.property.property;

( Promise EventEmitter)

+4

, , .

export const _ = {
  get DynamicModuleGetter() {return __globalFluxStorage.state.property.property.property.property}
}

export function DynamicModuleFunction() {return __globalFluxStorage.state.property.property.property.property}

import

import { _, DynamicModuleFunction } from 'dynamic-module'

// getter
const value1 = _.DynamicModuleGetter
const {DynamicModuleGetter} = _        // this evaluates the getter

// function
const value2 = DynamicModuleFunction()

let obj = {
  foo: {
    bar: {
      baz: {
        bak: {
          value: 1
        },
        fak: {
          value: 2
        }
      }
    }
  }
}

export const _ = {
  get shortcut() {return obj.foo.bar.baz}
}

export function shortcut() {return obj.foo.bar.baz}

import { _, shortcut } from './shortcut'

let g = _.shortcut.bak.value       // g = 1
let f = shortcut().fak.value       // f = 2
let {shortcut: {bak: {value}}} = _ // value = 1
0

Source: https://habr.com/ru/post/1627134/


All Articles