├── .babelrc
├── .editorconfig
├── .eslintignore
├── .eslintrc
├── .gitignore
├── .travis.yml
├── CHANGELOG.md
├── LICENSE.md
├── README.md
├── examples
├── .eslintrc
├── browser-basic
│ ├── .gitignore
│ ├── README.md
│ ├── package.json
│ ├── public
│ │ ├── favicon.ico
│ │ └── index.html
│ ├── src
│ │ ├── App.css
│ │ ├── App.js
│ │ ├── App.logic.js
│ │ ├── App.reducer.js
│ │ ├── App.test.js
│ │ ├── index.css
│ │ ├── index.js
│ │ ├── logo.svg
│ │ └── store.js
│ └── yarn.lock
├── buildAll.js
├── fullBuildAll.js
└── nodejs-basic
│ ├── index.js
│ └── package.json
├── package-lock.json
├── package.json
├── src
├── createMockStore.js
└── index.js
├── test
├── .eslintrc
├── createMockStore.spec.js
└── setup.js
└── webpack.config.js
/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | "plugins": [
3 | ["transform-es2015-template-literals", { "loose": true }],
4 | "transform-es2015-literals",
5 | "transform-es2015-function-name",
6 | "transform-es2015-arrow-functions",
7 | "transform-es2015-block-scoped-functions",
8 | ["transform-es2015-classes", { "loose": true }],
9 | "transform-es2015-object-super",
10 | "transform-es2015-shorthand-properties",
11 | ["transform-es2015-computed-properties", { "loose": true }],
12 | ["transform-es2015-for-of", { "loose": true }],
13 | "transform-es2015-sticky-regex",
14 | "transform-es2015-unicode-regex",
15 | "check-es2015-constants",
16 | ["transform-es2015-spread", { "loose": true }],
17 | "transform-es2015-parameters",
18 | ["transform-es2015-destructuring", { "loose": true }],
19 | "transform-es2015-block-scoping",
20 | "transform-object-rest-spread",
21 | "transform-es3-member-expression-literals",
22 | "transform-es3-property-literals"
23 | ],
24 | "env": {
25 | "commonjs": {
26 | "plugins": [
27 | ["transform-es2015-modules-commonjs", { "loose": true }]
28 | ]
29 | },
30 | "es": {
31 | "plugins": [
32 | ]
33 | },
34 | "test": {
35 | "plugins": [
36 | ["transform-es2015-modules-commonjs", { "loose": true }]
37 | ]
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/.editorconfig:
--------------------------------------------------------------------------------
1 | # EditorConfig helps developers define and maintain
2 | # consistent coding styles between different editors and IDEs.
3 |
4 | root = true
5 |
6 | [*]
7 | end_of_line = lf
8 | charset = utf-8
9 | trim_trailing_whitespace = true
10 | insert_final_newline = true
11 | indent_style = space
12 | indent_size = 2
13 |
14 | [*.md]
15 | trim_trailing_whitespace = false
16 |
--------------------------------------------------------------------------------
/.eslintignore:
--------------------------------------------------------------------------------
1 | **/dist/**
2 | **/build-es/**
3 | **/build-lib/**
4 | **/coverage/**
5 | **/node_modules/**
6 | **/server.js
7 | **/webpack.config*.js
8 | **/examples/**
9 |
--------------------------------------------------------------------------------
/.eslintrc:
--------------------------------------------------------------------------------
1 | {
2 | /* Also check test/.eslintrc for test specific config */
3 | "extends": "airbnb-base",
4 | "rules": {
5 | "arrow-body-style": [1, "as-needed"],
6 | "arrow-parens": 0,
7 | // don't require dangling commas
8 | "comma-dangle": 0,
9 | "consistent-return": 1,
10 | // causing issues with codacy
11 | "import/no-unresolved": 0,
12 | "indent": 0,
13 | "no-nested-ternary": 0,
14 | "no-plusplus": 0,
15 | "no-shadow": 0,
16 | // don't require functions to be declared before use
17 | "no-use-before-define": [2, { "functions": false, "classes": true }],
18 | // Disable until Flow supports let and const
19 | "no-var": 0,
20 | "object-property-newline": 0,
21 | "padded-blocks": 0,
22 | "valid-jsdoc": 2
23 | },
24 | "parserOptions": {
25 | "ecmaFeatures": {
26 | "experimentalObjectRestSpread": true
27 | }
28 | },
29 | "plugins": [
30 | ]
31 | }
32 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | *.log
3 | .nyc_output
4 | node_modules
5 | dist
6 | build-lib
7 | build-es
8 | coverage
9 | _book
10 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 | node_js:
3 | - "6"
4 | - "8"
5 | - "10"
6 | - node
7 | script:
8 | - npm run check:src
9 | - npm run build
10 | - npm run build:examples
11 | branches:
12 | except:
13 | - experimental
14 | cache:
15 | directories:
16 | - $HOME/.npm
17 | - examples/browser-basic/node_modules
18 | - examples/nodejs-basic/node_modules
19 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Change Log
2 |
3 | This project adheres to [Semantic Versioning](http://semver.org/).
4 | Every release, along with the migration instructions, is documented on the Github [Releases](https://github.com/jeffbski/redux-logic-test/releases) page.
5 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2017 Jeff Barczewski
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6 |
7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8 |
9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # redux-logic-test - redux-logic test utilities
2 |
3 | > "Simplifying testing with redux-logic"
4 |
5 | Utilities:
6 |
7 | - `createMockStore` - create a redux-logic middleware and a redux store, attaching the middleware and providing a mechanism to verify the dispatched actions
8 |
9 | [](http://travis-ci.org/jeffbski/redux-logic-test) [](https://snyk.io/test/github/jeffbski/redux-logic-test) [](https://www.npmjs.com/package/redux-logic-test)
10 |
11 | ## Installation
12 |
13 | redux-logic-test has peerDependencies of redux and redux-logic (which also needs rxjs)
14 |
15 | ```bash
16 | npm install rxjs --save
17 | npm install redux-logic --save
18 | npm install redux --save
19 | npm install redux-logic-test --save-dev
20 | ```
21 |
22 | ### ES6 module import
23 |
24 | ```js
25 | import { createMockStore } from 'redux-logic-test';
26 | ```
27 |
28 | ### Commonjs
29 |
30 | ```js
31 | const createMockStore = require('redux-logic-test').default.createMockStore;
32 | ```
33 |
34 | ### UMD/CDN use from script tags
35 |
36 | The UMD build is mainly used for using in online playgrounds like jsfiddle.
37 |
38 | ```html
39 |
40 |
41 |
42 |
47 | ```
48 |
49 | ## Usage
50 |
51 | ```js
52 | import { createMockStore } from 'redux-logic-test';
53 |
54 | // specify as much as necessary for your particular test
55 | const store = createMockStore({
56 | initialState: optionalObject,
57 | reducer: optionalFn, // default: identity reducer
58 | logic: optionalLogic, // default: []
59 | injectedDeps: optionalObject, // default {}
60 | middleware: optionalArr // other mw, exclude logicMiddleware
61 | });
62 |
63 | store.dispatch(...) // use as necessary for your test
64 |
65 | // when all inflight logic has all completed calls fn + returns promise
66 | store.whenComplete(fn) - shorthand for store.logicMiddleware.whenComplete(fn)
67 |
68 | store.actions - the actions dispatched, use store.resetActions() to clear
69 | store.resetActions() - clear store.actions
70 |
71 | // access the logicMiddleware created for logic/injectedDeps props
72 | // use addLogic, mergeNewLogic, replaceLogic, whenComplete, monitor$
73 | store.logicMiddleware
74 | ```
75 |
76 | ## Goals
77 |
78 | - simplify the creation of a testing redux store with logicMiddleware attached
79 | - add built-in middleware to track actions that are dispatched
80 | - make it easy to verify the actions that were dispatched
81 |
82 | ## Quick example
83 |
84 | ```js
85 | import { createMockStore } from 'redux-logic-test';
86 | import { createLogic } from 'redux-logic';
87 |
88 | const fooLogic = createLogic({
89 | type: 'FOO',
90 | process({ API, getState, action }, dispatch, done) {
91 | API.get(...)
92 | .then(results => {
93 | dispatch({ type: 'FOO_SUCCESS', payload: results });
94 | done();
95 | });
96 | }
97 | });
98 |
99 | const logic = [fooLogic]; // array of logic to use/test
100 | const injectedDeps = { // include what is needed for logic
101 | API: api // could include mocked API for easy testing
102 | };
103 |
104 | const initialState = {}; // optionally set
105 | const reducer = (state, action) => { return state; }; // optional
106 |
107 | const store = createMockStore({
108 | initialState,
109 | reducer,
110 | logic,
111 | injectedDeps
112 | });
113 |
114 | store.dispatch({ type: 'FOO' }); // kick off fetching
115 | store.dispatch({ type: 'BAR' }); // other dispatches
116 | store.whenComplete(() => { // runs this fn when all logic is complete
117 | expect(store.getState()).toEqual({...});
118 | expect(store.actions).toEqual([
119 | { type: 'FOO' },
120 | { type: 'BAR' },
121 | { type: 'FOO_SUCCESS', payload: [...] }
122 | ]);
123 | // if desired, can reset the actions for more tests
124 | // store.resetActions(); // clear for more tests
125 |
126 | // be sure to return the whenComplete promise to your test
127 | // or if using a done cb, call it to indicate that your async
128 | // test is finished
129 | });
130 | ```
131 |
132 | ## Examples
133 |
134 | ### Live examples
135 |
136 | - [basic usage](https://jsfiddle.net/jeffbski/w3k5t83x/) - simple use or createMockStore to test actions that were dispatched (jsfiddle)
137 | - [async search](https://jsfiddle.net/jeffbski/a2cd2h96/) - async search using createMockStore to setup a test store (jsfiddle)
138 |
139 | ### Full examples
140 |
141 | - [browser-basic](./examples/browser-basic/src/App.test.js) - basic example of using createMockStore to test logic
142 | - [nodejs-basic](./examples/nodejs-basic/index.js) - simple Node.js example using createMockStore via Commonjs to test logic
143 |
144 |
145 | ## Get involved
146 |
147 | If you have input or ideas or would like to get involved, you may:
148 |
149 | - contact me via twitter @jeffbski -
150 | - open an issue on github to begin a discussion -
151 | - fork the repo and send a pull request (ideally with tests) -
152 |
153 | ## Supporters
154 |
155 | This project is supported by [CodeWinds Training](https://codewinds.com/)
156 |
157 |
158 |
159 |
160 | ## License - MIT
161 |
162 | - [MIT license](http://github.com/jeffbski/redux-logic-test/raw/master/LICENSE.md)
163 |
--------------------------------------------------------------------------------
/examples/.eslintrc:
--------------------------------------------------------------------------------
1 | {
2 | "env": {
3 | "mocha": true
4 | },
5 | "rules": {
6 | "import/no-extraneous-dependencies": 0
7 | }
8 | }
--------------------------------------------------------------------------------
/examples/browser-basic/.gitignore:
--------------------------------------------------------------------------------
1 | # See http://help.github.com/ignore-files/ for more about ignoring files.
2 |
3 | # dependencies
4 | /node_modules
5 |
6 | # testing
7 | /coverage
8 |
9 | # production
10 | /build
11 |
12 | # misc
13 | .DS_Store
14 | .env
15 | npm-debug.log*
16 | yarn-debug.log*
17 | yarn-error.log*
18 |
19 |
--------------------------------------------------------------------------------
/examples/browser-basic/README.md:
--------------------------------------------------------------------------------
1 | This project was bootstrapped with [Create React App](https://github.com/facebookincubator/create-react-app).
2 |
3 | Below you will find some information on how to perform common tasks.
4 | You can find the most recent version of this guide [here](https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md).
5 |
6 | ## Table of Contents
7 |
8 | - [Updating to New Releases](#updating-to-new-releases)
9 | - [Sending Feedback](#sending-feedback)
10 | - [Folder Structure](#folder-structure)
11 | - [Available Scripts](#available-scripts)
12 | - [npm start](#npm-start)
13 | - [npm test](#npm-test)
14 | - [npm run build](#npm-run-build)
15 | - [npm run eject](#npm-run-eject)
16 | - [Syntax Highlighting in the Editor](#syntax-highlighting-in-the-editor)
17 | - [Displaying Lint Output in the Editor](#displaying-lint-output-in-the-editor)
18 | - [Changing the Page ``](#changing-the-page-title)
19 | - [Installing a Dependency](#installing-a-dependency)
20 | - [Importing a Component](#importing-a-component)
21 | - [Adding a Stylesheet](#adding-a-stylesheet)
22 | - [Post-Processing CSS](#post-processing-css)
23 | - [Adding Images and Fonts](#adding-images-and-fonts)
24 | - [Using the `public` Folder](#using-the-public-folder)
25 | - [Changing the HTML](#changing-the-html)
26 | - [Adding Assets Outside of the Module System](#adding-assets-outside-of-the-module-system)
27 | - [When to Use the `public` Folder](#when-to-use-the-public-folder)
28 | - [Using Global Variables](#using-global-variables)
29 | - [Adding Bootstrap](#adding-bootstrap)
30 | - [Adding Flow](#adding-flow)
31 | - [Adding Custom Environment Variables](#adding-custom-environment-variables)
32 | - [Can I Use Decorators?](#can-i-use-decorators)
33 | - [Integrating with an API Backend](#integrating-with-an-api-backend)
34 | - [Node](#node)
35 | - [Ruby on Rails](#ruby-on-rails)
36 | - [Proxying API Requests in Development](#proxying-api-requests-in-development)
37 | - [Using HTTPS in Development](#using-https-in-development)
38 | - [Generating Dynamic `` Tags on the Server](#generating-dynamic-meta-tags-on-the-server)
39 | - [Running Tests](#running-tests)
40 | - [Filename Conventions](#filename-conventions)
41 | - [Command Line Interface](#command-line-interface)
42 | - [Version Control Integration](#version-control-integration)
43 | - [Writing Tests](#writing-tests)
44 | - [Testing Components](#testing-components)
45 | - [Using Third Party Assertion Libraries](#using-third-party-assertion-libraries)
46 | - [Initializing Test Environment](#initializing-test-environment)
47 | - [Focusing and Excluding Tests](#focusing-and-excluding-tests)
48 | - [Coverage Reporting](#coverage-reporting)
49 | - [Continuous Integration](#continuous-integration)
50 | - [Disabling jsdom](#disabling-jsdom)
51 | - [Snapshot Testing](#snapshot-testing)
52 | - [Editor Integration](#editor-integration)
53 | - [Developing Components in Isolation](#developing-components-in-isolation)
54 | - [Making a Progressive Web App](#making-a-progressive-web-app)
55 | - [Deployment](#deployment)
56 | - [Serving Apps with Client-Side Routing](#serving-apps-with-client-side-routing)
57 | - [Building for Relative Paths](#building-for-relative-paths)
58 | - [Firebase](#firebase)
59 | - [GitHub Pages](#github-pages)
60 | - [Heroku](#heroku)
61 | - [Modulus](#modulus)
62 | - [Netlify](#netlify)
63 | - [Now](#now)
64 | - [S3 and CloudFront](#s3-and-cloudfront)
65 | - [Surge](#surge)
66 | - [Advanced Configuration](#advanced-configuration)
67 | - [Troubleshooting](#troubleshooting)
68 | - [`npm start` doesn’t detect changes](#npm-start-doesnt-detect-changes)
69 | - [`npm test` hangs on macOS Sierra](#npm-test-hangs-on-macos-sierra)
70 | - [`npm run build` silently fails](#npm-run-build-silently-fails)
71 | - [`npm run build` fails on Heroku](#npm-run-build-fails-on-heroku)
72 | - [Something Missing?](#something-missing)
73 |
74 | ## Updating to New Releases
75 |
76 | Create React App is divided into two packages:
77 |
78 | * `create-react-app` is a global command-line utility that you use to create new projects.
79 | * `react-scripts` is a development dependency in the generated projects (including this one).
80 |
81 | You almost never need to update `create-react-app` itself: it delegates all the setup to `react-scripts`.
82 |
83 | When you run `create-react-app`, it always creates the project with the latest version of `react-scripts` so you’ll get all the new features and improvements in newly created apps automatically.
84 |
85 | To update an existing project to a new version of `react-scripts`, [open the changelog](https://github.com/facebookincubator/create-react-app/blob/master/CHANGELOG.md), find the version you’re currently on (check `package.json` in this folder if you’re not sure), and apply the migration instructions for the newer versions.
86 |
87 | In most cases bumping the `react-scripts` version in `package.json` and running `npm install` in this folder should be enough, but it’s good to consult the [changelog](https://github.com/facebookincubator/create-react-app/blob/master/CHANGELOG.md) for potential breaking changes.
88 |
89 | We commit to keeping the breaking changes minimal so you can upgrade `react-scripts` painlessly.
90 |
91 | ## Sending Feedback
92 |
93 | We are always open to [your feedback](https://github.com/facebookincubator/create-react-app/issues).
94 |
95 | ## Folder Structure
96 |
97 | After creation, your project should look like this:
98 |
99 | ```
100 | my-app/
101 | README.md
102 | node_modules/
103 | package.json
104 | public/
105 | index.html
106 | favicon.ico
107 | src/
108 | App.css
109 | App.js
110 | App.test.js
111 | index.css
112 | index.js
113 | logo.svg
114 | ```
115 |
116 | For the project to build, **these files must exist with exact filenames**:
117 |
118 | * `public/index.html` is the page template;
119 | * `src/index.js` is the JavaScript entry point.
120 |
121 | You can delete or rename the other files.
122 |
123 | You may create subdirectories inside `src`. For faster rebuilds, only files inside `src` are processed by Webpack.
124 | You need to **put any JS and CSS files inside `src`**, or Webpack won’t see them.
125 |
126 | Only files inside `public` can be used from `public/index.html`.
127 | Read instructions below for using assets from JavaScript and HTML.
128 |
129 | You can, however, create more top-level directories.
130 | They will not be included in the production build so you can use them for things like documentation.
131 |
132 | ## Available Scripts
133 |
134 | In the project directory, you can run:
135 |
136 | ### `npm start`
137 |
138 | Runs the app in the development mode.
139 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
140 |
141 | The page will reload if you make edits.
142 | You will also see any lint errors in the console.
143 |
144 | ### `npm test`
145 |
146 | Launches the test runner in the interactive watch mode.
147 | See the section about [running tests](#running-tests) for more information.
148 |
149 | ### `npm run build`
150 |
151 | Builds the app for production to the `build` folder.
152 | It correctly bundles React in production mode and optimizes the build for the best performance.
153 |
154 | The build is minified and the filenames include the hashes.
155 | Your app is ready to be deployed!
156 |
157 | See the section about [deployment](#deployment) for more information.
158 |
159 | ### `npm run eject`
160 |
161 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!**
162 |
163 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
164 |
165 | Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
166 |
167 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
168 |
169 | ## Syntax Highlighting in the Editor
170 |
171 | To configure the syntax highlighting in your favorite text editor, head to the [relevant Babel documentation page](https://babeljs.io/docs/editors) and follow the instructions. Some of the most popular editors are covered.
172 |
173 | ## Displaying Lint Output in the Editor
174 |
175 | >Note: this feature is available with `react-scripts@0.2.0` and higher.
176 |
177 | Some editors, including Sublime Text, Atom, and Visual Studio Code, provide plugins for ESLint.
178 |
179 | They are not required for linting. You should see the linter output right in your terminal as well as the browser console. However, if you prefer the lint results to appear right in your editor, there are some extra steps you can do.
180 |
181 | You would need to install an ESLint plugin for your editor first.
182 |
183 | >**A note for Atom `linter-eslint` users**
184 |
185 | >If you are using the Atom `linter-eslint` plugin, make sure that **Use global ESLint installation** option is checked:
186 |
187 | >
188 |
189 |
190 | >**For Visual Studio Code users**
191 |
192 | >VS Code ESLint plugin automatically detects Create React App's configuration file. So you do not need to create `eslintrc.json` at the root directory, except when you want to add your own rules. In that case, you should include CRA's config by adding this line:
193 |
194 | >```js
195 | {
196 | // ...
197 | "extends": "react-app"
198 | }
199 | ```
200 |
201 | Then add this block to the `package.json` file of your project:
202 |
203 | ```js
204 | {
205 | // ...
206 | "eslintConfig": {
207 | "extends": "react-app"
208 | }
209 | }
210 | ```
211 |
212 | Finally, you will need to install some packages *globally*:
213 |
214 | ```sh
215 | npm install -g eslint-config-react-app@0.3.0 eslint@3.8.1 babel-eslint@7.0.0 eslint-plugin-react@6.4.1 eslint-plugin-import@2.0.1 eslint-plugin-jsx-a11y@2.2.3 eslint-plugin-flowtype@2.21.0
216 | ```
217 |
218 | We recognize that this is suboptimal, but it is currently required due to the way we hide the ESLint dependency. The ESLint team is already [working on a solution to this](https://github.com/eslint/eslint/issues/3458) so this may become unnecessary in a couple of months.
219 |
220 | ## Changing the Page ``
221 |
222 | You can find the source HTML file in the `public` folder of the generated project. You may edit the `` tag in it to change the title from “React App” to anything else.
223 |
224 | Note that normally you wouldn't edit files in the `public` folder very often. For example, [adding a stylesheet](#adding-a-stylesheet) is done without touching the HTML.
225 |
226 | If you need to dynamically update the page title based on the content, you can use the browser [`document.title`](https://developer.mozilla.org/en-US/docs/Web/API/Document/title) API. For more complex scenarios when you want to change the title from React components, you can use [React Helmet](https://github.com/nfl/react-helmet), a third party library.
227 |
228 | Finally, if you use a custom server for your app in production and want to modify the title before it gets sent to the browser, you can follow advice in [this section](#generating-dynamic-meta-tags-on-the-server).
229 |
230 | ## Installing a Dependency
231 |
232 | The generated project includes React and ReactDOM as dependencies. It also includes a set of scripts used by Create React App as a development dependency. You may install other dependencies (for example, React Router) with `npm`:
233 |
234 | ```
235 | npm install --save
236 | ```
237 |
238 | ## Importing a Component
239 |
240 | This project setup supports ES6 modules thanks to Babel.
241 | While you can still use `require()` and `module.exports`, we encourage you to use [`import` and `export`](http://exploringjs.com/es6/ch_modules.html) instead.
242 |
243 | For example:
244 |
245 | ### `Button.js`
246 |
247 | ```js
248 | import React, { Component } from 'react';
249 |
250 | class Button extends Component {
251 | render() {
252 | // ...
253 | }
254 | }
255 |
256 | export default Button; // Don’t forget to use export default!
257 | ```
258 |
259 | ### `DangerButton.js`
260 |
261 |
262 | ```js
263 | import React, { Component } from 'react';
264 | import Button from './Button'; // Import a component from another file
265 |
266 | class DangerButton extends Component {
267 | render() {
268 | return ;
269 | }
270 | }
271 |
272 | export default DangerButton;
273 | ```
274 |
275 | Be aware of the [difference between default and named exports](http://stackoverflow.com/questions/36795819/react-native-es-6-when-should-i-use-curly-braces-for-import/36796281#36796281). It is a common source of mistakes.
276 |
277 | We suggest that you stick to using default imports and exports when a module only exports a single thing (for example, a component). That’s what you get when you use `export default Button` and `import Button from './Button'`.
278 |
279 | Named exports are useful for utility modules that export several functions. A module may have at most one default export and as many named exports as you like.
280 |
281 | Learn more about ES6 modules:
282 |
283 | * [When to use the curly braces?](http://stackoverflow.com/questions/36795819/react-native-es-6-when-should-i-use-curly-braces-for-import/36796281#36796281)
284 | * [Exploring ES6: Modules](http://exploringjs.com/es6/ch_modules.html)
285 | * [Understanding ES6: Modules](https://leanpub.com/understandinges6/read#leanpub-auto-encapsulating-code-with-modules)
286 |
287 | ## Adding a Stylesheet
288 |
289 | This project setup uses [Webpack](https://webpack.github.io/) for handling all assets. Webpack offers a custom way of “extending” the concept of `import` beyond JavaScript. To express that a JavaScript file depends on a CSS file, you need to **import the CSS from the JavaScript file**:
290 |
291 | ### `Button.css`
292 |
293 | ```css
294 | .Button {
295 | padding: 20px;
296 | }
297 | ```
298 |
299 | ### `Button.js`
300 |
301 | ```js
302 | import React, { Component } from 'react';
303 | import './Button.css'; // Tell Webpack that Button.js uses these styles
304 |
305 | class Button extends Component {
306 | render() {
307 | // You can use them as regular CSS styles
308 | return ;
309 | }
310 | }
311 | ```
312 |
313 | **This is not required for React** but many people find this feature convenient. You can read about the benefits of this approach [here](https://medium.com/seek-ui-engineering/block-element-modifying-your-javascript-components-d7f99fcab52b). However you should be aware that this makes your code less portable to other build tools and environments than Webpack.
314 |
315 | In development, expressing dependencies this way allows your styles to be reloaded on the fly as you edit them. In production, all CSS files will be concatenated into a single minified `.css` file in the build output.
316 |
317 | If you are concerned about using Webpack-specific semantics, you can put all your CSS right into `src/index.css`. It would still be imported from `src/index.js`, but you could always remove that import if you later migrate to a different build tool.
318 |
319 | ## Post-Processing CSS
320 |
321 | This project setup minifies your CSS and adds vendor prefixes to it automatically through [Autoprefixer](https://github.com/postcss/autoprefixer) so you don’t need to worry about it.
322 |
323 | For example, this:
324 |
325 | ```css
326 | .App {
327 | display: flex;
328 | flex-direction: row;
329 | align-items: center;
330 | }
331 | ```
332 |
333 | becomes this:
334 |
335 | ```css
336 | .App {
337 | display: -webkit-box;
338 | display: -ms-flexbox;
339 | display: flex;
340 | -webkit-box-orient: horizontal;
341 | -webkit-box-direction: normal;
342 | -ms-flex-direction: row;
343 | flex-direction: row;
344 | -webkit-box-align: center;
345 | -ms-flex-align: center;
346 | align-items: center;
347 | }
348 | ```
349 |
350 | There is currently no support for preprocessors such as Less, or for sharing variables across CSS files.
351 |
352 | ## Adding Images and Fonts
353 |
354 | With Webpack, using static assets like images and fonts works similarly to CSS.
355 |
356 | You can **`import` an image right in a JavaScript module**. This tells Webpack to include that image in the bundle. Unlike CSS imports, importing an image or a font gives you a string value. This value is the final image path you can reference in your code.
357 |
358 | Here is an example:
359 |
360 | ```js
361 | import React from 'react';
362 | import logo from './logo.png'; // Tell Webpack this JS file uses this image
363 |
364 | console.log(logo); // /logo.84287d09.png
365 |
366 | function Header() {
367 | // Import result is the URL of your image
368 | return ;
369 | }
370 |
371 | export default Header;
372 | ```
373 |
374 | This ensures that when the project is built, Webpack will correctly move the images into the build folder, and provide us with correct paths.
375 |
376 | This works in CSS too:
377 |
378 | ```css
379 | .Logo {
380 | background-image: url(./logo.png);
381 | }
382 | ```
383 |
384 | Webpack finds all relative module references in CSS (they start with `./`) and replaces them with the final paths from the compiled bundle. If you make a typo or accidentally delete an important file, you will see a compilation error, just like when you import a non-existent JavaScript module. The final filenames in the compiled bundle are generated by Webpack from content hashes. If the file content changes in the future, Webpack will give it a different name in production so you don’t need to worry about long-term caching of assets.
385 |
386 | Please be advised that this is also a custom feature of Webpack.
387 |
388 | **It is not required for React** but many people enjoy it (and React Native uses a similar mechanism for images).
389 | An alternative way of handling static assets is described in the next section.
390 |
391 | ## Using the `public` Folder
392 |
393 | >Note: this feature is available with `react-scripts@0.5.0` and higher.
394 |
395 | ### Changing the HTML
396 |
397 | The `public` folder contains the HTML file so you can tweak it, for example, to [set the page title](#changing-the-page-title).
398 | The `