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

[OpenAI] Bundle using rollup v3 #27258

Merged
merged 3 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
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
89 changes: 89 additions & 0 deletions sdk/openai/openai/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// 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";

function getModule(pkg) {
const module = pkg["module"];
if (!module) {
throw new Error(`${pkg.name} does not specify a module field.`);
}
return module;
}

export function makeBrowserTestConfig(pkg) {
const module = getModule(pkg);
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 cjsOutput = pkg.exports?.["."]?.require ?? pkg.main;
if (!cjsOutput) {
throw new Error("Expecting valid main entry");
}

const baseConfig = {
input: getModule(pkg),
external: [
...nodeBuiltins,
...Object.keys(pkg.dependencies),
...Object.keys(pkg.devDependencies),
],
output: { file: cjsOutput, format: "cjs", sourcemap: true, exports: "named" },
preserveSymlinks: false,
plugins: [nodeResolve(), cjs(), json()],
};

const config = [baseConfig];

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

return config;
}

const openai = (await import("./package.json", { assert: { type: "json" } })).default;
export default makeConfig(openai);
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