Skip to content

Commit ffac8a7

Browse files
author
James Baxley
authored
setup umd and es:next builds (apollographql#691)
* setup umd and es:next builds * add config for test utils * fix lint rules * split up withApollo
1 parent 256282b commit ffac8a7

17 files changed

+219
-108
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ script:
2020
- npm test
2121
- coveralls < ./coverage/lcov.info || true # ignore coveralls error
2222
- npm run compile
23+
- npm run bundle
2324
- cd examples/create-react-app && npm test && cd ../../
2425
- npm run filesize
2526
- python node_modules/travis-weigh-in/weigh_in.py ./dist/index.min.js.gz || true # ignore size errors

Changelog.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Expect active development and potentially significant breaking changes in the `0
44

55
### vNext
66

7+
### 1.3.0
8+
- Feature: Support tree shaking and smaller (marginally) bundles via rollup [PR #691](https://github.com/apollographql/react-apollo/pull/691)
79
- Fix: Render full markup on the server when using the `cache-and-network` fetchPolicy [PR #688](https://github.com/apollographql/react-apollo/pull/688)
810

911
### 1.2.0

examples/create-react-app/src/App.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { Component } from 'react';
22
import ApolloClient, { createNetworkInterface } from 'apollo-client';
3-
import { ApolloProvider } from "../../../lib";
3+
import { ApolloProvider } from "../../../";
44

55
import Pokemon from "./Pokemon";
66

examples/create-react-app/src/Pokemon.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import gql from 'graphql-tag';
3-
import { graphql } from '../../../lib';
3+
import { graphql } from '../../../';
44

55
// The data prop, which is provided by the wrapper below contains,
66
// a `loading` key while the query is in flight and posts when it is ready

examples/create-react-app/src/Pokemon.test.js

+52-42
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,43 @@
1-
import React from 'react';
2-
import renderer from 'react-test-renderer';
3-
import { MockedProvider } from '../../../lib/test-utils';
4-
import { print } from 'graphql';
5-
import { addTypenameToDocument } from 'apollo-client/queries/queryTransform';
1+
import React from "react";
2+
import renderer from "react-test-renderer";
3+
import { MockedProvider } from "../../../lib/react-apollo.test-utils.umd";
4+
import { print } from "graphql";
5+
import { addTypenameToDocument } from "apollo-client/queries/queryTransform";
66

7-
import PokemonWithData, { POKEMON_QUERY, Pokemon, withPokemon } from './Pokemon';
7+
import PokemonWithData, {
8+
POKEMON_QUERY,
9+
Pokemon,
10+
withPokemon,
11+
} from "./Pokemon";
812

913
const mockedData = {
1014
pokemon: {
11-
__typename: 'Pokemon',
12-
name: 'Charmander',
13-
image: 'https://img.pokemondb.net/artwork/charmander.jpg',
15+
__typename: "Pokemon",
16+
name: "Charmander",
17+
image: "https://img.pokemondb.net/artwork/charmander.jpg",
1418
},
1519
};
1620

1721
const query = addTypenameToDocument(POKEMON_QUERY);
18-
const variables = { name: 'charmander' };
22+
const variables = { name: "charmander" };
1923

20-
describe('default export', () => {
21-
it('renders without crashing', () => {
24+
describe("default export", () => {
25+
it("renders without crashing", () => {
2226
const output = renderer.create(
23-
<MockedProvider mocks={[
24-
{ request: { query, variables }, result: { data: mockedData } }
25-
]}>
27+
<MockedProvider
28+
mocks={[
29+
{ request: { query, variables }, result: { data: mockedData } },
30+
]}
31+
>
2632
<PokemonWithData />
2733
</MockedProvider>
28-
)
34+
);
2935
expect(output.toJSON()).toMatchSnapshot();
3036
});
3137
});
3238

33-
describe('Pokemon enhancer', () => {
34-
it('renders with loading first', (done) => {
39+
describe("Pokemon enhancer", () => {
40+
it("renders with loading first", done => {
3541
class Container extends React.Component {
3642
componentWillMount() {
3743
expect(this.props.data.loading).toBe(true);
@@ -41,18 +47,20 @@ describe('Pokemon enhancer', () => {
4147
render() {
4248
return null;
4349
}
44-
};
50+
}
4551
const ContainerWithData = withPokemon(Container);
4652
const output = renderer.create(
47-
<MockedProvider mocks={[
48-
{ request: { query, variables }, result: { data: mockedData } }]
49-
}>
53+
<MockedProvider
54+
mocks={[
55+
{ request: { query, variables }, result: { data: mockedData } },
56+
]}
57+
>
5058
<ContainerWithData />
5159
</MockedProvider>
5260
);
5361
});
5462

55-
it('renders data without crashing', (done) => {
63+
it("renders data without crashing", done => {
5664
class Container extends React.Component {
5765
componentWillReceiveProps(props) {
5866
expect(props.data.loading).toBe(false);
@@ -62,18 +70,20 @@ describe('Pokemon enhancer', () => {
6270
render() {
6371
return null;
6472
}
65-
};
73+
}
6674
const ContainerWithData = withPokemon(Container);
6775
const output = renderer.create(
68-
<MockedProvider mocks={[
69-
{ request: { query, variables }, result: { data: mockedData } }
70-
]}>
76+
<MockedProvider
77+
mocks={[
78+
{ request: { query, variables }, result: { data: mockedData } },
79+
]}
80+
>
7181
<ContainerWithData />
7282
</MockedProvider>
7383
);
7484
});
7585

76-
it('renders with an error correctly', (done) => {
86+
it("renders with an error correctly", done => {
7787
try {
7888
class Container extends React.Component {
7989
componentWillReceiveProps(props) {
@@ -83,12 +93,12 @@ describe('Pokemon enhancer', () => {
8393
render() {
8494
return null;
8595
}
86-
};
96+
}
8797
const ContainerWithData = withPokemon(Container);
8898
const output = renderer.create(
89-
<MockedProvider mocks={[
90-
{ request: { query, variables }, error: new Error('fail') }
91-
]}>
99+
<MockedProvider
100+
mocks={[{ request: { query, variables }, error: new Error("fail") }]}
101+
>
92102
<ContainerWithData />
93103
</MockedProvider>
94104
);
@@ -98,33 +108,33 @@ describe('Pokemon enhancer', () => {
98108
});
99109
});
100110

101-
describe('Pokemon query', () => {
111+
describe("Pokemon query", () => {
102112
// it('should match expected structure', () => {
103113
// expect(POKEMON_QUERY).toMatchSnapshot();
104114
// });
105115

106-
it('should match expected shape', () => {
116+
it("should match expected shape", () => {
107117
expect(print(POKEMON_QUERY)).toMatchSnapshot();
108118
});
109119
});
110120

111-
describe('Pokemon Component', () => {
112-
it('should render a loading state without data', () => {
113-
const output = renderer.create(<Pokemon data={{ loading: true }} />)
121+
describe("Pokemon Component", () => {
122+
it("should render a loading state without data", () => {
123+
const output = renderer.create(<Pokemon data={{ loading: true }} />);
114124
expect(output.toJSON()).toMatchSnapshot();
115125
});
116126

117-
it('should render an error', () => {
118-
const output = renderer.create(<Pokemon data={{ error: new Error("ERROR") }} />)
127+
it("should render an error", () => {
128+
const output = renderer.create(
129+
<Pokemon data={{ error: new Error("ERROR") }} />
130+
);
119131
expect(output.toJSON()).toMatchSnapshot();
120132
});
121133

122-
it('should render name and image in order', () => {
134+
it("should render name and image in order", () => {
123135
const output = renderer.create(
124136
<Pokemon data={{ loading: false, content: mockedData.pokemon }} />
125137
);
126138
expect(output.toJSON()).toMatchSnapshot();
127139
});
128140
});
129-
130-

package.json

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
{
22
"name": "react-apollo",
3-
"version": "1.2.0",
3+
"version": "1.3.0",
44
"description": "React data container for Apollo Client",
5-
"main": "lib/index.js",
6-
"browser": "lib/browser.js",
5+
"main": "lib/react-apollo.umd.js",
6+
"module": "./lib/index.js",
7+
"jsnext:main": "./lib/index.js",
8+
"browser": "lib/react-apollo.browser.umd.js",
79
"typings": "lib/index.d.ts",
810
"scripts": {
911
"deploy": "npm run compile && npm test && npm publish --tag next && git push --tags",
@@ -13,7 +15,8 @@
1315
"posttest": "npm run lint",
1416
"filesize": "npm run compile:browser && ./scripts/filesize.js --file=./dist/index.min.js --maxGzip=20",
1517
"compile": "tsc",
16-
"compile:browser": "rm -rf ./dist && mkdir ./dist && browserify ./lib/index.js --i react --i apollo-client -o=./dist/index.js && npm run minify:browser && npm run compress:browser",
18+
"bundle": "rollup -c && rollup -c rollup.browser.config.js && rollup -c rollup.test-utils.config.js",
19+
"compile:browser": "rm -rf ./dist && mkdir ./dist && browserify ./lib/react-apollo.browser.umd.js --i graphql-tag --i react --i apollo-client -o=./dist/index.js && npm run minify:browser && npm run compress:browser",
1720
"minify:browser": "uglifyjs --compress --mangle --screw-ie8 -o=./dist/index.min.js -- ./dist/index.js",
1821
"compress:browser": "./scripts/gzip.js --file=./dist/index.min.js",
1922
"watch": "tsc -w",
@@ -119,6 +122,7 @@
119122
"redux-form": "^6.0.5",
120123
"redux-immutable": "^4.0.0",
121124
"redux-loop": "^2.2.2",
125+
"rollup": "^0.41.6",
122126
"source-map-support": "^0.4.0",
123127
"swapi-graphql": "0.0.6",
124128
"travis-weigh-in": "^1.0.2",
@@ -129,7 +133,7 @@
129133
"uglify-js": "^2.6.2"
130134
},
131135
"dependencies": {
132-
"apollo-client": "^1.2.1",
136+
"apollo-client": "^1.2.2",
133137
"graphql-anywhere": "^3.0.0",
134138
"graphql-tag": "^2.0.0",
135139
"hoist-non-react-statics": "^1.2.0",

rollup.browser.config.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export default {
2+
entry: "lib/browser.js",
3+
dest: "lib/react-apollo.browser.umd.js",
4+
format: "umd",
5+
sourceMap: true,
6+
moduleName: "react-apollo",
7+
onwarn,
8+
};
9+
10+
function onwarn(message) {
11+
const suppressed = ["UNRESOLVED_IMPORT", "THIS_IS_UNDEFINED"];
12+
13+
if (!suppressed.find(code => message.code === code)) {
14+
return console.warn(message.message);
15+
}
16+
}

rollup.config.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export default {
2+
entry: "lib/index.js",
3+
dest: "lib/react-apollo.umd.js",
4+
format: "umd",
5+
sourceMap: true,
6+
moduleName: "react-apollo",
7+
onwarn,
8+
};
9+
10+
function onwarn(message) {
11+
const suppressed = ["UNRESOLVED_IMPORT", "THIS_IS_UNDEFINED"];
12+
13+
if (!suppressed.find(code => message.code === code)) {
14+
return console.warn(message.message);
15+
}
16+
}

rollup.test-utils.config.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export default {
2+
entry: "lib/test-utils.js",
3+
dest: "lib/react-apollo.test-utils.umd.js",
4+
format: "umd",
5+
sourceMap: true,
6+
moduleName: "react-apollo",
7+
onwarn,
8+
};
9+
10+
function onwarn(message) {
11+
const suppressed = ["UNRESOLVED_IMPORT", "THIS_IS_UNDEFINED"];
12+
13+
if (!suppressed.find(code => message.code === code)) {
14+
return console.warn(message.message);
15+
}
16+
}

src/ApolloProvider.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Store } from 'redux';
66

77
import ApolloClient, { ApolloStore } from 'apollo-client';
88

9-
import invariant = require('invariant');
9+
const invariant = require('invariant');
1010

1111
export declare interface ProviderProps {
1212
store?: Store<any>;

src/browser.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export { default as ApolloProvider } from './ApolloProvider';
2-
export { default as graphql, withApollo, InjectedGraphQLProps } from './graphql';
2+
export { default as graphql, InjectedGraphQLProps } from './graphql';
3+
export { withApollo } from './withApollo';
34

45
// expose easy way to join queries from redux
56
export { compose } from 'redux';

src/graphql.tsx

+4-53
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ import {
66
} from 'react';
77
import * as PropTypes from 'prop-types';
88

9-
// modules don't export ES6 modules
10-
import pick = require('lodash.pick');
9+
const pick = require('lodash.pick');
1110
import shallowEqual from './shallowEqual';
1211

13-
import invariant = require('invariant');
14-
import assign = require('object-assign');
12+
const invariant = require('invariant');
13+
const assign = require('object-assign');
1514

16-
import hoistNonReactStatics = require('hoist-non-react-statics');
15+
const hoistNonReactStatics = require('hoist-non-react-statics');
1716

1817
import ApolloClient, {
1918
ObservableQuery,
@@ -95,54 +94,6 @@ function getDisplayName(WrappedComponent) {
9594
// Helps track hot reloading.
9695
let nextVersion = 0;
9796

98-
export function withApollo(
99-
WrappedComponent,
100-
operationOptions: OperationOption = {},
101-
) {
102-
103-
const withDisplayName = `withApollo(${getDisplayName(WrappedComponent)})`;
104-
105-
class WithApollo extends Component<any, any> {
106-
static displayName = withDisplayName;
107-
static WrappedComponent = WrappedComponent;
108-
static contextTypes = { client: PropTypes.object.isRequired };
109-
110-
// data storage
111-
private client: ApolloClient; // apollo client
112-
113-
constructor(props, context) {
114-
super(props, context);
115-
this.client = context.client;
116-
117-
invariant(!!this.client,
118-
`Could not find "client" in the context of ` +
119-
`"${withDisplayName}". ` +
120-
`Wrap the root component in an <ApolloProvider>`,
121-
);
122-
123-
}
124-
125-
getWrappedInstance() {
126-
invariant(operationOptions.withRef,
127-
`To access the wrapped instance, you need to specify ` +
128-
`{ withRef: true } in the options`,
129-
);
130-
131-
return (this.refs as any).wrappedInstance;
132-
}
133-
134-
render() {
135-
const props = assign({}, this.props);
136-
props.client = this.client;
137-
if (operationOptions.withRef) props.ref = 'wrappedInstance';
138-
return createElement(WrappedComponent, props);
139-
}
140-
}
141-
142-
// Make sure we preserve any custom statics on the original component.
143-
return hoistNonReactStatics(WithApollo, WrappedComponent, {});
144-
}
145-
14697
export interface OperationOption {
14798
options?: Object | ((props: any) => QueryOptions | MutationOptions);
14899
props?: (props: any) => any;

0 commit comments

Comments
 (0)