Skip to content

Commit 96147d9

Browse files
committed
Cleaned up the "templates" used for bundling.
Mindful of the inevitable shift away from the django dependency, I've stuck with string formatting, rather than a full-blown tempting solution.
1 parent 9fd0264 commit 96147d9

File tree

3 files changed

+102
-101
lines changed

3 files changed

+102
-101
lines changed

django_react/bundle.py

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import re
33
import tempfile
44
from django_webpack.compiler import webpack
5+
from .templates import BUNDLE_CONFIG, BUNDLE_TRANSLATE_CONFIG
56

67
COMPONENT_CONFIG_FILES = {}
78

@@ -20,53 +21,24 @@ def get_var_from_path(path):
2021

2122

2223
def get_webpack_config(path, translate=None, var=None):
23-
config = 'module.exports = {'
24-
2524
if var is None:
2625
var = get_var_from_path(path)
2726

28-
config += '''
29-
context: '{dir}',
30-
entry: '{file}',
31-
output: {{
32-
path: '[bundle_dir]/components',
33-
filename: '{var}-[hash].js',
34-
libraryTarget: 'umd',
35-
library: '{var}'
36-
}},
37-
externals: [
38-
{{
39-
'react': {{
40-
commonjs2: '{path_to_react}',
41-
root: 'React'
42-
}}
43-
}}
44-
],
45-
devtool: 'eval\''''.format(
46-
dir=os.path.dirname(path),
47-
file='./' + os.path.basename(path),
48-
var=var,
49-
path_to_react=os.path.join(os.path.dirname(__file__), 'services', 'node_modules', 'react'),
50-
)
51-
27+
translate_config = ''
5228
if translate:
5329
# JSX + ES6/7 support
54-
config += ''',
55-
module: {{
56-
loaders: [{{
57-
test: /\{ext}$/,
58-
exclude: /node_modules/,
59-
loader: 'babel-loader'
60-
}}]
61-
}},
62-
resolveLoader: {{
63-
root: '{node_modules}'
64-
}}'''.format(
30+
translate_config += BUNDLE_TRANSLATE_CONFIG.format(
6531
ext=os.path.splitext(path)[-1],
6632
node_modules=os.path.join(os.path.dirname(__file__), 'services', 'node_modules')
6733
)
6834

69-
return config + '\n};'
35+
return BUNDLE_CONFIG.format(
36+
dir=os.path.dirname(path),
37+
file='./' + os.path.basename(path),
38+
var=var,
39+
path_to_react=os.path.join(os.path.dirname(__file__), 'services', 'node_modules', 'react'),
40+
translate_config=translate_config
41+
)
7042

7143

7244
def get_component_config_filename(path, translate=None, var=None):

django_react/templates.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
BUNDLE_CONFIG = \
2+
"""module.exports = {{
3+
context: '{dir}',
4+
entry: '{file}',
5+
output: {{
6+
path: '[bundle_dir]/react-components',
7+
filename: '{var}-[hash].js',
8+
libraryTarget: 'umd',
9+
library: '{var}'
10+
}},
11+
externals: [{{
12+
'react': {{
13+
commonjs2: '{path_to_react}',
14+
root: 'React'
15+
}}
16+
}}],
17+
devtool: 'eval'{translate_config}
18+
}};
19+
"""
20+
21+
BUNDLE_TRANSLATE_CONFIG = \
22+
""",
23+
module: {{
24+
loaders: [{{
25+
test: /\{ext}$/,
26+
exclude: /node_modules/,
27+
loader: 'babel-loader'
28+
}}]
29+
}},
30+
resolveLoader: {{
31+
root: '{node_modules}'
32+
}}
33+
"""

tests/test_functionality.py

Lines changed: 59 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -49,72 +49,68 @@ def test_can_generate_a_var_from_a_path(self):
4949

5050
def test_can_generate_a_webpack_config_for_a_js_component(self):
5151
config = get_webpack_config(HELLO_WORLD_COMPONENT_JS)
52-
self.assertTrue(config.startswith('module.exports = {'))
53-
self.assertIn(
54-
"context: '" + os.path.join(os.path.dirname(__file__), 'components') + "',",
55-
config
56-
)
57-
self.assertIn("entry: './HelloWorld.js',", config)
58-
self.assertIn("output: {", config)
59-
self.assertIn("path: '[bundle_dir]/components',", config)
60-
self.assertIn("filename: 'components__HelloWorld-[hash].js',", config)
61-
self.assertIn("libraryTarget: 'umd',", config)
62-
self.assertIn("library: 'components__HelloWorld'", config)
63-
self.assertIn("},", config)
64-
self.assertIn("externals: [", config)
65-
self.assertIn("{", config)
66-
self.assertIn("'react': {", config)
67-
self.assertIn(
68-
"commonjs2: '" + os.path.join(os.path.dirname(django_react.__file__), 'services', 'node_modules', 'react') + "',",
69-
config
70-
)
71-
self.assertIn("root: 'React'", config)
72-
self.assertIn("}", config)
73-
self.assertIn("}", config)
74-
self.assertIn("],", config)
75-
self.assertIn("devtool: 'eval'\n", config)
76-
self.assertTrue(config.endswith('};'))
52+
expected = \
53+
"""module.exports = {
54+
context: '%s',
55+
entry: './HelloWorld.js',
56+
output: {
57+
path: '[bundle_dir]/react-components',
58+
filename: 'components__HelloWorld-[hash].js',
59+
libraryTarget: 'umd',
60+
library: 'components__HelloWorld'
61+
},
62+
externals: [{
63+
'react': {
64+
commonjs2: '%s',
65+
root: 'React'
66+
}
67+
}],
68+
devtool: 'eval'
69+
};
70+
""" % (
71+
os.path.join(os.path.dirname(__file__), 'components'),
72+
os.path.join(os.path.dirname(django_react.__file__), 'services', 'node_modules', 'react'),
73+
)
74+
75+
self.assertEqual(config, expected)
7776

7877
def test_can_generate_a_webpack_config_for_a_jsx_component(self):
7978
config = get_webpack_config(HELLO_WORLD_COMPONENT_JSX, translate=True)
80-
self.assertTrue(config.startswith('module.exports = {'))
81-
self.assertIn(
82-
"context: '" + os.path.join(os.path.dirname(__file__), 'components') + "',",
83-
config
84-
)
85-
self.assertIn("entry: './HelloWorld.jsx',", config)
86-
self.assertIn("output: {", config)
87-
self.assertIn("path: '[bundle_dir]/components',", config)
88-
self.assertIn("filename: 'components__HelloWorld-[hash].js',", config)
89-
self.assertIn("libraryTarget: 'umd',", config)
90-
self.assertIn("library: 'components__HelloWorld'", config)
91-
self.assertIn("},", config)
92-
self.assertIn("externals: [", config)
93-
self.assertIn("{", config)
94-
self.assertIn("'react': {", config)
95-
self.assertIn(
96-
"commonjs2: '" + os.path.join(os.path.dirname(django_react.__file__), 'services', 'node_modules', 'react') + "',",
97-
config
98-
)
99-
self.assertIn("root: 'React'", config)
100-
self.assertIn("}", config)
101-
self.assertIn("}", config)
102-
self.assertIn("],", config)
103-
self.assertIn("devtool: 'eval',", config)
104-
self.assertIn("module: {", config)
105-
self.assertIn("loaders: [{", config)
106-
self.assertIn("test: /\.jsx$/,", config)
107-
self.assertIn("exclude: /node_modules/,", config)
108-
self.assertIn("loader: 'babel-loader'", config)
109-
self.assertIn("}]", config)
110-
self.assertIn("},", config)
111-
self.assertIn("resolveLoader: {", config)
112-
self.assertIn(
113-
"root: '" + os.path.join(os.path.dirname(django_react.__file__), 'services', 'node_modules') + "'",
114-
config
115-
)
116-
self.assertIn("}", config)
117-
self.assertTrue(config.endswith('};'))
79+
expected = \
80+
"""module.exports = {
81+
context: '%s',
82+
entry: './HelloWorld.jsx',
83+
output: {
84+
path: '[bundle_dir]/react-components',
85+
filename: 'components__HelloWorld-[hash].js',
86+
libraryTarget: 'umd',
87+
library: 'components__HelloWorld'
88+
},
89+
externals: [{
90+
'react': {
91+
commonjs2: '%s',
92+
root: 'React'
93+
}
94+
}],
95+
devtool: 'eval',
96+
module: {
97+
loaders: [{
98+
test: /\.jsx$/,
99+
exclude: /node_modules/,
100+
loader: 'babel-loader'
101+
}]
102+
},
103+
resolveLoader: {
104+
root: '%s'
105+
}
106+
107+
};
108+
""" % (
109+
os.path.join(os.path.dirname(__file__), 'components'),
110+
os.path.join(os.path.dirname(django_react.__file__), 'services', 'node_modules', 'react'),
111+
os.path.join(os.path.dirname(django_react.__file__), 'services', 'node_modules'),
112+
)
113+
self.assertEqual(config, expected)
118114

119115
def test_can_generate_and_create_a_config_file(self):
120116
filename = get_component_config_filename(HELLO_WORLD_COMPONENT_JS)

0 commit comments

Comments
 (0)