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

Example: Development & Production + CJS & AMD environments #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions compiler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var React = require("react");
var Globalize = require("globalize");
var GlobalizeCompiler = require("globalize/tool/compiler");

function compiler() {
var components = [].slice.call(arguments, 0);

// Have react to render all passed components, therefore any formatters in use will be created.
components.forEach(function(component) {
React.renderToString(component);
});

// Compile all generated formatters.
return GlobalizeCompiler(Globalize.cache);
}

module.exports = compiler;
10 changes: 0 additions & 10 deletions examples/README.md

This file was deleted.

7 changes: 7 additions & 0 deletions examples/amd/.bowerrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"scripts": {
"preinstall": "npm install [email protected]",
"postinstall": "node ./node_modules/cldr-data-downloader/bin/download.js -i bower_components/cldr-data/index.json -o bower_components/cldr-data/"
}
}

17 changes: 17 additions & 0 deletions examples/amd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Example
=======

Development mode
-------
These instructions assume you have node and npm installed. For help, see the [npm docs](https://docs.npmjs.com/getting-started/installing-node)

1. Run `npm install` once.
3. Open browser and navigate to `react-globalize/examples/development.html`.

Production mode
-------

1. Run `npm install` once.
2. Run `npm run-script build` to generate the built JS file.
3. Open browser and navigate to `react-globalize/examples/production.html`.

14 changes: 14 additions & 0 deletions examples/amd/bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "react-globalize-example",
"devDependencies": {
"almond": "~0.3.1",
"cldr-data": "27.x",
"globalize": "jquery/globalize#fix-398-runtime",
"react": "0.13.1",
"react-globalize": "rxaviers/react-globalize#addons",
Copy link
Member Author

Choose a reason for hiding this comment

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

Temporary. Needs to be fixed before landing.

"requirejs": "2.1.x",
"requirejs-plugins": "1.0.2",
"requirejs-react-jsx": "0.13.1",
"requirejs-text": "2.0.x"
}
}
41 changes: 41 additions & 0 deletions examples/amd/build
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#! /usr/bin/env node

var requirejs = require("requirejs");
var util = require("util");

var config = require("./src/config");
config.paths.almond = "../bower_components/almond/almond";
config.paths["globalize-precompiled-formatters"] = "../tmp/globalize-precompiled-formatters";
config.paths["globalize-runtime"] = "../bower_components/globalize/dist/globalize-runtime";
config.map = {
"*": {
"globalize": "globalize-runtime"
}
};
delete config.paths["cldr-data"];

util._extend(config, {
dir: "tmp/.build",
appDir: "src",
baseUrl: ".",
optimize: "none",
skipDirOptimize: true,
skipSemiColonInsertion: true,
skipModuleInsertion: true,
findNestedDependencies: true,
stubModules : ["text"],
onBuildWrite: function (id, path, contents) {
return contents
.replace(/"globalize"/g, "\"globalize-runtime\"")
.replace(/"globalize\//g, "\"globalize-runtime\/")
.replace(/jsx!/g, "");
},
modules: [{
name: "app",
include: ["almond", "jsx!production"],
exclude: ["jsx"],
create: true
}]
});

requirejs.optimize(config, function(){}, console.error);
14 changes: 14 additions & 0 deletions examples/amd/development.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv='Content-type' content='text/html; charset=utf-8'>
<title>Examples</title>
</head>
<body>
<script src="src/config.js"></script>
<script src="bower_components/requirejs/require.js"></script>
<script>
require(["jsx!./src/development"]);
</script>
</body>
</html>
21 changes: 21 additions & 0 deletions examples/amd/globalize-compiler
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#! /usr/bin/env node

var GlobalizeCompiler = require("globalize/tool/compiler");
var React = require("react");
var requirejs = require("requirejs");
var config = require("./src/config");

config.nodeRequire = require;
config.baseUrl = __dirname + "/src";
requirejs.config(config);

Globalize = requirejs("globalize");
requirejs("load-globalize-i18n-data");
var App = requirejs("jsx!app");

// Have react to render all passed components, therefore any formatters in use will be created.
React.renderToString(React.createElement(App, {locale: "en"}));
React.renderToString(React.createElement(App, {locale: "pt"}));

// Compile all generated formatters.
console.log(GlobalizeCompiler(Globalize.cache));
15 changes: 15 additions & 0 deletions examples/amd/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "react-globalize-example",
"description": "Examples demonstrating use of the components",
"main": "app.js",
"devDependencies": {
"react": "~0.13.0",
"react-tools": "^0.13.2",
"requirejs": "^2.1.17",
"uglify-js": "^2.4.20"
},
"scripts": {
"clean": "for DIR in tmp dist; do ( test -d $DIR && rm -rf $DIR ); mkdir $DIR; done",
"build": "npm run-script clean && ./globalize-compiler > tmp/globalize-precompiled-formatters.js && ./build && cp tmp/.build/app.js dist/production.js && uglifyjs dist/production.js > dist/production.min.js"
}
}
10 changes: 10 additions & 0 deletions examples/amd/production.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv='Content-type' content='text/html; charset=utf-8'>
<title>Examples</title>
</head>
<body>
<script src="dist/production.js"></script>
</body>
</html>
49 changes: 49 additions & 0 deletions examples/amd/src/app.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
define([
"globalize",
"react",
"jsx!./components/currency",
"jsx!./components/dates",
"jsx!./components/messages",
"jsx!./components/numbers"
], function(Globalize, React, LocalizedCurrencies, LocalizedDates, LocalizedMessages, LocalizedNumbers) {

var App = React.createClass({
getInitialState: function() {
return {
locale: this.props.locale
};
},
changeLocale: function( event ) {
this.setState({
locale: event.target.value
});
},
render: function() {
Globalize.locale(this.state.locale);
return (
<div>
<h1>Select a locale:</h1>
<select onChange={this.changeLocale}>
<option value="en">en-US</option>
<option value="pt">pt-BR</option>
</select>

<h1>Currency</h1>
<LocalizedCurrencies />

<h1>Dates</h1>
<LocalizedDates />

<h1>Messages</h1>
<LocalizedMessages />

<h1>Numbers</h1>
<LocalizedNumbers />
</div>
);
}
});

return App;

});
38 changes: 38 additions & 0 deletions examples/amd/src/components/currency.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
define([
"react",
"react-globalize"
], function(React, ReactGlobalize) {

var FormatCurrency = ReactGlobalize.FormatCurrency;

var LocalizedCurrencies = React.createClass({
render: function() {
return (
<div>
USD, 150, default - <FormatCurrency currency="USD" value={150} />
<br/>
USD, -150, style: "accounting" - <FormatCurrency currency="USD" value={-150} options={{ style: "accounting" }} />
<br/>
USD, 150, style: "name" - <FormatCurrency currency="USD" value={150} options={{ style: "name" }} />
<br/>
USD, 150, style: "code" - <FormatCurrency currency="USD" value={150} options={{ style: "code" }} />
<br/>
USD, 1.491, round: "ceil" - <FormatCurrency currency="USD" value={1.491} options={{ round: "ceil" }} />
<br/>
EUR, 150, default - <FormatCurrency currency="EUR" value={150} />
<br/>
EUR, -150, style: "accounting" - <FormatCurrency currency="EUR" value={-150} options={{ style: "accounting" }} />
<br/>
EUR, 150, style: "name" - <FormatCurrency currency="EUR" value={150} options={{ style: "name" }} />
<br/>
EUR, 150, style: "code" - <FormatCurrency currency="EUR" value={150} options={{ style: "code" }} />
<br/>
EUR, 1.491, round: "ceil" - <FormatCurrency currency="EUR" value={1.491} options={{ round: "ceil" }} />
</div>
);
}
});

return LocalizedCurrencies;

});
28 changes: 28 additions & 0 deletions examples/amd/src/components/dates.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
define([
"react",
"react-globalize"
], function(React, ReactGlobalize) {

var FormatDate = ReactGlobalize.FormatDate;

var LocalizedDates = React.createClass({
render: function() {
return (
<div>
default - <FormatDate value={new Date()} />
<br />
skeleton: "GyMMMd" - <FormatDate value={new Date()} options={{ skeleton: "GyMMMd" }} />
<br/>
date: "medium" - <FormatDate value={new Date()} options={{ date: "medium" }} />
<br/>
time: "medium" - <FormatDate value={new Date()} options={{ time: "medium" }} />
<br/>
datetime: "medium" - <FormatDate value={new Date()} options={{ datetime: "medium" }} />
</div>
);
}
});

return LocalizedDates;

});
55 changes: 55 additions & 0 deletions examples/amd/src/components/messages.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
define([
"react",
"react-globalize"
], function(React, ReactGlobalize) {

var FormatMessage = ReactGlobalize.FormatMessage;
var FormatNumber = ReactGlobalize.FormatNumber;

var LocalizedMessages = React.createClass({
render: function() {
return (
<div>
<h3>Simple Salutation</h3>
hi - <FormatMessage path="salutations/hi" />
<br/>
bye - <FormatMessage path="salutations/bye" />
<h3>Variable Replacement</h3>
["Wolfgang", "Amadeus", "Mozart"] - <FormatMessage path="variables/hello" variables={["Wolfgang", "Amadeus", "Mozart"]} />
<br/>
{JSON.stringify({first:"Wolfgang", middle:"Amadeus", last:"Mozart"})} - <FormatMessage path="variables/hey" variables={{first:"Wolfgang", middle:"Amadeus", last:"Mozart"}} />
<h3>Gender Inflection</h3>
{JSON.stringify({guest:"Mozart", guestGender:"male", host:"Beethoven", hostGender:"male"})} - <FormatMessage path="party" variables={{guest:"Mozart", guestGender:"male", host:"Beethoven", hostGender:"male"}} />
<br/>
{JSON.stringify({guest:"Mozart", guestGender:"male", host:"Mendelssohn", hostGender:"female"})} - <FormatMessage path="party" variables={{guest:"Mozart", guestGender:"male", host:"Mendelssohn", hostGender:"female"}} />
<br/>
{JSON.stringify({guest:"Mozart", guestGender:"male", host:"Beethoven", hostGender:"other"})} - <FormatMessage path="party" variables={{guest:"Mozart", guestGender:"male", host:"Beethoven", hostGender:"other"}} />
<br/>
{JSON.stringify({guest:"Mozart", guestGender:"other", host:"Beethoven", hostGender:"male"})} - <FormatMessage path="party" variables={{guest:"Mozart", guestGender:"other", host:"Mendelssohn", hostGender:"female"}} />
<br/>
{JSON.stringify({guest:"Mozart", guestGender:"other", host:"Beethoven", hostGender:"other"})} - <FormatMessage path="party" variables={{guest:"Mozart", guestGender:"other", host:"Beethoven", hostGender:"other"}} />
<h3>Plural Inflection</h3>
task count 1 - <FormatMessage path="task" variables={{count: 1}} />
<br/>
task count 0 - <FormatMessage path="task" variables={{count: 0}} />
<br/>
task count 1000 formatted - <FormatMessage ref="formattedTask" path="task" variables={{
count: 1000,
formattedCount: <FormatNumber value={1000} />
}} />
<br/>
like count 0 with offset:1 - <FormatMessage path="likeIncludingMe" variables={{count: 0}} />
<br/>
like count 1 with offset:1 - <FormatMessage path="likeIncludingMe" variables={{count: 1}} />
<br/>
like count 2 with offset:1 - <FormatMessage path="likeIncludingMe" variables={{count: 2}} />
<br/>
like count 3 with offset:1 - <FormatMessage path="likeIncludingMe" variables={{count: 3}} />
</div>
);
}
});

return LocalizedMessages;

});
28 changes: 28 additions & 0 deletions examples/amd/src/components/numbers.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
define([
"react",
"react-globalize"
], function(React, ReactGlobalize) {

var FormatNumber = ReactGlobalize.FormatNumber;

var LocalizedNumbers = React.createClass({
render: function() {
return (
<div>
pi, no options - <FormatNumber value={Math.PI} />
<br/>
pi, maximumFractionDigits: 5 - <FormatNumber value={Math.PI} options={{ maximumFractionDigits: 5 }} />
<br/>
pi, round: "floor" - <FormatNumber value={Math.PI} options={{ round: "floor" }} />
<br/>
10000, minimumFractionDigits: 2 - <FormatNumber value={10000} options={{ minimumFractionDigits: 2 }} />
<br/>
0.5, style: "percent" - <FormatNumber value={0.5} options={{ style: "percent" }} />
</div>
);
}
});

return LocalizedNumbers;

});
Loading