Skip to content

Commit 0aeadef

Browse files
committed
Code refactoring
1 parent 23df1ae commit 0aeadef

File tree

5 files changed

+81
-70
lines changed

5 files changed

+81
-70
lines changed

compressor_toolkit/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33

44
# PEP 396 - module version variable
55
__version__ = '.'.join(map(str, VERSION))
6+
7+
default_app_config = 'compressor_toolkit.apps.CompressorToolkitConfig'

compressor_toolkit/apps.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from django.apps.config import AppConfig
2+
from django.conf import settings
3+
4+
5+
class CompressorToolkitConfig(AppConfig):
6+
name = 'compressor_toolkit'
7+
8+
# Custom SCSS transpiler command
9+
SCSS_COMPILER_CMD = getattr(settings, 'COMPRESS_SCSS_COMPILER_CMD', None)
10+
11+
# Custom ES6 transpiler command
12+
ES6_COMPILER_CMD = getattr(settings, 'COMPRESS_ES6_COMPILER_CMD', None)
13+
14+
# Path to 'node_modules' where browserify, babelify, autoprefixer, etc, are installed
15+
NODE_MODULES = getattr(settings, 'COMPRESS_NODE_MODULES', None) or '/usr/lib/node_modules'

compressor_toolkit/precompilers.py

Lines changed: 61 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,38 @@
11
import os
22

33
from compressor.filters import CompilerFilter
4+
from django.apps import apps
45
from django.conf import settings
56
from django.contrib.staticfiles import finders
67
from 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+
934
class 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

5156
class 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

8990
class 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)

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[pytest]
22
python_paths = tests/test_project
3-
testpaths = tests
3+
testpaths = tests/unit_tests/ tests/integration_tests/
44
addopts =
55
--ds test_project.settings
66
--cov compressor_toolkit

tests/test_project/test_project/settings.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,5 @@
2929
)
3030
COMPRESS_ENABLED = False
3131

32-
# path to folder with installed Node.js packages
33-
# auto-detect it for test purposes
34-
COMPRESS_NODE_PACKAGES = '`npm root -g`'
32+
# django-compressor-toolkit settings; see compressor_toolkit/apps.py for details
33+
COMPRESS_NODE_MODULES = os.getenv('COMPRESS_NODE_MODULES')

0 commit comments

Comments
 (0)