Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
stargazer33 opened this issue Aug 15, 2024 · 4 comments

Comments

@stargazer33
Copy link

stargazer33 commented Aug 15, 2024

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
Copy link
Member

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
Copy link
Author

stargazer33 commented Aug 17, 2024

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
...
└─┬ [email protected]
  ├── @babel/[email protected] deduped
  ├── [email protected]
  └── [email protected]

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
Copy link
Author

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...

@boldtrn
Copy link

boldtrn commented Dec 9, 2024

Yes, I fully agree, the bundle size should really be optimized. For browser use I am not even sure we need axios at all? Given that nodejs has a ntive fetch implementation by now, IMHO this should be completely removed.

To clarify, I compared the file size of our bundle with SearchClient and Client and there was no difference in kb size.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants