Exemplo n.º 1
0
 def test_transform_string(self):
     code = transformer.transform_string('''
     const a = () => 233;
     const b = <div></div>;
     ''')
     self.assertEqual(
         '"use strict";\n\nvar a = function a() {\n        return 233;\n};'
         '\nvar b = React.createElement("div", null);', code)
Exemplo n.º 2
0
def babel(config):
    """
    (DEPRECATED) Use babel.js with ES6/2015+.  Generates ES5-compatible
    JavaScript for older browsers.  Note that wq babel is run after
    wq optimize, on the compiled modules created by r.js.  For more control
    over the compilation process, use `wq start --with-npm` instead of
    an r.js-based build.

    Note that this command will be removed in wq.app 2.0 in favor of
    `wq start --with-npm`.
    """
    rconf = config.get('optimize', None)
    if not rconf:
        raise click.UsageError("optimize section not found in %s" %
                               config.filename)

    babel = config.get('babel', {})
    files = []
    if 'modules' in rconf and 'dir' in rconf:
        base_url = rconf.get('baseUrl', '.')
        for module in rconf['modules']:
            path = module['name']
            if path in rconf.get('paths', {}):
                path = rconf['paths'][path]
            path = os.path.join(rconf['dir'], base_url, path)
            files.append(path + '.js')

    for filename in files:
        label = os.path.normpath(filename)
        try:
            with open(filename) as f:
                content = f.read()
        except OSError:
            raise click.ClickException(
                "Error loading %s - run wq optimize first?" % label)
        try:
            print("Transforming %s with Babel..." % label)
            output = babeljs.transform_string(content, **babel)
        except babeljs.TransformError as e:
            raise click.ClickException(e.args[0])
        with open(filename, 'w') as f:
            f.write(output)
Exemplo n.º 3
0
def babel(config):
    """
    Use babel.js to compile ES6/2015+.  Generates ES5-compatible JavaScript for
    older browsers.  Note that wq babel is run after wq optimize, on the
    compiled modules created by r.js.  Support for running babel at other
    stages of the build process may be added in a future version of wq.app.
    """
    rconf = config.get('optimize', None)
    if not rconf:
        raise click.UsageError(
            "optimize section not found in %s" % config.filename
        )

    babel = config.get('babel', {})
    files = []
    if 'modules' in rconf and 'dir' in rconf:
        base_url = rconf.get('baseUrl', '.')
        for module in rconf['modules']:
            path = module['name']
            if path in rconf.get('paths', {}):
                path = rconf['paths'][path]
            path = os.path.join(rconf['dir'], base_url, path)
            files.append(path + '.js')

    for filename in files:
        label = os.path.normpath(filename)
        try:
            with open(filename) as f:
                content = f.read()
        except OSError:
            raise click.ClickException(
                "Error loading %s - run wq optimize first?" % label
            )
        try:
            print("Transforming %s with Babel..." % label)
            output = babeljs.transform_string(content, **babel)
        except babeljs.TransformError as e:
            raise click.ClickException(e.args[0])
        with open(filename, 'w') as f:
            f.write(output)
Exemplo n.º 4
0
def babel(config):
    """
    Use babel.js to compile ES6/2015+.  Generates ES5-compatible JavaScript for
    older browsers.  Note that wq babel is run after wq optimize, on the
    compiled modules created by r.js.  Support for running babel at other
    stages of the build process may be added in a future version of wq.app.
    """
    rconf = config.get('optimize', None)
    if not rconf:
        raise click.UsageError("optimize section not found in %s" %
                               config.filename)

    babel = config.get('babel', {})
    files = []
    if 'modules' in rconf and 'dir' in rconf:
        base_url = rconf.get('baseUrl', '.')
        for module in rconf['modules']:
            path = module['name']
            if path in rconf.get('paths', {}):
                path = rconf['paths'][path]
            path = os.path.join(rconf['dir'], base_url, path)
            files.append(path + '.js')

    for filename in files:
        label = os.path.normpath(filename)
        try:
            with open(filename) as f:
                content = f.read()
        except OSError:
            raise click.ClickException(
                "Error loading %s - run wq optimize first?" % label)
        try:
            print("Transforming %s with Babel..." % label)
            output = babeljs.transform_string(content, **babel)
        except babeljs.TransformError as e:
            raise click.ClickException(e.args[0])
        with open(filename, 'w') as f:
            f.write(output)
Exemplo n.º 5
0
 def test_invalid_js(self):
     with self.assertRaises(transformer.TransformError):
         transformer.transform_string('''
             invalid CODE; test(] /
         ''',
                                      runtime=self.runtime)
Exemplo n.º 6
0
 def test_invalid_runtime(self):
     with self.assertRaises(transformer.TransformError):
         transformer.transform_string("var a;", runtime='nonexistent')
Exemplo n.º 7
0
 def _transform_js(self, content):
     return ContentFile(
         transformer.transform_string(content.read().decode()))
Exemplo n.º 8
0
 def test_transform_string(self):
     code = transformer.transform_string('const a = () => 233')
     self.assertEqual(
         '"use strict";\n\nvar a = function a() {\n  return 233;\n};',
         code
     )