From 675ed9bbf217fa443ddf9b9948657bf31cbc32a0 Mon Sep 17 00:00:00 2001 From: Randall Schmidt Date: Fri, 9 Jul 2021 14:36:43 -0400 Subject: [PATCH] compile to commonjs --- .gitignore | 2 ++ commonjs/wrapper.cjs | 4 ++++ package-lock.json | 18 ++++++++++++++++++ package.json | 13 ++++++++++--- src/classes/headers.js | 31 ------------------------------- test/tests.cjs | 18 ++++++++++++++++++ test/tests.js | 1 - 7 files changed, 52 insertions(+), 35 deletions(-) create mode 100644 commonjs/wrapper.cjs delete mode 100644 src/classes/headers.js create mode 100644 test/tests.cjs diff --git a/.gitignore b/.gitignore index 057a81c..f812b11 100644 --- a/.gitignore +++ b/.gitignore @@ -105,3 +105,5 @@ dist # Other .cache + +commonjs diff --git a/commonjs/wrapper.cjs b/commonjs/wrapper.cjs new file mode 100644 index 0000000..b0e7dc4 --- /dev/null +++ b/commonjs/wrapper.cjs @@ -0,0 +1,4 @@ +const mod = require('./index.cjs'); + +module.exports = mod.default; +Object.assign(module.exports, mod); diff --git a/package-lock.json b/package-lock.json index 685ff4f..5e3f094 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3234,6 +3234,24 @@ "glob": "^7.1.3" } }, + "rollup": { + "version": "2.53.0", + "resolved": "https://registry.npmjs.org/rollup/-/rollup-2.53.0.tgz", + "integrity": "sha512-spgrY78Toh+m0+zaOoeaayJKuzFuWy6o1PdFIBMVwRcuxT0xCOX9A5rChyKe+2ruL4lePKWUMImS4mMW1QAkmQ==", + "dev": true, + "requires": { + "fsevents": "~2.3.2" + }, + "dependencies": { + "fsevents": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", + "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", + "dev": true, + "optional": true + } + } + }, "run-async": { "version": "2.4.0", "resolved": "https://registry.npmjs.org/run-async/-/run-async-2.4.0.tgz", diff --git a/package.json b/package.json index 7fad0fb..124cdbb 100644 --- a/package.json +++ b/package.json @@ -4,11 +4,17 @@ "description": "node-fetch with a persistent cache.", "main": "src/index.js", "type": "module", + "exports": { + "import": "./src/index.js", + "require": "./commonjs/wrapper.cjs" + }, "scripts": { - "test": "mocha --timeout 10000 --exit", + "buildcjs": "rollup src/index.js --file commonjs/index.cjs --format cjs", + "test": "npm run buildcjs && mocha --timeout 10000 --exit", "coverage": "nyc --reporter=lcov --reporter=text npm test", "lint": "./node_modules/.bin/eslint .", - "lintfix": "./node_modules/.bin/eslint . --fix" + "lintfix": "./node_modules/.bin/eslint . --fix", + "prepublishOnly": "npm test" }, "repository": { "type": "git", @@ -35,7 +41,8 @@ "husky": "^4.3.0", "mocha": "^8.2.1", "nyc": "^15.1.0", - "rimraf": "^3.0.2" + "rimraf": "^3.0.2", + "rollup": "^2.53.0" }, "dependencies": { "cacache": "^15.2.0", diff --git a/src/classes/headers.js b/src/classes/headers.js deleted file mode 100644 index fbbc6b0..0000000 --- a/src/classes/headers.js +++ /dev/null @@ -1,31 +0,0 @@ -export class Headers { - constructor(rawHeaders) { - this.rawHeaders = rawHeaders; - } - - entries() { - return Object.entries(this.rawHeaders) - .sort((e1, e2) => e1[0].localeCompare(e2[0])) - .map(([key, val]) => [key, val[0]]); - } - - keys() { - return this.entries().map((e) => e[0]); - } - - values() { - return this.entries().map((e) => e[1]); - } - - get(name) { - return (this.rawHeaders[name.toLowerCase()] || [])[0] || null; - } - - has(name) { - return !!this.get(name); - } - - raw() { - return this.rawHeaders; - } -} diff --git a/test/tests.cjs b/test/tests.cjs new file mode 100644 index 0000000..767b7a8 --- /dev/null +++ b/test/tests.cjs @@ -0,0 +1,18 @@ +const assert = require('assert'); +const fetch = require('../commonjs/wrapper.cjs'); + +const TWO_HUNDRED_URL = 'https://httpbin.org/status/200'; + +describe('Commonjs module tests', function() { + it('Can make a request', async function() { + const res = await fetch(TWO_HUNDRED_URL); + assert.strictEqual(res.status, 200); + }); + + it('Has expected properties', function() { + assert(typeof fetch === 'function'); + assert(fetch.MemoryCache); + assert(fetch.FileSystemCache); + assert(fetch.fetchBuilder); + }); +}); diff --git a/test/tests.js b/test/tests.js index 747d7b1..ac877d3 100644 --- a/test/tests.js +++ b/test/tests.js @@ -476,4 +476,3 @@ describe('File system cache tests', function() { assert.strictEqual(res.fromCache, true); }); }); -console.log(process.cwd()) \ No newline at end of file