├── .dockerignore
├── .gitignore
├── Dockerfile
├── LICENSE
├── README.md
├── app.py
├── front
├── .gitignore
├── README.md
├── package.json
├── public
│ ├── favicon.png
│ └── index.html
└── src
│ ├── App.css
│ ├── App.js
│ ├── App.test.js
│ ├── index.css
│ ├── index.js
│ ├── logo.svg
│ └── snippets.js
├── parse.py
└── requirements.txt
/.dockerignore:
--------------------------------------------------------------------------------
1 | front/node_modules
2 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.pyc
2 | **/node_modules
3 | VENV
4 | npm-debug.log
5 |
--------------------------------------------------------------------------------
/Dockerfile:
--------------------------------------------------------------------------------
1 | FROM python:2.7-alpine
2 |
3 | WORKDIR /opt/pyast
4 | ADD . /opt/pyast
5 |
6 | RUN apk update && \
7 | apk add g++ make && \
8 | apk add nodejs && \
9 | pip install -r requirements.txt && \
10 | cd /opt/pyast/front && \
11 | npm set progress=false && \
12 | npm install && \
13 | npm run build
14 |
15 | EXPOSE 4361
16 |
17 | CMD ["gunicorn", "app:app", "-w" "4", "-b", "'0.0.0.0:4361'"]
18 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # python-ast-explorer - a simple AST visualizer
2 |
3 | The illustrous code behind [python-ast-explorer.com](https://python-ast-explorer.com), you can touch it.
4 |
5 | ## Making sense of it
6 |
7 | See the `Dockerfile` for steps to get it up and running. It's basically a pretty bare `react-create-app` artifact that talks to a hacked up Flask app via repeated, desperate calls to `/api/_parse`.
8 |
--------------------------------------------------------------------------------
/app.py:
--------------------------------------------------------------------------------
1 | from flask import Flask
2 | from flask import request
3 | from flask import jsonify
4 | from flask import send_file
5 |
6 | from parse import make_ast
7 |
8 | app = Flask(__name__, static_folder='front/build/static')
9 |
10 | @app.route('/')
11 | def index():
12 | return send_file('front/build/index.html')
13 |
14 | @app.route('/favicon.ico')
15 | def favicon_ico():
16 | return send_file('front/build/favicon.png')
17 |
18 | @app.route('/api/_parse', methods=['POST'])
19 | def api_parse():
20 | body = request.get_data()
21 | try:
22 | return jsonify({ 'ast': make_ast(body) })
23 | except Exception as e:
24 | return jsonify({ 'ast': { 'error': 'yes', 'why_error?': 'it doesn\'t compile, yo' }})
25 |
26 | @app.after_request
27 | def after_request(response):
28 | response.headers.add('Access-Control-Allow-Origin', '*')
29 | response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
30 | response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
31 | return response
32 |
33 | if __name__ == "__main__":
34 | app.debug = True
35 | app.run('0.0.0.0', 4361)
36 |
--------------------------------------------------------------------------------
/front/.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 |
--------------------------------------------------------------------------------
/front/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 | - [Displaying Lint Output in the Editor](#displaying-lint-output-in-the-editor)
17 | - [Installing a Dependency](#installing-a-dependency)
18 | - [Importing a Component](#importing-a-component)
19 | - [Adding a Stylesheet](#adding-a-stylesheet)
20 | - [Post-Processing CSS](#post-processing-css)
21 | - [Adding Images and Fonts](#adding-images-and-fonts)
22 | - [Using the `public` Folder](#using-the-public-folder)
23 | - [Adding Bootstrap](#adding-bootstrap)
24 | - [Adding Flow](#adding-flow)
25 | - [Adding Custom Environment Variables](#adding-custom-environment-variables)
26 | - [Can I Use Decorators?](#can-i-use-decorators)
27 | - [Integrating with a Node Backend](#integrating-with-a-node-backend)
28 | - [Proxying API Requests in Development](#proxying-api-requests-in-development)
29 | - [Using HTTPS in Development](#using-https-in-development)
30 | - [Generating Dynamic `` Tags on the Server](#generating-dynamic-meta-tags-on-the-server)
31 | - [Running Tests](#running-tests)
32 | - [Filename Conventions](#filename-conventions)
33 | - [Command Line Interface](#command-line-interface)
34 | - [Version Control Integration](#version-control-integration)
35 | - [Writing Tests](#writing-tests)
36 | - [Testing Components](#testing-components)
37 | - [Using Third Party Assertion Libraries](#using-third-party-assertion-libraries)
38 | - [Initializing Test Environment](#initializing-test-environment)
39 | - [Focusing and Excluding Tests](#focusing-and-excluding-tests)
40 | - [Coverage Reporting](#coverage-reporting)
41 | - [Continuous Integration](#continuous-integration)
42 | - [Disabling jsdom](#disabling-jsdom)
43 | - [Experimental Snapshot Testing](#experimental-snapshot-testing)
44 | - [Deployment](#deployment)
45 | - [Building for Relative Paths](#building-for-relative-paths)
46 | - [GitHub Pages](#github-pages)
47 | - [Heroku](#heroku)
48 | - [Modulus](#modulus)
49 | - [Now](#now)
50 | - [Surge](#surge)
51 | - [Something Missing?](#something-missing)
52 |
53 | ## Updating to New Releases
54 |
55 | Create React App is divided into two packages:
56 |
57 | * `create-react-app` is a global command-line utility that you use to create new projects.
58 | * `react-scripts` is a development dependency in the generated projects (including this one).
59 |
60 | You almost never need to update `create-react-app` itself: it’s delegates all the setup to `react-scripts`.
61 |
62 | 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.
63 |
64 | 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.
65 |
66 | 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.
67 |
68 | We commit to keeping the breaking changes minimal so you can upgrade `react-scripts` painlessly.
69 |
70 | ## Sending Feedback
71 |
72 | We are always open to [your feedback](https://github.com/facebookincubator/create-react-app/issues).
73 |
74 | ## Folder Structure
75 |
76 | After creation, your project should look like this:
77 |
78 | ```
79 | my-app/
80 | README.md
81 | node_modules/
82 | package.json
83 | public/
84 | index.html
85 | favicon.ico
86 | src/
87 | App.css
88 | App.js
89 | App.test.js
90 | index.css
91 | index.js
92 | logo.svg
93 | ```
94 |
95 | For the project to build, **these files must exist with exact filenames**:
96 |
97 | * `public/index.html` is the page template;
98 | * `src/index.js` is the JavaScript entry point.
99 |
100 | You can delete or rename the other files.
101 |
102 | You may create subdirectories inside `src`. For faster rebuilds, only files inside `src` are processed by Webpack.
103 | You need to **put any JS and CSS files inside `src`**, or Webpack won’t see them.
104 |
105 | Only files inside `public` can be used from `public/index.html`.
106 | Read instructions below for using assets from JavaScript and HTML.
107 |
108 | You can, however, create more top-level directories.
109 | They will not be included in the production build so you can use them for things like documentation.
110 |
111 | ## Available Scripts
112 |
113 | In the project directory, you can run:
114 |
115 | ### `npm start`
116 |
117 | Runs the app in the development mode.
118 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
119 |
120 | The page will reload if you make edits.
121 | You will also see any lint errors in the console.
122 |
123 | ### `npm test`
124 |
125 | Launches the test runner in the interactive watch mode.
126 | See the section about [running tests](#running-tests) for more information.
127 |
128 | ### `npm run build`
129 |
130 | Builds the app for production to the `build` folder.
131 | It correctly bundles React in production mode and optimizes the build for the best performance.
132 |
133 | The build is minified and the filenames include the hashes.
134 | Your app is ready to be deployed!
135 |
136 | ### `npm run eject`
137 |
138 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!**
139 |
140 | 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.
141 |
142 | 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.
143 |
144 | 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.
145 |
146 | ## Displaying Lint Output in the Editor
147 |
148 | >Note: this feature is available with `react-scripts@0.2.0` and higher.
149 |
150 | Some editors, including Sublime Text, Atom, and Visual Studio Code, provide plugins for ESLint.
151 |
152 | 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.
153 |
154 | You would need to install an ESLint plugin for your editor first.
155 |
156 | >**A note for Atom `linter-eslint` users**
157 |
158 | >If you are using the Atom `linter-eslint` plugin, make sure that **Use global ESLint installation** option is checked:
159 |
160 | >
161 |
162 | Then add this block to the `package.json` file of your project:
163 |
164 | ```js
165 | {
166 | // ...
167 | "eslintConfig": {
168 | "extends": "react-app"
169 | }
170 | }
171 | ```
172 |
173 | Finally, you will need to install some packages *globally*:
174 |
175 | ```sh
176 | npm install -g eslint-config-react-app@0.2.1 eslint@3.5.0 babel-eslint@6.1.2 eslint-plugin-react@6.3.0 eslint-plugin-import@1.12.0 eslint-plugin-jsx-a11y@2.2.2 eslint-plugin-flowtype@2.18.1
177 | ```
178 |
179 | 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.
180 |
181 | ## Installing a Dependency
182 |
183 | 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`:
184 |
185 | ```
186 | npm install --save
187 | ```
188 |
189 | ## Importing a Component
190 |
191 | This project setup supports ES6 modules thanks to Babel.
192 | 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.
193 |
194 | For example:
195 |
196 | ### `Button.js`
197 |
198 | ```js
199 | import React, { Component } from 'react';
200 |
201 | class Button extends Component {
202 | render() {
203 | // ...
204 | }
205 | }
206 |
207 | export default Button; // Don’t forget to use export default!
208 | ```
209 |
210 | ### `DangerButton.js`
211 |
212 |
213 | ```js
214 | import React, { Component } from 'react';
215 | import Button from './Button'; // Import a component from another file
216 |
217 | class DangerButton extends Component {
218 | render() {
219 | return ;
220 | }
221 | }
222 |
223 | export default DangerButton;
224 | ```
225 |
226 | 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.
227 |
228 | 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'`.
229 |
230 | 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.
231 |
232 | Learn more about ES6 modules:
233 |
234 | * [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)
235 | * [Exploring ES6: Modules](http://exploringjs.com/es6/ch_modules.html)
236 | * [Understanding ES6: Modules](https://leanpub.com/understandinges6/read#leanpub-auto-encapsulating-code-with-modules)
237 |
238 | ## Adding a Stylesheet
239 |
240 | 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**:
241 |
242 | ### `Button.css`
243 |
244 | ```css
245 | .Button {
246 | padding: 20px;
247 | }
248 | ```
249 |
250 | ### `Button.js`
251 |
252 | ```js
253 | import React, { Component } from 'react';
254 | import './Button.css'; // Tell Webpack that Button.js uses these styles
255 |
256 | class Button extends Component {
257 | render() {
258 | // You can use them as regular CSS styles
259 | return ;
260 | }
261 | }
262 | ```
263 |
264 | **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.
265 |
266 | 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.
267 |
268 | 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.
269 |
270 | ## Post-Processing CSS
271 |
272 | 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.
273 |
274 | For example, this:
275 |
276 | ```css
277 | .App {
278 | display: flex;
279 | flex-direction: row;
280 | align-items: center;
281 | }
282 | ```
283 |
284 | becomes this:
285 |
286 | ```css
287 | .App {
288 | display: -webkit-box;
289 | display: -ms-flexbox;
290 | display: flex;
291 | -webkit-box-orient: horizontal;
292 | -webkit-box-direction: normal;
293 | -ms-flex-direction: row;
294 | flex-direction: row;
295 | -webkit-box-align: center;
296 | -ms-flex-align: center;
297 | align-items: center;
298 | }
299 | ```
300 |
301 | There is currently no support for preprocessors such as Less, or for sharing variables across CSS files.
302 |
303 | ## Adding Images and Fonts
304 |
305 | With Webpack, using static assets like images and fonts works similarly to CSS.
306 |
307 | 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.
308 |
309 | Here is an example:
310 |
311 | ```js
312 | import React from 'react';
313 | import logo from './logo.png'; // Tell Webpack this JS file uses this image
314 |
315 | console.log(logo); // /logo.84287d09.png
316 |
317 | function Header() {
318 | // Import result is the URL of your image
319 | return ;
320 | }
321 |
322 | export default function Header;
323 | ```
324 |
325 | This ensures that when the project is built, webpack will correctly move the images into the build folder, and provide us with correct paths.
326 |
327 | This works in CSS too:
328 |
329 | ```css
330 | .Logo {
331 | background-image: url(./logo.png);
332 | }
333 | ```
334 |
335 | 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.
336 |
337 | Please be advised that this is also a custom feature of Webpack.
338 |
339 | **It is not required for React** but many people enjoy it (and React Native uses a similar mechanism for images).
340 | An alternative way of handling static assets is described in the next section.
341 |
342 | ## Using the `public` Folder
343 |
344 | >Note: this feature is available with `react-scripts@0.5.0` and higher.
345 |
346 | Normally we encourage you to `import` assets in JavaScript files as described above. This mechanism provides a number of benefits:
347 |
348 | * Scripts and stylesheets get minified and bundled together to avoid extra network requests.
349 | * Missing files cause compilation errors instead of 404 errors for your users.
350 | * Result filenames include content hashes so you don’t need to worry about browsers caching their old versions.
351 |
352 | However there is an **escape hatch** that you can use to add an asset outside of the module system.
353 |
354 | If you put a file into the `public` folder, it will **not** be processed by Webpack. Instead it will be copied into the build folder untouched. To reference assets in the `public` folder, you need to use a special variable called `PUBLIC_URL`.
355 |
356 | Inside `index.html`, you can use it like this:
357 |
358 | ```html
359 |
360 | ```
361 |
362 | Only files inside the `public` folder will be accessible by `%PUBLIC_URL%` prefix. If you need to use a file from `src` or `node_modules`, you’ll have to copy it there to explicitly specify your intention to make this file a part of the build.
363 |
364 | When you run `npm run build`, Create React App will substitute `%PUBLIC_URL%` with a correct absolute path so your project works even if you use client-side routing or host it at a non-root URL.
365 |
366 | In JavaScript code, you can use `process.env.PUBLIC_URL` for similar purposes:
367 |
368 | ```js
369 | render() {
370 | // Note: this is an escape hatch and should be used sparingly!
371 | // Normally we recommend using `import` for getting asset URLs
372 | // as described in “Adding Images and Fonts” above this section.
373 | return ;
374 | }
375 | ```
376 |
377 | Keep in mind the downsides of this approach:
378 |
379 | * None of the files in `public` folder get post-processed or minified.
380 | * Missing files will not be called at compilation time, and will cause 404 errors for your users.
381 | * Result filenames won’t include content hashes so you’ll need to add query arguments or rename them every time they change.
382 |
383 | However, it can be handy for referencing assets like [`manifest.webmanifest`](https://developer.mozilla.org/en-US/docs/Web/Manifest) from HTML, or including small scripts like [`pace.js`](http://github.hubspot.com/pace/docs/welcome/) outside of the bundled code.
384 |
385 | ## Adding Bootstrap
386 |
387 | You don’t have to use [React Bootstrap](https://react-bootstrap.github.io) together with React but it is a popular library for integrating Bootstrap with React apps. If you need it, you can integrate it with Create React App by following these steps:
388 |
389 | Install React Bootstrap and Bootstrap from NPM. React Bootstrap does not include Bootstrap CSS so this needs to be installed as well:
390 |
391 | ```
392 | npm install react-bootstrap --save
393 | npm install bootstrap@3 --save
394 | ```
395 |
396 | Import Bootstrap CSS and optionally Bootstrap theme CSS in the ```src/index.js``` file:
397 |
398 | ```js
399 | import 'bootstrap/dist/css/bootstrap.css';
400 | import 'bootstrap/dist/css/bootstrap-theme.css';
401 | ```
402 |
403 | Import required React Bootstrap components within ```src/App.js``` file or your custom component files:
404 |
405 | ```js
406 | import { Navbar, Jumbotron, Button } from 'react-bootstrap';
407 | ```
408 |
409 | Now you are ready to use the imported React Bootstrap components within your component hierarchy defined in the render method. Here is an example [`App.js`](https://gist.githubusercontent.com/gaearon/85d8c067f6af1e56277c82d19fd4da7b/raw/6158dd991b67284e9fc8d70b9d973efe87659d72/App.js) redone using React Bootstrap.
410 |
411 | ## Adding Flow
412 |
413 | Flow typing is currently [not supported out of the box](https://github.com/facebookincubator/create-react-app/issues/72) with the default `.flowconfig` generated by Flow. If you run it, you might get errors like this:
414 |
415 | ```js
416 | node_modules/fbjs/lib/Deferred.js.flow:60
417 | 60: Promise.prototype.done.apply(this._promise, arguments);
418 | ^^^^ property `done`. Property not found in
419 | 495: declare class Promise<+R> {
420 | ^ Promise. See lib: /private/tmp/flow/flowlib_34952d31/core.js:495
421 |
422 | node_modules/fbjs/lib/shallowEqual.js.flow:29
423 | 29: return x !== 0 || 1 / (x: $FlowIssue) === 1 / (y: $FlowIssue);
424 | ^^^^^^^^^^ identifier `$FlowIssue`. Could not resolve name
425 | ```
426 |
427 | To fix this, change your `.flowconfig` to look like this:
428 |
429 | ```ini
430 | [ignore]
431 | /node_modules/fbjs/.*
432 | ```
433 |
434 | Re-run flow, and you shouldn’t get any extra issues.
435 |
436 | ## Adding Custom Environment Variables
437 |
438 | >Note: this feature is available with `react-scripts@0.2.3` and higher.
439 |
440 | Your project can consume variables declared in your environment as if they were declared locally in your JS files. By
441 | default you will have `NODE_ENV` defined for you, and any other environment variables starting with
442 | `REACT_APP_`. These environment variables will be defined for you on `process.env`. For example, having an environment
443 | variable named `REACT_APP_SECRET_CODE` will be exposed in your JS as `process.env.REACT_APP_SECRET_CODE`, in addition
444 | to `process.env.NODE_ENV`.
445 |
446 | These environment variables can be useful for displaying information conditionally based on where the project is
447 | deployed or consuming sensitive data that lives outside of version control.
448 |
449 | First, you need to have environment variables defined. For example, let’s say you wanted to consume a secret defined
450 | in the environment inside a `