24
24
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25
25
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
26
27
- """Bundle assets using WebAssets ."""
27
+ """Bundle assets."""
28
28
29
29
30
30
import configparser
31
31
import io
32
32
import itertools
33
33
import os
34
-
35
- try :
36
- import webassets
37
- except ImportError :
38
- webassets = None # NOQA
34
+ import shutil
39
35
40
36
from nikola .plugin_categories import LateTask
41
37
from nikola import utils
42
38
43
39
44
40
class BuildBundles (LateTask ):
45
- """Bundle assets using WebAssets ."""
41
+ """Bundle assets."""
46
42
47
43
name = "create_bundles"
48
44
49
- def set_site (self , site ):
50
- """Set Nikola site."""
51
- super (BuildBundles , self ).set_site (site )
52
- if webassets is None and site .configured and site .config ['USE_BUNDLES' ]:
53
- utils .req_missing (['webassets' ], 'USE_BUNDLES' , optional = True )
54
- self .logger .warn ('Setting USE_BUNDLES to False.' )
55
- site .config ['USE_BUNDLES' ] = False
56
- site ._GLOBAL_CONTEXT ['use_bundles' ] = False
57
-
58
45
def gen_tasks (self ):
59
- """Bundle assets using WebAssets ."""
46
+ """Bundle assets."""
60
47
kw = {
61
48
'filters' : self .site .config ['FILTERS' ],
62
49
'output_folder' : self .site .config ['OUTPUT_FOLDER' ],
@@ -70,28 +57,20 @@ def gen_tasks(self):
70
57
def build_bundle (output , inputs ):
71
58
out_dir = os .path .join (kw ['output_folder' ],
72
59
os .path .dirname (output ))
73
- inputs = [os .path .relpath (i , out_dir ) for i in inputs if os .path .isfile (i )]
74
- cache_dir = os .path .join (kw ['cache_folder' ], 'webassets' )
75
- utils .makedirs (cache_dir )
76
- env = webassets .Environment (out_dir , os .path .dirname (output ),
77
- cache = cache_dir )
78
- if inputs :
79
- bundle = webassets .Bundle (* inputs , output = os .path .basename (output ))
80
- env .register (output , bundle )
81
- # This generates the file
82
- try :
83
- env [output ].build (force = True )
84
- except Exception as e :
85
- self .logger .error ("Failed to build bundles." )
86
- self .logger .exception (e )
87
- self .logger .notice ("Try running ``nikola clean`` and building again." )
88
- else :
89
- with open (os .path .join (out_dir , os .path .basename (output )), 'wb+' ):
90
- pass # Create empty file
60
+ inputs = [
61
+ os .path .join (
62
+ out_dir ,
63
+ os .path .relpath (i , out_dir ))
64
+ for i in inputs if os .path .isfile (i )
65
+ ]
66
+ with open (os .path .join (out_dir , os .path .basename (output )), 'wb+' ) as out_fh :
67
+ for i in inputs :
68
+ with open (i , 'rb' ) as in_fh :
69
+ shutil .copyfileobj (in_fh , out_fh )
91
70
92
71
yield self .group_task ()
93
- if ( webassets is not None and self . site . config [ 'USE_BUNDLES' ] is not
94
- False ) :
72
+
73
+ if self . site . config [ 'USE_BUNDLES' ] :
95
74
for name , _files in kw ['theme_bundles' ].items ():
96
75
output_path = os .path .join (kw ['output_folder' ], name )
97
76
dname = os .path .dirname (name )
0 commit comments