Skip to content

Commit ce5cded

Browse files
author
Andrey Fedoseev
committed
Merge pull request #46 from sepulchered/master
es6 compiler via 6to5 added
2 parents b7eddc3 + 1e6ee57 commit ce5cded

File tree

6 files changed

+89
-0
lines changed

6 files changed

+89
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# coding: utf-8
22
from static_precompiler.compilers.coffeescript import CoffeeScript
3+
from static_precompiler.compilers.es6 import ES6Script
34
from static_precompiler.compilers.scss import SASS, SCSS
45
from static_precompiler.compilers.less import LESS
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from static_precompiler.exceptions import StaticCompilationError
2+
from static_precompiler.compilers.base import BaseCompiler
3+
from static_precompiler.settings import ES6_EXECUTABLE
4+
from static_precompiler.utils import run_command
5+
6+
7+
class ES6Script(BaseCompiler):
8+
name = "es6"
9+
input_extension = "es6"
10+
output_extension = "js"
11+
12+
def compile_file(self, source_path):
13+
return self.compile_source(self.get_source(source_path))
14+
15+
def compile_source(self, source):
16+
args = [
17+
ES6_EXECUTABLE
18+
]
19+
out, errors = run_command(args, source)
20+
if errors:
21+
raise StaticCompilationError(errors)
22+
23+
return out

static_precompiler/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
COMPILERS = getattr(settings, "STATIC_PRECOMPILER_COMPILERS", (
1515
"static_precompiler.compilers.CoffeeScript",
16+
"static_precompiler.compilers.ES6Script",
1617
"static_precompiler.compilers.SASS",
1718
"static_precompiler.compilers.SCSS",
1819
"static_precompiler.compilers.LESS",
@@ -54,6 +55,7 @@
5455

5556

5657
COFFEESCRIPT_EXECUTABLE = getattr(settings, "COFFEESCRIPT_EXECUTABLE", "coffee")
58+
ES6_EXECUTABLE = getattr(settings, "ES6_EXECUTABLE", "6to5")
5759
SCSS_EXECUTABLE = getattr(settings, "SCSS_EXECUTABLE", "sass")
5860
SCSS_USE_COMPASS = getattr(settings, "SCSS_USE_COMPASS", False)
5961
LESS_EXECUTABLE = getattr(settings, "LESS_EXECUTABLE", "lessc")
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from django.template.base import Library
2+
from static_precompiler.compilers import ES6Script
3+
from static_precompiler.templatetags.compile_static import register_compiler_tags
4+
5+
6+
register = Library()
7+
compiler = ES6Script()
8+
9+
10+
register_compiler_tags(register, compiler)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("Hello, World!");
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# coding: utf-8
2+
from static_precompiler.compilers.es6 import ES6Script
3+
from static_precompiler.exceptions import StaticCompilationError
4+
import unittest
5+
6+
7+
class ES6ScriptTestCase(unittest.TestCase):
8+
9+
@staticmethod
10+
def clean_javascript(js):
11+
""" Remove comments and all blank lines. """
12+
return "\n".join(
13+
line for line in js.split("\n") if line.strip() and not line.startswith("//")
14+
)
15+
16+
def test_compile_file(self):
17+
compiler = ES6Script()
18+
19+
self.assertEqual(
20+
self.clean_javascript(compiler.compile_file("scripts/test.es6")),
21+
""""use strict";\nconsole.log("Hello, World!");"""
22+
)
23+
24+
def test_compile_source(self):
25+
compiler = ES6Script()
26+
27+
self.assertEqual(
28+
self.clean_javascript(compiler.compile_source('console.log("Hello, World!");')),
29+
""""use strict";\nconsole.log("Hello, World!");"""
30+
)
31+
32+
self.assertRaises(
33+
StaticCompilationError,
34+
lambda: compiler.compile_source('console.log "Hello, World!')
35+
)
36+
37+
# Test non-ascii
38+
self.assertEqual(
39+
self.clean_javascript(compiler.compile_source('console.log("Привет, Мир!");')),
40+
""""use strict";\nconsole.log("Привет, Мир!");"""
41+
)
42+
43+
44+
def suite():
45+
loader = unittest.TestLoader()
46+
test_suite = unittest.TestSuite()
47+
test_suite.addTest(loader.loadTestsFromTestCase(ES6ScriptTestCase))
48+
return test_suite
49+
50+
51+
if __name__ == '__main__':
52+
unittest.TextTestRunner(verbosity=2).run(suite())

0 commit comments

Comments
 (0)