Skip to content

Commit 3605ea2

Browse files
authored
Migrate from tsc to rollup for the build process (#23)
this PR migrates the build chain from tsc to rollup, as part of the changes discovered in the investigation from this [PR](yext/chat-ui-react#38): in short, using rollup, the esm path files now include `.mjs` extensions and we no longer need `"type": "module"` in our package.json. J=CLIP-556 TEST=manual see that all unit tests and the the two test repo, node-cjs, and brower-esm, still works as expected published an alpha version `[0.7.0-alpha.23](https://www.npmjs.com/package/@yext/chat-core/v/0.7.0-alpha.23)`, and see that it still works as expected in vite/pagesjs and sonic/alpha. Will publish v0.7.0 once merged
1 parent 66824a4 commit 3605ea2

File tree

12 files changed

+200
-33
lines changed

12 files changed

+200
-33
lines changed

package-lock.json

Lines changed: 120 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"name": "@yext/chat-core",
3-
"version": "0.6.1",
3+
"version": "0.7.0",
44
"description": "Typescript Networking Library for the Yext Chat API",
55
"main": "./dist/commonjs/index.js",
6-
"module": "./dist/esm/index.js",
6+
"module": "./dist/esm/index.mjs",
77
"types": "./dist/esm/index.d.ts",
88
"sideEffects": false,
99
"keywords": [
@@ -26,12 +26,12 @@
2626
"scripts": {
2727
"test": "jest --config=jest.config.json",
2828
"lint": "prettier --write . && eslint --fix --max-warnings=0 .",
29-
"tsc-cjs": "tsc -p tsconfig.cjs.json",
30-
"tsc-esm": "tsc -p tsconfig.esm.json",
31-
"dev": "npm run tsc-esm -- --watch",
29+
"tsc": "tsc -p tsconfig.json",
30+
"dev": "npm run tsc -- --watch",
3231
"generate-notices": "generate-license-file --input package.json --output THIRD-PARTY-NOTICES --overwrite",
3332
"generate-docs": "api-extractor run --local --verbose && api-documenter markdown --input-folder temp --output-folder docs && rm -rf temp",
34-
"build": "rm -rf dist/** && npm run tsc-esm && npm run tsc-cjs && npm run generate-docs && npm run generate-notices"
33+
"build:js": "rollup --config rollup.config.mjs",
34+
"build": "rm -rf dist/** && npm run build:js && npm run generate-docs && npm run generate-notices"
3535
},
3636
"repository": {
3737
"type": "git",
@@ -60,6 +60,8 @@
6060
"jest": "^29.5.0",
6161
"jest-environment-jsdom": "^29.5.0",
6262
"prettier": "^2.8.8",
63+
"rollup": "^3.29.0",
64+
"rollup-plugin-typescript2": "^0.35.0",
6365
"typescript": "^5.0.4"
6466
}
6567
}

rollup.config.mjs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* \@rollup/plugin-typescript had issue with "export type":
3+
* [!] RollupError: Unexpected token (Note that you need plugins to import files that are not JavaScript)
4+
* Switched over to a fork rollup-plugin-typescript2 resolved the issue.
5+
*/
6+
import typescript from "rollup-plugin-typescript2";
7+
import { defineConfig } from "rollup";
8+
9+
export default defineConfig({
10+
input: "src/index.ts",
11+
output: [
12+
{
13+
dir: "./dist/esm",
14+
/**
15+
* use mjs extension so NodeJS will recognize the bundle as ES modules
16+
* https://nodejs.org/api/packages.html#determining-module-system
17+
*/
18+
entryFileNames: "[name].mjs",
19+
format: "esm",
20+
/** preserve original module files instead of compressing all to one file */
21+
preserveModules: true,
22+
sourcemap: true,
23+
/**
24+
* set to "auto" to follow TypeScript's esModuleInterop behavior to ensures compatibility
25+
* of default, namespace, and dynamic imports from external deps (e.g. react-textarea-autosize).
26+
*/
27+
interop: "auto",
28+
},
29+
{
30+
dir: "./dist/commonjs",
31+
format: "cjs",
32+
preserveModules: true,
33+
sourcemap: true,
34+
interop: "auto",
35+
},
36+
],
37+
plugins: [
38+
typescript({
39+
tsconfig: "./tsconfig.json",
40+
}),
41+
],
42+
});

src/infra/ChatCoreImpl.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ import {
99
MessageRequest,
1010
MessageResponse,
1111
Endpoints,
12-
ChatPrompt
12+
ChatPrompt,
1313
} from "../models";
14-
import {
15-
ApiMessageRequest,
16-
} from "../models/endpoints/MessageRequest";
14+
import { ApiMessageRequest } from "../models/endpoints/MessageRequest";
1715
import { ApiResponse } from "../models/http/ApiResponse";
1816
import { QueryParams } from "../models/http/params";
1917
import { ApiResponseValidator } from "../validation/ApiResponseValidator";

src/models/ChatCore.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { StreamResponse } from "../infra/StreamResponse"
2-
import { MessageRequest } from "./endpoints/MessageRequest"
3-
import { MessageResponse } from "./endpoints/MessageResponse"
1+
import { StreamResponse } from "../infra/StreamResponse";
2+
import { MessageRequest } from "./endpoints/MessageRequest";
3+
import { MessageResponse } from "./endpoints/MessageResponse";
44

55
/**
66
* Provide methods for interacting with Chat API.
7-
*
7+
*
88
* @public
99
*/
1010
export interface ChatCore {
@@ -16,7 +16,7 @@ export interface ChatCore {
1616
*
1717
* @param request - request to get next message
1818
*/
19-
getNextMessage(request: MessageRequest): Promise<MessageResponse>
19+
getNextMessage(request: MessageRequest): Promise<MessageResponse>;
2020
/**
2121
* Make a request to Chat streaming API to generate the next message
2222
* and consume its tokens via server-sent events.
@@ -28,5 +28,5 @@ export interface ChatCore {
2828
*
2929
* @param request - request to get next message
3030
*/
31-
streamNextMessage(request: MessageRequest): Promise<StreamResponse>
32-
}
31+
streamNextMessage(request: MessageRequest): Promise<StreamResponse>;
32+
}

src/models/InternalConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* package and subsequently moved into "stable" after performance and
55
* reliability are verified. It is STRONGLY recommended not to use
66
* "nightly" in production Chat bots.
7-
*
7+
*
88
* @internal
99
*/
1010
export type ChatPrompt = "stable" | "nightly";

src/models/endpoints/MessageResponse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { MessageNotes } from "./MessageNotes";
99
export interface MessageResponse {
1010
/**
1111
* The id corresponds to the current conversation.
12-
*
12+
*
1313
* @remarks
1414
* This is undefined only when it's an initial bot response without any user message present.
1515
*/

src/models/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
export { ChatCore } from "./ChatCore"
1+
export { ChatCore } from "./ChatCore";
22
export { ChatConfig } from "./ChatConfig";
3-
export { InternalConfig, ChatPrompt } from "./InternalConfig"
3+
export { InternalConfig, ChatPrompt } from "./InternalConfig";
44

55
export { Endpoints } from "./endpoints/Endpoints";
66
export { Environment } from "./endpoints/Environment";

test-browser-esm/package-lock.json

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test-browser-esm/src/script.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { ChatCore, StreamEventName } from "@yext/chat-core";
1+
import { StreamEventName, provideChatCore } from "@yext/chat-core";
22

3-
let chatCore = new ChatCore({
3+
let chatCore = provideChatCore({
44
// will be replace with actual env value during rollup build process
55
apiKey: process.env.TEST_BOT_API_KEY || "API_KEY_PLACEHOLDER",
66
botId: process.env.TEST_BOT_ID,
77
endpoints: {
88
chat: `https://liveapi-dev.yext.com/v2/accounts/me/chat/${process.env.TEST_BOT_ID}/message`,
99
chatStream: `https://liveapi-dev.yext.com/v2/accounts/me/chat/${process.env.TEST_BOT_ID}/message/streaming`,
10-
}
10+
},
1111
});
1212

1313
window.getNextMessage = async () => {

test-node-cjs/node.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import http from "http";
3-
import { ChatCore, StreamEventName } from "@yext/chat-core";
3+
import { ChatConfig, StreamEventName, provideChatCore } from "@yext/chat-core";
44
import dotenv from "dotenv";
55

66
dotenv.config();
77

8-
const config = {
8+
const config: ChatConfig = {
99
apiKey: process.env["TEST_BOT_API_KEY"] || "API_KEY_PLACEHOLDER",
1010
botId: process.env["TEST_BOT_ID"] || "BOT_ID_PLACEHOLDER",
11-
apiDomain: "liveapi-dev.yext.com",
11+
endpoints: {
12+
chat: `https://liveapi-dev.yext.com/v2/accounts/me/chat/${process.env.TEST_BOT_ID}/message`,
13+
chatStream: `https://liveapi-dev.yext.com/v2/accounts/me/chat/${process.env.TEST_BOT_ID}/message/streaming`,
14+
},
1215
};
1316

1417
async function stream(res: any) {
15-
const chatCore = new ChatCore(config);
18+
const chatCore = provideChatCore(config);
1619
const stream = await chatCore.streamNextMessage({
1720
messages: [
1821
{
@@ -39,7 +42,7 @@ const server = http.createServer(async (req: any, res: any) => {
3942
if (req.url === "/streaming") {
4043
stream(res);
4144
} else {
42-
const chatCore = new ChatCore(config);
45+
const chatCore = provideChatCore(config);
4346
const reply = await chatCore.getNextMessage({
4447
messages: [],
4548
});

0 commit comments

Comments
 (0)