Skip to content

Commit e0ae527

Browse files
bors[bot]bidoubiwa
andauthored
Merge #756
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]>
2 parents 36c720a + 61431da commit e0ae527

File tree

19 files changed

+67
-39
lines changed

19 files changed

+67
-39
lines changed

.code-samples.meilisearch.yaml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,22 @@ getting_started_add_documents_md: |-
293293
$ yarn add meilisearch
294294
```
295295
296+
**Import**
297+
298+
`require` syntax:
296299
```js
297-
const MeiliSearch = require('meilisearch')
300+
const { MeiliSearch } = require('meilisearch')
298301
const movies = require('./movies.json')
302+
```
303+
304+
`import` syntax:
305+
```js
306+
import { MeiliSearch } from 'meilisearch'
307+
import movies from '../small_movies.json'
308+
```
299309
310+
**Use**
311+
```js
300312
const client = new MeiliSearch({ host: 'http://127.0.0.1:7700' })
301313
client.index('movie').addDocuments(movies)
302314
.then((res) => console.log(res))

README.md

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,36 +70,41 @@ NB: you can also download MeiliSearch from **Homebrew** or **APT**.
7070

7171
### Import <!-- omit in toc -->
7272

73-
#### Front End or ESmodule <!-- omit in toc -->
73+
Depending on the environment on which you are using MeiliSearch, imports may differ.
74+
75+
#### Import Syntax <!-- omit in toc -->
76+
77+
Usage in a ES module environment:
7478

7579
```javascript
76-
import MeiliSearch from 'meilisearch'
80+
import { MeiliSearch } from 'meilisearch'
7781

7882
const client = new MeiliSearch({
7983
host: 'http://127.0.0.1:7700',
8084
apiKey: 'masterKey',
8185
})
8286
```
8387

84-
#### HTML Import <!-- omit in toc -->
88+
#### Include Script Tag <!-- omit in toc -->
8589

86-
```javascript
90+
Usage in an HTML (or alike) file:
91+
92+
```html
8793
<script src="https://cdn.jsdelivr.net/npm/meilisearch@latest/dist/bundles/meilisearch.umd.js"></script>
8894
<script>
8995
const client = new MeiliSearch({
9096
host: 'http://127.0.0.1:7700',
9197
apiKey: 'masterKey',
9298
})
93-
client.listIndexes().then(res => {
94-
console.log({ res });
95-
})
9699
</script>
97100
```
98101

99-
#### Back-End CommonJs <!-- omit in toc -->
102+
#### Require Syntax <!-- omit in toc -->
103+
104+
Usage in a back-end node environment
100105

101106
```javascript
102-
const MeiliSearch = require('meilisearch')
107+
const { MeiliSearch } = require('meilisearch')
103108

104109
const client = new MeiliSearch({
105110
host: 'http://127.0.0.1:7700',
@@ -116,9 +121,9 @@ To make this package work with React Native, please add the [react-native-url-po
116121
#### Add Documents <!-- omit in toc -->
117122

118123
```js
119-
const MeiliSearch = require('meilisearch')
120-
// Or if you are on a front-end environment:
121-
import MeiliSearch from 'meilisearch'
124+
const { MeiliSearch } = require('meilisearch')
125+
// Or if you are in a ES environment
126+
import { MeiliSearch } from 'meilisearch'
122127

123128
;(async () => {
124129
const client = new MeiliSearch({

examples/node/search_example.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const MeiliSearch = require('../../dist/bundles/meilisearch.umd.js')
1+
const { MeiliSearch } = require('../../dist/bundles/meilisearch.umd.js')
22
const dataset = require('./small_movies.json')
33

44
const config = {

examples/typescript-browser/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "MeiliSearch Typescript Demo",
55
"main": "index.js",
66
"scripts": {
7-
"build": "webpack"
7+
"build": "webpack --resolveJsonModule"
88
},
99
"keywords": [
1010
"demo",

examples/typescript-browser/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import MeiliSearch, { IndexResponse } from '../../../'
1+
import { MeiliSearch, IndexResponse } from '../../../'
22

33
const config = {
44
host: 'http://127.0.0.1:7700',
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{
22
"compilerOptions": {
3+
"resolveJsonModule": true,
4+
"esModuleInterop": true
35
}
46
}

examples/typescript-browser/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ let config = {
77
path: path.resolve(__dirname, './public'),
88
filename: './bundle.js',
99
},
10-
mode: 'production',
10+
mode: 'development',
1111
module: {
1212
rules: [
1313
{

examples/typescript-node/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import MeiliSearch, { IndexResponse } from '../../../'
1+
import { MeiliSearch, IndexResponse } from '../../../'
22

33
const config = {
44
host: 'http://127.0.0.1:7700',

rollup.config.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ function getOutputFileName(fileName, isProd = false) {
1212
}
1313

1414
const env = process.env.NODE_ENV || 'development'
15-
const LIB_NAME = 'MeiliSearch'
1615
const ROOT = resolve(__dirname, '.')
1716

1817
const PLUGINS = [
@@ -22,7 +21,6 @@ const PLUGINS = [
2221
allowJs: false,
2322
includes: ['src'],
2423
exclude: ['tests', 'examples', '*.js', 'scripts'],
25-
esModuleInterop: true,
2624
},
2725
}),
2826
]
@@ -33,7 +31,8 @@ module.exports = [
3331
input: 'src/meilisearch.ts', // directory to transpilation of typescript
3432
external: ['cross-fetch', 'cross-fetch/polyfill'],
3533
output: {
36-
name: LIB_NAME,
34+
name: 'window',
35+
extend: true,
3736
file: getOutputFileName(
3837
// will add .min. in filename if in production env
3938
resolve(ROOT, pkg.browser),
@@ -88,7 +87,7 @@ module.exports = [
8887
resolve(ROOT, pkg.module),
8988
env === 'production'
9089
),
91-
exports: 'default',
90+
exports: 'named',
9291
format: 'es',
9392
sourcemap: env === 'production', // create sourcemap for error reporting in production mode
9493
},

src/meilisearch.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import HttpRequests from './http-requests'
1414

1515
type createPath = (x: string | number) => string
1616

17-
class MeiliSearch implements Types.MeiliSearchInterface {
17+
export class MeiliSearch implements Types.MeiliSearchInterface {
1818
config: Types.Config
1919
httpRequest: HttpRequests
2020
static apiRoutes: {
@@ -208,5 +208,3 @@ class MeiliSearch implements Types.MeiliSearchInterface {
208208
return await this.httpRequest.get<Types.EnqueuedDump>(url)
209209
}
210210
}
211-
212-
export default MeiliSearch

0 commit comments

Comments
 (0)