From 4939d86c20a3ff4880138408350b7ad7c9c266be Mon Sep 17 00:00:00 2001 From: Govind S Date: Thu, 30 Oct 2025 16:59:12 +0530 Subject: [PATCH] fix require issue by adding custom code to rollout config --- package.json | 8 ++++---- rollup.config.js | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 rollup.config.js diff --git a/package.json b/package.json index f9dc662..a8863dd 100644 --- a/package.json +++ b/package.json @@ -2,18 +2,18 @@ "name": "wikipedia", "version": "2.4.0", "description": "A JavaScript wrapper for the wikipedia apis", - "main": "dist/index.js", + "main": "./dist/index.js", "engines": { - "node": ">=10" + "node": ">=16" }, "exports": { "import": "./dist/index.js", "require": "./dist/index.cjs" }, "scripts": { - "prepublishOnly": "tsc --build --clean && tsc && npx rollup ./dist/index.js --file ./dist/index.cjs --format cjs", + "prepublishOnly": "tsc --build --clean && tsc && npx rollup -c rollup.config.js --bundleConfigAsCjs", "test": "jest --coverage", - "build": "yarn lint && tsc --build --clean && tsc && npx rollup ./dist/index.js --file ./dist/index.cjs --format cjs", + "build": "yarn lint && tsc --build --clean && tsc && npx rollup -c rollup.config.js --bundleConfigAsCjs", "prepare": "yarn build", "lint": "eslint source/*.ts", "release": "np" diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 0000000..550d3db --- /dev/null +++ b/rollup.config.js @@ -0,0 +1,25 @@ +module.exports = { + input: './dist/index.js', + output: { + file: './dist/index.cjs', + format: 'cjs', + exports: 'auto', + outro: ` +// CommonJS compatibility: make require('wikipedia') return the function directly +// while preserving named exports as properties +const errors = require("./errors"); +const resultTypes = require("./resultTypes"); +const optionTypes = require("./optionTypes"); +const page = require("./page"); + +// Make wiki function the main export +module.exports = module.exports.default; + +// Add all named exports as properties of the wiki function +Object.assign(module.exports, errors, resultTypes, optionTypes, page); + +// Keep .default for ES module compatibility +module.exports.default = module.exports; + `.trim() + } +}; \ No newline at end of file