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

Babel 7 support #104

Merged
merged 7 commits into from
Sep 6, 2018
Merged
Show file tree
Hide file tree
Changes from 6 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
21 changes: 10 additions & 11 deletions build-scripts/rollup.browser.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import deepmerge from 'deepmerge';
import json from 'rollup-plugin-json';
import alias from 'rollup-plugin-alias';
import babel from 'rollup-plugin-babel';
import { terser } from 'rollup-plugin-terser';
Copy link
Contributor Author

@jasonmit jasonmit Sep 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import commonjs from 'rollup-plugin-commonjs';
import { rollup as lerna } from 'lerna-alias';
import resolve from 'rollup-plugin-node-resolve';
import globals from 'rollup-plugin-node-globals';
import builtins from 'rollup-plugin-node-builtins';
import commonjs from 'rollup-plugin-commonjs';
import uglify from 'rollup-plugin-uglify';
import json from 'rollup-plugin-json';
import { input, output, pkg, production } from './rollup.utils';

export default function createBrowserConfig(options = {}, targets) {
Expand All @@ -23,9 +23,10 @@ export default function createBrowserConfig(options = {}, targets) {
babel({
babelrc: false,
runtimeHelpers: true,
exclude: '../../../node_modules/**',
presets: [
[
'env',
'@babel/preset-env',
{
modules: false,
targets: targets || {
Expand All @@ -35,16 +36,14 @@ export default function createBrowserConfig(options = {}, targets) {
]
],
plugins: [
'external-helpers',
'transform-runtime',
['transform-object-rest-spread', { useBuiltIns: true }]
],
exclude: ['node_modules/**'],
ignore: 'node_modules/**'
'@babel/plugin-external-helpers',
'@babel/plugin-transform-runtime',
['@babel/plugin-proposal-object-rest-spread', { useBuiltIns: true }]
]
}),
globals(),
builtins(),
production && uglify()
production && terser()
],
onwarn(message) {
/* nise uses eval for strings within native fns setTimeout('alert("foo")', 10) */
Expand Down
21 changes: 10 additions & 11 deletions build-scripts/rollup.node.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import deepmerge from 'deepmerge';
import json from 'rollup-plugin-json';
import babel from 'rollup-plugin-babel';
import resolve from 'rollup-plugin-node-resolve';
import { terser } from 'rollup-plugin-terser';
import commonjs from 'rollup-plugin-commonjs';
import uglify from 'rollup-plugin-uglify';
import json from 'rollup-plugin-json';
import resolve from 'rollup-plugin-node-resolve';
import { input, output, pkg, production } from './rollup.utils';

const external = Object.keys(pkg.dependencies || {});
Expand All @@ -21,9 +21,10 @@ export default function createNodeConfig(options = {}) {
babel({
babelrc: false,
runtimeHelpers: true,
exclude: '../../../node_modules/**',
presets: [
[
'env',
'@babel/preset-env',
{
modules: false,
targets: {
Expand All @@ -33,14 +34,12 @@ export default function createNodeConfig(options = {}) {
]
],
plugins: [
'external-helpers',
'transform-runtime',
['transform-object-rest-spread', { useBuiltIns: true }]
],
exclude: ['node_modules/**'],
ignore: 'node_modules/**'
'@babel/plugin-external-helpers',
'@babel/plugin-transform-runtime',
['@babel/plugin-proposal-object-rest-spread', { useBuiltIns: true }]
]
}),
production && uglify()
production && terser()
]
},
options
Expand Down
35 changes: 18 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"email": "[email protected]"
}
],
"workspaces": ["packages/@pollyjs/*"],
"workspaces": [
"packages/@pollyjs/*"
],
"scripts": {
"commitmsg": "commitlint -E GIT_PARAMS",
"build": "lerna run build --ignore=@pollyjs/ember --parallel",
Expand All @@ -30,24 +32,23 @@
"test:node": "mocha --opts tests/mocha.opts",
"test:jest": "jest",
"test:ember": "lerna run test --stream --no-prefix --scope=@pollyjs/ember",
"server:build":
"yarn build --scope=@pollyjs/node-server --scope=@pollyjs/utils",
"server:build": "yarn build --scope=@pollyjs/node-server --scope=@pollyjs/utils",
"docs:serve": "docsify serve ./docs",
"docs:publish":
"gh-pages --dist docs --dotfiles --message 'chore: Publish docs'",
"docs:publish": "gh-pages --dist docs --dotfiles --message 'chore: Publish docs'",
"release": "lerna publish",
"postrelease": "yarn docs:publish"
},
"devDependencies": {
"@babel/core": "^7.0.0",
"@babel/plugin-external-helpers": "^7.0.0",
"@babel/plugin-proposal-object-rest-spread": "^7.0.0",
"@babel/plugin-transform-runtime": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/runtime": "^7.0.0",
"@commitlint/cli": "^7.0.0",
"@commitlint/config-conventional": "^7.0.1",
"@commitlint/config-lerna-scopes": "^7.0.0",
"@commitlint/travis-cli": "^7.0.0",
"babel-core": "^6.26.3",
"babel-plugin-external-helpers": "^6.22.0",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.7.0",
"chai": "^4.1.2",
"deepmerge": "^2.1.1",
"docsify-cli": "^4.2.1",
Expand All @@ -67,17 +68,17 @@
"npm-run-all": "^4.1.3",
"prettier": "^1.14.2",
"rimraf": "^2.6.2",
"rollup": "^0.64.1",
"rollup": "^0.65.2",
"rollup-plugin-alias": "^1.4.0",
"rollup-plugin-babel": "^3.0.7",
"rollup-plugin-commonjs": "^9.1.5",
"rollup-plugin-babel": "^4.0.3",
"rollup-plugin-commonjs": "^9.1.6",
"rollup-plugin-json": "^3.0.0",
"rollup-plugin-multi-entry": "^2.0.2",
"rollup-plugin-node-builtins": "^2.1.2",
"rollup-plugin-node-globals": "^1.2.1",
"rollup-plugin-node-resolve": "^3.3.0",
"rollup-plugin-uglify": "^3.0.0",
"rollup-plugin-node-globals": "^1.3.0",
"rollup-plugin-node-resolve": "^3.4.0",
"rollup-plugin-terser": "^2.0.2",
"tap-merge": "^0.3.1",
"testem": "^2.9.2"
"testem": "^2.10.0"
}
}
2 changes: 1 addition & 1 deletion packages/@pollyjs/adapter-fetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@
"npm-run-all": "^4.1.3",
"prettier": "^1.14.2",
"rimraf": "^2.6.2",
"rollup": "^0.64.1"
"rollup": "^0.65.2"
}
}
2 changes: 1 addition & 1 deletion packages/@pollyjs/adapter-puppeteer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@
"prettier": "^1.14.2",
"puppeteer": "1.7.0",
"rimraf": "^2.6.2",
"rollup": "^0.64.1"
"rollup": "^0.65.2"
}
}
2 changes: 1 addition & 1 deletion packages/@pollyjs/adapter-xhr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@
"npm-run-all": "^4.1.3",
"prettier": "^1.14.2",
"rimraf": "^2.6.2",
"rollup": "^0.64.1"
"rollup": "^0.65.2"
}
}
2 changes: 1 addition & 1 deletion packages/@pollyjs/adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@
"npm-run-all": "^4.1.3",
"prettier": "^1.14.2",
"rimraf": "^2.6.2",
"rollup": "^0.64.1"
"rollup": "^0.65.2"
}
}
2 changes: 1 addition & 1 deletion packages/@pollyjs/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"npm-run-all": "^4.1.3",
"prettier": "^1.14.2",
"rimraf": "^2.6.2",
"rollup": "^0.64.1"
"rollup": "^0.65.2"
},
"bin": {
"polly": "./bin/cli.js"
Expand Down
2 changes: 1 addition & 1 deletion packages/@pollyjs/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@
"npm-run-all": "^4.1.3",
"prettier": "^1.14.2",
"rimraf": "^2.6.2",
"rollup": "^0.64.1"
"rollup": "^0.65.2"
}
}
5 changes: 3 additions & 2 deletions packages/@pollyjs/core/src/-private/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import PollyResponse from './response';
import NormalizeRequest from '../utils/normalize-request';
import parseUrl from '../utils/parse-url';
import serializeRequestBody from '../utils/serialize-request-body';
import DeferredPromise from '../utils/deferred-promise';
import defer from '../utils/deferred-promise';
import isAbsoluteUrl from 'is-absolute-url';
import { URL, assert, timestamp } from '@pollyjs/utils';
import HTTPBase from './http-base';
Expand All @@ -22,14 +22,15 @@ export default class PollyRequest extends HTTPBase {
assert('Url is required.', typeof request.url === 'string');
assert('Method is required.', typeof request.method === 'string');

this.didRespond = false;
this.url = request.url;
this.method = request.method.toUpperCase();
this.body = request.body;
this.setHeaders(request.headers);
this.recordingName = polly.recordingName;
this.recordingId = polly.recordingId;
this.requestArguments = freeze(request.requestArguments || []);
this.promise = new DeferredPromise();
this.promise = defer();
this[POLLY] = polly;

/*
Expand Down
21 changes: 11 additions & 10 deletions packages/@pollyjs/core/src/utils/deferred-promise.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
/**
* Create a deferred promise with `resolve` and `reject` methods.
*/
export default class DeferredPromise extends Promise {
constructor() {
let resolve, reject;
export default function defer() {
let _resolve;
let _reject;

super((_resolve, _reject) => {
resolve = _resolve;
reject = _reject;
});
const promise = new Promise((resolve, reject) => {
_resolve = resolve;
_reject = reject;
});

this.resolve = resolve;
this.reject = reject;
}
promise.resolve = _resolve;
promise.reject = _reject;

return promise;
}
5 changes: 3 additions & 2 deletions packages/@pollyjs/core/src/utils/serialize-request-body.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ async function serialize(body) {

for (const [key, value] of body.entries()) {
if (supportsBlob && value instanceof Blob) {
data.push(`${key}=${await readBlob(value)}`);
const blobContent = await readBlob(value);

data.push(`${key}=${blobContent}`);
} else {
data.push(`${key}=${value}`);
}
Expand All @@ -36,7 +38,6 @@ function readBlob(blob) {
reader.onend = reject;
reader.onabort = reject;
reader.onload = () => resolve(reader.result);

reader.readAsDataURL(new Blob([blob], { type: blob.type }));
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,31 @@ describe('Unit | Utils | serializeRequestBody', function() {
});

it('should handle files', async function() {
expect(await serializeRequestBody(new File(['file'], 'file.txt'))).to.equal(
`data:;base64,${btoa('file')}`
);
expect(
await serializeRequestBody(
new File(['file'], 'file.txt', {
Copy link
Contributor Author

@jasonmit jasonmit Sep 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readAsDataURL function is supported, but the mediatype in Chrome depends on entry name extension, mediatype in IE is always empty (which is the same as text-plain according the specification), the mediatype in Firefox is always application/octet-stream. For example, if the content is abcdefg then Firefox returns data:application/octet-stream;base64,YWJjZGVmZw==, IE returns data:;base64,YWJjZGVmZw==, Chrome returns data:;base64,YWJjZGVmZw==.

Seems how the type is negotiated when not supplied is a browser quirk. In Chrome 68 if the type is '' it would not default to application/octet-stream - now it does... like Firefox.

type: 'text/plain'
})
)
).to.equal(`data:text/plain;base64,${btoa('file')}`);
});

it('should handle form-data', async function() {
const formData = new FormData();

formData.append('string', 'string');
formData.append('array', [1, 2]);
formData.append('blob', new Blob(['blob']));
formData.append('file', new File(['file'], 'file.txt'));
formData.append('blob', new Blob(['blob'], { type: 'text/plain' }));
formData.append(
'file',
new File(['file'], 'file.txt', { type: 'text/plain' })
);

const data = await serializeRequestBody(formData);

expect(data).to.include('string=string');
expect(data).to.include('array=1,2');
expect(data).to.include(`blob=data:;base64,${btoa('blob')}`);
expect(data).to.include(`file=data:;base64,${btoa('file')}`);
expect(data).to.include(`blob=data:text/plain;base64,${btoa('blob')}`);
expect(data).to.include(`file=data:text/plain;base64,${btoa('file')}`);
});
});
12 changes: 6 additions & 6 deletions packages/@pollyjs/core/tests/unit/utils/deferred-promise-test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import DeferredPromise from '../../../src/utils/deferred-promise';
import defer from '../../../src/utils/deferred-promise';

describe('Unit | Utils | DeferredPromise', function() {
it('should exist', function() {
expect(DeferredPromise).to.be.a('function');
expect(new DeferredPromise().resolve).to.be.a('function');
expect(new DeferredPromise().reject).to.be.a('function');
expect(defer).to.be.a('function');
expect(defer().resolve).to.be.a('function');
expect(defer().reject).to.be.a('function');
});

it('should resolve when calling .resolve()', async function() {
const promise = new DeferredPromise();
const promise = defer();

promise.resolve(42);
expect(await promise).to.equal(42);
});

it('should reject when calling .reject()', async function() {
const promise = new DeferredPromise();
const promise = defer();

promise.reject(new Error('42'));

Expand Down
2 changes: 1 addition & 1 deletion packages/@pollyjs/node-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@
"npm-run-all": "^4.1.3",
"prettier": "^1.14.2",
"rimraf": "^2.6.2",
"rollup": "^0.64.1"
"rollup": "^0.65.2"
}
}
2 changes: 1 addition & 1 deletion packages/@pollyjs/persister-fs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@
"npm-run-all": "^4.1.3",
"prettier": "^1.14.2",
"rimraf": "^2.6.2",
"rollup": "^0.64.1"
"rollup": "^0.65.2"
}
}
2 changes: 1 addition & 1 deletion packages/@pollyjs/persister-local-storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@
"npm-run-all": "^4.1.3",
"prettier": "^1.14.2",
"rimraf": "^2.6.2",
"rollup": "^0.64.1"
"rollup": "^0.65.2"
}
}
2 changes: 1 addition & 1 deletion packages/@pollyjs/persister-rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@
"npm-run-all": "^4.1.3",
"prettier": "^1.14.2",
"rimraf": "^2.6.2",
"rollup": "^0.64.1"
"rollup": "^0.65.2"
}
}
2 changes: 1 addition & 1 deletion packages/@pollyjs/persister/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@
"npm-run-all": "^4.1.3",
"prettier": "^1.14.2",
"rimraf": "^2.6.2",
"rollup": "^0.64.1"
"rollup": "^0.65.2"
}
}
Loading