Class: Condenser::JstTransformer

Inherits:
NodeProcessor show all
Defined in:
lib/condenser/transformers/jst_transformer.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NodeProcessor

#binary, #exec_runtime, #exec_runtime_error, #exec_syntax_error, node_modules_path, #node_modules_path, setup

Class Method Details

.call(environment, input) ⇒ Object



3
4
5
# File 'lib/condenser/transformers/jst_transformer.rb', line 3

def self.call(environment, input)
  new.call(environment, input)
end

Instance Method Details

#call(environment, input) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/condenser/transformers/jst_transformer.rb', line 7

def call(environment, input)
  opts = {
    filename:         input[:filename],
    moduleId:         input[:filename].sub(/(\..+)+/, ''),
    cwd:              '/assets/',
    filenameRelative: input[:filename],
    sourceFileName:   input[:filename],
    ast:              false,
    compact:          false,
    plugins:          []
  }

  result = exec_runtime(<<-JS)
    const babel = require("#{File.expand_path('../../processors/node_modules', __FILE__)}/@babel/core");
    const source = #{JSON.generate(input[:source])};
    const options = #{JSON.generate(opts).gsub(/"@?babel[\/-][^"]+"/) { |m| "require(#{m})"}};

    function globalVar(scope, name) {
      if (name in scope.globals) {
        return true;
      } else if (scope.parent === null) {
        return false;
      } else {
        return globalVar(scope.parent, name);
      }
    }

    options['plugins'].unshift(function({ types: t }) {
      return {
        visitor: {
          Identifier(path, state) {
            if ( path.parent.type == 'MemberExpression' && path.parent.object != path.node) {
              return;
            }
            if ( path.parent.type == 'ImportSpecifier' || path.parent.type == 'ImportDefaultSpecifier' || path.parent.type =='FunctionDeclaration') {
              return;
            }

            if (
              path.node.name !== 'document' &&
              path.node.name !== 'window' &&
              !(path.node.name in global) &&
              globalVar(path.scope, path.node.name)
            ) {
              path.replaceWith(
                t.memberExpression(t.identifier("locals"), path.node)
              );
            }
          }
        }
      };
    });

    try {
      const result = babel.transform(source, options);
      console.log(JSON.stringify(result));
    } catch(e) {
      console.log(JSON.stringify({'error': [e.name, e.message, e.stack]}));
      process.exit(0);
    }
  JS

  if result['error']
    if result['error'][0] == 'SyntaxError'
      raise exec_syntax_error(result['error'][1], "/assets/#{input[:filename]}")
    else
      raise exec_runtime_error(result['error'][0] + ': ' + result['error'][1])
    end
  else
    input[:source] = result['code']
  end
  
  environment.preprocessors['application/javascript'].each do |processor|
    processor_klass = (processor.is_a?(Class) ? processor : processor.class)
    input[:processors] << processor_klass.name
    environment.load_processors(processor_klass)
    processor.call(environment, input)
  end
end