Skip to content

Commit 91e6a7a

Browse files
committed
The bundle component's devtool is now optional and respects the DEV_TOOL setting.
1 parent 2c35e21 commit 91e6a7a

File tree

10 files changed

+47
-22
lines changed

10 files changed

+47
-22
lines changed

django_react/bundle.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import re
33
import tempfile
44
from django_webpack.compiler import webpack
5-
from .templates import BUNDLE_CONFIG, BUNDLE_TRANSLATE_CONFIG
5+
from .templates import BUNDLE_CONFIG, BUNDLE_TRANSLATE_CONFIG, DEV_TOOL_CONFIG
6+
from .conf import settings
67

78
COMPONENT_CONFIG_FILES = {}
89

@@ -33,12 +34,17 @@ def get_webpack_config(path, translate=None):
3334
node_modules=PATH_TO_NODE_MODULES
3435
)
3536

37+
dev_tool_config = ''
38+
if settings.DEV_TOOL:
39+
dev_tool_config = DEV_TOOL_CONFIG
40+
3641
return BUNDLE_CONFIG.format(
3742
path_to_resolve=os.path.join(PATH_TO_NODE_MODULES, 'resolve'),
3843
dir=os.path.dirname(path),
3944
file='./' + os.path.basename(path),
4045
var=var,
41-
translate_config=translate_config
46+
translate_config=translate_config,
47+
dev_tool_config=dev_tool_config,
4248
)
4349

4450

django_react/conf.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from optional_django import conf
2+
from optional_django.env import DJANGO_SETTINGS
3+
4+
5+
class Conf(conf.Conf):
6+
django_namespace = 'DJANGO_REACT'
7+
8+
DEV_TOOL = DJANGO_SETTINGS.DEBUG if DJANGO_SETTINGS else False
9+
WATCH_SOURCE = DJANGO_SETTINGS.DEBUG if DJANGO_SETTINGS else False
10+
11+
settings = Conf()

django_react/render.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from optional_django import six
77
from .exceptions import ComponentSourceFileNotFound, ComponentWasNotBundled
88
from .services import RenderService
9-
from .settings import WATCH_SOURCE
9+
from .conf import settings
1010
from .bundle import bundle_component
1111
from .templates import MOUNT_JS
1212

@@ -99,7 +99,7 @@ def render_component(
9999
raise ComponentSourceFileNotFound(path_to_source)
100100

101101
if watch_source is None:
102-
watch_source = WATCH_SOURCE
102+
watch_source = settings.WATCH_SOURCE
103103

104104
bundled_component = None
105105
if bundle or translate or watch_source:

django_react/settings.py

-8
This file was deleted.

django_react/templates.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
commonjs2: resolve.sync('react/addons', {{basedir: '{dir}'}}),
2121
root: 'React'
2222
}}
23-
}}],
24-
devtool: 'eval'{translate_config}
23+
}}]{dev_tool_config}{translate_config}
2524
}};
2625
"""
2726

27+
DEV_TOOL_CONFIG = """,\n devtool: 'eval'"""
28+
2829
BUNDLE_TRANSLATE_CONFIG = \
2930
""",
3031
module: {{

example/django_react

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../django_react

example/requirements.txt

-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
django
2-
django-react==0.10.0
32
django-webpack==3.0.1

requirements.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
django
22
django-node==4.0.0
3-
django-webpack==3.0.1
4-
optional-django==0.1.0
3+
django-webpack==3.1.0
4+
optional-django==0.2.1

setup.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
},
1515
install_requires=[
1616
'django-node==4.0.0',
17-
'django-webpack==3.0.1'
17+
'django-webpack==3.1.0',
18+
'optional-django==0.2.1'
1819
],
1920
description='Render and bundle React components from a Django application',
2021
long_description='Documentation at https://github.com/markfinger/django-react',

tests/test_functionality.py

+18-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
)
1212
from django_react.exceptions import ComponentRenderingError, ComponentSourceFileNotFound, ComponentWasNotBundled
1313
from django_webpack.compiler import WebpackBundle
14+
from django_react.conf import settings
1415
from .settings import STATIC_ROOT
1516

1617
NODE_MODULES = os.path.join(os.path.dirname(django_react.__file__), 'services', 'node_modules')
@@ -50,7 +51,12 @@ def test_can_generate_a_var_from_a_path(self):
5051
self.assertEqual(get_var_from_path('foo/test/one/two/bar/a'), 'bar__a')
5152
self.assertEqual(get_var_from_path('foo/test/one/two/bar/.a'), 'bar___a')
5253

54+
"\nvar resolve = require('/Users/markfinger/Projects/django-react/django_react/services/node_modules/resolve');\n\nmodule.exports = {\n context: '/Users/markfinger/Projects/django-react/tests/components',\n entry: './HelloWorld.js',\n output: {\n path: '[bundle_dir]/react-components',\n filename: 'components__HelloWorld-[hash].js',\n libraryTarget: 'umd',\n library: 'components__HelloWorld'\n },\n externals: [{\n react: {\n commonjs2: resolve.sync('react', {basedir: '/Users/markfinger/Projects/django-react/tests/components'}),\n root: 'React'\n },\n 'react/addons': {\n commonjs2: resolve.sync('react/addons', {basedir: '/Users/markfinger/Projects/django-react/tests/components'}),\n root: 'React'\n }\n }]\n};\n"
55+
"\nvar resolve = require('/Users/markfinger/Projects/django-react/django_react/services/node_modules/resolve');\n\nmodule.exports = {\n context: '/Users/markfinger/Projects/django-react/tests/components',\n entry: './HelloWorld.js',\n output: {\n path: '[bundle_dir]/react-components',\n filename: 'components__HelloWorld-[hash].js',\n libraryTarget: 'umd',\n library: 'components__HelloWorld'\n },\n externals: [{\n react: {\n commonjs2: resolve.sync('react', {basedir: '/Users/markfinger/Projects/django-react/tests/components'}),\n root: 'React'\n },\n 'react/addons': {\n commonjs2: resolve.sync('react/addons', {basedir: '/Users/markfinger/Projects/django-react/tests/components'}),\n root: 'React'\n }\n }],\n devtool: 'eval'\n};\n"
5356
def test_can_generate_a_webpack_config_for_a_js_component(self):
57+
_DEV_TOOL = settings.DEV_TOOL
58+
settings._unlock()
59+
settings.DEV_TOOL = True
5460
config = get_webpack_config(HELLO_WORLD_COMPONENT_JS)
5561
expected = \
5662
"""
@@ -83,10 +89,16 @@ def test_can_generate_a_webpack_config_for_a_js_component(self):
8389
COMPONENT_ROOT,
8490
COMPONENT_ROOT,
8591
)
86-
8792
self.assertEqual(config, expected)
88-
93+
settings.DEV_TOOL = _DEV_TOOL
94+
settings._lock()
95+
"\nvar resolve = require('/Users/markfinger/Projects/django-react/django_react/services/node_modules/resolve');\n\nmodule.exports = {\n context: '/Users/markfinger/Projects/django-react/tests/components',\n entry: './HelloWorld.jsx',\n output: {\n path: '[bundle_dir]/react-components',\n filename: 'components__HelloWorld-[hash].js',\n libraryTarget: 'umd',\n library: 'components__HelloWorld'\n },\n externals: [{\n react: {\n commonjs2: resolve.sync('react', {basedir: '/Users/markfinger/Projects/django-react/tests/components'}),\n root: 'React'\n },\n 'react/addons': {\n commonjs2: resolve.sync('react/addons', {basedir: '/Users/markfinger/Projects/django-react/tests/components'}),\n root: 'React'\n }\n }],\n devtool: 'eval'\n,\n module: {\n loaders: [{\n test: /\\.jsx$/,\n exclude: /node_modules/,\n loader: 'babel-loader'\n }]\n },\n resolveLoader: {\n root: '/Users/markfinger/Projects/django-react/django_react/services/node_modules'\n }\n\n};\n"
96+
"\nvar resolve = require('/Users/markfinger/Projects/django-react/django_react/services/node_modules/resolve');\n\nmodule.exports = {\n context: '/Users/markfinger/Projects/django-react/tests/components',\n entry: './HelloWorld.jsx',\n output: {\n path: '[bundle_dir]/react-components',\n filename: 'components__HelloWorld-[hash].js',\n libraryTarget: 'umd',\n library: 'components__HelloWorld'\n },\n externals: [{\n react: {\n commonjs2: resolve.sync('react', {basedir: '/Users/markfinger/Projects/django-react/tests/components'}),\n root: 'React'\n },\n 'react/addons': {\n commonjs2: resolve.sync('react/addons', {basedir: '/Users/markfinger/Projects/django-react/tests/components'}),\n root: 'React'\n }\n }],\n devtool: 'eval',\n\n module: {\n loaders: [{\n test: /\\.jsx$/,\n exclude: /node_modules/,\n loader: 'babel-loader'\n }]\n },\n resolveLoader: {\n root: '/Users/markfinger/Projects/django-react/django_react/services/node_modules'\n }\n\n};\n"
8997
def test_can_generate_a_webpack_config_for_a_jsx_component(self):
98+
_DEV_TOOL = settings.DEV_TOOL
99+
settings._unlock()
100+
settings.DEV_TOOL = True
101+
90102
config = get_webpack_config(HELLO_WORLD_COMPONENT_JSX, translate=True)
91103
expected = \
92104
"""
@@ -132,6 +144,8 @@ def test_can_generate_a_webpack_config_for_a_jsx_component(self):
132144
NODE_MODULES,
133145
)
134146
self.assertEqual(config, expected)
147+
settings.DEV_TOOL = _DEV_TOOL
148+
settings._lock()
135149

136150
def test_can_generate_and_create_a_config_file(self):
137151
filename = get_component_config_filename(HELLO_WORLD_COMPONENT_JS)
@@ -388,5 +402,5 @@ def test_bundled_components_omit_react_and_react_addons(self):
388402
bundle = bundle_component(REACT_ADDONS_COMPONENT, translate=True)
389403
with open(bundle.get_assets()[0]['path'], 'r') as bundle_file:
390404
content = bundle_file.read()
391-
self.assertIn(os.path.join(NODE_MODULES, 'react', 'react.js'), content)
392-
self.assertIn(os.path.join(NODE_MODULES, 'react', 'addons.js'), content)
405+
# A bit hacky, but seems to work
406+
self.assertNotIn('Facebook, Inc', content)

0 commit comments

Comments
 (0)