Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure Pretender is importable in node #269

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
presets: [
[
'@babel/preset-env',
{
targets: {
node: 'current',
},
},
],
],
};
26 changes: 21 additions & 5 deletions iife-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ var Pretender = (function(self) {
return module.default || module;
}

var isBrowserLikeEnvironment = typeof window !== 'undefined';

var appearsBrowserified =
typeof self !== 'undefined' &&
typeof process !== 'undefined' &&
Expand All @@ -12,14 +14,28 @@ var Pretender = (function(self) {
var RouteRecognizer = appearsBrowserified
? getModuleDefault(require('route-recognizer'))
: self.RouteRecognizer;

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

// fetch related ponyfills
var FakeFetch = appearsBrowserified
? getModuleDefault(require('whatwg-fetch'))
: self.WHATWGFetch;
/*
The whatwg-fetch package is not importable in node because it accesses window.

To keep the iife build of Pretender importable in node, we don't include
whatwg-fetch if we're in a a non-browser-like enviroment. (Note: it's not
enough to check if we're running in node, because Pretender works fine in a
browser-like node enviroment, e.g. Jest.)

The ES build of Pretender uses cross-fetch instead of whatwg-fetch, which
works in both environments but doesn't provide a UMD build.
*/
if (isBrowserLikeEnvironment) {
// fetch related ponyfills
var FakeFetch = appearsBrowserified
? getModuleDefault(require('whatwg-fetch'))
: self.WHATWGFetch;
}

/*==ROLLUP_CONTENT==*/

Expand All @@ -34,4 +50,4 @@ var Pretender = (function(self) {
self.Pretender = Pretender;

return Pretender;
})(self);
})(typeof self !== 'undefined' ? self : {});
26 changes: 26 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Create React App-like environment
let browserLikeEnvironmentInNode = {
displayName: 'browserLikeEnvironmentInNode',
testEnvironment: 'jsdom',
testMatch: ['**/test-esm/**/*-test.[jt]s?(x)'],
moduleNameMapper: {
'pretender': '<rootDir>/dist/pretender.es.js'
}
};

// Gatsby-like environment (SSR of client-side code)
let nonBrowserLikeEnvironmentInNode = {
displayName: 'nonBrowserLikeEnvironmentInNode',
testEnvironment: 'node',
testMatch: ['**/test-esm/**/*-test.[jt]s?(x)'],
moduleNameMapper: {
'pretender': '<rootDir>/dist/pretender.es.js'
}
};

module.exports = {
projects: [
browserLikeEnvironmentInNode,
nonBrowserLikeEnvironmentInNode
]
};
23 changes: 15 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
"build": "rollup --config",
"test": "npm run lint && npm run eslint && npm run tests-only",
"test-ci": "npm run pretest && npm run build && npm run lint && npm run eslint && npm run tests-only-ci",
"tests-only": "karma start --single-run",
"tests-only-ci": "karma start --single-run --browsers PhantomJS",
"tests-only": "karma start --single-run && npm run tests-node",
"tests-only-ci": "karma start --single-run --browsers PhantomJS && npm run tests-node",
"tests-node": "npm run tests-iife && npm run tests-es",
"tests-iife": "qunit test-iife/**",
"tests-es": "jest",
"lint": "jshint test",
"eslint": "eslint src/**/*.ts test",
"test:server": "karma start --no-single-run"
Expand All @@ -26,11 +29,15 @@
"url": "https://github.com/pretenderjs/pretender.git"
},
"devDependencies": {
"@babel/core": "^7.6.0",
"@babel/preset-env": "^7.6.0",
"abortcontroller-polyfill": "^1.1.9",
"babel-jest": "^24.9.0",
"bower": "^1.8.2",
"coveralls": "^2.11.3",
"es6-promise": "^4.0.5",
"eslint": "^5.12.0",
"jest": "^24.9.0",
"jshint": "^2.8.0",
"karma": "^1.7.0",
"karma-chrome-launcher": "^2.2.0",
Expand All @@ -41,19 +48,19 @@
"phantomjs": "^2.1.7",
"qunit": "^2.6.1",
"rollup": "^1.1.2",
"rollup-plugin-commonjs": "^9.2.0",
"rollup-plugin-alias": "^2.0.0",
"rollup-plugin-multi-entry": "^2.1.0",
"rollup-plugin-node-resolve": "^4.0.0",
"rollup-plugin-typescript": "^1.0.0",
"rollup-plugin-typescript": "^1.0.1",
"sinon": "^3.2.1",
"tslib": "^1.9.3",
"typescript": "~3.1.1",
"typescript-eslint-parser": "^21.0.2"
"typescript-eslint-parser": "^21.0.2",
"whatwg-fetch": "^3.0.0"
},
"dependencies": {
"cross-fetch": "^3.0.4",
"fake-xml-http-request": "^2.0.0",
"route-recognizer": "^0.3.3",
"whatwg-fetch": "^3.0.0"
"route-recognizer": "^0.3.3"
},
"files": [
"dist",
Expand Down
57 changes: 36 additions & 21 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const commonjs = require('rollup-plugin-commonjs');
const resolve = require('rollup-plugin-node-resolve');
const typescript = require('rollup-plugin-typescript');
const alias = require('rollup-plugin-alias');
const pkg = require('./package.json');
const fs = require('fs');
const globals = {
Expand All @@ -12,22 +11,38 @@ const globals = {
const rollupTemplate = fs.readFileSync('./iife-wrapper.js').toString();
const [ banner, footer ] = rollupTemplate.split('/*==ROLLUP_CONTENT==*/');

module.exports = {
input: 'src/index.ts',
external: Object.keys(pkg.dependencies),
output: [
{
name: 'Pretender',
file: pkg.main,
format: 'iife',
globals,
banner,
footer
},
{
file: pkg.module,
format: 'es'
}
],
plugins: [commonjs(), resolve(), typescript()]
};
module.exports = [
{
input: 'src/index.ts',
external: ['fake-xml-http-request', 'route-recognizer', 'whatwg-fetch'],
output: [
{
name: 'Pretender',
file: pkg.main,
format: 'iife',
globals,
banner,
footer
},
],
plugins: [
alias({
entries: [{ find: 'cross-fetch', replacement: 'whatwg-fetch' }]
}),
typescript()
]
},
{
input: 'src/index.ts',
external: ['fake-xml-http-request', 'route-recognizer', 'cross-fetch'],
output: [
{
file: pkg.module,
format: 'es',
},
],
plugins: [
typescript()
]
}
];
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import FakeXMLHttpRequest from 'fake-xml-http-request';
import * as FakeFetch from 'whatwg-fetch';
import * as FakeFetch from 'cross-fetch';
import parseURL from './parse-url';
import Registry from './registry';

Expand Down
5 changes: 5 additions & 0 deletions test-esm/import-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Pretender from 'pretender';

test('the esm build is importable in node', () => {
expect(typeof Pretender).toEqual('function');
});
9 changes: 9 additions & 0 deletions test-iife/import-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let QUnit = require('qunit');

let it = QUnit.test;

it('the iife build is importable in node', function(assert) {
let Pretender = require('../dist/pretender');

assert.equal(typeof Pretender, 'function');
});
Loading