Skip to content

Commit 55a1370

Browse files
authored
Merge pull request #3 from oyve/dev
Support commonjs and ESM
2 parents 6077d5f + 16b4263 commit 55a1370

File tree

7 files changed

+107
-27
lines changed

7 files changed

+107
-27
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,8 @@ If you are using a CommonJS project, you can dynamically import the package:
8080
```javascript
8181
(async () => {
8282
const weatherFormulas = await import('weather-formulas');
83-
const { temperature } = weatherFormulas;
84-
const RH = temperature.relativeHumidity(TEMPERATURE, DEW_POINT);
85-
console.log(`Relative Humidity: ${RH}%`);
83+
const { humidity } = weatherFormulas;
84+
const RH = humidity.relativeHumidity(TEMPERATURE, DEW_POINT);
8685
})();
8786
```
8887

add-js-extensions.cjs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
4+
const esmDir = path.join(__dirname, "dist/esm");
5+
6+
function addJsExtensions(dir) {
7+
const files = fs.readdirSync(dir);
8+
for (const file of files) {
9+
const fullPath = path.join(dir, file);
10+
11+
// If it's a directory, recursively process it
12+
if (fs.statSync(fullPath).isDirectory()) {
13+
addJsExtensions(fullPath);
14+
}
15+
// If it's a .js file, process its content
16+
else if (file.endsWith(".js")) {
17+
let content = fs.readFileSync(fullPath, "utf8");
18+
19+
// Replace import/export paths without .js extensions
20+
content = content.replace(/from\s+['"](\..*?[^.])['"]/g, (match, p1) => {
21+
if (!p1.endsWith(".js")) {
22+
return `from '${p1}.js'`;
23+
}
24+
return match;
25+
});
26+
27+
// Write the updated content back to the file
28+
fs.writeFileSync(fullPath, content, "utf8");
29+
}
30+
}
31+
}
32+
33+
// Run the script on the esm directory
34+
addJsExtensions(esmDir);

package.json

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
{
22
"name": "weather-formulas",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "A collection of atmospheric, meteorological weather calculations",
55
"type": "module",
6-
"main": "dist/index.js",
6+
"main": "dist/cjs/index.cjs",
7+
"module": "dist/esm/index.js",
78
"types": "dist/types/index.d.ts",
9+
"exports": {
10+
".": {
11+
"require": "./dist/cjs/index.cjs",
12+
"import": "./dist/esm/index.js"
13+
}
14+
},
815
"files": [
916
"dist",
1017
"README.md",
1118
"LICENSE"
1219
],
1320
"scripts": {
14-
"build": "tsc",
15-
"watch": "tsc --watch",
16-
"test": "jest"
17-
},
21+
"build": "tsc && tsc --project tsconfig.cjs.json && node rename-cjs-files.cjs && node add-js-extensions.cjs",
22+
"watch": "tsc --watch",
23+
"test": "jest"
24+
},
1825
"repository": {
1926
"type": "git",
2027
"url": "git+https://github.com/oyve/weather-formulas.git"
@@ -23,23 +30,24 @@
2330
"weather",
2431
"atmospheric",
2532
"calculations",
26-
"calculation",
2733
"temperature",
2834
"dew point",
2935
"wind chill",
30-
"australian apparent temperature",
3136
"heat index",
3237
"humidex",
3338
"relative humidity",
3439
"specific humidity",
35-
"mixing ratio",
3640
"vapor pressure",
3741
"potential temperature",
3842
"virtual temperature",
3943
"pressure altitude",
4044
"density altitude",
41-
"saturation vapor pressure",
42-
"adjust pressure to sea level"
45+
"barometric formula",
46+
"adjust pressure to sea level",
47+
"lapse rate",
48+
"dynamic lapse rate",
49+
"temperature inversion",
50+
"weighted average temperature"
4351
],
4452
"author": "Øyvind Hansen <oyve-github@outlook.com>",
4553
"license": "GPL-3.0-or-later",

rename-cjs-files.cjs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
4+
const cjsDir = path.join(__dirname, "dist/cjs");
5+
6+
function renameJsToCjs(dir) {
7+
const files = fs.readdirSync(dir);
8+
for (const file of files) {
9+
const fullPath = path.join(dir, file);
10+
11+
// If it's a directory, recursively process it
12+
if (fs.statSync(fullPath).isDirectory()) {
13+
renameJsToCjs(fullPath);
14+
}
15+
// If it's a .js file, rename it to .cjs
16+
else if (file.endsWith(".js")) {
17+
const newFullPath = fullPath.replace(/\.js$/, ".cjs");
18+
fs.renameSync(fullPath, newFullPath);
19+
20+
// Update require statements in the renamed file
21+
let content = fs.readFileSync(newFullPath, "utf8");
22+
content = content.replace(/require\(['"](\..*?)['"]\)/g, (match, p1) => {
23+
if (!p1.endsWith(".cjs")) {
24+
return `require('${p1}.cjs')`;
25+
}
26+
return match;
27+
});
28+
fs.writeFileSync(newFullPath, content, "utf8");
29+
}
30+
}
31+
}
32+
33+
// Run the script on the cjs directory
34+
renameJsToCjs(cjsDir);

src/formulas/humidity.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import c from '../../src/constants';
1+
import c from '../constants';
22

33
/**
44
* Calculate Relative Humidity

tsconfig.cjs.json

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

tsconfig.json

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
1212

1313
/* Language and Environment */
14-
"target": "ESNext", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
14+
"target": "ES2020", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
1515
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
1616
// "jsx": "preserve", /* Specify what JSX code is generated. */
1717
// "libReplacement": true, /* Enable lib replacement. */
@@ -26,9 +26,9 @@
2626
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
2727

2828
/* Modules */
29-
"module": "CommonJS", /* Specify what module code is generated. */
30-
"rootDir": "src", /* Specify the root folder within your source files. */
31-
"moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
29+
"module": "ESNext", /* Specify what module code is generated. */
30+
"rootDir": "./src", /* Ensure the directory structure is preserved. */
31+
"moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
3232
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
3333
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
3434
//"rootDirs": ["./src"], /* Allow multiple folders to be treated as one when resolving modules. */
@@ -54,12 +54,12 @@
5454
/* Emit */
5555
"declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
5656
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
57-
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
57+
"emitDeclarationOnly": false, /* Only output d.ts files and not JavaScript files. */
5858
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
5959
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
6060
// "noEmit": true, /* Disable emitting files from a compilation. */
6161
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
62-
"outDir": "dist", /* Specify an output folder for all emitted files. */
62+
"outDir": "./dist/esm", /* Specify an output folder for all emitted files. */
6363
// "removeComments": true, /* Disable emitting comments. */
6464
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
6565
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
@@ -72,7 +72,7 @@
7272
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
7373
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
7474
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
75-
"declarationDir": "dist/types", /* Specify the output directory for generated declaration files. */
75+
"declarationDir": "./dist/types", /* Specify the output directory for generated declaration files. */
7676

7777
/* Interop Constraints */
7878
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
@@ -108,10 +108,8 @@
108108

109109
/* Completeness */
110110
//"skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
111-
"skipLibCheck": true, /* Skip type checking all .d.ts files. */
111+
"skipLibCheck": true /* Skip type checking all .d.ts files. */
112112
},
113-
"exclude": [
114-
"**/*.test.ts",
115-
"./dist/**/*"
116-
]
113+
"include": ["src/**/*"], // Include all files in the src directory
114+
"exclude": ["node_modules", "dist"] // Exclude unnecessary files
117115
}

0 commit comments

Comments
 (0)