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

Hybrid NPM Module for ESM and CommonJS #3

Closed
wants to merge 2 commits into from
Closed
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
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
},
Expand Down
15 changes: 15 additions & 0 deletions scripts/build-cjs-mjs.js
Original file line number Diff line number Diff line change
@@ -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')
16 changes: 16 additions & 0 deletions tsconfig-base.json
Original file line number Diff line number Diff line change
@@ -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"]
}
8 changes: 8 additions & 0 deletions tsconfig-cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig-base.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "dist/cjs",
"target": "es2015"
}
}
26 changes: 4 additions & 22 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"],
}