Skip to content

fix(javascript): use exports field to pick correct bundle #947

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

Merged
merged 4 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions playground/javascript/node/search.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { searchClient } from '@algolia/client-search';
import { apiClientVersion } from '@algolia/client-search/src/searchClient';
import { SearchQuery } from '@algolia/client-search/model';
import { ApiError } from '@algolia/client-common';
import dotenv from 'dotenv';

Expand All @@ -15,13 +17,18 @@ const client = searchClient(appId, apiKey);

client.addAlgoliaAgent('Node playground', '0.0.1');

const requests: SearchQuery[] = [
{ indexName: searchIndex, query: searchQuery },
];
console.log('verison', apiClientVersion, 'requests', requests);

async function testSearch() {
try {
const res = await client.search<{ name: string }>({
requests: [{ indexName: searchIndex, query: searchQuery }],
requests,
});

console.log(`[OK]`, res.results[0].hits![0].name);
console.log(`[OK]`, res);
} catch (e: any) {
// Instance of
if (e instanceof ApiError) {
Expand Down
44 changes: 30 additions & 14 deletions templates/javascript/clients/package.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,31 @@
"repository": "{{gitUserId}}/{{gitRepoId}}",
"license": "MIT",
"author": "Algolia",
"types": "./index.d.ts",
{{^isAlgoliasearchClient}}
"name": "{{{npmNamespace}}}/{{packageName}}",
"description": "JavaScript client for {{packageName}}",
"main": "index.js",
"jsdelivr": "dist/{{packageName}}.umd.js",
"unpkg": "dist/{{packageName}}.umd.js",
"module": "dist/{{packageName}}.esm.node.js",
"browser": "dist/{{packageName}}.umd.js",
"jsdelivr": "./dist/{{packageName}}.umd.js",
"unpkg": "./dist/{{packageName}}.umd.js",
"browser": "./dist/{{packageName}}.umd.js",
"exports": {
".": {
"node": {
"import": "./dist/{{packageName}}.esm.node.js",
"module": "./dist/{{packageName}}.esm.node.js",
"require": "./dist/{{packageName}}.cjs.js",
"default": "./dist/{{packageName}}.cjs.js"
},
"default": {
"umd": "./dist/{{packageName}}.umd.js",
"module": "./dist/{{packageName}}.esm.browser.js",
"import": "./dist/{{packageName}}.esm.browser.js",
"default": "./dist/{{packageName}}.umd.js"
}
},
"./src/*": "./src/*.ts",
Copy link
Member Author

@francoischalifour francoischalifour Aug 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should rather be one of those:

{
    "./src/*": "./src/*",
    "./src/*.ts": "./src/*.ts",
}

Otherwise if there's another extension in the folder, it would incorrectly get imported as TypeScript.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those clients all follow the same pattern with a single API file in their src folder, we could be more precise on the name instead of using globs but it shouldn't change for now at least

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, but it's less error prone and reduce the public API if we went for "./src/*.ts": "./src/*.ts". It would get tricky to debug if you encounter this bug. So we better address it now imo.

"./model": "./model/index.ts"
},
"files": [
"dist",
"model",
Expand All @@ -35,6 +52,12 @@
{{#isAlgoliasearchClient}}
"name": "{{packageName}}",
"description": "A fully-featured and blazing-fast JavaScript API client to interact with Algolia API.",
"jsdelivr": "./dist/algoliasearch.umd.js",
"unpkg": "./dist/algoliasearch.umd.js",
"browser": {
"./index.js": "./dist/algoliasearch.umd.js",
"./lite.js": "./dist/lite/lite.umd.js"
},
"exports": {
".": {
"types": "./index.d.ts",
Expand All @@ -44,7 +67,7 @@
"require": "./dist/algoliasearch.cjs.js",
"default": "./dist/algoliasearch.cjs.js"
},
"browser": {
"default": {
"umd": "./dist/algoliasearch.umd.js",
"module": "./dist/algoliasearch.esm.browser.js",
"import": "./dist/algoliasearch.esm.browser.js",
Expand All @@ -59,20 +82,14 @@
"require": "./dist/lite/lite.cjs.js",
"default": "./dist/lite/lite.cjs.js"
},
"browser": {
"default": {
"umd": "./dist/lite/lite.umd.js",
"module": "./dist/lite/lite.esm.browser.js",
"import": "./dist/lite/lite.esm.browser.js",
"default": "./dist/lite/lite.umd.js"
}
}
},
"jsdelivr": "./dist/algoliasearch.umd.js",
"unpkg": "./dist/algoliasearch.umd.js",
"browser": {
"./index.js": "./dist/algoliasearch.umd.js",
"./lite.js": "./dist/lite/lite.umd.js"
},
"files": [
"dist",
"builds",
Expand Down Expand Up @@ -105,7 +122,6 @@
"rollup": "2.78.1"
},
{{/isAlgoliasearchClient}}
"types": "index.d.ts",
"engines": {
"node": ">= 14.0.0"
}
Expand Down