From 3f10167696b317c5d137638bf66b0011a3f66dd1 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Tue, 6 Apr 2021 14:32:44 +0100 Subject: [PATCH] fix: make esm mjs Tools such as `create-react-app` require special configuration to handle `.cjs` as an extension. Setting `type: "module"` in `package.json` means we have to use the `.cjs` extension for commons js which then breaks these sorts of tools. This change removes `type: "module"` and uses the `.js` extension for cjs files and `.mjs` for esm for maximum compatibiliy. --- package.json | 17 ++++++++--------- src/lib.cjs | 7 ------- src/lib.js | 12 ++++++------ src/lib.mjs | 7 +++++++ test/test-lib.react-native.spec.js | 4 ++-- test/test-lib.spec.js | 4 ++-- test/{test-lib.spec.cjs => test-lib.spec.mjs} | 4 ++-- 7 files changed, 27 insertions(+), 28 deletions(-) delete mode 100644 src/lib.cjs create mode 100644 src/lib.mjs rename test/{test-lib.spec.cjs => test-lib.spec.mjs} (83%) diff --git a/package.json b/package.json index cc9d458..4b7ef3b 100644 --- a/package.json +++ b/package.json @@ -6,23 +6,22 @@ "TextEncoder", "TextDecoder" ], - "type": "module", "react-native": "./src/lib.react-native.js", - "main": "./src/lib.cjs", - "module": "./src/lib.js", - "browser": "./src/lib.cjs", + "main": "./src/lib.js", + "module": "./src/lib.mjs", + "browser": "./src/lib.js", "types": "./src/lib.d.ts", "exports": { ".": { - "import": "./src/lib.js", - "require": "./src/lib.cjs" + "import": "./src/lib.mjs", + "require": "./src/lib.js" } }, "scripts": { - "test:node": "mocha test/test-*.spec.cjs", - "test:browser": "playwright-test test/test-*.cjs", + "test:node": "mocha test/test-*.spec.js", + "test:browser": "playwright-test test/test-*.js", "test:react-native": "jest test/test-lib.react-native.spec.js", - "test:es": "mocha test/test-lib.spec.js", + "test:es": "mocha test/test-lib.spec.mjs", "test:cjs": "npm run test:node && npm run test:browser", "test:types:ts": "npm test --prefix test/ts-use", "test:types:esm": "npm test --prefix test/esm-use", diff --git a/src/lib.cjs b/src/lib.cjs deleted file mode 100644 index e2553da..0000000 --- a/src/lib.cjs +++ /dev/null @@ -1,7 +0,0 @@ -"use strict" - -exports.TextEncoder = - typeof TextEncoder !== "undefined" ? TextEncoder : require("util").TextEncoder - -exports.TextDecoder = - typeof TextDecoder !== "undefined" ? TextDecoder : require("util").TextDecoder diff --git a/src/lib.js b/src/lib.js index 16bd041..e2553da 100644 --- a/src/lib.js +++ b/src/lib.js @@ -1,7 +1,7 @@ -// In node `export { TextEncoder }` throws: -// "Export 'TextEncoder' is not defined in module" -// To workaround we first define constants and then export with as. -const Encoder = TextEncoder -const Decoder = TextDecoder +"use strict" -export { Encoder as TextEncoder, Decoder as TextDecoder } +exports.TextEncoder = + typeof TextEncoder !== "undefined" ? TextEncoder : require("util").TextEncoder + +exports.TextDecoder = + typeof TextDecoder !== "undefined" ? TextDecoder : require("util").TextDecoder diff --git a/src/lib.mjs b/src/lib.mjs new file mode 100644 index 0000000..16bd041 --- /dev/null +++ b/src/lib.mjs @@ -0,0 +1,7 @@ +// In node `export { TextEncoder }` throws: +// "Export 'TextEncoder' is not defined in module" +// To workaround we first define constants and then export with as. +const Encoder = TextEncoder +const Decoder = TextDecoder + +export { Encoder as TextEncoder, Decoder as TextDecoder } diff --git a/test/test-lib.react-native.spec.js b/test/test-lib.react-native.spec.js index 34d482b..179b6e0 100644 --- a/test/test-lib.react-native.spec.js +++ b/test/test-lib.react-native.spec.js @@ -1,5 +1,5 @@ -import { TextEncoder, TextDecoder } from "../src/lib.react-native.js" -import assert from "assert" +const { TextEncoder, TextDecoder } = require("../src/lib.react-native.js") +const assert = require("assert") describe("text encode/decode", () => { const data = Uint8Array.from([ diff --git a/test/test-lib.spec.js b/test/test-lib.spec.js index 066b719..0565386 100644 --- a/test/test-lib.spec.js +++ b/test/test-lib.spec.js @@ -1,5 +1,5 @@ -import { TextEncoder, TextDecoder } from "../src/lib.js" -import assert from "assert" +const { TextEncoder, TextDecoder } = require("../src/lib") +const assert = require("assert") describe("text encode/decode", () => { const data = Uint8Array.from([ diff --git a/test/test-lib.spec.cjs b/test/test-lib.spec.mjs similarity index 83% rename from test/test-lib.spec.cjs rename to test/test-lib.spec.mjs index 6fc48d4..066b719 100644 --- a/test/test-lib.spec.cjs +++ b/test/test-lib.spec.mjs @@ -1,5 +1,5 @@ -const { TextEncoder, TextDecoder } = require("..") -const assert = require("assert") +import { TextEncoder, TextDecoder } from "../src/lib.js" +import assert from "assert" describe("text encode/decode", () => { const data = Uint8Array.from([