Skip to content

Commit

Permalink
Converting packages to type: module by default (#2565)
Browse files Browse the repository at this point in the history
Updating Turf packags to be ESM modules rather than a CommonJS project. The build still generates and packages both ESM and CJS variants of implementation and types. From a client perspective, nothing should change about the way Turf modules are loaded or called.


* Marking all packages as "ESM first", with additional CJS files for compatibility. Flow on changes from that include requiring a js extension for relative includes, and workarounds for a couple of older third party libraries that were no longer importing smoothly. Builds successfully. Updating tests next.

* Adding js extension to more local imports in tests, and removing references to __dirname. Added some re-exporting wrapper files for older packages like rbush and sweepline-intersections. See turf-line-intersects for an example.

* Disabling some type tests in packages we were having export / import issues in post type: module. Maybe someone in future can exercise some typescript jujitsu to re-enable them.

* Reverting dist/ generated JS to .js extension where possible. Was identified that the .cjs extension was causing problems for some consumers. Would have like to use .d.ts rather .d.cts for CJS type defs, but there's a but in tsup at the moment that won't allow that to be customised.

* Reintroducing __dirname references, though need to add implementation as not supplied automatically by node any more. Updating benchmark tests. Adding some missing bench and tape @types libraries.

* Added a few __dirname references that should have been included, and removed a few that had inadvertently been added.

* Explicitly using cjs extension for CommonJS distributed source files. Got disabled types tests working again by adjusting tsc command (needed to include node16 module and moduleResolution switched on command line). Also needed to refer to "index", etc as "index.js", etc from test.ts files to avoid "Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16'" errors.

* Missed a .js -> .cjs in turf/turf last-checks target.

* Removing hacky cjs/esm export/import wrappers in favour of defaultImport library which takes care of finding the correct default to import from CJS packages.

* Revert "Removing hacky cjs/esm export/import wrappers in favour of defaultImport library which takes care of finding the correct default to import from CJS packages."

This reverts commit f8184c3.

* Found a few more spots to add a js extension to satisfy newer node16 module resolution.
  • Loading branch information
smallsaucepan authored Mar 11, 2024
1 parent 876702a commit e0bdd0a
Show file tree
Hide file tree
Showing 425 changed files with 1,912 additions and 1,214 deletions.
41 changes: 21 additions & 20 deletions .monorepolint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,29 @@ export default {
packageEntry({
options: {
entries: {
type: "module",
main: "dist/cjs/index.cjs",
module: "dist/esm/index.js",
types: "dist/esm/index.d.ts",
sideEffects: false,
publishConfig: {
access: "public",
},
// @turf/turf is commonly consumed through CDNs, moving this output file is a breaking change for anyone
// who has a hardcoded reference to this specific file, instead of letting the CDN pick the path.
// Example of a URL that will break: https://unpkg.com/@turf/turf/dist/turf.min.js
// Example of a URL that will keep working: https://unpkg.com/@turf/turf
browser: "turf.min.js",
files: ["dist", "index.d.ts", "turf.min.js"],
files: ["dist", "turf.min.js"],
exports: {
"./package.json": "./package.json",
".": {
import: {
types: "./dist/esm/index.d.mts",
default: "./dist/esm/index.mjs",
types: "./dist/esm/index.d.ts",
default: "./dist/esm/index.js",
},
require: {
types: "./dist/cjs/index.d.ts",
types: "./dist/cjs/index.d.cts",
default: "./dist/cjs/index.cjs",
},
},
Expand All @@ -115,9 +123,10 @@ export default {
packageEntry({
options: {
entries: {
type: "module",
main: "dist/cjs/index.cjs",
module: "dist/esm/index.mjs",
types: "dist/cjs/index.d.ts",
module: "dist/esm/index.js",
types: "dist/esm/index.d.ts",
sideEffects: false,
publishConfig: {
access: "public",
Expand All @@ -126,11 +135,11 @@ export default {
"./package.json": "./package.json",
".": {
import: {
types: "./dist/esm/index.d.mts",
default: "./dist/esm/index.mjs",
types: "./dist/esm/index.d.ts",
default: "./dist/esm/index.js",
},
require: {
types: "./dist/cjs/index.d.ts",
types: "./dist/cjs/index.d.cts",
default: "./dist/cjs/index.cjs",
},
},
Expand All @@ -146,16 +155,7 @@ export default {
files: ["dist"],
},
},
includePackages: TS_PACKAGES,
}),

packageEntry({
options: {
entries: {
files: ["dist", "index.d.ts"],
},
},
includePackages: JS_PACKAGES,
includePackages: [...TS_PACKAGES, ...JS_PACKAGES],
}),

packageEntry({
Expand Down Expand Up @@ -208,7 +208,8 @@ export default {
packageScript({
options: {
scripts: {
"test:types": "tsc --esModuleInterop --noEmit --strict types.ts",
"test:types":
"tsc --esModuleInterop --module node16 --moduleResolution node16 --noEmit --strict types.ts",
},
},
includePackages: TYPES_PACKAGES,
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"scripts": {
"docs": "tsx ./scripts/generate-readmes.ts",
"lint": "npm-run-all lint:*",
"lint:docs": "documentation lint packages/turf-*/index.js packages/turf-*/index.mjs",
"lint:docs": "documentation lint packages/turf-*/index.js",
"lint:escheck-cjs": "es-check es8 packages/*/dist/cjs/index.cjs packages/turf/turf.min.js",
"lint:escheck-esm": "es-check --module es8 packages/*/dist/esm/index.mjs",
"lint:escheck-esm": "es-check --module es8 packages/*/dist/esm/index.js",
"lint:escheck-web": "es-check es5 packages/turf/turf.min.js",
"lint:eslint": "eslint packages",
"lint:mrl": "mrl check",
Expand Down
6 changes: 5 additions & 1 deletion packages/turf-along/bench.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import Benchmark from "benchmark";
import { along } from "./index";
import { along } from "./index.js";
import { Feature, LineString } from "geojson";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

const line: Feature<LineString> = {
type: "Feature",
properties: {},
Expand Down
12 changes: 6 additions & 6 deletions packages/turf-along/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@
"turf",
"distance"
],
"type": "commonjs",
"type": "module",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
"types": "dist/cjs/index.d.ts",
"module": "dist/esm/index.js",
"types": "dist/esm/index.d.ts",
"exports": {
"./package.json": "./package.json",
".": {
"import": {
"types": "./dist/esm/index.d.mts",
"default": "./dist/esm/index.mjs"
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"types": "./dist/cjs/index.d.ts",
"types": "./dist/cjs/index.d.cts",
"default": "./dist/cjs/index.cjs"
}
}
Expand Down
5 changes: 4 additions & 1 deletion packages/turf-along/test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import path from "path";
import { fileURLToPath } from "url";
import test from "tape";
import { loadJsonFileSync } from "load-json-file";
import { Units, featureCollection } from "@turf/helpers";
import { along } from "./index";
import { along } from "./index.js";
import { Feature, LineString } from "geojson";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

const line: Feature<LineString> = loadJsonFileSync(
path.join(__dirname, "test", "fixtures", "dc-line.geojson")
);
Expand Down
2 changes: 1 addition & 1 deletion packages/turf-angle/bench.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Benchmark from "benchmark";
import { angle } from "./index";
import { angle } from "./index.js";

/**
* Benchmark Results
Expand Down
12 changes: 6 additions & 6 deletions packages/turf-angle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@
"turf",
"angle"
],
"type": "commonjs",
"type": "module",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
"types": "dist/cjs/index.d.ts",
"module": "dist/esm/index.js",
"types": "dist/esm/index.d.ts",
"exports": {
"./package.json": "./package.json",
".": {
"import": {
"types": "./dist/esm/index.d.mts",
"default": "./dist/esm/index.mjs"
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"types": "./dist/cjs/index.d.ts",
"types": "./dist/cjs/index.d.cts",
"default": "./dist/cjs/index.cjs"
}
}
Expand Down
5 changes: 4 additions & 1 deletion packages/turf-angle/test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import test from "tape";
import path from "path";
import { fileURLToPath } from "url";
import { glob } from "glob";
import { loadJsonFileSync } from "load-json-file";
import { writeJsonFileSync } from "write-json-file";
Expand All @@ -8,7 +9,9 @@ import { bearing } from "@turf/bearing";
import { truncate } from "@turf/truncate";
import { distance } from "@turf/distance";
import { point, round, lineString, featureCollection } from "@turf/helpers";
import { angle } from "./index";
import { angle } from "./index.js";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

test("turf-angle", (t) => {
glob
Expand Down
5 changes: 4 additions & 1 deletion packages/turf-area/bench.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { loadJsonFileSync } from "load-json-file";
import Benchmark from "benchmark";
import { area } from "./index";
import { area } from "./index.js";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

// Define fixtures
const directory = path.join(__dirname, "test", "in") + path.sep;
Expand Down
12 changes: 6 additions & 6 deletions packages/turf-area/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@
"polygon",
"multipolygon"
],
"type": "commonjs",
"type": "module",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
"types": "dist/cjs/index.d.ts",
"module": "dist/esm/index.js",
"types": "dist/esm/index.d.ts",
"exports": {
"./package.json": "./package.json",
".": {
"import": {
"types": "./dist/esm/index.d.mts",
"default": "./dist/esm/index.mjs"
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"types": "./dist/cjs/index.d.ts",
"types": "./dist/cjs/index.d.cts",
"default": "./dist/cjs/index.cjs"
}
}
Expand Down
5 changes: 4 additions & 1 deletion packages/turf-area/test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import fs from "fs";
import test from "tape";
import path from "path";
import { fileURLToPath } from "url";
import { loadJsonFileSync } from "load-json-file";
import { writeJsonFileSync } from "write-json-file";
import { area } from "./index";
import { area } from "./index.js";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

const directories = {
in: path.join(__dirname, "test", "in") + path.sep,
Expand Down
5 changes: 4 additions & 1 deletion packages/turf-bbox-clip/bench.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { loadJsonFileSync } from "load-json-file";
import Benchmark from "benchmark";
import { bbox } from "@turf/bbox";
import { bboxClip } from "./index";
import { bboxClip } from "./index.js";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

const directory = path.join(__dirname, "test", "in") + path.sep;
const fixtures = fs.readdirSync(directory).map((filename) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/turf-bbox-clip/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
polygon,
} from "@turf/helpers";
import { getGeom } from "@turf/invariant";
import { lineclip, polygonclip } from "./lib/lineclip";
import { lineclip, polygonclip } from "./lib/lineclip.js";

/**
* Takes a {@link Feature} and a bbox and clips the feature to the bbox using
Expand Down
12 changes: 6 additions & 6 deletions packages/turf-bbox-clip/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@
"bbox",
"clip"
],
"type": "commonjs",
"type": "module",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
"types": "dist/cjs/index.d.ts",
"module": "dist/esm/index.js",
"types": "dist/esm/index.d.ts",
"exports": {
"./package.json": "./package.json",
".": {
"import": {
"types": "./dist/esm/index.d.mts",
"default": "./dist/esm/index.mjs"
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"types": "./dist/cjs/index.d.ts",
"types": "./dist/cjs/index.d.cts",
"default": "./dist/cjs/index.cjs"
}
}
Expand Down
5 changes: 4 additions & 1 deletion packages/turf-bbox-clip/test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import fs from "fs";
import test from "tape";
import path from "path";
import { fileURLToPath } from "url";
import { loadJsonFileSync } from "load-json-file";
import { writeJsonFileSync } from "write-json-file";
import { point, feature, featureCollection } from "@turf/helpers";
import { bbox as turfBBox } from "@turf/bbox";
import { bboxClip } from "./index";
import { bboxClip } from "./index.js";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

const directories = {
in: path.join(__dirname, "test", "in") + path.sep,
Expand Down
2 changes: 1 addition & 1 deletion packages/turf-bbox-polygon/bench.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Benchmark from "benchmark";
import { bboxPolygon } from "./index";
import { bboxPolygon } from "./index.js";

/**
* Benchmark Results
Expand Down
12 changes: 6 additions & 6 deletions packages/turf-bbox-polygon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@
"extent",
"bbox"
],
"type": "commonjs",
"type": "module",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
"types": "dist/cjs/index.d.ts",
"module": "dist/esm/index.js",
"types": "dist/esm/index.d.ts",
"exports": {
"./package.json": "./package.json",
".": {
"import": {
"types": "./dist/esm/index.d.mts",
"default": "./dist/esm/index.mjs"
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"types": "./dist/cjs/index.d.ts",
"types": "./dist/cjs/index.d.cts",
"default": "./dist/cjs/index.cjs"
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/turf-bbox-polygon/test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from "tape";
import { bboxPolygon } from "./index";
import { bboxPolygon } from "./index.js";

test("bbox-polygon", (t) => {
const poly = bboxPolygon([0, 0, 10, 10]);
Expand Down
2 changes: 1 addition & 1 deletion packages/turf-bbox/bench.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Benchmark from "benchmark";
import { lineString } from "@turf/helpers";
import { bbox } from "./index";
import { bbox } from "./index.js";

const line = lineString([
[-74, 40],
Expand Down
12 changes: 6 additions & 6 deletions packages/turf-bbox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@
"featurecollection",
"geojson"
],
"type": "commonjs",
"type": "module",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
"types": "dist/cjs/index.d.ts",
"module": "dist/esm/index.js",
"types": "dist/esm/index.d.ts",
"exports": {
"./package.json": "./package.json",
".": {
"import": {
"types": "./dist/esm/index.d.mts",
"default": "./dist/esm/index.mjs"
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"types": "./dist/cjs/index.d.ts",
"types": "./dist/cjs/index.d.cts",
"default": "./dist/cjs/index.cjs"
}
}
Expand Down
Loading

0 comments on commit e0bdd0a

Please sign in to comment.