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

Use UMD bundle and ES6 modules #1069

Merged
merged 18 commits into from
Jan 5, 2017
Merged
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
10 changes: 10 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"plugins": [
"transform-es2015-modules-commonjs"
],
"ignore": [
"lib/**/*.d.ts",
"lib/bundles/**",
"node_modules/**"
]
}
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ appveyor.yml
CHANGELOG.md
design.md
Gruntfile.js
rollup.config.js
tsconfig.json
tslint.json
typings.json
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Expect active development and potentially significant breaking changes in the `0

- ...

- Support AMD [PR #1069](https://github.com/apollostack/apollo-client/pull/1069)
- Support ES6 Modules and tree-shaking (`module`, `jsnext:main`) [PR #1069](https://github.com/apollostack/apollo-client/pull/1069)
- Replace `@types/redux` with official typescript definitions [PR #1069](https://github.com/apollostack/apollo-client/pull/1069)

- Remove fragment option from query, watchQuery etc. [PR #1096](https://github.com/apollostack/apollo-client/pull/1096)
- Broadcast new store state only when Apollo state was affected by an action [PR #1118](https://github.com/apollostack/apollo-client/pull/1118)

Expand Down
26 changes: 16 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@
"name": "apollo-client",
"version": "0.5.26",
"description": "A simple yet functional GraphQL client.",
"main": "./lib/src/index.js",
"main": "./lib/apollo.umd.js",
"module": "./lib/src/index.js",
"jsnext:main": "./lib/src/index.js",
"typings": "./lib/src/index.d.ts",
"scripts": {
"dev": "./scripts/dev.sh",
"deploy": "rm -rf ./lib && npm run compile && ./scripts/deploy.sh",
"deploy": "rm -rf ./lib && ./scripts/deploy.sh",
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the assumption here that the compiler and bundler already ran at this point?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

npm run compile is here, so there's no point of running it twice

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense.

"pretest": "npm run compile",
"test": "npm run testonly --",
"posttest": "npm run lint",
"filesize": "npm run compile:browser && ./scripts/filesize.js --file=./dist/index.min.js --maxGzip=38",
"compile": "tsc",
"compile:browser": "rm -rf ./dist && mkdir ./dist && browserify ./lib/src/index.js -o=./dist/index.js && npm run minify:browser",
"compile:browser": "rm -rf ./dist && mkdir ./dist && browserify ./lib/apollo.umd.js -o=./dist/index.js && npm run minify:browser",
"minify:browser": "uglifyjs --compress --mangle --screw-ie8 -o=./dist/index.min.js -- ./dist/index.js",
"watch": "tsc -w",
"bundle": "rollup -c",
"postcompile": "npm run bundle",
"prepublish": "npm run compile",
"lint": "grunt tslint",
"coverage": "istanbul cover ./node_modules/mocha/bin/_mocha -- --reporter dot --full-trace lib/test/tests.js",
"postcoverage": "remap-istanbul --input coverage/coverage.json --type lcovonly --output coverage/lcov.info",
"testonly": "mocha --reporter spec --full-trace lib/test/tests.js",
"coverage": "istanbul cover ./node_modules/mocha/bin/_mocha -- --compilers js:babel-core/register --reporter dot --full-trace lib/test/tests.js",
"postcoverage": "remap-istanbul --input coverage/coverage.raw.json --type lcovonly --output coverage/lcov.info",
"testonly": "mocha --compilers js:babel-core/register --reporter spec --full-trace lib/test/tests.js",
"preanalyze": "npm run compile",
"analyze": "webpack -p --config analyze/webpack.config.js"
},
Expand All @@ -42,13 +46,15 @@
"graphql-anywhere": "^1.0.0",
"graphql-tag": "^1.1.1",
"lodash": "^4.17.2",
"redux": "^3.3.1",
"redux": "^3.4.0",
"symbol-observable": "^1.0.2",
"whatwg-fetch": "^2.0.0"
},
"devDependencies": {
"@types/chai-as-promised": "0.0.28",
"@types/mocha": "^2.2.31",
"babel-core": "^6.21.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.18.0",
"browserify": "^13.0.0",
"chai": "^3.5.0",
"chai-as-promised": "^6.0.0",
Expand All @@ -59,13 +65,14 @@
"grunt-tslint": "4.0.0",
"gzip-size": "^3.0.0",
"isomorphic-fetch": "^2.2.1",
"istanbul": "^0.4.5",
"istanbul": "^1.0.0-alpha",
"lodash": "^4.17.1",
"minimist": "^1.2.0",
"mocha": "^3.0.0",
"pretty-bytes": "^4.0.0",
"remap-istanbul": "0.8.0",
"request": "^2.75.0",
"rollup": "^0.40.0",
"rxjs": "^5.0.0-beta.11",
"sinon": "^1.17.4",
"source-map-support": "^0.4.0",
Expand All @@ -79,10 +86,9 @@
"@types/async": "^2.0.31",
"@types/chai": "^3.4.32",
"@types/isomorphic-fetch": "0.0.30",
"@types/lodash": "^4.14.34",
"@types/lodash": "4.14.42",
"@types/node": "^6.0.38",
"@types/promises-a-plus": "0.0.26",
"@types/redux": "^3.5.29",
"@types/sinon": "^1.16.29",
"typed-graphql": "1.0.2"
}
Expand Down
15 changes: 15 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function globals(mod) {
if (mod.indexOf('lodash/') === 0) return '_';
}

export default {
entry: 'lib/src/index.js',
dest: 'lib/apollo.umd.js',
format: 'umd',
sourceMap: true,
moduleName: 'apollo',
external: [
'lodash'
],
globals
};
7 changes: 5 additions & 2 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ npm run compile

rm -rf ./npm
mkdir ./npm
cd ./lib/src && cp -r ./ ../../npm/ && cd ../../
cd ./lib/src && cp -r ./ ../../npm/ && cd ../
cp apollo.umd.js ../npm/ && cp apollo.umd.js.map ../npm/ && cd ../

# Ensure a vanilla package.json before deploying so other tools do not interpret
# The built output as requiring any further transformation.
node -e "var package = require('./package.json'); \
delete package.babel; delete package.scripts; delete package.options; \
package.main = 'index.js'; \
package.main = 'apollo.umd.js'; \
package.module = 'index.js'; \
package.typings = 'index.d.ts'; \
var fs = require('fs'); \
fs.writeFileSync('./npm/version.js', 'exports.version = \"' + package.version + '\"'); \
fs.writeFileSync('./npm/package.json', JSON.stringify(package, null, 2));"
Expand Down
9 changes: 6 additions & 3 deletions src/ApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import {

} from 'graphql';

import isUndefined = require('lodash/isUndefined');
import isString = require('lodash/isString');
import isUndefined from 'lodash/isUndefined';
import isString from 'lodash/isString';

import {
createApolloStore,
Expand All @@ -30,10 +30,13 @@ import {

import {
QueryManager,
} from './core/QueryManager';

import {
ApolloQueryResult,
ResultComparator,
ResultTransformer,
} from './core/QueryManager';
} from './core/types';

import {
ObservableQuery,
Expand Down
7 changes: 5 additions & 2 deletions src/core/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ import {

import {
QueryManager,
} from './QueryManager';

import {
ApolloQueryResult,
FetchType,
} from './QueryManager';
} from './types';

import { tryFunctionOrLogError } from '../util/errorHandling';

import { NetworkStatus } from '../queries/store';

import isEqual = require('lodash/isEqual');
import isEqual from 'lodash/isEqual';

export type ApolloCurrentResult = {
data: any;
Expand Down
65 changes: 16 additions & 49 deletions src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,22 @@ import {
Deduplicator,
} from '../transport/Deduplicator';

import forOwn = require('lodash/forOwn');
import isEqual = require('lodash/isEqual');
import forOwn from 'lodash/forOwn';
import isEqual from 'lodash/isEqual';

import {
ResultTransformer,
ResultComparator,
QueryListener,
ApolloQueryResult,
FetchType,
SubscriptionOptions,
} from './types';

import {
QueryStoreValue,
NetworkStatus,
} from '../queries/store';

import {
ApolloStore,
Expand All @@ -18,10 +32,6 @@ import {
ApolloReducerConfig,
} from '../store';

import {
QueryStoreValue,
} from '../queries/store';

import {
checkDocument,
getQueryDefinition,
Expand Down Expand Up @@ -82,10 +92,6 @@ import {
Observable,
} from '../util/Observable';

import {
NetworkStatus,
} from '../queries/store';

import { tryFunctionOrLogError } from '../util/errorHandling';

import {
Expand All @@ -97,44 +103,6 @@ import { WatchQueryOptions } from './watchQueryOptions';

import { ObservableQuery } from './ObservableQuery';

export type QueryListener = (queryStoreValue: QueryStoreValue) => void;

export interface SubscriptionOptions {
document: Document;
variables?: { [key: string]: any };
};

export type ApolloQueryResult = {
data: any;
loading: boolean;
networkStatus: NetworkStatus;

// This type is different from the GraphQLResult type because it doesn't include errors.
// Those are thrown via the standard promise/observer catch mechanism.
};

// A result transformer is given the data that is to be returned from the store from a query or
// mutation, and can modify or observe it before the value is provided to your application.
//
// For watched queries, the transformer is only called when the data retrieved from the server is
// different from previous.
//
// If the transformer wants to mutate results (say, by setting the prototype of result data), it
// will likely need to be paired with a custom resultComparator. By default, Apollo performs a
// deep equality comparsion on results, and skips those that are considered equal - reducing
// re-renders.
export type ResultTransformer = (resultData: ApolloQueryResult) => ApolloQueryResult;

// Controls how Apollo compares two query results and considers their equality. Two equal results
// will not trigger re-renders.
export type ResultComparator = (result1: ApolloQueryResult, result2: ApolloQueryResult) => boolean;

export enum FetchType {
normal = 1,
refetch = 2,
poll = 3,
}

export class QueryManager {
public pollingTimers: {[queryId: string]: NodeJS.Timer | any}; //oddity in Typescript
public scheduler: QueryScheduler;
Expand Down Expand Up @@ -598,7 +566,6 @@ export class QueryManager {
this.fetchQueryPromises[requestId.toString()] = { promise, resolve, reject };
}


// Removes the promise in this.fetchQueryPromises for a particular request ID.
public removeFetchQueryPromise(requestId: number) {
delete this.fetchQueryPromises[requestId.toString()];
Expand Down
43 changes: 43 additions & 0 deletions src/core/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Document } from 'graphql';
import {
QueryStoreValue,
NetworkStatus,
} from '../queries/store';

export interface SubscriptionOptions {
document: Document;
variables?: { [key: string]: any };
};

export type QueryListener = (queryStoreValue: QueryStoreValue) => void;

export type ApolloQueryResult = {
data: any;
loading: boolean;
networkStatus: NetworkStatus;

// This type is different from the GraphQLResult type because it doesn't include errors.
// Those are thrown via the standard promise/observer catch mechanism.
};

// A result transformer is given the data that is to be returned from the store from a query or
// mutation, and can modify or observe it before the value is provided to your application.
//
// For watched queries, the transformer is only called when the data retrieved from the server is
// different from previous.
//
// If the transformer wants to mutate results (say, by setting the prototype of result data), it
// will likely need to be paired with a custom resultComparator. By default, Apollo performs a
// deep equality comparsion on results, and skips those that are considered equal - reducing
// re-renders.
export type ResultTransformer = (resultData: ApolloQueryResult) => ApolloQueryResult;

// Controls how Apollo compares two query results and considers their equality. Two equal results
// will not trigger re-renders.
export type ResultComparator = (result1: ApolloQueryResult, result2: ApolloQueryResult) => boolean;

export enum FetchType {
normal = 1,
refetch = 2,
poll = 3,
}
6 changes: 3 additions & 3 deletions src/data/debug.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// For development only!
import isObject = require('lodash/isObject');
import omit = require('lodash/omit');
import mapValues = require('lodash/mapValues');
import isObject from 'lodash/isObject';
import omit from 'lodash/omit';
import mapValues from 'lodash/mapValues';

export function stripLoc(obj: Object) {
if (Array.isArray(obj)) {
Expand Down
4 changes: 2 additions & 2 deletions src/data/mutationResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
GraphQLResult,
} from 'graphql';

import mapValues = require('lodash/mapValues');
import cloneDeep = require('lodash/cloneDeep');
import mapValues from 'lodash/mapValues';
import cloneDeep from 'lodash/cloneDeep';

import { replaceQueryResults } from './replaceQueryResults';

Expand Down
2 changes: 1 addition & 1 deletion src/data/scopeQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
resultKeyNameFromField,
} from './storeUtils';

import isNumber = require('lodash/isNumber');
import isNumber from 'lodash/isNumber';

// The type of a path
export type StorePath = (string|number)[];
Expand Down
2 changes: 1 addition & 1 deletion src/data/storeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
Name,
} from 'graphql';

import isObject = require('lodash/isObject');
import isObject from 'lodash/isObject';

function isStringValue(value: Value): value is StringValue {
return value.kind === 'StringValue';
Expand Down
6 changes: 3 additions & 3 deletions src/data/writeToStore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import isNull = require('lodash/isNull');
import isUndefined = require('lodash/isUndefined');
import isObject = require('lodash/isObject');
import isNull from 'lodash/isNull';
import isUndefined from 'lodash/isUndefined';
import isObject from 'lodash/isObject';

import {
getOperationDefinition,
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ import ApolloClient from './ApolloClient';

import {
ApolloQueryResult,
} from './core/QueryManager';
} from './core/types';

import {
toIdValue,
Expand Down
Loading