Skip to content

Commit 52b9423

Browse files
Nicolas Gallagherrxaviers
authored andcommitted
Remove development-only code from production builds
1 parent 1a9765f commit 52b9423

File tree

3 files changed

+89
-78
lines changed

3 files changed

+89
-78
lines changed

src/generator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function generator(fn, localPropNames, options) {
4343
this.globalizePropValues[0] = props.children;
4444

4545
beforeFormat.call(this);
46-
const formattedValue = this.globalize[fn].apply(this.globalize, this.globalizePropValues);
46+
var formattedValue = this.globalize[fn].apply(this.globalize, this.globalizePropValues);
4747
this.value = afterFormat.call(this, formattedValue);
4848
},
4949
render: function() {

src/message.js

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ import generator from "./generator";
44
import "globalize/message";
55
import "globalize/plural";
66

7-
function messageSetup(componentProps, globalize, args) {
8-
var defaultMessage, path;
9-
var children = componentProps.children;
10-
var scope = componentProps.scope;
11-
var pathProperty = componentProps.path;
7+
function messageSetup(globalize, props, globalizePropValues) {
8+
var defaultMessage;
9+
var children = props.children;
10+
var scope = props.scope;
1211

1312
function getDefaultMessage(children) {
1413
if (typeof children === "string") {
@@ -18,71 +17,72 @@ function messageSetup(componentProps, globalize, args) {
1817
}
1918
}
2019

21-
function getMessage() {
22-
return globalize.cldr.get(["globalize-messages/{bundle}"].concat(path));
23-
}
24-
25-
function setMessage(message) {
26-
var data = {};
27-
function set(data, path, value) {
28-
var i;
29-
var node = data;
30-
var length = path.length;
31-
32-
for (i = 0; i < length - 1; i++) {
33-
if (!node[path[i]]) {
34-
node[path[i]] = {};
35-
}
36-
node = node[path[i]];
37-
}
38-
node[path[i]] = value;
39-
}
40-
set(data, [globalize.cldr.attributes.bundle].concat(path), message);
41-
Globalize.loadMessages(data);
42-
}
43-
4420
// Set path - path as props supercedes default value.
45-
if (pathProperty) {
46-
// Override generator assumption. The generator assumes the args[0]
21+
if (props.path) {
22+
// Override generator assumption. The generator assumes the globalizePropValues[0]
4723
// (path) and props.children to be mutually exclusive, but this isn't
4824
// true here for messages. Because, it's possible to use props.path (for
4925
// path) and props.children for defaultMessage, which are two different
5026
// variables.
51-
args[0] = pathProperty;
52-
path = pathProperty.split("/");
27+
globalizePropValues[0] = props.path;
5328
} else {
54-
// Although the generator had already set args[0] (path) as
29+
// Although the generator had already set globalizePropValues[0] (path) as
5530
// props.children, here its type is checked and its value is sanitized.
5631
defaultMessage = getDefaultMessage(children);
57-
args[0] = sanitizePath(defaultMessage);
58-
path = [args[0]];
32+
globalizePropValues[0] = sanitizePath(defaultMessage);
5933
}
6034

6135
// Scope path.
6236
if (scope) {
63-
args[0] = scope + "/" + args[0];
64-
path = scope.split("/").concat(path);
37+
globalizePropValues[0] = scope + "/" + globalizePropValues[0];
6538
}
6639

6740
// Development mode only.
68-
if (globalize.cldr) {
69-
if (!getMessage()) {
70-
defaultMessage = defaultMessage || getDefaultMessage(children);
71-
setMessage(defaultMessage);
41+
if (process.env.NODE_ENV !== "production") {
42+
var path = props.path ? props.path.split("/") : [globalizePropValues[0]];
43+
/* eslint-disable no-inner-declarations */
44+
function getMessage(globalize, path) {
45+
return globalize.cldr.get(["globalize-messages/{bundle}"].concat(path));
7246
}
73-
}
7447

48+
function setMessage(globalize, path, message) {
49+
var data = {};
50+
function set(data, path, value) {
51+
var i;
52+
var node = data;
53+
var length = path.length;
54+
55+
for (i = 0; i < length - 1; i++) {
56+
if (!node[path[i]]) {
57+
node[path[i]] = {};
58+
}
59+
node = node[path[i]];
60+
}
61+
node[path[i]] = value;
62+
}
63+
set(data, [globalize.cldr.attributes.bundle].concat(path), message);
64+
Globalize.loadMessages(data);
65+
}
66+
/* eslint-enable no-inner-declarations */
67+
68+
if (globalize.cldr) {
69+
if (!getMessage(globalize, path)) {
70+
defaultMessage = defaultMessage || getDefaultMessage(children);
71+
setMessage(globalize, path, defaultMessage);
72+
}
73+
}
74+
}
7575
}
7676

77-
function replaceElements(componentProps, formatted) {
78-
var elements = componentProps.elements;
77+
function replaceElements(props, formatted) {
78+
var elements = props.elements;
7979

8080
function _replaceElements(string, elements) {
8181
if (typeof string !== "string") {
82-
throw new Error("missing or invalid string `" + string + "` (" + typeof string + ")");
82+
throw new Error("Missing or invalid string `" + string + "` (" + typeof string + ")");
8383
}
8484
if (typeof elements !== "object") {
85-
throw new Error("missing or invalid elements `" + elements + "` (" + typeof elements + ")");
85+
throw new Error("Missing or invalid elements `" + elements + "` (" + typeof elements + ")");
8686
}
8787

8888
// Given [x, y, z], it returns [x, element, y, element, z].
@@ -156,9 +156,8 @@ function sanitizePath(pathString) {
156156
}
157157

158158
// Overload Globalize's `.formatMessage` to allow default message.
159-
var messageFormatterSuper = Globalize.messageFormatter;
160-
Globalize.messageFormatter =
161-
Globalize.prototype.messageFormatter = function(pathOrMessage) {
159+
var globalizeMessageFormatter = Globalize.messageFormatter;
160+
Globalize.messageFormatter = Globalize.prototype.messageFormatter = function(pathOrMessage) {
162161
var aux = {};
163162
var sanitizedPath = sanitizePath(pathOrMessage);
164163

@@ -167,9 +166,9 @@ Globalize.prototype.messageFormatter = function(pathOrMessage) {
167166
// On runtime, the only way for deciding between using sanitizedPath or
168167
// pathOrMessage as path is by checking which formatter exists.
169168
arguments[0] = sanitizedPath;
170-
aux = messageFormatterSuper.apply(this, arguments);
169+
aux = globalizeMessageFormatter.apply(this, arguments);
171170
arguments[0] = pathOrMessage;
172-
return aux || messageFormatterSuper.apply(this, arguments);
171+
return aux || globalizeMessageFormatter.apply(this, arguments);
173172
}
174173

175174
var sanitizedPathExists = this.cldr.get(["globalize-messages/{bundle}", sanitizedPath]) !== undefined;
@@ -186,12 +185,12 @@ Globalize.prototype.messageFormatter = function(pathOrMessage) {
186185
}
187186

188187
arguments[0] = sanitizedPathExists ? sanitizedPath : pathOrMessage;
189-
return messageFormatterSuper.apply(this, arguments);
188+
return globalizeMessageFormatter.apply(this, arguments);
190189
};
191190

192191
export default generator("formatMessage", ["path", "variables"], {
193192
beforeFormat: function() {
194-
messageSetup(this.props, this.globalize, this.globalizePropValues);
193+
messageSetup(this.globalize, this.props, this.globalizePropValues);
195194
},
196195
afterFormat: function(formattedValue) {
197196
return replaceElements(this.props, formattedValue);

test/formatMessage.spec.js

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,46 @@ Globalize.loadMessages({
2626
}
2727
});
2828

29-
describe("formatMessage Component", () => {
30-
it("renders as a <span>", () => {
31-
const wrapper = shallow(<FormatMessage path="salutations/hi" />);
32-
expect(wrapper.type()).to.equal("span");
33-
});
29+
[ "development", "production" ].forEach((env) => {
30+
describe(`formatMessage Component (${env})`, () => {
31+
var originalEnv = process.env.NODE_ENV;
3432

35-
it("uses default message and prints 'Hi'", () => {
36-
const wrapper = shallow(<FormatMessage>Hi</FormatMessage>);
37-
expect(wrapper.text()).to.equal("Hi");
38-
});
33+
before(() => {
34+
process.env.NODE_ENV = env;
35+
});
3936

40-
it("resolves path and prints 'Hi'", () => {
41-
const wrapper = shallow(<FormatMessage path="salutations/hi" />);
42-
expect(wrapper.text()).to.equal("Hi");
43-
});
37+
after(() => {
38+
process.env.NODE_ENV = originalEnv;
39+
});
4440

45-
it("properly replaces variables", () => {
46-
const wrapper = shallow(<FormatMessage path="variables/hello" variables={["Wolfgang", "Amadeus", "Mozart"]} />);
47-
expect(wrapper.text()).to.equal("Hello, Wolfgang Amadeus Mozart");
48-
});
41+
it("renders as a <span>", () => {
42+
const wrapper = shallow(<FormatMessage path="salutations/hi" />);
43+
expect(wrapper.type()).to.equal("span");
44+
});
4945

50-
it("uses proper gender inflection", () => {
51-
const wrapper = shallow(<FormatMessage path="party" variables={{guest:"Mozart", guestGender:"male", host:"Beethoven", hostGender:"other"}} />);
52-
expect(wrapper.text()).to.equal("Beethoven invites Mozart to their party");
53-
});
46+
it("uses default message and prints 'Hi'", () => {
47+
const wrapper = shallow(<FormatMessage>Hi</FormatMessage>);
48+
expect(wrapper.text()).to.equal("Hi");
49+
});
50+
51+
it("resolves path and prints 'Hi'", () => {
52+
const wrapper = shallow(<FormatMessage path="salutations/hi" />);
53+
expect(wrapper.text()).to.equal("Hi");
54+
});
55+
56+
it("properly replaces variables", () => {
57+
const wrapper = shallow(<FormatMessage path="variables/hello" variables={["Wolfgang", "Amadeus", "Mozart"]} />);
58+
expect(wrapper.text()).to.equal("Hello, Wolfgang Amadeus Mozart");
59+
});
60+
61+
it("uses proper gender inflection", () => {
62+
const wrapper = shallow(<FormatMessage path="party" variables={{guest:"Mozart", guestGender:"male", host:"Beethoven", hostGender:"other"}} />);
63+
expect(wrapper.text()).to.equal("Beethoven invites Mozart to their party");
64+
});
5465

55-
it("uses proper plural inflection", () => {
56-
const wrapper = shallow(<FormatMessage path="task" variables={{count: 1}} />);
57-
expect(wrapper.text()).to.equal("You have one task remaining");
66+
it("uses proper plural inflection", () => {
67+
const wrapper = shallow(<FormatMessage path="task" variables={{count: 1}} />);
68+
expect(wrapper.text()).to.equal("You have one task remaining");
69+
});
5870
});
5971
});

0 commit comments

Comments
 (0)