11import os
22
33from compressor .filters import CompilerFilter
4+ from django .apps import apps
45from django .conf import settings
56from django .contrib .staticfiles import finders
67from django .core .files .temp import NamedTemporaryFile
78
89
10+ app_config = apps .get_app_config ('compressor_toolkit' )
11+
12+
13+ def get_all_static ():
14+ """
15+ Get all the static files directories found by ``STATICFILES_FINDERS``
16+
17+ :return: set of paths (top-level folders only)
18+ """
19+ static_dirs = set ()
20+
21+ for finder in settings .STATICFILES_FINDERS :
22+ finder = finders .get_finder (finder )
23+
24+ if hasattr (finder , 'storages' ):
25+ for storage in finder .storages .values ():
26+ static_dirs .add (storage .location )
27+
28+ if hasattr (finder , 'storage' ):
29+ static_dirs .add (finder .storage .location )
30+
31+ return static_dirs
32+
33+
934class BaseCompiler (CompilerFilter ):
35+ # Temporary input file extension
1036 infile_ext = ''
1137
1238 def input (self , ** kwargs ):
@@ -26,27 +52,6 @@ def input(self, **kwargs):
2652 )
2753 return super (BaseCompiler , self ).input (** kwargs )
2854
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 ()
37-
38- for finder in settings .STATICFILES_FINDERS :
39- finder = finders .get_finder (finder )
40-
41- if hasattr (finder , 'storages' ):
42- for storage in finder .storages .values ():
43- static_dirs .add (storage .location )
44-
45- if hasattr (finder , 'storage' ):
46- static_dirs .add (finder .storage .location )
47-
48- return static_dirs
49-
5055
5156class SCSSCompiler (BaseCompiler ):
5257 """
@@ -56,66 +61,56 @@ class SCSSCompiler(BaseCompiler):
5661
5762 1. ``node-sass input.scss output.css``
5863 2. ``postcss --use autoprefixer -r output.css``
59- """
60- command = (
61- 'node-sass --output-style expanded {paths} {infile} {outfile} && '
62- 'postcss --use autoprefixer --autoprefixer.browsers "ie >= 9, > 5%" -r {outfile}'
63- )
6464
65- infile_ext = '.scss'
65+ Includes all available 'static' dirs:
6666
67- def __init__ (self , content , attrs , * args , ** kwargs ):
68- """
69- Include all available 'static' dirs:
67+ node-sass --include-path path/to/app-1/static/ --include-path path/to/app-2/static/ ...
7068
71- node-sass --include-path path/to/app-1/static/ --include-path path/to/app-2/static/ ...
69+ So you can do imports inside your SCSS files:
7270
73- So you can do imports inside your SCSS files:
71+ @import "app-1/scss/mixins";
72+ @import "app-2/scss/variables";
7473
75- @import "app-1/scss/mixins";
76- @import "app-2/scss/variables";
77-
78- .page-title {
79- font-size: $title-font-size;
80- }
81- """
82- self .options += (
83- ('paths' , ' ' .join (['--include-path {}' .format (s ) for s in self .get_all_static ()])),
84- )
85-
86- super (SCSSCompiler , self ).__init__ (content , self .command , * args , ** kwargs )
74+ .page-title {
75+ font-size: $title-font-size;
76+ }
77+ """
78+ command = app_config .SCSS_COMPILER_CMD or (
79+ 'node-sass --output-style expanded {paths} {infile} {outfile} && '
80+ 'postcss --use {node_modules}/autoprefixer '
81+ '--autoprefixer.browsers "ie >= 9, > 5%" -r {outfile}'
82+ )
83+ options = (
84+ ('paths' , ' ' .join (['--include-path {}' .format (s ) for s in get_all_static ()])),
85+ ('node_modules' , app_config .NODE_MODULES ),
86+ )
87+ infile_ext = '.scss'
8788
8889
8990class ES6Compiler (BaseCompiler ):
9091 """
9192 django-compressor pre-compiler for ES6 files.
9293
9394 Transforms ES6 to ES5 using Browserify + Babel.
95+
96+ Includes all available 'static' dirs:
97+
98+ export NODE_PATH="path/to/app-1/static/:path/to/app-2/static/" && browserify ...
99+
100+ So you can do imports inside your ES6 modules:
101+
102+ import controller from 'app-1/page-controller';
103+ import { login, signup } from 'app-2/pages';
104+
105+ controller.registerPages(login, signup);
94106 """
95- command = (
107+ command = app_config . ES6_COMPILER_CMD or (
96108 'export NODE_PATH={paths} && '
97109 'browserify "{infile}" -o "{outfile}" --no-bundle-external --node '
98110 '-t [ {node_modules}/babelify --presets={node_modules}/babel-preset-es2015 ]'
99111 )
100-
112+ options = (
113+ ('paths' , os .pathsep .join (get_all_static ())),
114+ ('node_modules' , app_config .NODE_MODULES )
115+ )
101116 infile_ext = '.js'
102-
103- def __init__ (self , content , attrs , * args , ** kwargs ):
104- """
105- Include all available 'static' dirs:
106-
107- export NODE_PATH="path/to/app-1/static/:path/to/app-2/static/" && browserify ...
108-
109- So you can do imports inside your ES6 modules:
110-
111- import controller from 'app-1/page-controller';
112- import { login, signup } from 'app-2/pages';
113-
114- controller.registerPages(login, signup);
115- """
116- self .options += (
117- ('node_modules' , getattr (settings , 'COMPRESS_NODE_MODULES' , '/usr/lib/node_modules' )),
118- ('paths' , os .pathsep .join (self .get_all_static ())),
119- )
120-
121- super (ES6Compiler , self ).__init__ (content , self .command , * args , ** kwargs )
0 commit comments