Esempio n. 1
0
    def __init__(self, runtime='auto', **_opts):
        self.runtime = runtime
        opts = {'ast': False, 'presets': ['es2015', 'stage-0', 'react']}
        opts.update(_opts)
        plugins = opts.get('plugins', [])

        babel_path = get_abspath('babeljs/browser.js')

        codes = []
        with open(babel_path) as f:
            codes.append(f.read())
        codes.append("""
            var babel;
            if (typeof Babel != 'undefined')
                babel = Babel;
            else
                babel = module.exports;
        """)

        codes.append("""
            if (typeof console == 'undefined') {
                console = {};
                ['log', 'error', 'warn', 'info', 'debug'].forEach(
                    function(key) {
                        console[key] = function() {};
                    }
                );
            }
        """)

        for plugin in plugins:
            if plugin in EXTERNAL_PLUGINS:
                plugin_path = get_abspath(
                    'babeljs/babel-plugin-{}.min.js'.format(plugin))
                with open(plugin_path) as f:
                    codes.append(f.read())
                codes.append("""
                    babel.registerPlugin(
                        "{}",
                        this["babel-plugin-{}"] || module.exports
                    );
                """.format(plugin, plugin))
        try:
            self.opts = opts
            self.context = get_context(self.runtime)
            self.context.eval(''.join(codes))
        except:
            raise TransformError()
Esempio n. 2
0
 def __init__(self):
     path = get_abspath('babeljs/browser.js')
     try:
         self.context = execjs.compile(
             'var babel = require("{}");'.format(path))
     except:
         raise TransformError()
Esempio n. 3
0
 def __init__(self):
     path = get_abspath('babeljs/browser.js')
     try:
         self.context = execjs.compile(
             'var babel = require("{}");'.format(path)
         )
     except:
         raise TransformError()
Esempio n. 4
0
    def __init__(self, **_opts):
        opts = {'presets': ['es2015', 'stage-0', 'react']}
        opts.update(_opts)
        plugins = opts.get('plugins', [])

        babel_path = get_abspath('babeljs/browser.js')

        codes = ['var babel = require("{}");'.format(babel_path)]
        for plugin in plugins:
            if plugin == 'transform-vue-jsx':
                transform_vue_jsx_path = get_abspath(
                    'babeljs/babel-plugin-transform-vue-jsx.min.js')  # noqa
                codes.append(
                    'var transformVueJsx = require("{}");'
                    'babel.registerPlugin("{}", transformVueJsx);'.format(
                        transform_vue_jsx_path, plugin))
        try:
            self.opts = opts
            self.context = execjs.compile(''.join(codes))
        except:
            raise TransformError()