Skip to content

Commit c27cf86

Browse files
committed
Merge branch 'master' of github.com:markfinger/python-react
2 parents 474a398 + 934092d commit c27cf86

File tree

1 file changed

+61
-58
lines changed

1 file changed

+61
-58
lines changed

README.md

Lines changed: 61 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,11 @@ print(component)
2929
print(component.render_js())
3030
```
3131

32-
If you only want to plug your JSX files into the client-side, you can translate the source code and
33-
bundle it into a single file by using `bundle_component`.
32+
[render_component](#render_component) is the main entry point to pre-render components and package them for
33+
use on the client-side.
3434

35-
```python
36-
from react.bundle import bundle_component
37-
38-
bundle = bundle_component(
39-
# A path to a file exporting your React component
40-
'/path/to/component.jsx',
41-
# Translate the source to JavaScript from JSX + ES6/7
42-
translate=True
43-
)
44-
45-
# Renders a script element pointing to the bundled component
46-
print(bundle.render())
47-
48-
# Outputs the global variable name that the component is exposed as.
49-
print(bundle.get_var())
50-
```
35+
If you only want to use your JSX files on the client-side, you can use [bundle_component](#bundle_component)
36+
to translate and bundle the source code into a form that will run in a browser.
5137

5238

5339
Documentation
@@ -56,8 +42,8 @@ Documentation
5642
- [Installation](#installation)
5743
- [API](#api)
5844
- [render_component()](#render_component)
59-
- [RenderedComponent](#renderedcomponent)
6045
- [bundle_component()](#bundle_component)
46+
- [RenderedComponent](#renderedcomponent)
6147
- [Django integration](#django-integration)
6248
- [Settings](#settings)
6349
- [Running the tests](#running-the-tests)
@@ -141,11 +127,44 @@ render_component(
141127
```
142128

143129

130+
### bundle_component()
131+
132+
Packages a React component so that it can be re-used on the client-side. JSX + ES6+7 files are translated
133+
to JavaScript with [Babel](https://babeljs.io/).
134+
135+
Be aware that `bundle_component` is primarily a convenience method. Under the hood, it simply generates a webpack
136+
config file which is passed to [python-webpack](https://github.com/markfinger/python-webpack).
137+
138+
If you require more flexibility in the bundling process, you are recommended to read the code and then use
139+
python-webpack directly.
140+
141+
142+
#### Usage
143+
144+
```python
145+
from react.bundle import bundle_component
146+
147+
bundle = bundle_component(
148+
# A path to a file which exports the component. If the path is relative,
149+
# django's static file finders will attempt to find the file
150+
path='...',
151+
# An optional boolean indicating that the component should be translated
152+
# from JSX and ES6/7 during the bundling process
153+
translate = True,
154+
)
155+
156+
# Render a <script> element pointing to the bundle
157+
bundle.render()
158+
159+
# Returns the variable that the bundle exposes the component as
160+
bundle.get_var()
161+
```
162+
144163
### RenderedComponent
145164

146165
The result of rendering a component to its initial markup. RenderedComponents can be converted to
147166
strings to output their generated markup. If `translate` or `bundle` was provided to `render_component`,
148-
they can also be mounted on the client-side to provide immediate interactivity.
167+
the component can also be mounted on the client-side to provide immediate interactivity.
149168

150169
```python
151170
component = render_component(...)
@@ -161,14 +180,13 @@ component.render_markup()
161180
component.render_js()
162181
```
163182

164-
Note: if you wish to use the `render_js` method on the client-side, you **must** provide a
165-
`<script>` element pointing to React. React is omitted from the bundled component so that
166-
build times are reduced, and to ensure that multiple components can be included on a single
167-
page without duplicating React's codebase.
183+
Note: if you wish to use the `render_js` method on the client-side, you **must** provide a `<script>`
184+
element pointing to React. React is omitted from the bundled component so that build times are reduced,
185+
and to ensure that multiple components can be included on a single page without duplicating React's
186+
codebase.
168187

169-
Be aware that the mounting strategy used by `render_js` is only intended for convenience. If you
170-
want to use a more custom solution for mounting or bundling, there are a couple of helpers provided
171-
to assist you:
188+
Be aware that the mounting strategy used by `render_js` is only intended for convenience. If you want to
189+
use a more custom solution for mounting or bundling, there are a couple of helpers provided to assist you:
172190

173191
```python
174192
# The data used to render the component, this can be plugged straight into the client-side
@@ -195,33 +213,8 @@ print(component.markup)
195213
print(component.render_mount_js())
196214
```
197215

198-
199-
### bundle_component()
200-
201-
Packages a React component so that it can be re-used on the client-side. JSX + ES6+7 files are translated
202-
to JavaScript with [Babel](https://babeljs.io/).
203-
204-
Be aware that `bundle_component` is primarily a convenience method. Under the hood, it plugs a pre-built
205-
webpack config file into [python-webpack](https://github.com/markfinger/python-webpack).
206-
207-
If you require more flexibility in the bundling process, you are recommended to read the code to understand
208-
what is happening, and then use python-webpack yourself.
209-
210-
211-
#### Usage
212-
213-
```python
214-
from react.bundle import bundle_component
215-
216-
bundle_component(
217-
# A path to a file which exports the component. If the path is relative,
218-
# django's static file finders will attempt to find the file
219-
path='...',
220-
# An optional boolean indicating that the component should be translated
221-
# from JSX and ES6/7 during the bundling process
222-
translate = True,
223-
)
224-
```
216+
The codebase in `react/bundle.py` also provides a number of examples illustrating how you can
217+
programmatically generate configuration files for webpack.
225218

226219

227220
Django integration
@@ -245,8 +238,9 @@ REACT = {
245238
```
246239

247240
When calling `render_component` or `bundle_component`, python-react will attempt to use Django's
248-
static file finders to resolve relative paths. If you provide a path such as `'my_app/component.jsx'`,
249-
Django would resolve that path to an app's static folder, eg: `'my_app/static/my_app/component.jsx'`.
241+
static file finders to resolve relative paths. If you provide a relative path such as
242+
`'my_app/component.jsx'`, Django may resolve that path to an app's static folder, for example:
243+
`'my_app/static/my_app/component.jsx'`.
250244

251245

252246
Settings
@@ -279,8 +273,17 @@ Default: `None`
279273

280274
An import path that will be used when rendering bundled components.
281275

282-
If not defined, this will default to the version of React installed within the `node_modules` directory
283-
within your js-host `SOURCE_ROOT` setting.
276+
If not defined, the bundler will default to using js-host's SOURCE_ROOT via
277+
`os.path.join(SOURCE_ROOT, 'node_modules', 'react')`.
278+
279+
Default: `None`
280+
281+
### TRANSLATE_TEST
282+
283+
When bundling a component with `translate=True`, this JavaScript regex is tested against every file to
284+
determine if the babel loader should run over it.
285+
286+
If not defined, the bundler will default to using `'/.jsx$/'`.
284287

285288
Default: `None`
286289

0 commit comments

Comments
 (0)