Class: Condenser::EjsTransformer

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

Constant Summary collapse

STRICT =
true

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NodeProcessor

#binary, #exec_runtime, #exec_runtime_error

Class Method Details

.call(environment, input) ⇒ Object



15
16
17
# File 'lib/condenser/transformers/ejs.rb', line 15

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

.setup(environment) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/condenser/transformers/ejs.rb', line 7

def self.setup(environment)
  require 'ejs' unless defined?(::EJS)

  if !environment.path.include?(EJS::ASSET_DIR)
    environment.append_path(EJS::ASSET_DIR)
  end
end

Instance Method Details

#call(environment, input) ⇒ Object



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
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/condenser/transformers/ejs.rb', line 19

def call(environment, input)
  input[:source] = ::EJS.transform(input[:source], {strict: STRICT})
  
  if STRICT
    opts = {
      # 'moduleRoot' => nil,
      'filename' => input[:filename],
      'moduleId' => input[:filename].sub(/(\..+)+/, ''),
      'cwd' => '/assets/',
      'filenameRelative' => input[:filename],#split_subpath(input[:load_path], input[:filename]),
      'sourceFileName' => input[:filename],
      # 'sourceMapTarget' => input[:filename]
      # 'inputSourceMap'
      ast: false,
      compact: false,
      plugins: [
        ["#{File.expand_path('../../processors/node_modules', __FILE__)}/babel-plugin-transform-class-extended-hook", {}],
        ["#{File.expand_path('../../processors/node_modules', __FILE__)}/@babel/plugin-proposal-class-properties", {}],
        ["#{File.expand_path('../../processors/node_modules', __FILE__)}/@babel/plugin-transform-runtime", {
              corejs: 3,
        }],
      ],
      presets: [["#{File.expand_path('../../processors/node_modules', __FILE__)}/@babel/preset-env", {
        "modules": false,
        "targets": { "browsers": "> 1% and not dead" }
      } ]],
      sourceMap: true
    }

    result = exec_runtime("      const babel = require(\"\#{File.expand_path('../../processors/node_modules', __FILE__)}/@babel/core\");\n      const source = \#{JSON.generate(input[:source])};\n      const options = \#{JSON.generate(opts).gsub(/\"@?babel[\\/-][^\"]+\"/) { |m| \"require(\#{m})\"}};\n\n      function globalVar(scope, name) {\n        if (name in scope.globals) {\n          return true;\n        } else if (scope.parent === null) {\n          return false;\n        } else {\n          return globalVar(scope.parent, name);\n        }\n      }\n\n      options['plugins'].push(function({ types: t }) {\n        return {\n          visitor: {\n            Identifier(path, state) {\n              if ( path.parent.type == 'MemberExpression' && path.parent.object != path.node) {\n                return;\n              }\n\n              if (globalVar(path.scope, path.node.name) && !(path.node.name in global)) {\n                path.replaceWith(\n                  t.memberExpression(t.identifier(\"locals\"), path.node)\n                );\n              }\n            }\n          }\n        };\n      });\n\n\n      try {\n        const result = babel.transform(source, options);\n        console.log(JSON.stringify(result));\n      } catch(e) {\n        console.log(JSON.stringify({'error': e.name + \": \" + e.message}));\n        process.exit(1);\n      }\n    JS\n\n    if result['error']\n      raise Error, result['error']\n    else\n      input[:source] = result['code']\n      input[:map] = result['map']\n    end\n  end\nend\n")