33from compressor .filters import CompilerFilter
44from django .conf import settings
55from django .contrib .staticfiles import finders
6- from django .core .exceptions import ImproperlyConfigured
6+ from django .core .files . temp import NamedTemporaryFile
77
88
9- def get_all_static ():
10- """
11- Get all the static files directories found by ``STATICFILES_FINDERS``
9+ class BaseCompiler (CompilerFilter ):
10+ infile_ext = ''
1211
13- :return: set of paths (top-level folders only)
14- """
15- static_dirs = set ()
12+ def input (self , ** kwargs ):
13+ """
14+ Specify temporary input file extension.
15+
16+ Browserify requires explicit file extension (".js" or ".json" by default).
17+ https://github.com/substack/node-browserify/issues/1469
18+ """
19+ if self .infile is None and "{infile}" in self .command :
20+ if self .filename is None :
21+ self .infile = NamedTemporaryFile (mode = 'wb' , suffix = self .infile_ext )
22+ self .infile .write (self .content .encode (self .default_encoding ))
23+ self .infile .flush ()
24+ self .options += (
25+ ('infile' , self .infile .name ),
26+ )
27+ return super (BaseCompiler , self ).input (** kwargs )
28+
29+ @staticmethod
30+ def get_all_static ():
31+ """
32+ Get all the static files directories found by ``STATICFILES_FINDERS``
33+
34+ :return: set of paths (top-level folders only)
35+ """
36+ static_dirs = set ()
1637
17- for finder in settings .STATICFILES_FINDERS :
18- finder = finders .get_finder (finder )
38+ for finder in settings .STATICFILES_FINDERS :
39+ finder = finders .get_finder (finder )
1940
20- if hasattr (finder , 'storages' ):
21- for storage in finder .storages .values ():
22- static_dirs .add (storage .location )
41+ if hasattr (finder , 'storages' ):
42+ for storage in finder .storages .values ():
43+ static_dirs .add (storage .location )
2344
24- if hasattr (finder , 'storage' ):
25- static_dirs .add (finder .storage .location )
45+ if hasattr (finder , 'storage' ):
46+ static_dirs .add (finder .storage .location )
2647
27- return static_dirs
48+ return static_dirs
2849
2950
30- class SCSSFilter ( CompilerFilter ):
51+ class SCSSCompiler ( BaseCompiler ):
3152 """
3253 django-compressor pre-compiler for SCSS files.
3354
@@ -37,10 +58,12 @@ class SCSSFilter(CompilerFilter):
3758 2. ``postcss --use autoprefixer -r output.css``
3859 """
3960 command = (
40- 'node-sass --output-style expanded {include-static } {infile} {outfile} && '
61+ 'node-sass --output-style expanded {paths } {infile} {outfile} && '
4162 'postcss --use autoprefixer --autoprefixer.browsers "ie >= 9, > 5%" -r {outfile}'
4263 )
4364
65+ infile_ext = '.scss'
66+
4467 def __init__ (self , content , attrs , * args , ** kwargs ):
4568 """
4669 Include all available 'static' dirs:
@@ -56,47 +79,43 @@ def __init__(self, content, attrs, *args, **kwargs):
5679 font-size: $title-font-size;
5780 }
5881 """
59- static_dirs = get_all_static ()
60-
6182 self .options += (
62- ('include-static ' , ' ' .join (['--include-path {}' .format (s ) for s in static_dirs ])),
83+ ('paths ' , ' ' .join (['--include-path {}' .format (s ) for s in self . get_all_static () ])),
6384 )
6485
65- super (SCSSFilter , self ).__init__ (content , self .command , * args , ** kwargs )
86+ super (SCSSCompiler , self ).__init__ (content , self .command , * args , ** kwargs )
6687
6788
68- class ES6Filter ( CompilerFilter ):
89+ class ES6Compiler ( BaseCompiler ):
6990 """
7091 django-compressor pre-compiler for ES6 files.
7192
72- Transforms ES6 to ES5 AMD module using ``babel`` .
93+ Transforms ES6 to ES5 using Browserify + Babel .
7394 """
7495 command = (
75- 'NPM_ROOT=`npm root -g` && '
76- 'babel --presets=$NPM_ROOT/babel-preset-es2015 '
77- '--plugins=$NPM_ROOT/babel-plugin-transform-es2015-modules-amd '
78- '--module-id={module-id} "{infile}" -o "{outfile}"'
96+ 'export NODE_PATH={paths} && '
97+ 'browserify "{infile}" -o "{outfile}" --no-bundle-external --node '
98+ '-t [ {node_modules}/babelify --presets={node_modules}/babel-preset-es2015 ]'
7999 )
80100
101+ infile_ext = '.js'
102+
81103 def __init__ (self , content , attrs , * args , ** kwargs ):
82104 """
83- Add extra option for compiler :
105+ Include all available 'static' dirs :
84106
85- 'module-id': ' app/script'
107+ export NODE_PATH="path/to/app-1/static/:path/to/ app-2/static/" && browserify ...
86108
87- That's AMD module ID for '/static/app/script.js' static file.
88- """
89- module_id = attrs .get ('data-module-id' )
90- if not module_id :
91- module_src = attrs .get ('src' )
92- if not module_src :
93- raise ImproperlyConfigured (
94- "Module should contain either \" data-module-id\" or \" src\" attribute"
95- )
96- module_id = os .path .splitext (module_src .replace (settings .STATIC_URL , '' ))[0 ]
109+ So you can do imports inside your ES6 modules:
97110
111+ import controller from 'app-1/page-controller';
112+ import { login, signup } from 'app-2/pages';
113+
114+ controller.registerPages(login, signup);
115+ """
98116 self .options += (
99- ('module-id' , module_id ),
117+ ('node_modules' , getattr (settings , 'COMPRESS_NODE_MODULES' , '/usr/lib/node_modules' )),
118+ ('paths' , os .pathsep .join (self .get_all_static ())),
100119 )
101120
102- super (ES6Filter , self ).__init__ (content , self .command , * args , ** kwargs )
121+ super (ES6Compiler , self ).__init__ (content , self .command , * args , ** kwargs )
0 commit comments