Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Define entry points through the exports field and use .mjs suffixed ESM imports #110

Merged
merged 2 commits into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@
},
"main": "lib/index.js",
"module": "lib/index.mjs",
"exports": {
"./": {
"require": "./lib/index.js",
"import": "./lib/index.mjs"
},
"./lib/use/ws": {
"require": "./lib/use/ws.js",
"import": "./lib/use/ws.mjs"
}
},
"types": "lib/index.d.ts",
"browser": "umd/graphql-ws.js",
"files": [
Expand All @@ -43,7 +53,7 @@
"lint": "eslint 'src'",
"type-check": "tsc --noEmit",
"test": "jest",
"build:esm": "tsc -b tsconfig.esm.json && find ./lib -name \"*.js\" -exec sh -c 'mv \"$0\" \"${0%.js}.mjs\"' \"{}\" \\;",
"build:esm": "tsc -b tsconfig.esm.json && node scripts/esm-post-process.js",
"build:cjs": "tsc -b tsconfig.cjs.json",
"build:umd": "rollup -c && gzip umd/graphql-ws.min.js -c > umd/graphql-ws.min.js.gz",
"build": "yarn build:esm && yarn build:cjs && yarn build:umd",
Expand Down Expand Up @@ -71,9 +81,11 @@
"eslint": "^7.19.0",
"eslint-config-prettier": "^7.2.0",
"eslint-plugin-prettier": "^3.3.1",
"glob": "^7.1.6",
"graphql": "^15.5.0",
"jest": "^26.6.3",
"prettier": "^2.2.1",
"replacestream": "^4.0.3",
"rollup": "^2.38.5",
"rollup-plugin-terser": "^7.0.2",
"semantic-release": "^17.3.8",
Expand Down
20 changes: 20 additions & 0 deletions scripts/esm-post-process.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"use strict";

const fs = require("fs");
const path = require("path");
const glob = require("glob");
const replaceStream = require("replacestream");

glob("lib/**/*.js", (_, filePaths) => {
for (const filePath of filePaths) {
const parts = filePath.split(".")
parts.pop();
const mjsFileName = parts.join(".") + ".mjs"
fs.createReadStream(path.join(__dirname, "..", filePath))
.pipe(replaceStream(/from '(\.?\.\/[^']*)'/g, "from '$1.mjs'"))
.pipe(fs.createWriteStream(path.join(__dirname, "..", mjsFileName)))
.on("close", () => {
fs.unlinkSync(filePath);
});
}
});
enisdenjo marked this conversation as resolved.
Show resolved Hide resolved
17 changes: 15 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3940,7 +3940,7 @@ __metadata:
languageName: node
linkType: hard

"escape-string-regexp@npm:^1.0.5":
"escape-string-regexp@npm:^1.0.3, escape-string-regexp@npm:^1.0.5":
version: 1.0.5
resolution: "escape-string-regexp@npm:1.0.5"
checksum: f9484b8b4c8827d816e0fd905c25ed4b561376a9c220e1430403ea84619bf680c76a883a48cff8b8e091daf55d6a497e37479f9787b9f15f3c421b6054289744
Expand Down Expand Up @@ -4894,9 +4894,11 @@ __metadata:
eslint: ^7.19.0
eslint-config-prettier: ^7.2.0
eslint-plugin-prettier: ^3.3.1
glob: ^7.1.6
graphql: ^15.5.0
jest: ^26.6.3
prettier: ^2.2.1
replacestream: ^4.0.3
rollup: ^2.38.5
rollup-plugin-terser: ^7.0.2
semantic-release: ^17.3.8
Expand Down Expand Up @@ -7938,7 +7940,7 @@ __metadata:
languageName: node
linkType: hard

"object-assign@npm:^4.1.0":
"object-assign@npm:^4.0.1, object-assign@npm:^4.1.0":
version: 4.1.1
resolution: "object-assign@npm:4.1.1"
checksum: 66cf021898fc1b13ea573ea8635fbd5a76533f50cecbc2fcd5eee1e8029af41bcebe7023788b6d0e06cbe4401ecea075d972f78ec74467cdc571a0f1a4d1a081
Expand Down Expand Up @@ -9040,6 +9042,17 @@ __metadata:
languageName: node
linkType: hard

"replacestream@npm:^4.0.3":
version: 4.0.3
resolution: "replacestream@npm:4.0.3"
dependencies:
escape-string-regexp: ^1.0.3
object-assign: ^4.0.1
readable-stream: ^2.0.2
checksum: 180c09f94ce428ef7cc8f97a8f0e3a68624b58a0e8814f458801ebb3543a65a6789ca5df599c142612d4df27b04056644df8761e0cd4294b9783950c45163e7b
languageName: node
linkType: hard

"request-promise-core@npm:1.1.4":
version: 1.1.4
resolution: "request-promise-core@npm:1.1.4"
Expand Down