export const MyClass
export default MyClass
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
define('myModule', ['jquery'], function($) {
// $ is the export of the jquery module.
$('body').text('hello world');
});
// and use it
require(['myModule'], function(myModule) {});
CommonJS
function project() {
return 'myProjet';
}
module.exports.project = project;
module.exports = project;
const { project } = require('project');
export function project() {
return 'myProjet';
}
export default project
const { project } = import('project'); const toto = import('project');
export function project() {
return 'myProjet';
}
export default project
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.project = project;
function project() {
return "myProjet";
}
exports.default = project;
Dynamic
&
Sync
Static
&
Async
Cameron Spear
What is the benefit of prefer-default-export?
Jordan Harband
you'd basically never have to change a default export to a named export
Cory House
Agreed except for treeshaking. All else equal, a single export fights against tree shaking by importing all code when you may only need some.
Jordan Harband
No, it doesn't. 3 files each with 1 export, versus 1 file with 3 exports, is identically treeshakeable - except that the former doesn't need tree-shaking to be as small as possible.
Using named exports is why tree-shaking is even necessary in the first place.
Jordan Harband
The ideal module/file only default-exports one thing, ideally a pure function. There are always exceptions and justifications for deviating from this, of course - but that's the default I start from.
KayakinKoder
let's say we have 500 helper functions related to UI manipulation. What's best for the long term? 500 individual files each exporting one function seems overboard, but maybe not?
Jordan Harband
if you have 500 helper functions, have 500 individual files. Use directories if they need organization.
Text
// for test purpose
export const LoginEmail = ({ t }) => (
<Marger top="4" bottom="4">
<Marger bottom="1">
<Label htmlFor="email">{ t('login.form.email.label') }</Label>
</Marger>
<TextInputField
id="email"
placeholder={ t('login.form.email.placeholder') }
name="login_user[email]"
validate={ [Validators.required, Validators.validEmail] }
errorMessages={ {
required: t('login.form.email.error.missing'),
invalidEmail: t('login.form.email.error.invalid'),
} }
/>
</Marger>
)
// default public API
export default withTranslation(LoginEmail)
Ramda / date-fns / redux / recompose / history ...
1 FICHIER = 1 HELPER = default export
Bonnes pratiques
Bénéfices