Skip to content

Commit 415665b

Browse files
author
Andrey Fedoseev
committed
Add source maps support for Babel
1 parent 335a174 commit 415665b

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Dev
99
- Add source maps support for LESS
1010
- Add source maps support for CoffeeScript
1111
- Add source maps support for Stylus
12+
- Add source maps support for Babel
1213

1314

1415
1.0.1

README.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,16 @@ Babel
190190
``executable``
191191
Path to Babel compiler executable. Default: ``"babel"``.
192192

193+
``sourcemap_enabled``
194+
Boolean. Set to ``True`` to enable source maps. Default: ``False``
195+
193196
``modules``
194197
Babel `modules <https://babeljs.io/docs/usage/modules/>`_ command line option. Default: ``None`` (uses Babel's default option).
195198

196199
Example::
197200

198201
STATIC_PRECOMPILER_COMPILERS = (
199-
('static_precompiler.compilers.Babel', {"executable": "/usr/bin/babel", "modules": "amd"}),
202+
('static_precompiler.compilers.Babel', {"executable": "/usr/bin/babel", "sourcemap_enabled": True, "modules": "amd"}),
200203
)
201204

202205

static_precompiler/compilers/babel.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import json
12
import os
3+
import posixpath
24

35
from static_precompiler import exceptions, utils
46

@@ -15,16 +17,20 @@ class Babel(base.BaseCompiler):
1517
input_extension = "es6"
1618
output_extension = "js"
1719

18-
def __init__(self, executable="babel", modules=None):
20+
def __init__(self, executable="babel", sourcemap_enabled=False, modules=None):
1921
self.executable = executable
22+
self.is_sourcemap_enabled = sourcemap_enabled
2023
self.modules = modules
2124
super(Babel, self).__init__()
2225

2326
def compile_file(self, source_path):
2427
args = [
25-
self.executable
28+
self.executable,
2629
]
2730

31+
if self.is_sourcemap_enabled:
32+
args.append("-s")
33+
2834
if self.modules is not None:
2935
args.extend(["--modules", self.modules])
3036

@@ -41,6 +47,21 @@ def compile_file(self, source_path):
4147
if errors:
4248
raise exceptions.StaticCompilationError(errors)
4349

50+
if self.is_sourcemap_enabled:
51+
sourcemap_full_path = full_output_path + ".map"
52+
53+
with open(sourcemap_full_path) as sourcemap_file:
54+
sourcemap = json.loads(sourcemap_file.read())
55+
56+
# Babel can't add correct relative paths in source map when the compiled file
57+
# is not in the same dir as the source file. We fix it here.
58+
sourcemap["sourceRoot"] = "../" * len(source_path.split("/")) + posixpath.dirname(source_path)
59+
sourcemap["sources"] = [os.path.basename(source) for source in sourcemap["sources"]]
60+
sourcemap["file"] = posixpath.basename(os.path.basename(full_output_path))
61+
62+
with open(sourcemap_full_path, "w") as sourcemap_file:
63+
sourcemap_file.write(json.dumps(sourcemap))
64+
4465
return self.get_output_path(source_path)
4566

4667
def compile_source(self, source):

static_precompiler/tests/test_babel.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@ def test_compile_file(monkeypatch, tmpdir):
2626
"""
2727

2828

29+
def test_sourcemap(monkeypatch, tmpdir):
30+
31+
monkeypatch.setattr("static_precompiler.settings.ROOT", tmpdir.strpath)
32+
33+
compiler = compilers.Babel(sourcemap_enabled=False)
34+
compiler.compile_file("scripts/test.es6")
35+
full_output_path = compiler.get_full_output_path("scripts/test.es6")
36+
assert not os.path.exists(full_output_path + ".map")
37+
38+
compiler = compilers.Babel(sourcemap_enabled=True)
39+
compiler.compile_file("scripts/test.es6")
40+
full_output_path = compiler.get_full_output_path("scripts/test.es6")
41+
assert os.path.exists(full_output_path + ".map")
42+
43+
2944
def test_compile_source():
3045
compiler = compilers.Babel()
3146

0 commit comments

Comments
 (0)