@@ -29,25 +29,11 @@ print(component)
29
29
print (component.render_js())
30
30
```
31
31
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 .
34
34
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.
51
37
52
38
53
39
Documentation
@@ -56,8 +42,8 @@ Documentation
56
42
- [ Installation] ( #installation )
57
43
- [ API] ( #api )
58
44
- [ render_component()] ( #render_component )
59
- - [ RenderedComponent] ( #renderedcomponent )
60
45
- [ bundle_component()] ( #bundle_component )
46
+ - [ RenderedComponent] ( #renderedcomponent )
61
47
- [ Django integration] ( #django-integration )
62
48
- [ Settings] ( #settings )
63
49
- [ Running the tests] ( #running-the-tests )
@@ -141,11 +127,44 @@ render_component(
141
127
```
142
128
143
129
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
+
144
163
### RenderedComponent
145
164
146
165
The result of rendering a component to its initial markup. RenderedComponents can be converted to
147
166
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.
149
168
150
169
``` python
151
170
component = render_component(... )
@@ -161,14 +180,13 @@ component.render_markup()
161
180
component.render_js()
162
181
```
163
182
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.
168
187
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:
172
190
173
191
``` python
174
192
# The data used to render the component, this can be plugged straight into the client-side
@@ -195,33 +213,8 @@ print(component.markup)
195
213
print (component.render_mount_js())
196
214
```
197
215
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.
225
218
226
219
227
220
Django integration
@@ -245,8 +238,9 @@ REACT = {
245
238
```
246
239
247
240
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' ` .
250
244
251
245
252
246
Settings
@@ -279,8 +273,17 @@ Default: `None`
279
273
280
274
An import path that will be used when rendering bundled components.
281
275
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$/' ` .
284
287
285
288
Default: ` None `
286
289
0 commit comments