Skip to content

Commit 90cf673

Browse files
committed
build(esm): fix missing .js extensions in ESM imports v0.1.9
1 parent 8940aa4 commit 90cf673

10 files changed

Lines changed: 48 additions & 33 deletions

File tree

dist/constants.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Constants for @AstroFusion/sweph
33
*/
4-
import { PlanetId, AyanamsaType, HouseSystem } from './types';
4+
import { PlanetId, AyanamsaType, HouseSystem } from './types.js';
55
/**
66
* Planet definitions for Vedic astrology (9 grahas)
77
*/

dist/houses.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
22
* House and Lagna Calculations for @AstroFusion/sweph
33
*/
4-
import { HouseSystem } from './types';
5-
import { initializeSweph, getNativeModule, dateToJulian, getAyanamsa, normalizeLongitude, getRashi, getRashiDegree } from './utils';
4+
import { HouseSystem } from './types.js';
5+
import { initializeSweph, getNativeModule, dateToJulian, getAyanamsa, normalizeLongitude, getRashi, getRashiDegree } from './utils.js';
66
/**
77
* Calculate Lagna (Ascendant) and house cusps for Vedic astrology
88
* @param date - Birth date and time (local time)

dist/index.mjs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
* All public APIs are exported from here.
66
*/
77
// Types
8-
export * from './types';
8+
export * from './types.js';
99
// Core calculation functions
10-
export { calculatePlanets, calculateSinglePlanet, calculatePlanetRiseSetTimes, } from './planets';
11-
export { calculateLagna, calculateHouses, } from './houses';
12-
export { calculateSunTimes, calculateSolarNoon, calculateSunPath, } from './sun';
13-
export { calculateMoonData, calculateMoonPhase, calculateNextMoonPhases, } from './moon';
10+
export { calculatePlanets, calculateSinglePlanet, calculatePlanetRiseSetTimes, } from './planets.js';
11+
export { calculateLagna, calculateHouses, } from './houses.js';
12+
export { calculateSunTimes, calculateSolarNoon, calculateSunPath, } from './sun.js';
13+
export { calculateMoonData, calculateMoonPhase, calculateNextMoonPhases, } from './moon.js';
1414
// Utility functions
15-
export { getAyanamsa, setEphemerisPath, getJulianDay, dateToJulian, julianToDate, getNativeModule, } from './utils';
15+
export { getAyanamsa, setEphemerisPath, getJulianDay, dateToJulian, julianToDate, getNativeModule, } from './utils.js';
1616
// Constants
17-
export { PLANETS, AYANAMSA, HOUSE_SYSTEMS, RASHIS, NAKSHATRAS, VEDIC_PLANET_ORDER, } from './constants';
17+
export { PLANETS, AYANAMSA, HOUSE_SYSTEMS, RASHIS, NAKSHATRAS, VEDIC_PLANET_ORDER, } from './constants.js';
1818
// Legacy compatibility exports (for migration from @astrofusion/sweph-*)
1919
export {
2020
// Factory functions
21-
createSwephCalculator, createPlanetaryCalculator, createSwephAdapter, createNodeAdapter, initializeSweph, registerAdapter, calculateKundaliPageData, } from './legacy';
21+
createSwephCalculator, createPlanetaryCalculator, createSwephAdapter, createNodeAdapter, initializeSweph, registerAdapter, calculateKundaliPageData, } from './legacy.js';
2222
//# sourceMappingURL=index.js.map

dist/legacy.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* This file provides backwards-compatible exports for code migrating
55
* from the old @astrofusion/sweph-* packages.
66
*/
7-
import { calculatePlanets, calculateSinglePlanet, calculatePlanetRiseSetTimes, calculateLagna, calculateSunTimes, calculateSunPath, calculateMoonData, calculateMoonPhase, calculateNextMoonPhases, } from './index';
8-
import { initializeSweph as initSweph } from './utils';
7+
import { calculatePlanets, calculateSinglePlanet, calculatePlanetRiseSetTimes, calculateLagna, calculateSunTimes, calculateSunPath, calculateMoonData, calculateMoonPhase, calculateNextMoonPhases, } from './index.js';
8+
import { initializeSweph as initSweph } from './utils.js';
99
// ===== Legacy Factory Functions =====
1010
/**
1111
* Creates a PlanetaryCalculationProvider instance

dist/moon.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/**
22
* Moon Calculations for @AstroFusion/sweph
33
*/
4-
import { PlanetId } from './types';
5-
import { MOON_PHASES, LUNAR_MONTH_DAYS, DEFAULT_MOON_DISTANCE_KM, AU_IN_KM } from './constants';
6-
import { initializeSweph, getNativeModule, dateToJulian, julianToDate, callRiseTrans } from './utils';
4+
import { PlanetId } from './types.js';
5+
import { MOON_PHASES, LUNAR_MONTH_DAYS, DEFAULT_MOON_DISTANCE_KM, AU_IN_KM } from './constants.js';
6+
import { initializeSweph, getNativeModule, dateToJulian, julianToDate, callRiseTrans } from './utils.js';
77
/**
88
* Calculate comprehensive moon data including rise/set times and phase
99
* @param date - Date for moon calculation (local time)

dist/planets.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
22
* Planet Calculations for @AstroFusion/sweph
33
*/
4-
import { VEDIC_PLANET_ORDER, OUTER_PLANETS, CALC_FLAGS } from './constants';
5-
import { initializeSweph, getNativeModule, dateToJulian, julianToDate, normalizeLongitude, getRashi, getRashiDegree, isRetrograde, callRiseTrans, callAzAlt } from './utils';
4+
import { VEDIC_PLANET_ORDER, OUTER_PLANETS, CALC_FLAGS } from './constants.js';
5+
import { initializeSweph, getNativeModule, dateToJulian, julianToDate, normalizeLongitude, getRashi, getRashiDegree, isRetrograde, callRiseTrans, callAzAlt } from './utils.js';
66
/**
77
* Calculate azimuth and altitude for a celestial body
88
* @param sweph - Swiss Ephemeris native module instance

dist/sun.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
22
* Sun Calculations for @AstroFusion/sweph
33
*/
4-
import { PlanetId } from './types';
5-
import { initializeSweph, getNativeModule, dateToJulian, julianToDate, callRiseTrans, callAzAlt } from './utils';
4+
import { PlanetId } from './types.js';
5+
import { initializeSweph, getNativeModule, dateToJulian, julianToDate, callRiseTrans, callAzAlt } from './utils.js';
66
/**
77
* Calculate sunrise, sunset, and twilight times for a location
88
* @param date - Date for sun time calculation (local time)

dist/utils.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const __dirname = dirname(__filename);
1010
* Utility Functions for @AstroFusion/sweph
1111
*/
1212
import path from 'path';
13-
import { JULIAN_UNIX_EPOCH } from './constants';
13+
import { JULIAN_UNIX_EPOCH } from './constants.js';
1414
// Native module instance
1515
let sweph = null;
1616
let ephemerisPath = null;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@af/sweph",
3-
"version": "0.1.8",
3+
"version": "0.1.9",
44
"description": "Swiss Ephemeris library for Vedic astrology calculations with pre-built native binaries",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

scripts/post-build-esm.js

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ if (fs.existsSync(esmDir)) {
1919

2020
let content = fs.readFileSync(src, 'utf8');
2121

22-
// Inject ESM compatibility shims for utils.js (which becomes utils.mjs)
23-
// failing which `require` and `__dirname` would be undefined
22+
// 1. Inject ESM compatibility shims for utils.js (which becomes utils.mjs)
2423
if (file === 'utils.js') {
2524
const shim = [
2625
"import { createRequire } from 'module';",
@@ -34,24 +33,40 @@ if (fs.existsSync(esmDir)) {
3433
""
3534
].join('\n');
3635

37-
// Inject after imports (simplistic approach: prepend to file or after first few lines?)
38-
// Since tsc output puts imports at top, prepending works IF there are no shell shebangs (unlikely for library).
39-
// However, imports must come before other code.
40-
// But we are injecting imports ourselves.
41-
// The issue is if existing imports rely on `require`? No, existing imports are ESM `import ...`.
42-
// `require` is used in the BODY.
43-
// So prepending imports is fine.
44-
4536
content = shim + content;
4637
console.log(`✅ Injected ESM shims into ${file.replace('.js', '.mjs')}`);
4738
}
39+
40+
// 2. Add .js extensions to relative imports/exports
41+
// Matches: import ... from './foo' or export ... from './foo'
42+
// Capture group 1: import/export statement part
43+
// Capture group 2: quote
44+
// Capture group 3: path (starting with . or ..)
45+
// Capture group 4: quote
46+
// We only want to append .js if it doesn't already have an extension (simplistic check)
47+
// Actually simpler regex: replacing /from\s+['"](\..*?)['"]/g
48+
49+
content = content.replace(/from\s+['"](\.[^'"]+)['"]/g, (match, importPath) => {
50+
if (!importPath.endsWith('.js') && !importPath.endsWith('.mjs')) {
51+
return `from '${importPath}.js'`;
52+
}
53+
return match;
54+
});
55+
56+
// Also handle dynamic imports() if any (not present in this lib likely, but good practice)
57+
content = content.replace(/import\s*\(['"](\.[^'"]+)['"]\)/g, (match, importPath) => {
58+
if (!importPath.endsWith('.js') && !importPath.endsWith('.mjs')) {
59+
return `import('${importPath}.js')`;
60+
}
61+
return match;
62+
});
4863

4964
fs.writeFileSync(dest, content);
5065
});
5166

5267
// Cleanup temp esm dir
5368
fs.rmSync(esmDir, { recursive: true, force: true });
54-
console.log('✅ ESM build completed successfully');
69+
console.log('✅ ESM build completed successfully with extension fix');
5570
} else {
5671
console.log('⚠️ dist-esm directory not found, skipping ESM post-processing');
5772
}

0 commit comments

Comments
 (0)