Skip to content

Commit

Permalink
Create @apollo/client/common bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkisiela authored and hwillson committed Nov 11, 2019
1 parent c0f82b4 commit b4b1db4
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 24 deletions.
12 changes: 12 additions & 0 deletions common/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@apollo/client/common",
"description": "@apollo/client without React",
"author": "opensource@apollographql.com",
"license": "MIT",
"main": "./dist/common/apollo-client.cjs.js",
"module": "./dist/common/index.js",
"types": "./dist/common/index.d.ts",
"sideEffects": [
"./dist/cache/inmemory/fixPolyfills.js"
]
}
10 changes: 10 additions & 0 deletions config/prepareDist.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// `LICENSE`, etc.)

const packageJson = require('../package.json');
const commonPackageJson = require('../common/package.json');
const fs = require('fs');

// The root package.json is marked as private to prevent publishing
Expand All @@ -33,8 +34,17 @@ const distPackageJson = JSON.stringify(
2
);

const distCommonPackageJson = JSON.stringify(
commonPackageJson,
(_key, value) => (
typeof value === 'string' ? value.replace(/\.\/dist\/common\//, './').replace(/\.\/dist\//, '../') : value
),
2
);

// Save the modified package.json to "dist"
fs.writeFileSync(`${__dirname}/../dist/package.json`, distPackageJson);
fs.writeFileSync(`${__dirname}/../dist/common/package.json`, distCommonPackageJson);

// Copy supporting files into "dist"
const srcDir = `${__dirname}/..`;
Expand Down
70 changes: 47 additions & 23 deletions config/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import cjs from 'rollup-plugin-commonjs';
import fs from 'fs';

import packageJson from '../package.json';
import commonPackageJson from '../common/package.json';

const distDir = './dist';
const distCommonDir = `${distDir}/common`;

const globals = {
'tslib': 'tslib',
tslib: 'tslib',
'ts-invariant': 'invariant',
'symbol-observable': '$$observable',
'graphql/language/printer': 'print',
Expand All @@ -21,7 +23,7 @@ const globals = {
'@wry/equality': 'wryEquality',
graphql: 'graphql',
react: 'React',
'zen-observable': 'Observable'
'zen-observable': 'Observable',
};

const hasOwn = Object.prototype.hasOwnProperty;
Expand All @@ -30,12 +32,17 @@ function external(id) {
return hasOwn.call(globals, id);
}

function prepareESM() {
/**
*
* @param {string} input
* @param {string} outputDir
*/
function prepareESM(input, outputDir) {
return {
input: packageJson.module,
input, // packageJson.module,
external,
output: {
dir: distDir,
dir: outputDir, // distDir,
format: 'esm',
sourcemap: true,
},
Expand All @@ -58,39 +65,49 @@ function prepareESM() {
}),
cjs({
namedExports: {
'graphql-tag': ['gql']
}
'graphql-tag': ['gql'],
},
}),
]
],
};
}

function prepareCJS() {
/**
*
* @param {string} input
* @param {string} outputDir
*/
function prepareCJS(input, output) {
return {
input: packageJson.module,
input, // packageJson.module,
external,
output: {
file: packageJson.main,
file: output, // packageJson.main,
format: 'cjs',
sourcemap: true,
exports: 'named'
exports: 'named',
},
plugins: [
nodeResolve(),
cjs({
namedExports: {
'graphql-tag': ['gql']
}
'graphql-tag': ['gql'],
},
}),
]
}
],
};
}

function prepareCJSMinified() {
/**
*
* @param {string} input
* @param {string} outputDir
*/
function prepareCJSMinified(input) {
return {
input: packageJson.main,
input, // packageJson.main,
output: {
file: packageJson.main.replace('.js', '.min.js'),
file: input.replace('.js', '.min.js'), // packageJson.main.replace('.js', '.min.js'),
format: 'cjs',
},
plugins: [
Expand Down Expand Up @@ -141,10 +158,17 @@ function prepareTesting() {

function rollup() {
return [
prepareESM(),
prepareCJS(),
prepareCJSMinified(),
prepareTesting()
// @apollo/client
prepareESM(packageJson.module, distDir),
prepareCJS(packageJson.module, packageJson.main),
prepareCJSMinified(packageJson.main),
prepareTesting(),
// @apollo/client/common
prepareCJS(
commonPackageJson.module,
commonPackageJson.main,
),
prepareCJSMinified(commonPackageJson.main),
];
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"bundlesize": "npm run build && bundlesize",
"prepdist": "node ./config/prepareDist.js",
"predeploy": "npm run build && npm run prepdist",
"deploy": "cd dist && npm publish --tag beta"
"deploy": "cd dist && npm publish --tag beta --registry http://localhost:4873"
},
"bundlesize": [
{
Expand Down
84 changes: 84 additions & 0 deletions src/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/* Core */

export {
ApolloClient,
ApolloClientOptions,
DefaultOptions
} from '../ApolloClient';
export {
ObservableQuery,
FetchMoreOptions,
UpdateQueryOptions,
ApolloCurrentQueryResult,
} from '../core/ObservableQuery';
export {
QueryBaseOptions,
QueryOptions,
WatchQueryOptions,
MutationOptions,
SubscriptionOptions,
FetchPolicy,
WatchQueryFetchPolicy,
ErrorPolicy,
FetchMoreQueryOptions,
SubscribeToMoreOptions,
MutationUpdaterFn,
} from '../core/watchQueryOptions';
export { NetworkStatus } from '../core/networkStatus';
export * from '../core/types';
export {
Resolver,
FragmentMatcher as LocalStateFragmentMatcher,
} from '../core/LocalState';
export { isApolloError, ApolloError } from '../errors/ApolloError';

/* Cache */

export { Transaction, ApolloCache } from '../cache/core/cache';
export { Cache } from '../cache/core/types/Cache';
export { DataProxy } from '../cache/core/types/DataProxy';
export {
InMemoryCache,
InMemoryCacheConfig,
} from '../cache/inmemory/inMemoryCache';
export { defaultDataIdFromObject } from '../cache/inmemory/policies';
export * from '../cache/inmemory/types';

/* Link */

export { empty } from '../link/core/empty';
export { from } from '../link/core/from';
export { split } from '../link/core/split';
export { concat } from '../link/core/concat';
export { execute } from '../link/core/execute';
export { ApolloLink } from '../link/core/ApolloLink';
export * from '../link/core/types';
export {
parseAndCheckHttpResponse,
ServerParseError
} from '../link/http/parseAndCheckHttpResponse';
export {
serializeFetchParameter,
ClientParseError
} from '../link/http/serializeFetchParameter';
export {
HttpOptions,
fallbackHttpConfig,
selectHttpOptionsAndBody,
UriFunction
} from '../link/http/selectHttpOptionsAndBody';
export { checkFetcher } from '../link/http/checkFetcher';
export { createSignalIfSupported } from '../link/http/createSignalIfSupported';
export { selectURI } from '../link/http/selectURI';
export { createHttpLink } from '../link/http/createHttpLink';
export { HttpLink } from '../link/http/HttpLink';
export { fromError } from '../link/utils/fromError';
export { ServerError, throwServerError } from '../link/utils/throwServerError';

/* Utilities */

export { Observable } from '../utilities/observables/Observable';

/* Supporting */

export { default as gql } from 'graphql-tag';

0 comments on commit b4b1db4

Please sign in to comment.