Skip to content

Commit

Permalink
Vendor whatwg-fetch using rollup+devDep
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanto committed Mar 3, 2020
1 parent 6497376 commit f659126
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 36 deletions.
23 changes: 9 additions & 14 deletions iife-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,24 @@ var Pretender = (function(self) {
}

var appearsBrowserified =
typeof self !== 'undefined' &&
typeof process !== 'undefined' &&
(Object.prototype.toString.call(process) === '[object Object]' ||
Object.prototype.toString.call(process) === '[object process]');
typeof self !== "undefined" &&
typeof process !== "undefined" &&
(Object.prototype.toString.call(process) === "[object Object]" ||
Object.prototype.toString.call(process) === "[object process]");

var RouteRecognizer = appearsBrowserified
? getModuleDefault(require('route-recognizer'))
? getModuleDefault(require("route-recognizer"))
: self.RouteRecognizer;
var FakeXMLHttpRequest = appearsBrowserified
? getModuleDefault(require('fake-xml-http-request'))
? getModuleDefault(require("fake-xml-http-request"))
: self.FakeXMLHttpRequest;

// fetch related ponyfills
var FakeFetch = appearsBrowserified
? getModuleDefault(require('whatwg-fetch'))
: self.WHATWGFetch;

/*==ROLLUP_CONTENT==*/

if (typeof module === 'object') {
if (typeof module === "object") {
module.exports = Pretender;
} else if (typeof define !== 'undefined') {
define('pretender', [], function() {
} else if (typeof define !== "undefined") {
define("pretender", [], function() {
return Pretender;
});
}
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
"devDependencies": {
"@rollup/plugin-commonjs": "^11.0.2",
"@rollup/plugin-node-resolve": "^7.1.1",
"rollup-plugin-typescript": "^1.0.0",
"abortcontroller-polyfill": "^1.1.9",
"coveralls": "^3.0.9",
"dedent": "^0.7.0",
"es6-promise": "^4.0.5",
"eslint": "^5.12.0",
"jshint": "^2.8.0",
Expand All @@ -45,16 +45,17 @@
"release-it": "^12.4.3",
"release-it-lerna-changelog": "^1.0.3",
"rollup": "^1.1.2",
"rollup-plugin-typescript": "^1.0.0",
"sinon": "^9.0.0",
"tslib": "^1.9.3",
"typescript": "~3.1.1",
"typescript-eslint-parser": "^21.0.2",
"url-parse": "^1.4.7"
"url-parse": "^1.4.7",
"whatwg-fetch": "^3.0.0"
},
"dependencies": {
"fake-xml-http-request": "^2.1.1",
"route-recognizer": "^0.3.3",
"whatwg-fetch": "^3.0.0"
"route-recognizer": "^0.3.3"
},
"files": [
"dist",
Expand All @@ -65,8 +66,7 @@
"pretender": {
"deps": [
"route-recognizer",
"fake-xml-http-request",
"whatwg-fetch"
"fake-xml-http-request"
],
"exports": "Pretender"
}
Expand Down
50 changes: 34 additions & 16 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,52 @@
const commonjs = require('@rollup/plugin-commonjs');
const resolve = require('@rollup/plugin-node-resolve');
const typescript = require('rollup-plugin-typescript');
const pkg = require('./package.json');
const fs = require('fs');
const commonjs = require("@rollup/plugin-commonjs");
const resolve = require("@rollup/plugin-node-resolve");
const typescript = require("rollup-plugin-typescript");
const pkg = require("./package.json");
const fs = require("fs");
const dedent = require("dedent");
const globals = {
'whatwg-fetch': 'FakeFetch',
'fake-xml-http-request': 'FakeXMLHttpRequest',
'route-recognizer': 'RouteRecognizer',
'url-parse': 'urlParse'
"fake-xml-http-request": "FakeXMLHttpRequest",
"route-recognizer": "RouteRecognizer",
"url-parse": "urlParse"
};

const rollupTemplate = fs.readFileSync('./iife-wrapper.js').toString();
const [banner, footer] = rollupTemplate.split('/*==ROLLUP_CONTENT==*/');
const rollupTemplate = fs.readFileSync("./iife-wrapper.js").toString();
const [banner, footer] = rollupTemplate.split("/*==ROLLUP_CONTENT==*/");

let importableInNode = {
transform(code, id) {
let before = dedent`
if (typeof global !== "undefined" && typeof global.self === 'undefined') {
global.self = {};
global.__pretenderNodePolyfill = true;
}`;

let after = dedent`
if (typeof global !== "undefined" && global.__pretenderNodePolyfill) {
delete global.self;
delete global.__pretenderNodePolyfill;
}`;

return id.match(/whatwg-fetch/) ? `${before}\n${code}\n${after}` : code;
}
};

module.exports = {
input: 'src/index.ts',
input: "src/index.ts",
external: Object.keys(pkg.dependencies),
output: [
{
name: 'Pretender',
name: "Pretender",
file: pkg.main,
format: 'iife',
format: "iife",
globals,
banner,
footer
},
{
file: pkg.module,
format: 'es'
format: "es"
}
],
plugins: [commonjs(), resolve(), typescript()]
plugins: [commonjs(), resolve(), importableInNode, typescript()]
};
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1351,6 +1351,11 @@ decompress-response@^3.3.0:
dependencies:
mimic-response "^1.0.0"

dedent@^0.7.0:
version "0.7.0"
resolved "https://registry.yarnpkg.com/dedent/-/dedent-0.7.0.tgz#2495ddbaf6eb874abb0e1be9df22d2e5a544326c"
integrity sha1-JJXduvbrh0q7Dhvp3yLS5aVEMmw=

deep-extend@^0.6.0:
version "0.6.0"
resolved "https://registry.yarnpkg.com/deep-extend/-/deep-extend-0.6.0.tgz#c4fa7c95404a17a9c3e8ca7e1537312b736330ac"
Expand Down

0 comments on commit f659126

Please sign in to comment.