Skip to content

Commit

Permalink
fix: Remove unnecessary code from production
Browse files Browse the repository at this point in the history
  • Loading branch information
diegohaz committed Oct 20, 2019
1 parent 71ee110 commit a0d22bf
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
12 changes: 7 additions & 5 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import * as React from "react";
import { SplitValueFunction, ContextHookReturn } from "./types";

const NO_PROVIDER = "CONSTATE_NO_PROVIDER" as any;
const isDev = process.env.NODE_ENV !== "production";

const NO_PROVIDER = "_NP_" as any;

function createUseContext(context: React.Context<any>): any {
return () => {
const value = React.useContext(context);
if (process.env.NODE_ENV !== "production" && value === NO_PROVIDER) {
if (isDev && value === NO_PROVIDER) {
// eslint-disable-next-line no-console
console.warn("[constate] Component not wrapped within a Provider.");
}
Expand All @@ -25,7 +27,7 @@ function constate<P, V, S extends Array<SplitValueFunction<V>>>(
const [createMemoDeps] = splitValues;
const deps = createMemoDeps && createMemoDeps(value);

if (process.env.NODE_ENV !== "production" && Array.isArray(deps)) {
if (isDev && Array.isArray(deps)) {
// eslint-disable-next-line no-console
console.warn(
"[constate] Passing `createMemoDeps` as the second argument is deprecated.",
Expand All @@ -46,7 +48,7 @@ function constate<P, V, S extends Array<SplitValueFunction<V>>>(
);
};

if (useValue.name) {
if (isDev && useValue.name) {
Context.displayName = `${useValue.name}.Context`;
Provider.displayName = `${useValue.name}.Provider`;
}
Expand Down Expand Up @@ -84,7 +86,7 @@ function constate<P, V, S extends Array<SplitValueFunction<V>>>(
return children;
};

if (useValue.name) {
if (isDev && useValue.name) {
SplitProvider.displayName = `${useValue.name}.Provider`;
}

Expand Down
2 changes: 1 addition & 1 deletion test/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ test("without provider", () => {
};
const App = () => <Count />;
const { getByText } = render(<App />);
expect(getByText("CONSTATE_NO_PROVIDER")).toBeDefined();
expect(getByText("_NP_")).toBeDefined();
});

test("displayName with named hook", () => {
Expand Down

0 comments on commit a0d22bf

Please sign in to comment.