Skip to content

Commit

Permalink
build: export ES module as well as CommonJS
Browse files Browse the repository at this point in the history
  • Loading branch information
alanpoulain committed Dec 18, 2020
1 parent d316764 commit ac23a2f
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ npm install --save universal-geocoder
Usage
-----

You can either use Universal Geocoder as a module or as a direct dependency.
You can either use Universal Geocoder as a module (both CommonJS and ES module syntaxes are provided) or as a direct dependency.

As a module:

Expand Down
4 changes: 2 additions & 2 deletions example/node/bing.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-console */
import UniversalGeocoder from "../../dist/UniversalGeocoder";
import GeoJsonDumper from "../../dist/GeoJsonDumper";
import UniversalGeocoder from "../../dist/cjs/UniversalGeocoder";
import GeoJsonDumper from "../../dist/cjs/GeoJsonDumper";

const bingGeocoder = UniversalGeocoder.createGeocoder({
provider: "bing",
Expand Down
2 changes: 1 addition & 1 deletion example/node/chain.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-console */
import UniversalGeocoder from "../../dist/UniversalGeocoder";
import UniversalGeocoder from "../../dist/cjs/UniversalGeocoder";

const yandexGeocoder = UniversalGeocoder.createGeocoder("yandex");
const openStreetMapGeocoder = UniversalGeocoder.createGeocoder({
Expand Down
2 changes: 1 addition & 1 deletion example/node/geoplugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-console */
import UniversalGeocoder from "../../dist/UniversalGeocoder";
import UniversalGeocoder from "../../dist/cjs/UniversalGeocoder";

const geoPluginGeocoder = UniversalGeocoder.createGeocoder({
provider: "geoplugin",
Expand Down
2 changes: 1 addition & 1 deletion example/node/googlemaps.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-console */
import UniversalGeocoder from "../../dist/UniversalGeocoder";
import UniversalGeocoder from "../../dist/cjs/UniversalGeocoder";

const googleGeocoder = UniversalGeocoder.createGeocoder({
provider: "googlemaps",
Expand Down
2 changes: 1 addition & 1 deletion example/node/mapbox.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-console */
import UniversalGeocoder from "../../dist/UniversalGeocoder";
import UniversalGeocoder from "../../dist/cjs/UniversalGeocoder";

const mapboxGeocoder = UniversalGeocoder.createGeocoder({ provider: "mapbox" });

Expand Down
9 changes: 6 additions & 3 deletions example/node/mapquest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/* eslint-disable no-console */
import UniversalGeocoder from "../../dist/UniversalGeocoder";
import Geocoded from "../../dist/Geocoded";
import { MapQuestGeocodeQuery, MapQuestLocation } from "../../dist/provider";
import UniversalGeocoder from "../../dist/cjs/UniversalGeocoder";
import Geocoded from "../../dist/cjs/Geocoded";
import {
MapQuestGeocodeQuery,
MapQuestLocation,
} from "../../dist/cjs/provider";

let mapQuestGeocoder = UniversalGeocoder.createGeocoder({
provider: "mapquest",
Expand Down
2 changes: 1 addition & 1 deletion example/node/opencage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-console */
import UniversalGeocoder from "../../dist/UniversalGeocoder";
import UniversalGeocoder from "../../dist/cjs/UniversalGeocoder";

const openCageGeocoder = UniversalGeocoder.createGeocoder({
provider: "opencage",
Expand Down
2 changes: 1 addition & 1 deletion example/node/openstreetmap.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-console */
import UniversalGeocoder from "../../dist/UniversalGeocoder";
import UniversalGeocoder from "../../dist/cjs/UniversalGeocoder";

const openStreetMapGeocoder = UniversalGeocoder.createGeocoder({
provider: "openstreetmap",
Expand Down
2 changes: 1 addition & 1 deletion example/node/yandex.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-console */
import UniversalGeocoder from "../../dist/UniversalGeocoder";
import UniversalGeocoder from "../../dist/cjs/UniversalGeocoder";

const yandexGeocoder = UniversalGeocoder.createGeocoder("yandex");

Expand Down
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@
"type": "git",
"url": "git+https://github.com/universal-geocoder/universal-geocoder-js.git"
},
"main": "dist/index.js",
"main": "./dist/cjs/index.js",
"exports": {
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js"
},
"module": "./dist/esm/index.js",
"types": "types/index.d.ts",
"files": [
"/dist",
Expand Down Expand Up @@ -78,13 +83,13 @@
"webpack-merge": "^5.1.0"
},
"scripts": {
"build": "rm -rf types && rm -rf dist && webpack --config webpack.dev.js && webpack --config webpack.prod.js && ttsc",
"build": "rm -rf types && rm -rf dist && webpack --config webpack.dev.js && webpack --config webpack.prod.js && ttsc -p tsconfig-cjs.json && ttsc",
"coverage": "nyc npm test",
"coverage-lcov": "nyc --reporter=lcov npm test",
"lint": "eslint --fix --ext .ts .",
"prepublishOnly": "npm run build",
"serve": "http-server",
"test": "ts-node --compiler ttypescript -r tsconfig-paths/register node_modules/.bin/jasmine",
"test": "ts-node --project tsconfig-cjs.json --compiler ttypescript -r tsconfig-paths/register node_modules/.bin/jasmine",
"test-record": "POLLY_RECORD=1 npm test",
"ts-node": "ts-node --skip-project",
"watch": "webpack --config webpack.dev.js --watch"
Expand Down
7 changes: 4 additions & 3 deletions src/ExternalLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export default class ExternalLoader implements ExternalLoaderInterface {
method,
body: method === "POST" ? JSON.stringify(body) : undefined,
})
.then((response) => {
.then((response: Response) => {
if (!response.ok) {
throw new ResponseError(
`Received HTTP status code ${response.status} when attempting geocoding request.`,
Expand All @@ -114,8 +114,9 @@ export default class ExternalLoader implements ExternalLoaderInterface {
}
return response.json();
})
.then((data) => callback(data))
.catch((error) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.then((data: any) => callback(data))
.catch((error: Error | ResponseError) => {
if (errorCallback && error instanceof ResponseError) {
errorCallback(error);
return;
Expand Down
7 changes: 7 additions & 0 deletions tsconfig-cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "CommonJS",
"outDir": "dist/cjs"
}
}
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
"declarationDir": "types",
"declarationMap": true,
"esModuleInterop": true,
"outDir": "dist",
"outDir": "dist/esm",
"sourceMap": true,
"strict": true,
"target": "ES5",
"module": "ES2020",
"moduleResolution": "node",
"plugins": [
{
"transform": "@zerollup/ts-transform-paths",
Expand Down

0 comments on commit ac23a2f

Please sign in to comment.