Skip to content

Commit 0af3dfb

Browse files
committed
cr
1 parent b1d06e4 commit 0af3dfb

File tree

100 files changed

+4860
-1359
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+4860
-1359
lines changed

β€Žinternal/build/.prettierrcβ€Ž

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"$schema": "https://json.schemastore.org/prettierrc",
3+
"printWidth": 80,
4+
"tabWidth": 2,
5+
"useTabs": false,
6+
"semi": true,
7+
"singleQuote": false,
8+
"quoteProps": "as-needed",
9+
"jsxSingleQuote": false,
10+
"trailingComma": "es5",
11+
"bracketSpacing": true,
12+
"arrowParens": "always",
13+
"requirePragma": false,
14+
"insertPragma": false,
15+
"proseWrap": "preserve",
16+
"htmlWhitespaceSensitivity": "css",
17+
"vueIndentScriptAndStyle": false,
18+
"endOfLine": "lf"
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { langchainConfig, type ConfigArray } from "@langchain/eslint";
2+
3+
const config: ConfigArray = [
4+
...langchainConfig,
5+
{
6+
rules: {
7+
"no-process-env": "off",
8+
},
9+
},
10+
];
11+
12+
export default config;

β€Žinternal/build/package.jsonβ€Ž

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
"private": true,
44
"version": "0.1.0",
55
"type": "module",
6-
"main": "./index.ts",
7-
"exports": {
8-
".": {
9-
"types": "./index.ts",
10-
"import": "./index.ts"
11-
}
6+
"main": "./src/index.ts",
7+
"types": "./src/index.ts",
8+
"scripts": {
9+
"lint:eslint": "eslint --cache src/",
10+
"lint:dpdm": "dpdm --skip-dynamic-imports circular --exit-code circular:1 --no-warning --no-tree src/*.ts src/**/*.ts",
11+
"lint": "pnpm lint:eslint && pnpm lint:dpdm",
12+
"lint:fix": "pnpm lint:eslint --fix && pnpm lint:dpdm",
13+
"format": "prettier --config .prettierrc --write \"src\"",
14+
"format:check": "prettier --config .prettierrc --check \"src\""
1215
},
1316
"dependencies": {
1417
"@arethetypeswrong/core": "^0.18.2",
@@ -18,10 +21,13 @@
1821
"rolldown": "1.0.0-beta.30",
1922
"tsdown": "^0.15.12",
2023
"type-fest": "^4.41.0",
24+
"typescript": "^5.9.3",
2125
"unplugin-unused": "^0.5.1"
2226
},
2327
"devDependencies": {
24-
"@tsconfig/recommended": "^1.0.10"
28+
"@langchain/eslint": "workspace:*",
29+
"@tsconfig/recommended": "^1.0.10",
30+
"eslint": "^9.39.1"
2531
},
2632
"optionalDependencies": {
2733
"@esbuild/win32-x64": "*",

β€Žinternal/build/index.tsβ€Ž renamed to β€Žinternal/build/src/index.tsβ€Ž

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
import { Options as BuildOptions } from "tsdown";
2+
import { PackageJson } from "type-fest";
3+
import path from "node:path";
24

35
export {
46
type CjsCompatPluginOptions,
57
cjsCompatPlugin,
6-
} from "./plugins/cjs-compat";
8+
} from "./plugins/cjs-compat.js";
79
export {
810
type ImportConstantsPluginOptions,
911
importConstantsPlugin,
10-
} from "./plugins/import-constants";
12+
} from "./plugins/import-constants.js";
1113
export {
1214
type ImportMapPluginOptions,
1315
importMapPlugin,
14-
} from "./plugins/import-map";
16+
} from "./plugins/import-map.js";
1517
export {
1618
type SecretPluginOptions,
1719
lcSecretsPlugin,
18-
} from "./plugins/lc-secrets";
20+
} from "./plugins/lc-secrets.js";
1921

2022
/**
2123
* Creates a standardized tsdown build configuration for LangChain packages.
@@ -59,7 +61,43 @@ export function getBuildConfig(options?: Partial<BuildOptions>): BuildOptions {
5961
},
6062
sourcemap: true,
6163
unbundle: true,
62-
exports: true,
64+
exports: {
65+
customExports: async (exports) => {
66+
return Object.entries(exports).reduce(
67+
(acc, [key, value]) => {
68+
if (
69+
typeof value === "object" &&
70+
value !== null &&
71+
"import" in value
72+
) {
73+
const outputPath = path.join(
74+
path.dirname(value.import),
75+
path.basename(value.import, path.extname(value.import))
76+
);
77+
const inputPath = path.join(
78+
path.dirname(value.import).replace("./dist", "./src"),
79+
`${path.basename(value.import, path.extname(value.import))}.ts`
80+
);
81+
acc[key] = {
82+
input: `./${inputPath}`,
83+
require: {
84+
types: `./${outputPath}.d.cts`,
85+
default: `./${outputPath}.cjs`,
86+
},
87+
import: {
88+
types: `./${outputPath}.d.ts`,
89+
default: `./${outputPath}.js`,
90+
},
91+
};
92+
} else {
93+
acc[key] = value;
94+
}
95+
return acc;
96+
},
97+
{} as Record<string, PackageJson.ExportConditions>
98+
);
99+
},
100+
},
63101
attw: {
64102
profile: "node16",
65103
level: "error",

β€Žinternal/build/plugins/cjs-compat.tsβ€Ž renamed to β€Žinternal/build/src/plugins/cjs-compat.tsβ€Ž

Lines changed: 74 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from "node:path";
22
import fs from "node:fs/promises";
33
import type { Plugin } from "rolldown";
4-
import { isSafeProjectPath } from "../utils";
4+
import { isSafeProjectPath } from "../utils.js";
55

66
/**
77
* Options for configuring the CJS compatibility plugin.
@@ -44,6 +44,14 @@ export interface CjsCompatPluginOptions {
4444
*/
4545
esm?: boolean;
4646
};
47+
/**
48+
* Specifies which files should be included in the package.json `files` array.
49+
*
50+
* Because this plugin generates files in the root directory, it's important
51+
* to specify which files should be included in the `files` array. In the case
52+
* you need to include extra files in the npm bundle, you can specify them here.
53+
*/
54+
files?: string[];
4755
}
4856

4957
/**
@@ -80,6 +88,8 @@ export function cjsCompatPlugin(param: CjsCompatPluginOptions = {}): Plugin {
8088
},
8189
};
8290

91+
const pathsToEmit = new Set<string>();
92+
8393
return {
8494
name: "cjs-compat",
8595
async buildStart({ input }) {
@@ -108,70 +118,45 @@ export function cjsCompatPlugin(param: CjsCompatPluginOptions = {}): Plugin {
108118
const importPath =
109119
barrelDepth === 0
110120
? `./dist/${barrelTarget}`
111-
: "../".repeat(barrelDepth) + `dist/${barrelTarget}`;
121+
: `${"../".repeat(barrelDepth)}dist/${barrelTarget}`;
112122

113123
// Skip the root barrel file
114124
if (barrelPath === ".") {
115125
continue;
116126
}
117127

118-
if (options.mode === "generate") {
119-
if (options.shouldGenerate.dcts) {
120-
this.emitFile({
121-
type: "asset",
122-
fileName: `../${barrelPath}.d.cts`,
123-
source: generateDctsBarrel(importPath),
124-
});
125-
}
126-
if (options.shouldGenerate.cjs) {
127-
this.emitFile({
128-
type: "asset",
129-
fileName: `../${barrelPath}.cjs`,
130-
source: generateCjsBarrel(importPath),
131-
});
132-
}
133-
if (options.shouldGenerate.dts) {
128+
const emitFile = async (fileName: string, source: string) => {
129+
const topLevelPath = fileName.split("/")[0];
130+
pathsToEmit.add(topLevelPath);
131+
if (options.mode === "generate") {
134132
this.emitFile({
135133
type: "asset",
136-
fileName: `../${barrelPath}.d.ts`,
137-
source: generateDtsBarrel(importPath),
134+
fileName: `../${fileName}`,
135+
source,
138136
});
139137
}
140-
if (options.shouldGenerate.esm) {
141-
this.emitFile({
142-
type: "asset",
143-
fileName: `../${barrelPath}.js`,
144-
source: generateEsmBarrel(importPath),
145-
});
146-
}
147-
}
148-
149-
if (options.mode === "clean") {
150-
if (options.shouldGenerate.dcts) {
151-
const target = path.resolve(`./${barrelPath}.d.cts`);
152-
if (isSafeProjectPath(target)) {
153-
await fs.unlink(target).catch(() => {});
154-
}
155-
}
156-
if (options.shouldGenerate.cjs) {
157-
const target = path.resolve(`./${barrelPath}.cjs`);
158-
if (isSafeProjectPath(target)) {
159-
await fs.unlink(target).catch(() => {});
160-
}
161-
}
162-
if (options.shouldGenerate.dts) {
163-
const target = path.resolve(`./${barrelPath}.d.ts`);
164-
if (isSafeProjectPath(target)) {
165-
await fs.unlink(target).catch(() => {});
166-
}
167-
}
168-
if (options.shouldGenerate.esm) {
169-
const target = path.resolve(`./${barrelPath}.js`);
138+
if (options.mode === "clean") {
139+
const target = path.resolve(`./${fileName}`);
170140
if (isSafeProjectPath(target)) {
171141
await fs.unlink(target).catch(() => {});
172142
}
173143
}
144+
};
174145

146+
if (options.shouldGenerate.dcts) {
147+
await emitFile(`${barrelPath}.d.cts`, generateDctsBarrel(importPath));
148+
}
149+
if (options.shouldGenerate.cjs) {
150+
await emitFile(`${barrelPath}.cjs`, generateCjsBarrel(importPath));
151+
}
152+
if (options.shouldGenerate.dts) {
153+
await emitFile(`${barrelPath}.d.ts`, generateDtsBarrel(importPath));
154+
}
155+
if (options.shouldGenerate.esm) {
156+
await emitFile(`${barrelPath}.js`, generateEsmBarrel(importPath));
157+
}
158+
159+
if (options.mode === "clean") {
175160
// Remove any directories that were created for nested entrypoints
176161
const dirPath = path.dirname(barrelPath);
177162
if (dirPath !== ".") {
@@ -182,6 +167,45 @@ export function cjsCompatPlugin(param: CjsCompatPluginOptions = {}): Plugin {
182167
}
183168
}
184169
}
170+
171+
// Add the files to the package.json files array
172+
const packageJsonPath = path.resolve(
173+
process.env.INIT_CWD ?? "",
174+
"package.json"
175+
);
176+
const packageJson = JSON.parse(
177+
await fs.readFile(packageJsonPath, "utf-8")
178+
);
179+
const newPackageJson = {
180+
...packageJson,
181+
files: [...(options.files ?? []), ...Array.from(pathsToEmit)],
182+
};
183+
await fs.writeFile(
184+
packageJsonPath,
185+
JSON.stringify(newPackageJson, null, 2)
186+
);
187+
},
188+
buildEnd: {
189+
async handler() {
190+
if (!options.enabled) return;
191+
192+
const packageJsonPath = path.resolve(
193+
process.env.INIT_CWD ?? "",
194+
"package.json"
195+
);
196+
const packageJson = JSON.parse(
197+
await fs.readFile(packageJsonPath, "utf-8")
198+
);
199+
packageJson.files = [
200+
...(options.files ?? []),
201+
...Array.from(pathsToEmit),
202+
];
203+
await fs.writeFile(
204+
packageJsonPath,
205+
JSON.stringify(packageJson, null, 2)
206+
);
207+
},
208+
order: "post",
185209
},
186210
};
187211
}

β€Žinternal/build/plugins/import-map.tsβ€Ž renamed to β€Žinternal/build/src/plugins/import-map.tsβ€Ž

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from "node:path";
22
import type { Plugin } from "rolldown";
3-
import { formatWithPrettier } from "../utils";
3+
import { formatWithPrettier } from "../utils.js";
44

55
/**
66
* Configuration for an extra import map entry that should be included
@@ -112,6 +112,15 @@ export function importMapPlugin(param: ImportMapPluginOptions = {}): Plugin {
112112
return true;
113113
});
114114

115+
const dedupedImportEntries = options.extraEntries.reduce(
116+
(acc, { modules, path }) => {
117+
acc[path] ??= { modules: [], alias: [], path };
118+
acc[path].modules.push(...modules);
119+
return acc;
120+
},
121+
{} as Record<string, ExtraImportMapEntry>
122+
);
123+
115124
const lines = [
116125
`/** Auto-generated by import-map plugin. Do not edit manually */`,
117126
``,
@@ -152,12 +161,17 @@ export function importMapPlugin(param: ImportMapPluginOptions = {}): Plugin {
152161
return acc;
153162
}, [] as string[]),
154163
// Generate regular imports statements for extra entries
155-
...options.extraEntries.reduce((acc, { modules, path }) => {
156-
if (!modules.includes("*")) {
157-
acc.push(`import {\n ${modules.join(",\n ")}\n} from "${path}";`);
158-
}
159-
return acc;
160-
}, [] as string[]),
164+
...Object.values(dedupedImportEntries).reduce(
165+
(acc, { modules, path }) => {
166+
if (!modules.includes("*")) {
167+
acc.push(
168+
`import {\n ${modules.join(",\n ")}\n} from "${path}";`
169+
);
170+
}
171+
return acc;
172+
},
173+
[] as string[]
174+
),
161175
// Generate alias declarations and exports for extra entries
162176
...options.extraEntries.reduce((acc, { modules, alias }) => {
163177
const exportAlias = alias.join("__");

β€Žinternal/build/plugins/lc-secrets.tsβ€Ž renamed to β€Žinternal/build/src/plugins/lc-secrets.tsβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from "node:fs";
22
import path from "node:path";
33
import ts from "typescript";
44
import type { Plugin, PluginContext } from "rolldown";
5-
import { formatWithPrettier } from "../utils";
5+
import { formatWithPrettier } from "../utils.js";
66

77
/**
88
* Configuration options for the lc-secrets plugin.
@@ -190,7 +190,7 @@ function scanForSecrets(excludePatterns: string[]): SecretInfo[] {
190190
);
191191

192192
scanSourceFile(sourceFile, fileName, secrets);
193-
} catch (error) {
193+
} catch {
194194
// Silently skip files that can't be parsed
195195
}
196196
}
File renamed without changes.

0 commit comments

Comments
Β (0)