最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to export constants defined using object destructuring - Stack Overflow

programmeradmin1浏览0评论

Guided by eslint's prefer-destructuring rule, I defined some constants like this:

const {
    NODE_ENV,
    API_URL,
} = process.env;

Is it possible to export these constants by prefixing the statement by export?

export const {
    NODE_ENV,
    API_URL,
} = process.env;

This would seem natural, but eslint-plugin-import plains about a violation of the import/named rule: API_URL not found in '../constants'. In fact, this usage of export is also not described on the relevant MDN page.

Do we then have to repeat all constants in a separate export statement?

const {
    NODE_ENV,
    API_URL,
} = process.env;

export {
    NODE_ENV,
    API_URL,
};

Guided by eslint's prefer-destructuring rule, I defined some constants like this:

const {
    NODE_ENV,
    API_URL,
} = process.env;

Is it possible to export these constants by prefixing the statement by export?

export const {
    NODE_ENV,
    API_URL,
} = process.env;

This would seem natural, but eslint-plugin-import plains about a violation of the import/named rule: API_URL not found in '../constants'. In fact, this usage of export is also not described on the relevant MDN page.

Do we then have to repeat all constants in a separate export statement?

const {
    NODE_ENV,
    API_URL,
} = process.env;

export {
    NODE_ENV,
    API_URL,
};
Share asked Jan 1, 2019 at 12:43 ClaudioClaudio 3,9983 gold badges20 silver badges25 bronze badges 1
  • 1 @CertainPerformance No, that question has nothing to do with it. OP wants syntax for named exports, not build another object. – Bergi Commented Jan 1, 2019 at 13:53
Add a ment  | 

2 Answers 2

Reset to default 5

Is it possible to export these constants by prefixing the statement by export?

export const {
    NODE_ENV,
    API_URL,
} = process.env;

Yes, this is totally valid according to the spec. You can use destructuring patterns in the declarations of exported consts.

This would seem natural, but eslint-plugin-import plains about a violation of the import/named rule: API_URL not found in '../constants'.

Sounds like that plugin is broken. In fact, your exact use case was reported as working before.

Article 15.2.2.3 of the spec says:

 ...
ExportDeclaration : export VariableStatement
ExportDeclaration : export Declaration

Article 13.1.4 says:

Declaration : LexicalDeclaration

Article 13.3 says:

LexicalDeclaration:
   LetOrConst BindingList;

LetOrConst :
 let
 const

BindingList :
 LexicalBinding
 BindingList, LexicalBinding

 LexicalBinding:
  BindingPattern Initializer

Therefore this:

 // ExportDeclaration
  export  // export
    // Declaration
    // LexicalDeclaration:
    const // LetOrConst
     // LexicalBindingList
     // LexicalBinding
     { NODE_ENV, API_URL } // BindingPattern
      = process.env; // Initializer

is totally valid JavaScript.

发布评论

评论列表(0)

  1. 暂无评论