Skip to content

Commit 47a7997

Browse files
Merge #1740
1740: Switch from rollup to vite, from commonjs to ecmascript modules r=Strift a=flevi29 # Pull Request ## Related issue Fixes #1626 ~~Waiting on #1739~~ ## What does this PR do? - switches bundler from [`rollup`](https://rollupjs.org/) to [`vite`](https://vite.dev/), which internally uses `rollup`, but makes things a lot shorter and simpler - removes the need for separate `vitest.config.js` - removes the need for [`scripts/file-size.js`](https://github.com/meilisearch/meilisearch-js/blob/main/scripts/file-size.js), packages `kleur`, `pretty-bytes`, `brotli-size`, `gzip-size`, as it prints sizes by default - removes the need for all of the `rollup` plugins and dependencies (``@rollup/plugin-terser`,` ``@babel/core`,` ``@babel/preset-env`,` ``@rollup/plugin-babel`,` ``@rollup/plugin-commonjs`,` ``@rollup/plugin-json`,` ``@rollup/plugin-node-resolve`,` `rollup-plugin-typescript2`) - removes the need for `shx` package, and `cleanup` script, as it clears `dist` itself - switches from CJS to ESM ([`"type": "module"`](https://nodejs.org/api/packages.html#type)) - every `.js` project file now needs to be written in ESM syntax, the web standard syntax - TypeScript files were already written in ESM syntax, so this just makes things more consistent - updates TypeScript and makes some changes to `tsconfig.json` according to https://www.typescriptlang.org/docs/handbook/modules/guides/choosing-compiler-options.html#im-writing-a-library - is now used to transpile ESM version of this package - most importantly sets `module` to `node16` and `verbatimModuleSyntax` to `true`, this requires changes to all TypeScript files so they may be compatible with the web standard ESM - all imported files now require a `.js` extension - there is no more support for `index` directory imports, as that is only a Node.js thing, the web doesn't support it - when importing types, we need to explicitly specify that we're doing so with the `type` keyword > [!WARNING] > - DEPRECATE default export #1789 > - DEPRECATION: UMD bundle will no longer extend `window` with the exports in the future, instead it will only extend it with property `meilisearch` which contains the exports #1806 > ### Old > ```html > <script> > const client = new MeiliSearch(/* ... */); > </script> > ``` > ### New > ```html > <script> > const client = new meilisearch.MeiliSearch(/* ... */); > </script> > ``` ## PR checklist Please check if your PR fulfills the following requirements: - [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [x] Have you read the contributing guidelines? - [x] Have you made sure that the title is accurate and descriptive of the changes? Thank you so much for contributing to Meilisearch! Co-authored-by: F. Levi <[email protected]>
2 parents 7032370 + 12c0df8 commit 47a7997

File tree

83 files changed

+654
-2238
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+654
-2238
lines changed

.prettierrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// https://prettier.io/docs/en/options.html
22

3-
module.exports = {
3+
export default {
44
plugins: ["./node_modules/prettier-plugin-jsdoc/dist/index.js"],
55
// https://github.com/hosseinmd/prettier-plugin-jsdoc#tsdoc
66
tsdoc: true,

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ Instead of using a package manager, you may also import the library directly int
6969

7070
After installing `meilisearch-js`, you must import it into your application. There are many ways of doing that depending on your development environment.
7171

72+
> [!WARNING]
73+
> - [default export](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export#using_the_default_export) is deprecated and will be removed in a future version https://github.com/meilisearch/meilisearch-js/issues/1789
74+
> - exports will stop being directly available on the global object (usually `window`) https://github.com/meilisearch/meilisearch-js/issues/1806
75+
7276
#### `import` syntax <!-- omit in toc -->
7377

7478
Usage in an ES module environment:
@@ -89,7 +93,7 @@ Usage in an HTML (or alike) file:
8993
```html
9094
<script src='https://cdn.jsdelivr.net/npm/meilisearch@latest/dist/bundles/meilisearch.umd.js'></script>
9195
<script>
92-
const client = new MeiliSearch({
96+
const client = new meilisearch.MeiliSearch({
9397
host: 'http://127.0.0.1:7700',
9498
apiKey: 'masterKey',
9599
})

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ services:
1616
meilisearch:
1717
image: getmeili/meilisearch:latest
1818
ports:
19-
- "7700"
19+
- "7700:7700"
2020
environment:
2121
- MEILI_MASTER_KEY=masterKey
2222
- MEILI_NO_ANALYTICS=true

eslint.config.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
const eslint = require("@eslint/js");
2-
const tseslint = require("typescript-eslint");
3-
const tsdoc = require("eslint-plugin-tsdoc");
4-
const vitest = require("@vitest/eslint-plugin");
5-
const globals = require("globals");
6-
const prettier = require("eslint-config-prettier");
1+
import eslint from "@eslint/js";
2+
import tseslint from "typescript-eslint";
3+
import tsdoc from "eslint-plugin-tsdoc";
4+
import vitest from "@vitest/eslint-plugin";
5+
import globals from "globals";
6+
import prettier from "eslint-config-prettier";
77

8-
module.exports = tseslint.config([
8+
export default tseslint.config([
99
{ ignores: ["dist/", "tests/env/", "coverage/", "playgrounds/", "docs/"] },
1010
eslint.configs.recommended,
1111
{
1212
files: ["**/*.js"],
13-
languageOptions: { sourceType: "commonjs", globals: globals.node },
13+
languageOptions: { globals: globals.node },
1414
},
1515
// TSDoc
1616
{
@@ -25,7 +25,7 @@ module.exports = tseslint.config([
2525
languageOptions: {
2626
parserOptions: {
2727
projectService: true,
28-
tsconfigRootDir: __dirname,
28+
tsconfigRootDir: import.meta.dirname,
2929
},
3030
},
3131
rules: {

package.json

Lines changed: 22 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,20 @@
1616
"qdequele <[email protected]>"
1717
],
1818
"license": "MIT",
19-
"main": "./dist/bundles/meilisearch.cjs",
20-
"module": "./dist/bundles/meilisearch.mjs",
21-
"typings": "./dist/types/index.d.ts",
22-
"types": "./dist/types/index.d.ts",
23-
"jsdelivr": "./dist/bundles/meilisearch.umd.js",
24-
"unpkg": "./dist/bundles/meilisearch.umd.js",
19+
"type": "module",
20+
"main": "./dist/umd/index.min.js",
2521
"exports": {
2622
".": {
2723
"types": "./dist/types/index.d.ts",
28-
"browser": "./dist/bundles/meilisearch.umd.js",
29-
"import": "./dist/bundles/meilisearch.mjs",
30-
"require": "./dist/bundles/meilisearch.cjs",
31-
"default": "./dist/bundles/meilisearch.umd.js"
24+
"import": "./dist/esm/index.js",
25+
"require": "./dist/cjs/index.cjs",
26+
"default": "./dist/umd/index.min.js"
3227
},
3328
"./token": {
3429
"types": "./dist/types/token.d.ts",
35-
"import": "./dist/bundles/token.mjs",
36-
"require": "./dist/bundles/token.cjs",
37-
"default": "./dist/bundles/token.cjs"
30+
"import": "./dist/esm/token.js",
31+
"require": "./dist/cjs/token.cjs",
32+
"default": "./dist/cjs/token.cjs"
3833
}
3934
},
4035
"sideEffects": false,
@@ -43,32 +38,28 @@
4338
"url": "https://github.com/meilisearch/meilisearch-js"
4439
},
4540
"scripts": {
46-
"playground:javascript": "yarn --cwd ./playgrounds/javascript && yarn --cwd ./playgrounds/javascript start",
47-
"cleanup": "shx rm -rf dist/",
48-
"build": "yarn cleanup && rollup -c && rollup -c --environment NODE_ENV:production",
41+
"playground:javascript": "yarn --cwd ./playgrounds/javascript && yarn --cwd ./playgrounds/javascript dev",
4942
"build:docs": "typedoc",
50-
"watch": "yarn cleanup && rollup -c --watch",
51-
"postbuild": "yarn size && yarn typingsheader",
43+
"build": "vite build && tsc -p tsconfig.build.json && vite --mode production-umd build",
44+
"postbuild": "node scripts/build.js",
5245
"test": "vitest run --coverage",
53-
"types:watch": "nodemon --config nodemon.json",
54-
"types": "yarn tsc",
55-
"test:env:browser": "yarn build && yarn --cwd tests/env/express && yarn --cwd tests/env/express test",
46+
"types": "tsc -p tsconfig.json --noEmit",
47+
"types:watch": "yarn types --watch",
48+
"test:env:browser": "yarn build && node scripts/copy-umd-file.js --to ./tests/env/express/public && yarn --cwd tests/env/express && yarn --cwd tests/env/express test",
5649
"test:watch": "vitest watch",
5750
"test:coverage": "yarn test",
5851
"test:ci": "yarn test",
5952
"test:env": "yarn build && yarn test:env:nodejs && yarn test:env:esm && yarn test:env:node-ts",
6053
"test:env:node-ts": "yarn --cwd tests/env/typescript-node start",
61-
"test:env:nodejs": "yarn build && node tests/env/node/index.js && node tests/env/node/getting_started.js",
54+
"test:env:nodejs": "yarn build && node tests/env/node/index.cjs && node tests/env/node/getting_started.cjs",
6255
"test:env:esm": "yarn --cwd tests/env/esm && yarn --cwd tests/env/esm start",
6356
"test:env:nitro-app": "yarn build && yarn --cwd tests/env/nitro-app test",
64-
"size": "node scripts/file-size ./dist/bundles/meilisearch.mjs ./dist/bundles/meilisearch.umd.min.js",
65-
"style": "yarn fmt && yarn lint",
66-
"style:fix": "yarn fmt:fix && yarn lint:fix",
6757
"fmt": "prettier -c ./**/*.{js,ts}",
6858
"fmt:fix": "prettier -w ./**/*.{js,ts}",
6959
"lint": "eslint",
7060
"lint:fix": "eslint --fix",
71-
"typingsheader": "node scripts/build.js"
61+
"style": "yarn fmt && yarn lint",
62+
"style:fix": "yarn fmt:fix && yarn lint:fix"
7263
},
7364
"files": [
7465
"src",
@@ -86,35 +77,23 @@
8677
]
8778
},
8879
"devDependencies": {
89-
"@rollup/plugin-terser": "^0.4.4",
90-
"@babel/core": "^7.25.2",
91-
"@babel/preset-env": "^7.25.4",
9280
"@eslint/js": "^9.16.0",
93-
"@rollup/plugin-babel": "^6.0.4",
94-
"@rollup/plugin-commonjs": "28.0.0",
95-
"@rollup/plugin-json": "^6.1.0",
96-
"@rollup/plugin-node-resolve": "15.3.0",
9781
"@types/eslint__js": "^8.42.3",
9882
"@vitest/coverage-v8": "2.0.5",
99-
"@vitest/eslint-plugin": "^1.1.4",
10083
"@types/node": "^22.10.1",
101-
"brotli-size": "^4.0.0",
10284
"eslint": "^9.16.0",
85+
"eslint-plugin-tsdoc": "^0.4.0",
86+
"@vitest/eslint-plugin": "^1.1.23",
10387
"eslint-config-prettier": "^9.1.0",
104-
"eslint-plugin-tsdoc": "^0.3.0",
88+
"typescript": "^5.7.2",
89+
"vite": "^6.0.7",
90+
"@typescript-eslint/utils": "^8.19.0",
10591
"globals": "^15.14.0",
106-
"gzip-size": "^6.0.0",
107-
"kleur": "^4.1.5",
10892
"lint-staged": "15.3.0",
10993
"nodemon": "^3.1.9",
11094
"prettier": "^3.4.2",
11195
"prettier-plugin-jsdoc": "^1.3.0",
112-
"pretty-bytes": "^5.6.0",
113-
"rollup": "^4.22.5",
114-
"rollup-plugin-typescript2": "^0.36.0",
115-
"shx": "^0.3.2",
11696
"typedoc": "^0.27.6",
117-
"typescript": "^5.4.5",
11897
"typescript-eslint": "^8.19.0",
11998
"vitest": "2.0.5"
12099
},

rollup.config.js

Lines changed: 0 additions & 125 deletions
This file was deleted.

scripts/build.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/** This file only purpose is to execute any build related tasks */
22

3-
const { resolve, normalize } = require("node:path");
4-
const { readFileSync, writeFileSync } = require("node:fs");
3+
import { resolve, normalize } from "node:path";
4+
import { fileURLToPath } from "node:url";
5+
import { readFileSync, writeFileSync } from "node:fs";
6+
import pkg from "../package.json" with { type: "json" };
57

6-
const pkg = require("../package.json");
7-
8-
const ROOT = resolve(__dirname, "..");
9-
const TYPES_ROOT_FILE = resolve(ROOT, normalize(pkg.typings));
8+
const ROOT = fileURLToPath(new URL("..", import.meta.url));
9+
const TYPES_ROOT_FILE = resolve(ROOT, normalize(pkg.exports["."].types));
1010

1111
main();
1212

scripts/copy-umd-file.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { parseArgs } from "node:util";
2+
import pkg from "../package.json" with { type: "json" };
3+
import { fileURLToPath } from "node:url";
4+
import { resolve, dirname, join, basename } from "node:path";
5+
import { copyFile } from "node:fs/promises";
6+
7+
const {
8+
values: { to },
9+
} = parseArgs({ options: { to: { type: "string" } } });
10+
11+
if (to === undefined) {
12+
throw new Error("required argument `to` missing");
13+
}
14+
15+
const umdAbsolutePath = resolve(
16+
dirname(fileURLToPath(import.meta.url)),
17+
join("..", pkg.main),
18+
);
19+
20+
await copyFile(umdAbsolutePath, join(to, basename(pkg.main)));

0 commit comments

Comments
 (0)