typesense / typesense-js

JavaScript / TypeScript client for Typesense
https://typesense.org/docs/api
Apache License 2.0
400 stars 74 forks source link

[Feature] Create small .js file conaining ONLY search-related functionality (for use in browser) #222

Open stargazer33 opened 1 month ago

stargazer33 commented 1 month ago

Description

Having full functionality of Typeset API in one file (typesense.min.js, about 95 kB) is great - when you need this functionality. But when your code works in web browser - all you do is something like this:

   typesense = new Typesense.SearchClient(...)
   ...
   typesense.collections('test').documents().search({...}).then(
        function (searchResults){
            ...
            searchResults.hits
            ...
        }       

I mean - the end-user is not creating collections/documents/synonyms/aliases... The end-user just searching.

Expected Behavior

There is a need for something like typesense-search.js. It should contains Typesense.SearchClient and ... basically that's it. Should have enough functionality to do search in browser. Should be... I would say... less than 30 kB

jasonbosco commented 1 month ago

There's a SearchClient class that does exactly this: https://github.com/typesense/typesense-js/blob/master/src/Typesense/SearchClient.ts

If you import that class (instead of `Client), your bundler should automatically tree-shake and only include a smaller subset of the files just required for search in your final build.

stargazer33 commented 1 month ago

If you import that class (instead of `Client) ...

OK, I did it:

  "dependencies": {
    ...
    "typesense": "1.8.2"
  },
import { SearchClient as TypesenseSearchClient } from "typesense";
const typesense = new TypesenseSearchClient({
....
npm ls --depth 1
...
└─┬ typesense@1.8.2
  ├── @babel/runtime@7.25.0 deduped
  ├── axios@1.7.4
  └── loglevel@1.9.1

parcel build --dist-dir ./dist --reporter @parcel/reporter-bundle-analyzer

And analyzer tells us this: axios 36.8 KB typesense 32.3 KB 71 KB of "Code from unknown sources"

69 KB altogether, plus 71 KB "unknown sources" Not a small thing.

(correct me if i wrong. The sizes this analyzer shows - are they minimized sizes or not?)

stargazer33 commented 1 month ago

UPDATE:

Running parcel build --log-level verbose I got a long list of messages like this:

...
@parcel/transformer-js: Unknown usage of CommonJS `exports` object. This causes tree shaking to be disabled.
  ...node_modules/typesense/lib/Typesense/Preset.js:38:24
    37 | };
  > 38 | var __importDefault = (this && this.__importDefault) || function (mod) {
  >    |                        ^
    39 |     return (mod && mod.__esModule) ? mod : { "default": mod };
    40 | };
...
@parcel/transformer-js: Conditional or non-top-level `require()` call. This causes the resolved module and all dependencies to be wrapped.
  ...node_modules/typesense/lib/Typesense/Client.js:22:1
    21 | var Preset_1 = __importDefault(require("./Preset"));
  > 22 | var Analytics_1 = __importDefault(require("./Analytics"));
  >    | ^
    23 | var Stopwords_1 = __importDefault(require("./Stopwords"));
    24 | var Stopword_1 = __importDefault(require("./Stopword"));
...    

About 10 screens of such messages. Well, it could be that something should be improved in code... and after this - tree shaking will work better...