diff --git a/package.json b/package.json index c626188..fd1eadd 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,20 @@ { - "type": "module", "name": "ollama", "version": "0.4.0", "description": "Ollama Javascript library", - "main": "dist/index.js", + "main": "dist/cjs/index.js", + "module": "dist/mjs/index.js", "types": "dist/index.d.ts", + "exports": { + ".": { + "import": "./dist/mjs/index.js", + "require": "./dist/cjs/index.js" + } + }, "scripts": { "format": "prettier --write .", "test": "jest --config=jest.config.cjs ./test/*", - "build": "mkdir -p dist && touch dist/cleanup && rm dist/* && tsc -b", + "build": "rm -fr dist/* && tsc -p tsconfig.json && tsc -p tsconfig-cjs.json && node scripts/build-cjs-mjs.js", "lint": "eslint ./src/* ./test/*", "prepublishOnly": "npm run build" }, diff --git a/scripts/build-cjs-mjs.js b/scripts/build-cjs-mjs.js new file mode 100644 index 0000000..278df87 --- /dev/null +++ b/scripts/build-cjs-mjs.js @@ -0,0 +1,15 @@ +const fs = require('fs') +const path = require('path') + +// Function to create a package.json file with specified type +function createPackageJson(dir, type) { + const content = { + type: type, + } + const filePath = path.join(dir, 'package.json') + fs.mkdirSync(dir, { recursive: true }) + fs.writeFileSync(filePath, JSON.stringify(content, null, 4)) +} + +createPackageJson('dist/cjs', 'commonjs') +createPackageJson('dist/mjs', 'module') diff --git a/tsconfig-base.json b/tsconfig-base.json new file mode 100644 index 0000000..4595fd3 --- /dev/null +++ b/tsconfig-base.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "noImplicitAny": false, + "noImplicitThis": true, + "strictNullChecks": true, + "esModuleInterop": true, + "declaration": true, + "declarationMap": true, + "skipLibCheck": true, + "strict": true, + "forceConsistentCasingInFileNames": true, + "moduleResolution": "node" + }, + "include": ["./src/**/*.ts"], + "exclude": ["node_modules"] +} diff --git a/tsconfig-cjs.json b/tsconfig-cjs.json new file mode 100644 index 0000000..01351fd --- /dev/null +++ b/tsconfig-cjs.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig-base.json", + "compilerOptions": { + "module": "commonjs", + "outDir": "dist/cjs", + "target": "es2015" + } +} diff --git a/tsconfig.json b/tsconfig.json index 102fe1f..edb3e90 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,26 +1,8 @@ { + "extends": "./tsconfig-base.json", "compilerOptions": { - "noImplicitAny": false, - "noImplicitThis": true, - "strictNullChecks": true, - "esModuleInterop": true, - "declaration": true, - "declarationMap": true, - "skipLibCheck": true, - "strict": true, - "forceConsistentCasingInFileNames": true, - "moduleResolution": "node", - "module": "ES2022", - "outDir": "./dist", - "target": "ES6", + "module": "esnext", + "outDir": "dist/mjs", + "target": "esnext", }, - - "ts-node": { - "swc": true, - "esm": true, - }, - - "include": ["./src/**/*.ts"], - - "exclude": ["node_modules"], }