forked from markfinger/python-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbundle.py
More file actions
142 lines (108 loc) · 3.53 KB
/
Copy pathbundle.py
File metadata and controls
142 lines (108 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import json
import os
import re
from optional_django import staticfiles
from webpack.compiler import webpack
from webpack.config_file import ConfigFile, JS
from js_host.conf import settings as js_host_settings
from .exceptions import ComponentSourceFileNotFound
from .conf import settings
def bundle_component(path, translate=None, devtool=None):
if not os.path.isabs(path):
abs_path = staticfiles.find(path)
if not abs_path:
raise ComponentSourceFileNotFound(path)
path = abs_path
if not os.path.exists(path):
raise ComponentSourceFileNotFound(path)
config = generate_config_for_component(path, translate=translate, devtool=devtool)
config_file = generate_config_file(config)
var = generate_var_from_path(path)
path_to_config_file = get_path_to_config_file(config_file, prefix=var + '.')
return webpack(path_to_config_file)
def get_path_to_config_file(config_file, prefix=None):
path = config_file.generate_path_to_file(prefix=prefix)
return config_file.write(path, force=False)
def generate_config_file(config):
return ConfigFile(
JS('var path = require("path");\n'),
JS('module.exports = '),
config,
JS(';'),
)
def generate_config_for_component(path, translate=None, devtool=None):
"""
Generates a webpack config object to bundle a component
"""
var = generate_var_from_path(path)
node_modules = os.path.join(js_host_settings.SOURCE_ROOT, 'node_modules')
config = {
'context': js_path_join(os.path.dirname(path)),
'entry': './{}'.format(os.path.basename(path)),
'output': {
'path': js_path_join(os.path.join('[bundle_dir]', 'components')),
'filename': var + '-[hash].js',
'libraryTarget': 'umd',
'library': var
},
'externals': [{
'react': {
'commonjs2': 'react',
'root': 'React'
},
'react/addons': {
'commonjs2': 'react',
'root': 'React'
}
}]
}
if translate:
config.update({
'module': {
'loaders': [{
'test': JS(settings.TRANSLATE_TEST),
'exclude': JS('/node_modules/'),
'loader': 'babel-loader'
}]
},
'resolveLoader': {
'root': js_path_join(node_modules)
}
})
if devtool:
config['devtool'] = devtool
return config
def split_path(path):
"""
Splits a path into the various parts and returns a list
"""
parts = []
drive, path = os.path.splitdrive(path)
while True:
newpath, tail = os.path.split(path)
if newpath == path:
assert not tail
if path:
parts.append(path)
break
parts.append(tail)
path = newpath
if drive:
parts.append(drive)
parts.reverse()
return parts
def js_path_join(path):
"""
Splits a path so that it can be rejoined by the JS engine. Helps to avoid
OS compatibility issues due to string encoding
"""
return JS('path.join.apply(path, ' + json.dumps(split_path(path)) + ')')
def generate_var_from_path(path):
"""
Infer a variable name from a path
"""
var = '{parent_dir}__{filename}'.format(
parent_dir=os.path.basename(os.path.dirname(path)),
filename=os.path.splitext(os.path.basename(path))[0]
)
return re.sub(r'\W+', '_', var)