compile to commonjs

This commit is contained in:
Randall Schmidt 2021-07-09 14:36:43 -04:00
parent 998d18ae84
commit 675ed9bbf2
7 changed files with 52 additions and 35 deletions

2
.gitignore vendored

@ -105,3 +105,5 @@ dist
# Other
.cache
commonjs

4
commonjs/wrapper.cjs Normal file

@ -0,0 +1,4 @@
const mod = require('./index.cjs');
module.exports = mod.default;
Object.assign(module.exports, mod);

18
package-lock.json generated

@ -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",

@ -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",

@ -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;
}
}

18
test/tests.cjs Normal file

@ -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);
});
});

@ -476,4 +476,3 @@ describe('File system cache tests', function() {
assert.strictEqual(res.fromCache, true);
});
});
console.log(process.cwd())