| 1 | # import/prefer-default-export
|
|---|
| 2 |
|
|---|
| 3 | <!-- end auto-generated rule header -->
|
|---|
| 4 |
|
|---|
| 5 | In exporting files, this rule checks if there is default export or not.
|
|---|
| 6 |
|
|---|
| 7 | ## Rule Details
|
|---|
| 8 |
|
|---|
| 9 | ### rule schema
|
|---|
| 10 |
|
|---|
| 11 | ```javascript
|
|---|
| 12 | "import/prefer-default-export": [
|
|---|
| 13 | ( "off" | "warn" | "error" ),
|
|---|
| 14 | { "target": "single" | "any" } // default is "single"
|
|---|
| 15 | ]
|
|---|
| 16 | ```
|
|---|
| 17 |
|
|---|
| 18 | ### Config Options
|
|---|
| 19 |
|
|---|
| 20 | There are two options available: `single` and `any`. By default, if you do not specify the option, rule will assume it is `single`.
|
|---|
| 21 |
|
|---|
| 22 | #### single
|
|---|
| 23 |
|
|---|
| 24 | **Definition**: When there is only a single export from a module, prefer using default export over named export.
|
|---|
| 25 |
|
|---|
| 26 | How to setup config file for this rule:
|
|---|
| 27 |
|
|---|
| 28 | ```javascript
|
|---|
| 29 | // you can manually specify it
|
|---|
| 30 | "rules": {
|
|---|
| 31 | "import/prefer-default-export": [
|
|---|
| 32 | ( "off" | "warn" | "error" ),
|
|---|
| 33 | { "target": "single" }
|
|---|
| 34 | ]
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | // config setup below will also work
|
|---|
| 38 | "rules": {
|
|---|
| 39 | "import/prefer-default-export": "off" | "warn" | "error"
|
|---|
| 40 | }
|
|---|
| 41 | ```
|
|---|
| 42 |
|
|---|
| 43 | The following patterns are considered warnings:
|
|---|
| 44 |
|
|---|
| 45 | ```javascript
|
|---|
| 46 | // bad.js
|
|---|
| 47 |
|
|---|
| 48 | // There is only a single module export and it's a named export.
|
|---|
| 49 | export const foo = 'foo';
|
|---|
| 50 |
|
|---|
| 51 | ```
|
|---|
| 52 |
|
|---|
| 53 | The following patterns are not warnings:
|
|---|
| 54 |
|
|---|
| 55 | ```javascript
|
|---|
| 56 | // good1.js
|
|---|
| 57 |
|
|---|
| 58 | // There is a default export.
|
|---|
| 59 | export const foo = 'foo';
|
|---|
| 60 | const bar = 'bar';
|
|---|
| 61 | export default bar;
|
|---|
| 62 | ```
|
|---|
| 63 |
|
|---|
| 64 | ```javascript
|
|---|
| 65 | // good2.js
|
|---|
| 66 |
|
|---|
| 67 | // There is more than one named export in the module.
|
|---|
| 68 | export const foo = 'foo';
|
|---|
| 69 | export const bar = 'bar';
|
|---|
| 70 | ```
|
|---|
| 71 |
|
|---|
| 72 | ```javascript
|
|---|
| 73 | // good3.js
|
|---|
| 74 |
|
|---|
| 75 | // There is more than one named export in the module
|
|---|
| 76 | const foo = 'foo';
|
|---|
| 77 | const bar = 'bar';
|
|---|
| 78 | export { foo, bar }
|
|---|
| 79 | ```
|
|---|
| 80 |
|
|---|
| 81 | ```javascript
|
|---|
| 82 | // good4.js
|
|---|
| 83 |
|
|---|
| 84 | // There is a default export.
|
|---|
| 85 | const foo = 'foo';
|
|---|
| 86 | export { foo as default }
|
|---|
| 87 | ```
|
|---|
| 88 |
|
|---|
| 89 | ```javascript
|
|---|
| 90 | // export-star.js
|
|---|
| 91 |
|
|---|
| 92 | // Any batch export will disable this rule. The remote module is not inspected.
|
|---|
| 93 | export * from './other-module'
|
|---|
| 94 | ```
|
|---|
| 95 |
|
|---|
| 96 | #### any
|
|---|
| 97 |
|
|---|
| 98 | **Definition**: any exporting file must contain a default export.
|
|---|
| 99 |
|
|---|
| 100 | How to setup config file for this rule:
|
|---|
| 101 |
|
|---|
| 102 | ```javascript
|
|---|
| 103 | // you have to manually specify it
|
|---|
| 104 | "rules": {
|
|---|
| 105 | "import/prefer-default-export": [
|
|---|
| 106 | ( "off" | "warn" | "error" ),
|
|---|
| 107 | { "target": "any" }
|
|---|
| 108 | ]
|
|---|
| 109 | }
|
|---|
| 110 | ```
|
|---|
| 111 |
|
|---|
| 112 | The following patterns are *not* considered warnings:
|
|---|
| 113 |
|
|---|
| 114 | ```javascript
|
|---|
| 115 | // good1.js
|
|---|
| 116 |
|
|---|
| 117 | //has default export
|
|---|
| 118 | export default function bar() {};
|
|---|
| 119 | ```
|
|---|
| 120 |
|
|---|
| 121 | ```javascript
|
|---|
| 122 | // good2.js
|
|---|
| 123 |
|
|---|
| 124 | // has default export
|
|---|
| 125 | let foo;
|
|---|
| 126 | export { foo as default }
|
|---|
| 127 | ```
|
|---|
| 128 |
|
|---|
| 129 | ```javascript
|
|---|
| 130 | // good3.js
|
|---|
| 131 |
|
|---|
| 132 | //contains multiple exports AND default export
|
|---|
| 133 | export const a = 5;
|
|---|
| 134 | export function bar(){};
|
|---|
| 135 | let foo;
|
|---|
| 136 | export { foo as default }
|
|---|
| 137 | ```
|
|---|
| 138 |
|
|---|
| 139 | ```javascript
|
|---|
| 140 | // good4.js
|
|---|
| 141 |
|
|---|
| 142 | // does not contain any exports => file is not checked by the rule
|
|---|
| 143 | import * as foo from './foo';
|
|---|
| 144 | ```
|
|---|
| 145 |
|
|---|
| 146 | ```javascript
|
|---|
| 147 | // export-star.js
|
|---|
| 148 |
|
|---|
| 149 | // Any batch export will disable this rule. The remote module is not inspected.
|
|---|
| 150 | export * from './other-module'
|
|---|
| 151 | ```
|
|---|
| 152 |
|
|---|
| 153 | The following patterns are considered warnings:
|
|---|
| 154 |
|
|---|
| 155 | ```javascript
|
|---|
| 156 | // bad1.js
|
|---|
| 157 |
|
|---|
| 158 | //has 2 named exports, but no default export
|
|---|
| 159 | export const foo = 'foo';
|
|---|
| 160 | export const bar = 'bar';
|
|---|
| 161 | ```
|
|---|
| 162 |
|
|---|
| 163 | ```javascript
|
|---|
| 164 | // bad2.js
|
|---|
| 165 |
|
|---|
| 166 | // does not have default export
|
|---|
| 167 | let foo, bar;
|
|---|
| 168 | export { foo, bar }
|
|---|
| 169 | ```
|
|---|
| 170 |
|
|---|
| 171 | ```javascript
|
|---|
| 172 | // bad3.js
|
|---|
| 173 |
|
|---|
| 174 | // does not have default export
|
|---|
| 175 | export { a, b } from "foo.js"
|
|---|
| 176 | ```
|
|---|
| 177 |
|
|---|
| 178 | ```javascript
|
|---|
| 179 | // bad4.js
|
|---|
| 180 |
|
|---|
| 181 | // does not have default export
|
|---|
| 182 | let item;
|
|---|
| 183 | export const foo = item;
|
|---|
| 184 | export { item };
|
|---|
| 185 | ```
|
|---|