You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
756: Named exports r=bidoubiwa a=bidoubiwa
Fixes: #686
Based on: meilisearch/meilisearch-js-plugins#122
# Changes in README
To showcase named exports a few changes have been made in README and code-samples:
- Separate ES, CJS and browser syntax for clarity
- Changed code-samples the same way
**These changes in the readme and code samples are open for discussion.**
# Named exports
MeiliSearch Js decided to go in the direction of new best practices in JS libraries by changing from default export to named export.
## New usage
ES:
```javascript
import { MeiliSearch } from 'meilisearch' // ES
import DefaultMeiliSearch from 'meilisearch' // ES
// new MeiliSearch(..)
// new DefaultMeiliSearch.MeiliSearch(..)
```
Node
```javascript
const { MeiliSearch } = require('meilisearch') // Node
const DefaultMeiliSearch = require('meilisearch') // Node
// new MeiliSearch(..)
// new DefaultMeiliSearch.MeiliSearch(..)
```
```javascript
MeiliSearch // browser
window.MeiliSearch // browser
// new MeiliSearch(..)
// new window.MeiliSearch(..)
```
### Typescript
```javascript
import { MeiliSearch, IndexResponse } from 'meilisearch' // ES
import DefaultMeiliSearch from 'meilisearch' // ES
// IndexResponse
// DefaultMeiliSearch.IndexResponse
```
## Explanation
Default export brought a bunch of problems with it. Having as pros only easy of use (which becomes less and less true).
This is how it worked before this PR:
Most basic usage:
```javascript
import MeiliSearch from 'meilisearch' // ES
const MeiliSearch = require('meilisearch') // Node
// new MeiliSearch(...)
```
```js
window.MeiliSearch // browser
```
### Typescript
Where things got ugly:
In typescript types are not exported by default as the default export is already used by `MeiliSearch`.
Meaning you would have to do one of the following to get both MeiliSearch and a specific type.
```javascript
import MeiliSearch, { IndexResponse } from 'meilisearch'
```
Or this
```javascript
import MeiliSearch, * as Types from 'meilisearch'
// Types.IndexResponse
```
## Why the changes
Add to that, Some other arguments ( a [complete argument list is available here](meilisearch/meilisearch-js-plugins#122))
- `intellisense` does not work well with default export as it [lack in suggesting autocomplete](https://stackoverflow.com/questions/54427609/vscode-intellisense-not-fully-working-with-es6-imports)
- CommonJs interop. Without proper configuration of bundle, default export are weird to use in CJS.
```javascript
const {default} = require('meilisearch');
new default.MeiliSearch(...)
```
And other that you mind find in the linked github page.
Co-authored-by: Charlotte Vermandel <[email protected]>
0 commit comments