11import os
22import re
33import tempfile
4- from django_webpack .compiler import webpack
4+ from webpack .compiler import webpack
5+ from service_host .conf import settings as service_host_settings
56from .templates import BUNDLE_CONFIG , BUNDLE_TRANSLATE_CONFIG , DEV_TOOL_CONFIG
67from .conf import settings
78
8- COMPONENT_CONFIG_FILES = {}
9-
10- PATH_TO_NODE_MODULES = os .path .join (os .path .dirname (__file__ ), 'services' , 'node_modules' )
11-
129
1310def bundle_component (path , translate = None , watch_source = None ):
1411 filename = get_component_config_filename (path , translate )
1512 return webpack (filename , watch_source = watch_source )
1613
14+ # TODO: replace this with a deterministic config file writer in webpack
15+ COMPONENT_CONFIG_FILES = {}
1716
18- def get_var_from_path (path ):
19- var = '{parent_dir}__{filename}' .format (
20- parent_dir = os .path .basename (os .path .dirname (path )),
21- filename = os .path .splitext (os .path .basename (path ))[0 ]
22- )
23- return re .sub (r'\W+' , '_' , var )
17+
18+ def get_component_config_filename (path , translate = None ):
19+ cache_key = (path , translate )
20+ if cache_key in COMPONENT_CONFIG_FILES :
21+ return COMPONENT_CONFIG_FILES [cache_key ]
22+
23+ config = get_webpack_config (path , translate )
24+ filename = tempfile .mkstemp (suffix = '.webpack.config.js' )[1 ]
25+ with open (filename , 'w' ) as config_file :
26+ config_file .write (config )
27+
28+ COMPONENT_CONFIG_FILES [cache_key ] = filename
29+
30+ return filename
2431
2532
2633def get_webpack_config (path , translate = None ):
2734 var = get_var_from_path (path )
2835
36+ # TODO: clean up and scrap resolve?
37+ # TODO: probably easiest to rely on node_modules babel-loader and provide a `path_to_react` arg
38+ # TODO: actually, given that this is a pure convenience thing, I think just make it as rigid as possible
39+ node_modules = os .path .join (service_host_settings .SOURCE_ROOT , 'node_modules' )
40+
2941 translate_config = ''
3042 if translate :
3143 # JSX + ES6/7 support
3244 translate_config += BUNDLE_TRANSLATE_CONFIG .format (
3345 ext = os .path .splitext (path )[- 1 ],
34- node_modules = PATH_TO_NODE_MODULES
46+ node_modules = node_modules
3547 )
3648
3749 dev_tool_config = ''
3850 if settings .DEV_TOOL :
3951 dev_tool_config = DEV_TOOL_CONFIG
4052
4153 return BUNDLE_CONFIG .format (
42- path_to_resolve = os .path .join (PATH_TO_NODE_MODULES , 'resolve ' ),
54+ path_to_react = os .path .join (node_modules , 'react ' ),
4355 dir = os .path .dirname (path ),
4456 file = './' + os .path .basename (path ),
4557 var = var ,
@@ -48,16 +60,9 @@ def get_webpack_config(path, translate=None):
4860 )
4961
5062
51- def get_component_config_filename (path , translate = None ):
52- cache_key = (path , translate )
53- if cache_key in COMPONENT_CONFIG_FILES :
54- return COMPONENT_CONFIG_FILES [cache_key ]
55-
56- config = get_webpack_config (path , translate )
57- filename = tempfile .mkstemp (suffix = '.webpack.config.js' )[1 ]
58- with open (filename , 'w' ) as config_file :
59- config_file .write (config )
60-
61- COMPONENT_CONFIG_FILES [cache_key ] = filename
62-
63- return filename
63+ def get_var_from_path (path ):
64+ var = '{parent_dir}__{filename}' .format (
65+ parent_dir = os .path .basename (os .path .dirname (path )),
66+ filename = os .path .splitext (os .path .basename (path ))[0 ]
67+ )
68+ return re .sub (r'\W+' , '_' , var )
0 commit comments