Skip to content

Commit

Permalink
[OpenAI] Bundle using rollup v3
Browse files Browse the repository at this point in the history
  • Loading branch information
deyaaeldeen committed Sep 27, 2023
1 parent 8d8ded0 commit fd484bc
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 37 deletions.
121 changes: 113 additions & 8 deletions common/config/rush/pnpm-lock.yaml

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

15 changes: 11 additions & 4 deletions sdk/openai/openai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@
"build:samples": "echo Obsolete",
"generate": "pwsh ../../../eng/common/scripts/TypeSpec-Project-Sync.ps1 . && pwsh ../../../eng/common/scripts/TypeSpec-Project-Generate.ps1 .",
"customize": "rimraf src && dev-tool customization apply -s sources/generated/src && npm run format",
"build": "npm run clean && tsc -p . && dev-tool run bundle && api-extractor run --local",
"build:test": "tsc -p . && dev-tool run bundle",
"bundle": "tsc -p . && rollup -c ./rollup.config.js",
"build": "npm run clean && npm run bundle && api-extractor run --local",
"build:test": "npm run bundle",
"check-format": "prettier --list-different --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"",
"clean": "rimraf dist dist-* temp types *.tgz *.log",
"execute:samples": "dev-tool samples run samples-dev",
Expand Down Expand Up @@ -104,8 +105,13 @@
"@azure/test-utils": "^1.0.0",
"@azure/core-rest-pipeline": "^1.12.0",
"@microsoft/api-extractor": "^7.31.1",
"@rollup/plugin-commonjs": "^25.0.4",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-multi-entry": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.2.1",
"@types/mocha": "^10.0.0",
"@types/node": "^14.0.0",
"builtin-modules": "^3.3.0",
"cross-env": "^7.0.3",
"dotenv": "^16.0.0",
"eslint": "^8.16.0",
Expand All @@ -128,6 +134,7 @@
"prettier": "^2.5.1",
"puppeteer": "^19.2.2",
"rimraf": "^3.0.2",
"rollup": "^3.29.3",
"ts-node": "^10.0.0",
"typescript": "~5.0.0"
},
Expand All @@ -138,8 +145,8 @@
"@azure/core-lro": "^2.5.3",
"@azure/core-rest-pipeline": "^1.10.2",
"@azure/logger": "^1.0.3",
"formdata-node": "^4.0.0",
"form-data-encoder": "1.7.2",
"formdata-node": "^5.0.0",
"form-data-encoder": "^3.0.0",
"tslib": "^2.4.0"
},
"//sampleConfiguration": {
Expand Down
80 changes: 80 additions & 0 deletions sdk/openai/openai/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import nodeResolve from "@rollup/plugin-node-resolve";
import cjs from "@rollup/plugin-commonjs";
import multiEntry from "@rollup/plugin-multi-entry";
import json from "@rollup/plugin-json";
import * as path from "path";

import nodeBuiltins from "builtin-modules";

import * as openaiPkg from "./package.json" assert { type: "json" };

// #endregion

export function makeBrowserTestConfig(pkg) {
const module = pkg["module"] ?? "dist-esm/src/index.js";
const basePath = path.dirname(path.parse(module).dir);

const config = {
input: path.join(basePath, "test", "**", "*.spec.js"),
output: {
file: `dist-test/index.browser.js`,
format: "umd",
sourcemap: true,
},
preserveSymlinks: false,
plugins: [
multiEntry({ exports: false, exclude: ["**/test/**/node/*.js"] }),
nodeResolve({
mainFields: ["module", "browser"],
preferBuiltins: false,
browser: true,
}),
cjs(),
json(),
],
// Disable tree-shaking of test code. In rollup-plugin-node-resolve@5.0.0,
// rollup started respecting the "sideEffects" field in package.json. Since
// our package.json sets "sideEffects=false", this also applies to test
// code, which causes all tests to be removed by tree-shaking.
treeshake: false,
};

return config;
}

const defaultConfigurationOptions = {
disableBrowserBundle: false,
};

export function makeConfig(pkg, options) {
options = {
...defaultConfigurationOptions,
...(options ?? {}),
};

const baseConfig = {
// Use the package's module field if it has one
input: pkg["module"] ?? "dist-esm/src/index.js",
external: [
...nodeBuiltins,
...Object.keys(pkg.dependencies),
...Object.keys(pkg.devDependencies),
],
output: { file: "dist/index.cjs", format: "cjs", sourcemap: true, exports: "named" },
preserveSymlinks: false,
plugins: [nodeResolve(), cjs(), json()],
};

const config = [baseConfig];

if (!options.disableBrowserBundle) {
config.push(makeBrowserTestConfig(pkg));
}

return config;
}

export default makeConfig(openaiPkg.default);
4 changes: 2 additions & 2 deletions sdk/openai/openai/sources/customizations/api/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export async function getAudioTranslation<Format extends AudioResultFormat>(
.pathUnchecked("deployments/{deploymentId}/audio/translations", deploymentName)
.post({
body: {
file: createFile(fileContent),
file: await createFile(fileContent),
...(response_format && { response_format }),
...(temperature !== undefined ? { temperature } : {}),
...(prompt && { prompt }),
Expand Down Expand Up @@ -275,7 +275,7 @@ export async function getAudioTranscription<Format extends AudioResultFormat>(
.pathUnchecked("deployments/{deploymentId}/audio/transcriptions", deploymentName)
.post({
body: {
file: createFile(fileContent),
file: await createFile(fileContent),
...(response_format && { response_format }),
...(language && { language }),
...(temperature !== undefined ? { temperature } : {}),
Expand Down
Loading

0 comments on commit fd484bc

Please sign in to comment.