Skip to content

Commit 6d456d2

Browse files
committed
feat: add ESM build support with dual CommonJS/ESM exports
- Add separate TypeScript configs for ESM and CommonJS builds - Configure package.json with proper dual exports - Add build script to fix ESM imports with .js extensions - Support both import and require syntax - Add CONTRIBUTING.md with contribution guidelines
1 parent 9dafe03 commit 6d456d2

File tree

5 files changed

+182
-24
lines changed

5 files changed

+182
-24
lines changed

CONTRIBUTING.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Contributing to DevMe SDK JS
2+
3+
Thank you for your interest in contributing to DevMe SDK JS! We welcome contributions from the community.
4+
5+
## Getting Started
6+
7+
1. Fork the repository
8+
2. Clone your fork locally
9+
3. Create a new branch for your feature or fix
10+
4. Make your changes
11+
5. Push to your fork
12+
6. Submit a pull request to the `master` branch
13+
14+
## Development Setup
15+
16+
```bash
17+
# Install dependencies
18+
npm install
19+
20+
# Run tests
21+
npm test
22+
23+
# Run linter
24+
npm run lint
25+
26+
# Run type checking
27+
npm run typecheck
28+
29+
# Build the project
30+
npm run build
31+
```
32+
33+
## Code Style
34+
35+
- Follow the existing code style in the project
36+
- Use TypeScript for all new code
37+
- Ensure all tests pass before submitting
38+
- Run linter and fix any issues
39+
- Ensure type checking passes
40+
41+
## Commit Messages
42+
43+
- Use clear and descriptive commit messages
44+
- Follow conventional commit format when possible:
45+
- `feat:` for new features
46+
- `fix:` for bug fixes
47+
- `docs:` for documentation changes
48+
- `refactor:` for code refactoring
49+
- `test:` for test additions or changes
50+
- `chore:` for maintenance tasks
51+
52+
## Pull Request Process
53+
54+
1. Update the README.md with details of changes if applicable
55+
2. Ensure all tests pass
56+
3. Update documentation as needed
57+
4. Request review from maintainers
58+
5. Address any feedback provided
59+
60+
## Reporting Issues
61+
62+
- Use GitHub Issues to report bugs
63+
- Provide clear reproduction steps
64+
- Include relevant system information
65+
- Attach any relevant logs or screenshots
66+
67+
## Questions?
68+
69+
Feel free to open an issue for any questions about contributing.
70+
71+
## License
72+
73+
By contributing, you agree that your contributions will be licensed under the same license as the project.

package.json

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,39 +28,55 @@
2828
},
2929
"license": "MIT",
3030
"author": "DEV.ME <support@dev.me> (https://dev.me)",
31-
"main": "./dist/index.js",
32-
"types": "./dist/index.d.ts",
33-
"typings": "./dist/index.d.ts",
31+
"main": "./dist/cjs/index.js",
32+
"module": "./dist/esm/index.js",
33+
"types": "./dist/cjs/index.d.ts",
34+
"typings": "./dist/cjs/index.d.ts",
3435
"exports": {
3536
".": {
36-
"types": "./dist/index.d.ts",
37-
"require": "./dist/index.js",
38-
"import": "./dist/index.js",
39-
"default": "./dist/index.js"
37+
"types": {
38+
"import": "./dist/esm/index.d.ts",
39+
"require": "./dist/cjs/index.d.ts"
40+
},
41+
"import": "./dist/esm/index.js",
42+
"require": "./dist/cjs/index.js",
43+
"default": "./dist/cjs/index.js"
4044
},
4145
"./api": {
42-
"types": "./dist/api.d.ts",
43-
"require": "./dist/api.js",
44-
"import": "./dist/api.js",
45-
"default": "./dist/api.js"
46+
"types": {
47+
"import": "./dist/esm/api.d.ts",
48+
"require": "./dist/cjs/api.d.ts"
49+
},
50+
"import": "./dist/esm/api.js",
51+
"require": "./dist/cjs/api.js",
52+
"default": "./dist/cjs/api.js"
4653
},
4754
"./configuration": {
48-
"types": "./dist/configuration.d.ts",
49-
"require": "./dist/configuration.js",
50-
"import": "./dist/configuration.js",
51-
"default": "./dist/configuration.js"
55+
"types": {
56+
"import": "./dist/esm/configuration.d.ts",
57+
"require": "./dist/cjs/configuration.d.ts"
58+
},
59+
"import": "./dist/esm/configuration.js",
60+
"require": "./dist/cjs/configuration.js",
61+
"default": "./dist/cjs/configuration.js"
5262
},
5363
"./base": {
54-
"types": "./dist/base.d.ts",
55-
"require": "./dist/base.js",
56-
"import": "./dist/base.js",
57-
"default": "./dist/base.js"
64+
"types": {
65+
"import": "./dist/esm/base.d.ts",
66+
"require": "./dist/cjs/base.d.ts"
67+
},
68+
"import": "./dist/esm/base.js",
69+
"require": "./dist/cjs/base.js",
70+
"default": "./dist/cjs/base.js"
5871
},
5972
"./common": {
60-
"types": "./dist/common.d.ts",
61-
"require": "./dist/common.js",
62-
"import": "./dist/common.js",
63-
"default": "./dist/common.js"
73+
"types": {
74+
"import": "./dist/esm/common.d.ts",
75+
"require": "./dist/cjs/common.d.ts"
76+
},
77+
"import": "./dist/esm/common.js",
78+
"require": "./dist/cjs/common.js",
79+
"default": "./dist/cjs/common.js"
6480
},
6581
"./package.json": "./package.json"
6682
},
@@ -72,7 +88,11 @@
7288
"package.json"
7389
],
7490
"scripts": {
75-
"build": "rm -rf dist && tsc -p .",
91+
"build": "rm -rf dist && yarn build:cjs && yarn build:esm && yarn build:fix-esm && yarn build:package-json",
92+
"build:cjs": "tsc -p tsconfig.cjs.json",
93+
"build:esm": "tsc -p tsconfig.esm.json",
94+
"build:fix-esm": "node scripts/fix-esm-imports.js",
95+
"build:package-json": "echo '{\"type\": \"module\"}' > dist/esm/package.json && echo '{\"type\": \"commonjs\"}' > dist/cjs/package.json",
7696
"lint": "biome check .",
7797
"lint:fix": "biome check --write .",
7898
"format": "biome format --write .",

scripts/fix-esm-imports.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
function addJsExtensions(dir) {
5+
const files = fs.readdirSync(dir, { withFileTypes: true });
6+
7+
for (const file of files) {
8+
const filePath = path.join(dir, file.name);
9+
10+
if (file.isDirectory()) {
11+
addJsExtensions(filePath);
12+
} else if (file.name.endsWith('.js')) {
13+
let content = fs.readFileSync(filePath, 'utf8');
14+
15+
// Fix relative imports to add .js extension
16+
content = content.replace(
17+
/from\s+['"](\.\/[^'"]+)(?<!\.js)['"]/g,
18+
"from '$1.js'"
19+
);
20+
21+
// Fix export statements
22+
content = content.replace(
23+
/export\s+\*\s+from\s+['"](\.\/[^'"]+)(?<!\.js)['"]/g,
24+
"export * from '$1.js'"
25+
);
26+
27+
// Fix import statements
28+
content = content.replace(
29+
/import\s+(.+)\s+from\s+['"](\.\/[^'"]+)(?<!\.js)['"]/g,
30+
"import $1 from '$2.js'"
31+
);
32+
33+
fs.writeFileSync(filePath, content);
34+
}
35+
}
36+
}
37+
38+
// Fix ESM imports in dist/esm directory
39+
const esmDir = path.join(__dirname, '..', 'dist', 'esm');
40+
if (fs.existsSync(esmDir)) {
41+
addJsExtensions(esmDir);
42+
console.log('Fixed ESM import extensions');
43+
} else {
44+
console.error('ESM build directory not found');
45+
process.exit(1);
46+
}

tsconfig.cjs.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "./dist/cjs",
5+
"module": "commonjs",
6+
"declaration": true,
7+
"declarationDir": "./dist/cjs"
8+
}
9+
}

tsconfig.esm.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "./dist/esm",
5+
"module": "ES2020",
6+
"target": "ES2020",
7+
"declaration": true,
8+
"declarationDir": "./dist/esm"
9+
}
10+
}

0 commit comments

Comments
 (0)