Skip to content

Commit

Permalink
Cleaned up the "templates" used for bundling.
Browse files Browse the repository at this point in the history
Mindful of the inevitable shift away from the django dependency, I've stuck with string formatting, rather than a full-blown tempting solution.
  • Loading branch information
markfinger committed Apr 13, 2015
1 parent 9fd0264 commit 96147d9
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 101 deletions.
48 changes: 10 additions & 38 deletions django_react/bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import re
import tempfile
from django_webpack.compiler import webpack
from .templates import BUNDLE_CONFIG, BUNDLE_TRANSLATE_CONFIG

COMPONENT_CONFIG_FILES = {}

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


def get_webpack_config(path, translate=None, var=None):
config = 'module.exports = {'

if var is None:
var = get_var_from_path(path)

config += '''
context: '{dir}',
entry: '{file}',
output: {{
path: '[bundle_dir]/components',
filename: '{var}-[hash].js',
libraryTarget: 'umd',
library: '{var}'
}},
externals: [
{{
'react': {{
commonjs2: '{path_to_react}',
root: 'React'
}}
}}
],
devtool: 'eval\''''.format(
dir=os.path.dirname(path),
file='./' + os.path.basename(path),
var=var,
path_to_react=os.path.join(os.path.dirname(__file__), 'services', 'node_modules', 'react'),
)

translate_config = ''
if translate:
# JSX + ES6/7 support
config += ''',
module: {{
loaders: [{{
test: /\{ext}$/,
exclude: /node_modules/,
loader: 'babel-loader'
}}]
}},
resolveLoader: {{
root: '{node_modules}'
}}'''.format(
translate_config += BUNDLE_TRANSLATE_CONFIG.format(
ext=os.path.splitext(path)[-1],
node_modules=os.path.join(os.path.dirname(__file__), 'services', 'node_modules')
)

return config + '\n};'
return BUNDLE_CONFIG.format(
dir=os.path.dirname(path),
file='./' + os.path.basename(path),
var=var,
path_to_react=os.path.join(os.path.dirname(__file__), 'services', 'node_modules', 'react'),
translate_config=translate_config
)


def get_component_config_filename(path, translate=None, var=None):
Expand Down
33 changes: 33 additions & 0 deletions django_react/templates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
BUNDLE_CONFIG = \
"""module.exports = {{
context: '{dir}',
entry: '{file}',
output: {{
path: '[bundle_dir]/react-components',
filename: '{var}-[hash].js',
libraryTarget: 'umd',
library: '{var}'
}},
externals: [{{
'react': {{
commonjs2: '{path_to_react}',
root: 'React'
}}
}}],
devtool: 'eval'{translate_config}
}};
"""

BUNDLE_TRANSLATE_CONFIG = \
""",
module: {{
loaders: [{{
test: /\{ext}$/,
exclude: /node_modules/,
loader: 'babel-loader'
}}]
}},
resolveLoader: {{
root: '{node_modules}'
}}
"""
122 changes: 59 additions & 63 deletions tests/test_functionality.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,72 +49,68 @@ def test_can_generate_a_var_from_a_path(self):

def test_can_generate_a_webpack_config_for_a_js_component(self):
config = get_webpack_config(HELLO_WORLD_COMPONENT_JS)
self.assertTrue(config.startswith('module.exports = {'))
self.assertIn(
"context: '" + os.path.join(os.path.dirname(__file__), 'components') + "',",
config
)
self.assertIn("entry: './HelloWorld.js',", config)
self.assertIn("output: {", config)
self.assertIn("path: '[bundle_dir]/components',", config)
self.assertIn("filename: 'components__HelloWorld-[hash].js',", config)
self.assertIn("libraryTarget: 'umd',", config)
self.assertIn("library: 'components__HelloWorld'", config)
self.assertIn("},", config)
self.assertIn("externals: [", config)
self.assertIn("{", config)
self.assertIn("'react': {", config)
self.assertIn(
"commonjs2: '" + os.path.join(os.path.dirname(django_react.__file__), 'services', 'node_modules', 'react') + "',",
config
)
self.assertIn("root: 'React'", config)
self.assertIn("}", config)
self.assertIn("}", config)
self.assertIn("],", config)
self.assertIn("devtool: 'eval'\n", config)
self.assertTrue(config.endswith('};'))
expected = \
"""module.exports = {
context: '%s',
entry: './HelloWorld.js',
output: {
path: '[bundle_dir]/react-components',
filename: 'components__HelloWorld-[hash].js',
libraryTarget: 'umd',
library: 'components__HelloWorld'
},
externals: [{
'react': {
commonjs2: '%s',
root: 'React'
}
}],
devtool: 'eval'
};
""" % (
os.path.join(os.path.dirname(__file__), 'components'),
os.path.join(os.path.dirname(django_react.__file__), 'services', 'node_modules', 'react'),
)

self.assertEqual(config, expected)

def test_can_generate_a_webpack_config_for_a_jsx_component(self):
config = get_webpack_config(HELLO_WORLD_COMPONENT_JSX, translate=True)
self.assertTrue(config.startswith('module.exports = {'))
self.assertIn(
"context: '" + os.path.join(os.path.dirname(__file__), 'components') + "',",
config
)
self.assertIn("entry: './HelloWorld.jsx',", config)
self.assertIn("output: {", config)
self.assertIn("path: '[bundle_dir]/components',", config)
self.assertIn("filename: 'components__HelloWorld-[hash].js',", config)
self.assertIn("libraryTarget: 'umd',", config)
self.assertIn("library: 'components__HelloWorld'", config)
self.assertIn("},", config)
self.assertIn("externals: [", config)
self.assertIn("{", config)
self.assertIn("'react': {", config)
self.assertIn(
"commonjs2: '" + os.path.join(os.path.dirname(django_react.__file__), 'services', 'node_modules', 'react') + "',",
config
)
self.assertIn("root: 'React'", config)
self.assertIn("}", config)
self.assertIn("}", config)
self.assertIn("],", config)
self.assertIn("devtool: 'eval',", config)
self.assertIn("module: {", config)
self.assertIn("loaders: [{", config)
self.assertIn("test: /\.jsx$/,", config)
self.assertIn("exclude: /node_modules/,", config)
self.assertIn("loader: 'babel-loader'", config)
self.assertIn("}]", config)
self.assertIn("},", config)
self.assertIn("resolveLoader: {", config)
self.assertIn(
"root: '" + os.path.join(os.path.dirname(django_react.__file__), 'services', 'node_modules') + "'",
config
)
self.assertIn("}", config)
self.assertTrue(config.endswith('};'))
expected = \
"""module.exports = {
context: '%s',
entry: './HelloWorld.jsx',
output: {
path: '[bundle_dir]/react-components',
filename: 'components__HelloWorld-[hash].js',
libraryTarget: 'umd',
library: 'components__HelloWorld'
},
externals: [{
'react': {
commonjs2: '%s',
root: 'React'
}
}],
devtool: 'eval',
module: {
loaders: [{
test: /\.jsx$/,
exclude: /node_modules/,
loader: 'babel-loader'
}]
},
resolveLoader: {
root: '%s'
}
};
""" % (
os.path.join(os.path.dirname(__file__), 'components'),
os.path.join(os.path.dirname(django_react.__file__), 'services', 'node_modules', 'react'),
os.path.join(os.path.dirname(django_react.__file__), 'services', 'node_modules'),
)
self.assertEqual(config, expected)

def test_can_generate_and_create_a_config_file(self):
filename = get_component_config_filename(HELLO_WORLD_COMPONENT_JS)
Expand Down

0 comments on commit 96147d9

Please sign in to comment.