Skip to content

Add the implementation of LiveScript #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Dev
- Add source maps support for CoffeeScript
- Add source maps support for Stylus
- Add source maps support for Babel

- Add the implementation of LiveScript

1.0.1
=====
Expand Down
22 changes: 21 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Django Static Precompiler
=========================

Django Static Precompiler provides template tags and filters to compile CoffeeScript, SASS / SCSS, LESS, Stylus, and Babel.
Django Static Precompiler provides template tags and filters to compile CoffeeScript, LiveScript, SASS / SCSS, LESS, Stylus, and Babel.
It works with both inline code and external files.

.. image:: https://circleci.com/gh/andreyfedoseev/django-static-precompiler.svg?style=shield
Expand Down Expand Up @@ -69,12 +69,14 @@ Example Usage
{% load static %}

<script src="{% static "path/to/script.coffee"|compile %}"></script>
<script src="{% static "path/to/script2.ls"|compile %}"></script>
<link rel="stylesheet" href="{% static "path/to/styles1.less"|compile %}" />
<link rel="stylesheet" href="{% static "path/to/styles2.scss"|compile %}" />

renders to::

<script src="/static/COMPILED/path/to/script.js"></script>
<script src="/static/COMPILED/path/to/script2.js"></script>
<link rel="stylesheet" href="/static/COMPILED/path/to/styles1.css" />
<link rel="stylesheet" href="/static/COMPILED/path/to/styles2.css" />

Expand All @@ -90,6 +92,7 @@ Compiler must be specified in ``STATIC_PRECOMPILER_COMPILERS`` setting. Names fo
* ``sass``
* ``scss``
* ``stylus``
* ``livescript``

Example Usage
-------------
Expand Down Expand Up @@ -125,6 +128,7 @@ General settings
'static_precompiler.compilers.SCSS',
'static_precompiler.compilers.LESS',
'static_precompiler.compilers.Stylus',
'static_precompiler.compilers.LiveScript',
)

You can specify compiler options using the following format::
Expand Down Expand Up @@ -184,6 +188,22 @@ Example::
)


LiveScript
------------

``executable``
Path to LiveScript compiler executable. Default: ``"lsc"``.

``sourcemap_enabled``
Boolean. Set to ``True`` to enable source maps. Default: ``False``

Example::

STATIC_PRECOMPILER_COMPILERS = (
('static_precompiler.compilers.LiveScript', {"executable": "/usr/bin/lsc", "sourcemap_enabled": True}),
)


Babel
-----

Expand Down
1 change: 1 addition & 0 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ dependencies:
pre:
- sudo apt-get install python2.7-dev python3.4-dev
- npm install -g [email protected]
- npm install -g [email protected]
- npm install -g [email protected]
- npm install -g [email protected]
- npm install -g [email protected]
Expand Down
1 change: 1 addition & 0 deletions provision.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ if [ ! -e /usr/local/bin/node ]; then
fi

sudo npm install -g [email protected]
sudo npm install -g [email protected]
sudo npm install -g [email protected]
sudo npm install -g [email protected]

Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def read(fname):
author_email="[email protected]",
url="https://github.com/andreyfedoseev/django-static-precompiler",
description="Django template tags to compile all kinds of static files "
"(SASS, LESS, Stylus, CoffeeScript, Babel).",
"(SASS, LESS, Stylus, CoffeeScript, Babel, LiveScript).",
long_description="\n\n".join([README, CHANGES]),
classifiers=[
'Development Status :: 4 - Beta',
Expand All @@ -58,7 +58,7 @@ def read(fname):
'Programming Language :: Python :: 3',
'Topic :: Internet :: WWW/HTTP',
],
keywords=["sass", "scss", "less", "stylus", "css", "coffeescript", "javascript", "babel"],
keywords=["sass", "scss", "less", "stylus", "css", "coffeescript", "javascript", "babel", "livescript"],
tests_require=[
"pytest",
"pytest-django",
Expand Down
1 change: 1 addition & 0 deletions static_precompiler/compilers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
from .scss import * # noqa
from .less import * # noqa
from .stylus import * # noqa
from .livescript import * # noqa
78 changes: 78 additions & 0 deletions static_precompiler/compilers/livescript.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import json
import os
import posixpath

from static_precompiler import exceptions, utils

from . import base

__all__ = (
"LiveScript",
)


class LiveScript(base.BaseCompiler):

name = "livescript"
input_extension = "ls"
output_extension = "js"

def __init__(self, executable='lsc', sourcemap_enabled=False):
self.executable = executable
self.is_sourcemap_enabled = sourcemap_enabled
super(LiveScript, self).__init__()

def compile_file(self, source_path):
full_output_path = self.get_full_output_path(source_path)
# LiveScript bug with source map if the folder isn't already present
if not os.path.exists(os.path.dirname(full_output_path)):
os.makedirs(os.path.dirname(full_output_path))
args = [
self.executable,
"-c",
]
if self.is_sourcemap_enabled:
args.append("-m")
args.append("linked")
args.extend([
"-o", os.path.dirname(full_output_path),
self.get_full_source_path(source_path),
])
out, errors = utils.run_command(args)

if errors:
raise exceptions.StaticCompilationError(errors)

if self.is_sourcemap_enabled:
# Livescript writes source maps to compiled.js.map
sourcemap_full_path = full_output_path + ".map"

with open(sourcemap_full_path) as sourcemap_file:
sourcemap = json.loads(sourcemap_file.read())

# LiveScript, unlike SASS, can't add correct relative paths in source map when the compiled file
# is not in the same dir as the source file. We fix it here.
sourcemap["sourceRoot"] = "../" * len(source_path.split("/")) + posixpath.dirname(source_path)
sourcemap["sources"] = [os.path.basename(source) for source in sourcemap["sources"]]
sourcemap["file"] = posixpath.basename(os.path.basename(full_output_path))

with open(sourcemap_full_path, "w") as sourcemap_file:
sourcemap_file.write(json.dumps(sourcemap))

return self.get_output_path(source_path)

def compile_source(self, source):
args = [
self.executable,
"-c",
"-s",
"-p",
]
out, errors = utils.run_command(args, source)
if errors:
raise exceptions.StaticCompilationError(errors)

return out

def find_dependencies(self, source_path):
return []
1 change: 1 addition & 0 deletions static_precompiler/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"static_precompiler.compilers.SCSS",
"static_precompiler.compilers.LESS",
"static_precompiler.compilers.Stylus",
"static_precompiler.compilers.LiveScript",
))

ROOT = getattr(settings, "STATIC_PRECOMPILER_ROOT",
Expand Down
1 change: 1 addition & 0 deletions static_precompiler/tests/static/scripts/test.ls
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log "Hello, World!"
57 changes: 57 additions & 0 deletions static_precompiler/tests/test_livescript.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# coding: utf-8
import os

import pytest

from static_precompiler import compilers, exceptions


def clean_javascript(js):
""" Remove comments and all blank lines. """
return "\n".join(
line for line in js.split("\n") if line.strip() and not line.startswith("//")
)


def test_compile_file(monkeypatch, tmpdir):
monkeypatch.setattr("static_precompiler.settings.ROOT", tmpdir.strpath)

compiler = compilers.LiveScript()

assert clean_javascript(compiler.compile_file("scripts/test.ls")) == "COMPILED/scripts/test.js"
assert os.path.exists(compiler.get_full_output_path("scripts/test.ls"))
with open(compiler.get_full_output_path("scripts/test.ls")) as compiled:
assert clean_javascript(compiled.read()) == """(function(){\n console.log("Hello, World!");\n}).call(this);"""


def test_sourcemap(monkeypatch, tmpdir):

monkeypatch.setattr("static_precompiler.settings.ROOT", tmpdir.strpath)

compiler = compilers.LiveScript(sourcemap_enabled=False)
compiler.compile_file("scripts/test.ls")
full_output_path = compiler.get_full_output_path("scripts/test.ls")
assert not os.path.exists(full_output_path + ".map")

compiler = compilers.LiveScript(sourcemap_enabled=True)
compiler.compile_file("scripts/test.ls")
full_output_path = compiler.get_full_output_path("scripts/test.ls")
assert os.path.exists(full_output_path + ".map")


def test_compile_source():
compiler = compilers.LiveScript()

assert (
clean_javascript(compiler.compile_source('console.log "Hello, World!"')) ==
"""(function(){\n console.log("Hello, World!");\n}).call(this);"""
)

with pytest.raises(exceptions.StaticCompilationError):
compiler.compile_source('console.log "Hello, World!')

# Test non-ascii
assert (
clean_javascript(compiler.compile_source('console.log "Привет, Мир!"')) ==
"""(function(){\n console.log("Привет, Мир!");\n}).call(this);"""
)