Skip to content

Commit 58270e3

Browse files
committed
Add loader to build page
1 parent 6d0dbda commit 58270e3

2 files changed

Lines changed: 67 additions & 28 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
This is the source for [messageformat.github.io](https://messageformat.github.io/), the site for [messageformat.js](https://github.com/messageformat/messageformat.js).
1+
This is the source for [messageformat.github.io](https://messageformat.github.io/), the site for [messageformat](https://github.com/messageformat/messageformat.js).

build.md

Lines changed: 66 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,32 @@ layout: page
33
title: Build-time Compilation
44
---
55

6-
For a significant decrease in filesize and execution time, you should precompile your messages to JavaScript during your build phase. It works like this:
6+
For a significant decrease in filesize and execution time, you should precompile your messages to JavaScript during your build phase. If you're using [Webpack], we also provide **[messageformat-loader]** to help with that.
77

8-
```js
9-
var mf = new MessageFormat('en');
10-
var messages = {
11-
simple: 'A simple message.',
12-
var: 'Message with {X}.',
13-
plural: 'You have {N, plural, =0{no messages} one{1 message} other{# messages}}.',
14-
select: '{GENDER, select, male{He has} female{She has} other{They have}} sent you a message.',
15-
ordinal: 'The {N, selectordinal, one{1st} two{2nd} few{3rd} other{#th}} message.'
16-
};
8+
[Webpack]: https://webpack.js.org/
9+
[messageformat-loader]: https://github.com/messageformat/loader
1710

18-
var mfunc = mf.compile(messages);
19-
mfunc().ordinal({ N: 1 })
20-
// "The 1st message."
11+
## Webpack
2112

22-
var efunc = new Function('return (' + mfunc.toString() + ')()');
13+
With the [loader](https://github.com/messageformat/loader) (see its README for configuration instructions), this will "just work" in your code:
2314

24-
efunc();
25-
// { simple: [Function],
26-
// var: [Function],
27-
// plural: [Function],
28-
// select: [Function],
29-
// ordinal: [Function] }
15+
### messages.json
3016

31-
efunc().ordinal({ N: 2 });
32-
// "The 2nd message."
17+
```json
18+
{
19+
"ordinal": "The {N, selectordinal, one{1st} two{2nd} few{3rd} other{#th}} message."
20+
}
21+
```
22+
23+
### example.js
24+
25+
```js
26+
import messages from './messages.json'
27+
messages.ordinal({ N: 1 })
28+
// => 'The 1st message.'
3329
```
3430

35-
Note that as `efunc` is defined as a `new Function()`, it has no access to the surrounding scope. This means that the output of `mfunc().toString()` can be saved as a file and later included with `require()` or `<script src=...>`, providing access to the compiled functions completely independently of messageformat.js, or any other dependencies.
31+
During the build, the loader will compile your messages into their respective functions, and package only those into the webpack output.
3632

3733

3834
## CLI Compiler
@@ -41,12 +37,18 @@ A [CLI compiler](https://github.com/messageformat/messageformat.js/tree/master/b
4137

4238
```text
4339
$ messageformat
44-
usage: messageformat [-l lc] [-n ns] [-p] input
40+
usage: messageformat [-i] [-l lc] [-n ns] [-p] input
4541
4642
Parses the input JSON file(s) of MessageFormat strings into a JS module of
4743
corresponding hierarchical functions, written to stdout. Directories are
4844
recursively scanned for all .json files.
4945
46+
-i, --enable-intl-support
47+
Because native or polyfilled support for global Intl object is not
48+
guaranteed, messageformat will disable Intl formatters by default.
49+
If you require Intl support, you can use this argument to enable
50+
Intl formatters for your messages. [default: false]
51+
5052
-l lc, --locale=lc
5153
The locale(s) lc to include; if multiple, selected by matching
5254
message key. [default: en]
@@ -58,23 +60,60 @@ recursively scanned for all .json files.
5860
'module.exports' (node.js) are special. [default: module.exports]
5961
6062
-p, --disable-plural-key-checks
61-
By default, messageformat.js throws an error when a statement uses a
63+
By default, messageformat throws an error when a statement uses a
6264
non-numerical key that will never be matched as a pluralization
6365
category for the current locale. Use this argument to disable the
6466
validation and allow unused plural keys. [default: false]
6567
```
6668

6769

68-
## Using compiled messageformat.js output
70+
## Using compiled messageformat output
6971

7072
With default options, compiled messageformat functions are available through `module.exports`. However, using e.g. `-n i18n`, an UMD pattern is used, falling back to a global `i18n` object, with each source json having a corresponding subobject. Hence the compiled function corresponding to the `test` message defined in [`example/en/sub/folder/plural.json`](https://github.com/messageformat/messageformat.js/tree/master/example/en/sub/folder/plural.json) is available as [`i18n.sub.folder.plural.test`](https://github.com/messageformat/messageformat.js/tree/master/example/en/i18n.js):
7173

7274
```html
7375
<script src="path/to/messageformat/example/en/i18n.js"></script>
7476
<script>
75-
console.log(i18n.sub.folder.plural.test({ NUM: 3 }));
77+
console.log(i18n.sub.folder.plural.test({ NUM: 3 }))
7678
</script>
7779
```
7880
will log `"Your 3 messages go here."`
7981

8082
A working example is available [here](/messageformat.js/example/index.html).
83+
84+
85+
## Other JavaSCript Build Environments
86+
87+
To precompile messages in other JavaScript environments, you should make use of the object input format of [`MessageFormat#compile()`](http://messageformat.github.io/messageformat.js/doc/MessageFormat.html#compile), the output of which is stringifiable for later execution in other environments.
88+
89+
It works like this:
90+
91+
```js
92+
const mf = new MessageFormat('en')
93+
const messages = {
94+
simple: 'A simple message.',
95+
var: 'Message with {X}.',
96+
plural: 'You have {N, plural, =0{no messages} one{1 message} other{# messages}}.',
97+
select: '{GENDER, select, male{He has} female{She has} other{They have}} sent you a message.',
98+
ordinal: 'The {N, selectordinal, one{1st} two{2nd} few{3rd} other{#th}} message.'
99+
}
100+
101+
const mfunc = mf.compile(messages)
102+
mfunc().ordinal({ N: 1 })
103+
// "The 1st message."
104+
105+
const efunc = new Function('return (' + mfunc.toString() + ')()')
106+
107+
efunc()
108+
// { simple: [Function],
109+
// var: [Function],
110+
// plural: [Function],
111+
// select: [Function],
112+
// ordinal: [Function] }
113+
114+
efunc().ordinal({ N: 2 })
115+
// "The 2nd message."
116+
```
117+
118+
Note that as `efunc` is defined as a `new Function()`, it has no access to the surrounding scope. This means that the output of `mfunc().toString()` can be saved as a file and later included with `require()` or `<script src=...>`, providing access to the compiled functions completely independently of messageformat, or any other dependencies.
119+

0 commit comments

Comments
 (0)