Skip to content

Instantly share code, notes, and snippets.

@azu
Last active January 6, 2021 11:47
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
ES6 modulesのexport と CommonJS moduleのexports それぞれの使い分けってあるのかな

感覚的な話なので早く落ち着くといいなー

ES6 modulesでexportしたい

Entry pointが一つのみ

// Single default export
export default function entryPoint(){}

Entry pointが複数

// Multiple named exports
export function a(){}
export function b(){}

Caution ⚠️

  • defaultとnamed exportをまぜない

Bad

export default function entryPoint(){}
export function SomeThing(){}

Good

export function entryPoint(){}
export function SomeThing(){}

16.4.5.1 Recommendation: avoid mixing default exports and named exports
-- http://exploringjs.com/es6/ch_modules.html

CommonJS modulesでexportしたい

EntryPointが一つ

module.exports = function entryPoint(){};

EntryPointが複数

// exports object
module.exports = {
  a: function a(){},
  b: function b(){}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment