Skip to content

Commit

Permalink
Distribute as UMD
Browse files Browse the repository at this point in the history
  • Loading branch information
jayphelps committed Apr 6, 2018
1 parent fcc4e1b commit 18f8d4a
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 16 deletions.
47 changes: 31 additions & 16 deletions fetch.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
(function(self) {
(function (root, factory) {
/* jshint strict:false */
/* globals define, module */
if (typeof define === 'function' && define.amd) {
define([], function() { return factory({}, root) })
} else if (typeof module === 'object' && module.exports) {
factory(module.exports, root)
} else {
root.WHATWGFetch = factory({}, root)
}
})(typeof self !== 'undefined' ? self : this, function(exports, global) {
'use strict';

if (self.fetch) {
return
}

var support = {
searchParams: 'URLSearchParams' in self,
iterable: 'Symbol' in self && 'iterator' in Symbol,
blob: 'FileReader' in self && 'Blob' in self && (function() {
searchParams: 'URLSearchParams' in global,
iterable: 'Symbol' in global && 'iterator' in Symbol,
blob: 'FileReader' in global && 'Blob' in global && (function() {
try {
new Blob()
return true
} catch(e) {
return false
}
})(),
formData: 'FormData' in self,
arrayBuffer: 'ArrayBuffer' in self
formData: 'FormData' in global,
arrayBuffer: 'ArrayBuffer' in global
}

if (support.arrayBuffer) {
Expand Down Expand Up @@ -415,11 +421,11 @@
return new Response(null, {status: status, headers: {location: url}})
}

self.Headers = Headers
self.Request = Request
self.Response = Response
exports.Headers = Headers
exports.Request = Request
exports.Response = Response

self.fetch = function(input, init) {
exports.fetch = function(input, init) {
return new Promise(function(resolve, reject) {
var request = new Request(input, init)
var xhr = new XMLHttpRequest()
Expand Down Expand Up @@ -462,5 +468,14 @@
xhr.send(typeof request._bodyInit === 'undefined' ? null : request._bodyInit)
})
}
self.fetch.polyfill = true
})(typeof self !== 'undefined' ? self : this);
exports.fetch.polyfill = true

if (!global.fetch) {
global.fetch = exports.fetch
global.Headers = Headers
global.Request = Request
global.Response = Response
}

return exports
});
36 changes: 36 additions & 0 deletions script/cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

set -e

port=3900

# Find next available port
while lsof -i :$((++port)) >/dev/null; do true; done

# Spin a test server in the background
node ./script/server $port &>/dev/null &
server_pid=$!
trap "kill $server_pid" INT EXIT

STATUS=0

reporter=dot
[ -z "$CI" ] || reporter=spec

if [ -n "$TRAVIS" ]; then
make phantomjs/bin/phantomjs
export PATH="$PWD/phantomjs/bin:$PATH"
fi

run() {
phantomjs ./node_modules/mocha-phantomjs-core/mocha-phantomjs-core.js \
"$1" $reporter "{\"useColors\":true, \"hooks\":\"$PWD/test/mocha-phantomjs-hooks.js\"}" \
|| STATUS=$?
}

[ -z "$CI" ] || echo "phantomjs $(phantomjs -v)"

run "http://localhost:$port/"
run "http://localhost:$port/test/test-worker.html"

exit $STATUS
2 changes: 2 additions & 0 deletions script/test
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ if [ -n "$SAUCE_BROWSER" ]; then
else
./script/phantomjs
fi

./node_modules/mocha/bin/mocha test/cjs.js
13 changes: 13 additions & 0 deletions test/cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* global describe, it, require */

var isFunction = require('chai').assert.isFunction
var WHATWGFetch = require('../')

describe('Common JS exports', function() {
it('should provide all the CJS exports', function() {
isFunction(WHATWGFetch.fetch)
isFunction(WHATWGFetch.Headers)
isFunction(WHATWGFetch.Request)
isFunction(WHATWGFetch.Response)
})
})

0 comments on commit 18f8d4a

Please sign in to comment.