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

fix: cjs module resolve esm module unexpectedly #1662

Merged
merged 2 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 10 additions & 10 deletions e2e/node/smoke.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ const { Document, MetadataMode, VectorStoreIndex } = require('llamaindex')
const { OpenAIEmbedding } = require('@llamaindex/openai')
const { Settings } = require('@llamaindex/core/global')`;
const mainCode = `
async function main() {
Settings.embedModel = new OpenAIEmbedding({
model: 'text-embedding-3-small',
apiKey: '${process.env.OPENAI_API_KEY}',
})
const model = Settings.embedModel
if (model == null) {
process.exit(-1)
}
Settings.embedModel = new OpenAIEmbedding({
model: 'text-embedding-3-small',
apiKey: '${process.env.OPENAI_API_KEY}',
})
const model = Settings.embedModel
if (model == null) {
process.exit(-1)
}
main().catch(console.error)`;
const document = new Document({ text: 'Hello, world!' })
const index = await VectorStoreIndex.fromDocuments([document])
`;
t.before(async () => {
await mkdir(resolve(testRootDir, ".temp"), {
recursive: true,
Expand Down
1 change: 1 addition & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@llamaindex/clip": "^0.0.36",
"@llamaindex/cloud": "^3.0.0",
"@llamaindex/cohere": "^0.0.5",
"@llamaindex/core": "^0.5.0",
"@llamaindex/deepinfra": "^0.0.36",
"@llamaindex/env": "^0.1.27",
"@llamaindex/google": "^0.0.7",
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,10 @@
"eslint"
],
"*.{json,md}": "prettier --check"
},
"pnpm": {
"patchedDependencies": {
"bunchee@6.3.4": "patches/bunchee@6.3.4.patch"
}
}
}
4 changes: 2 additions & 2 deletions packages/core/src/vector-store/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { BaseEmbedding } from "../embeddings/base.js";
import type { BaseEmbedding } from "../embeddings";
import { Settings } from "../global";
import type { BaseNode, ModalityType } from "../schema/node.js";
import type { BaseNode, ModalityType } from "../schema";

/**
* should compatible with npm:pg and npm:postgres
Expand Down
134 changes: 134 additions & 0 deletions patches/bunchee@6.3.4.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
diff --git a/dist/bin/cli.js b/dist/bin/cli.js
old mode 100755
new mode 100644
index e74abd6fc8bd1853b82e9b84691d433a5152f6f5..d6c4f2a94346c29c0e65708e91cbfba1bb424998
--- a/dist/bin/cli.js
+++ b/dist/bin/cli.js
@@ -1082,7 +1082,7 @@ Options:
--runtime <runtime> build runtime (nodejs, browser). default: browser
--env <env> inlined process env variables, separate by comma. default: NODE_ENV
--cwd <cwd> specify current working directory
- --sourcemap enable sourcemap generation, default: false
+ --sourcemap enable sourcemap generation
--no-dts do not generate types, default: undefined
--tsconfig path to tsconfig file, default: tsconfig.json
--dts-bundle bundle type declaration files, default: false
@@ -1141,7 +1141,7 @@ async function parseCliArgs(argv) {
description: 'js features target: swc target es versions'
}).option('sourcemap', {
type: 'boolean',
- default: false,
+ default: undefined,
description: 'enable sourcemap generation'
}).option('env', {
type: 'string',
@@ -1196,6 +1196,10 @@ async function parseCliArgs(argv) {
env: args['env'],
tsconfig: args['tsconfig']
};
+ // When minify is enabled, sourcemap should be enabled by default, unless explicitly opted out
+ if (parsedArgs.minify && typeof args['sourcemap'] === 'undefined') {
+ parsedArgs.sourcemap = true;
+ }
return parsedArgs;
}
async function run(args) {
diff --git a/dist/index.js b/dist/index.js
index 66c0eba9bbbb68ec7308e7a7fe528c6a764e09e7..c1301712afee9c637013756b151c1c07b0f066c1 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -1308,7 +1308,7 @@ function hasNoSpecialCondition(conditionNames) {
...conditionNames
].every((name)=>!specialExportConventions.has(name));
}
-function findJsBundlePathCallback({ format, bundlePath, conditionNames }, specialCondition) {
+function findJsBundlePathCallback({ format, bundlePath, conditionNames }, specialCondition, defaultFormat) {
const hasBundle = bundlePath != null;
const formatCond = format === 'cjs' ? 'require' : 'import';
const isTypesCondName = conditionNames.has('types');
@@ -1317,8 +1317,9 @@ function findJsBundlePathCallback({ format, bundlePath, conditionNames }, specia
// if there's condition existed, check if the format condition is matched;
// if there's no condition, just return true, assuming format doesn't matter;
const isMatchedFormat = hasFormatCond ? conditionNames.has(formatCond) : true;
+ const isDefaultMatch = conditionNames.size === 1 && conditionNames.has('default') ? defaultFormat === format : true;
const isMatchedConditionWithFormat = conditionNames.has(specialCondition) || !conditionNames.has('default') && hasNoSpecialCondition(conditionNames);
- const match = isMatchedConditionWithFormat && !isTypesCondName && hasBundle && isMatchedFormat;
+ const match = isMatchedConditionWithFormat && !isTypesCondName && hasBundle && isMatchedFormat && isDefaultMatch;
if (!match) {
const fallback = runtimeExportConventionsFallback.get(specialCondition);
if (!fallback) {
@@ -1341,17 +1342,37 @@ function findTypesFileCallback({ format, bundlePath, conditionNames }) {
return isTypesCondName && hasCondition && (formatCond ? conditionNames.has(formatCond) : true);
}
// Alias entry key to dist bundle path
-function aliasEntries({ entry: sourceFilePath, conditionNames, entries, format, dts, cwd }) {
+function aliasEntries({ entry: sourceFilePath, conditionNames, entries, defaultFormat, format, dts, cwd }) {
// <imported source file path>: <relative path to source's bundle>
const sourceToRelativeBundleMap = new Map();
const specialCondition = getSpecialExportTypeFromConditionNames(conditionNames);
for (const [, exportCondition] of Object.entries(entries)){
const exportDistMaps = exportCondition.export;
- const exportMapEntries = Object.entries(exportDistMaps).map(([composedKey, bundlePath])=>({
- conditionNames: new Set(composedKey.split('.')),
+ const exportMapEntries = Object.entries(exportDistMaps).map(([composedKey, bundlePath])=>{
+ const conditionNames = new Set(composedKey.split('.'));
+ return {
+ conditionNames,
bundlePath,
- format
- }));
+ format,
+ isFallback: conditionNames.size === 1 && conditionNames.has('default')
+ };
+ }).sort((a, b)=>{
+ // Always put special condition after the general condition (default, cjs, esm)
+ if (a.conditionNames.has(specialCondition)) {
+ return -1;
+ } else if (b.conditionNames.has(specialCondition)) {
+ return 1;
+ }
+ // Always put default condition at the end.
+ // In the case of cjs resolves default(esm)
+ if (a.isFallback) {
+ return 1;
+ }
+ if (b.isFallback) {
+ return -1;
+ }
+ return 0;
+ });
let matchedBundlePath;
if (dts) {
var _exportMapEntries_find;
@@ -1369,19 +1390,10 @@ function aliasEntries({ entry: sourceFilePath, conditionNames, entries, format,
})) == null ? undefined : _exportMapEntries_find1.bundlePath;
}
} else {
- var _exportMapEntries_sort_find;
- matchedBundlePath = (_exportMapEntries_sort_find = exportMapEntries.sort(// always put special condition after the general condition (default, cjs, esm)
- (a, b)=>{
- if (a.conditionNames.has(specialCondition)) {
- return -1;
- }
- if (b.conditionNames.has(specialCondition)) {
- return 1;
- }
- return 0;
- }).find((item)=>{
- return findJsBundlePathCallback(item, specialCondition);
- })) == null ? undefined : _exportMapEntries_sort_find.bundlePath;
+ var _exportMapEntries_find2;
+ matchedBundlePath = (_exportMapEntries_find2 = exportMapEntries.find((item)=>{
+ return findJsBundlePathCallback(item, specialCondition, defaultFormat);
+ })) == null ? undefined : _exportMapEntries_find2.bundlePath;
}
if (matchedBundlePath) {
if (!sourceToRelativeBundleMap.has(exportCondition.source)) sourceToRelativeBundleMap.set(exportCondition.source, matchedBundlePath);
@@ -1546,6 +1558,7 @@ async function buildInputConfig(entry, bundleConfig, exportCondition, buildConte
entry,
entries,
format: aliasFormat,
+ defaultFormat: isESModulePackage(pkg.type) ? 'esm' : 'cjs',
conditionNames: new Set(currentConditionNames.split('.')),
dts,
cwd
Loading
Loading