Skip to content

Commit

Permalink
Migrate from tsc to rollup for the build process (#23)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
yen-tt authored Sep 11, 2023
1 parent 66824a4 commit 3605ea2
Show file tree
Hide file tree
Showing 12 changed files with 200 additions and 33 deletions.
122 changes: 120 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "@yext/chat-core",
"version": "0.6.1",
"version": "0.7.0",
"description": "Typescript Networking Library for the Yext Chat API",
"main": "./dist/commonjs/index.js",
"module": "./dist/esm/index.js",
"module": "./dist/esm/index.mjs",
"types": "./dist/esm/index.d.ts",
"sideEffects": false,
"keywords": [
Expand All @@ -26,12 +26,12 @@
"scripts": {
"test": "jest --config=jest.config.json",
"lint": "prettier --write . && eslint --fix --max-warnings=0 .",
"tsc-cjs": "tsc -p tsconfig.cjs.json",
"tsc-esm": "tsc -p tsconfig.esm.json",
"dev": "npm run tsc-esm -- --watch",
"tsc": "tsc -p tsconfig.json",
"dev": "npm run tsc -- --watch",
"generate-notices": "generate-license-file --input package.json --output THIRD-PARTY-NOTICES --overwrite",
"generate-docs": "api-extractor run --local --verbose && api-documenter markdown --input-folder temp --output-folder docs && rm -rf temp",
"build": "rm -rf dist/** && npm run tsc-esm && npm run tsc-cjs && npm run generate-docs && npm run generate-notices"
"build:js": "rollup --config rollup.config.mjs",
"build": "rm -rf dist/** && npm run build:js && npm run generate-docs && npm run generate-notices"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -60,6 +60,8 @@
"jest": "^29.5.0",
"jest-environment-jsdom": "^29.5.0",
"prettier": "^2.8.8",
"rollup": "^3.29.0",
"rollup-plugin-typescript2": "^0.35.0",
"typescript": "^5.0.4"
}
}
42 changes: 42 additions & 0 deletions rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* \@rollup/plugin-typescript had issue with "export type":
* [!] RollupError: Unexpected token (Note that you need plugins to import files that are not JavaScript)
* Switched over to a fork rollup-plugin-typescript2 resolved the issue.
*/
import typescript from "rollup-plugin-typescript2";
import { defineConfig } from "rollup";

export default defineConfig({
input: "src/index.ts",
output: [
{
dir: "./dist/esm",
/**
* use mjs extension so NodeJS will recognize the bundle as ES modules
* https://nodejs.org/api/packages.html#determining-module-system
*/
entryFileNames: "[name].mjs",
format: "esm",
/** preserve original module files instead of compressing all to one file */
preserveModules: true,
sourcemap: true,
/**
* set to "auto" to follow TypeScript's esModuleInterop behavior to ensures compatibility
* of default, namespace, and dynamic imports from external deps (e.g. react-textarea-autosize).
*/
interop: "auto",
},
{
dir: "./dist/commonjs",
format: "cjs",
preserveModules: true,
sourcemap: true,
interop: "auto",
},
],
plugins: [
typescript({
tsconfig: "./tsconfig.json",
}),
],
});
6 changes: 2 additions & 4 deletions src/infra/ChatCoreImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import {
MessageRequest,
MessageResponse,
Endpoints,
ChatPrompt
ChatPrompt,
} from "../models";
import {
ApiMessageRequest,
} from "../models/endpoints/MessageRequest";
import { ApiMessageRequest } from "../models/endpoints/MessageRequest";
import { ApiResponse } from "../models/http/ApiResponse";
import { QueryParams } from "../models/http/params";
import { ApiResponseValidator } from "../validation/ApiResponseValidator";
Expand Down
14 changes: 7 additions & 7 deletions src/models/ChatCore.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { StreamResponse } from "../infra/StreamResponse"
import { MessageRequest } from "./endpoints/MessageRequest"
import { MessageResponse } from "./endpoints/MessageResponse"
import { StreamResponse } from "../infra/StreamResponse";
import { MessageRequest } from "./endpoints/MessageRequest";
import { MessageResponse } from "./endpoints/MessageResponse";

/**
* Provide methods for interacting with Chat API.
*
*
* @public
*/
export interface ChatCore {
Expand All @@ -16,7 +16,7 @@ export interface ChatCore {
*
* @param request - request to get next message
*/
getNextMessage(request: MessageRequest): Promise<MessageResponse>
getNextMessage(request: MessageRequest): Promise<MessageResponse>;
/**
* Make a request to Chat streaming API to generate the next message
* and consume its tokens via server-sent events.
Expand All @@ -28,5 +28,5 @@ export interface ChatCore {
*
* @param request - request to get next message
*/
streamNextMessage(request: MessageRequest): Promise<StreamResponse>
}
streamNextMessage(request: MessageRequest): Promise<StreamResponse>;
}
2 changes: 1 addition & 1 deletion src/models/InternalConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* package and subsequently moved into "stable" after performance and
* reliability are verified. It is STRONGLY recommended not to use
* "nightly" in production Chat bots.
*
*
* @internal
*/
export type ChatPrompt = "stable" | "nightly";
Expand Down
2 changes: 1 addition & 1 deletion src/models/endpoints/MessageResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { MessageNotes } from "./MessageNotes";
export interface MessageResponse {
/**
* The id corresponds to the current conversation.
*
*
* @remarks
* This is undefined only when it's an initial bot response without any user message present.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export { ChatCore } from "./ChatCore"
export { ChatCore } from "./ChatCore";
export { ChatConfig } from "./ChatConfig";
export { InternalConfig, ChatPrompt } from "./InternalConfig"
export { InternalConfig, ChatPrompt } from "./InternalConfig";

export { Endpoints } from "./endpoints/Endpoints";
export { Environment } from "./endpoints/Environment";
Expand Down
4 changes: 3 additions & 1 deletion test-browser-esm/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions test-browser-esm/src/script.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { ChatCore, StreamEventName } from "@yext/chat-core";
import { StreamEventName, provideChatCore } from "@yext/chat-core";

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

window.getNextMessage = async () => {
Expand Down
13 changes: 8 additions & 5 deletions test-node-cjs/node.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import http from "http";
import { ChatCore, StreamEventName } from "@yext/chat-core";
import { ChatConfig, StreamEventName, provideChatCore } from "@yext/chat-core";
import dotenv from "dotenv";

dotenv.config();

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

async function stream(res: any) {
const chatCore = new ChatCore(config);
const chatCore = provideChatCore(config);
const stream = await chatCore.streamNextMessage({
messages: [
{
Expand All @@ -39,7 +42,7 @@ const server = http.createServer(async (req: any, res: any) => {
if (req.url === "/streaming") {
stream(res);
} else {
const chatCore = new ChatCore(config);
const chatCore = provideChatCore(config);
const reply = await chatCore.getNextMessage({
messages: [],
});
Expand Down
Loading

0 comments on commit 3605ea2

Please sign in to comment.