Class: Typescript::Monkey::Transpiler

Inherits:
Object
  • Object
show all
Defined in:
lib/typescript/monkey/transpiler.rb

Overview

The Transpiler class.

A class that implements an interface to a dynamic runtime Typescript to javascript transpiler.

The scripts that are returned by this class are dependent upon Typescript Services to be available.

Class Method Summary collapse

Class Method Details

.dyrt_jsString Also known as: runner_js

Returns content for dynamic runtime transpiler

The transpiler can be included with web content to transform embedded Typescript wrapped in <script type=“text/typescript”></script> tags.

Returns:

  • (String)

    dyrt source



44
45
46
47
48
49
50
51
52
53
# File 'lib/typescript/monkey/transpiler.rb', line 44

def self.dyrt_js
  transpiler_js = ""
  transpiler_js_path = self.dyrt_js_path()

  unless transpiler_js_path.nil?
    transpiler_js = transpiler_js_path.read()
  end

  transpiler_js
end

.dyrt_js_pathPathname

Returns path to dynamic runtime transpiler

The transpiler can be included with web content to transform embedded Typescript wrapped in <script type=“text/typescript”></script> tags.

Returns:

  • (Pathname)

    path to dyrt source



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/typescript/monkey/transpiler.rb', line 24

def self.dyrt_js_path
  transpiler_js_path = gem_javascripts_path()

  if transpiler_js_path
    transpiler_js_path = transpiler_js_path.join("dyrt.js")
    unless transpiler_js_path.file? && transpiler_js_path.readable?
      transpiler_js_path = nil
    end
  end

  transpiler_js_path
end

.dyrt_once_jsString

Returns content for dynamic runtime transpiler

The transpiler can be included with web content to transform embedded Typescript wrapped in <script type=“text/typescript”></script> tags.

The “once” compilers include an immediately invoked function expression (IIFE) that triggers transpile exactly once. Add this script to the bottom of the body in an HTML page; ideally, this should run last. The script does not, and probably should not, be run after DOM ready; however the Typescript objects in your page can wait for DOM ready.

Returns:

  • (String)

    dyrt source



94
95
96
97
98
99
100
101
102
103
# File 'lib/typescript/monkey/transpiler.rb', line 94

def self.dyrt_once_js
  transpiler_js = ""
  transpiler_js_path = self.dyrt_once_js_path()

  unless transpiler_js_path.nil?
    transpiler_js = transpiler_js_path.read()
  end

  transpiler_js
end

.dyrt_once_js_pathPathname

Returns path to dynamic runtime transpiler

The transpiler can be included with web content to transform embedded Typescript wrapped in <script type=“text/typescript”></script> tags.

The “once” compilers include an immediately invoked function expression (IIFE) that triggers transpile exactly once. Add this script to the bottom of the body in an HTML page; ideally, this should run last. The script does not, and probably should not, be run after DOM ready; however the Typescript objects in your page can wait for DOM ready.

Returns:

  • (Pathname)

    path to dyrt source



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/typescript/monkey/transpiler.rb', line 68

def self.dyrt_once_js_path
  transpiler_js_path = gem_javascripts_path()

  if transpiler_js_path
    transpiler_js_path = transpiler_js_path.join("dyrt_once.js")
    unless transpiler_js_path.file? && transpiler_js_path.readable?
      transpiler_js_path = nil
    end
  end

  transpiler_js_path
end

.dyrt_tsString Also known as: runner_ts

Returns content for dynamic runtime transpiler (pre-transpiled version)

The transpiler can be included with web content to transform embedded Typescript wrapped in <script type=“text/typescript”></script> tags.

Returns:

  • (String)

    typescript transpiler source (pre-transpiled version)



132
133
134
135
136
137
138
139
140
141
# File 'lib/typescript/monkey/transpiler.rb', line 132

def self.dyrt_ts
  transpiler_ts = ""
  transpiler_ts_path = self.dyrt_ts_path()

  unless transpiler_ts_path.nil?
    transpiler_ts = transpiler_ts_path.read()
  end

  transpiler_ts
end

.dyrt_ts_pathPathname

Returns path to dynamic runtime transpiler (pre-transpiled version)

The transpiler can be included with web content to transform embedded Typescript wrapped in <script type=“text/typescript”></script> tags.

Returns:

  • (Pathname)

    path to dyrt source (pre-transpiled version)



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/typescript/monkey/transpiler.rb', line 112

def self.dyrt_ts_path
  transpiler_ts_path = gem_typescripts_path()

  if transpiler_ts_path
    transpiler_ts_path = transpiler_ts_path.join("transpiler.ts")
    unless transpiler_ts_path.file? && transpiler_ts_path.readable?
      transpiler_ts_path = nil
    end
  end

  transpiler_ts_path
end

.gem_assets_pathPathname

Returns assets directory for this gem

Returns:

  • (Pathname)

    path to directory on success, otherwise nil

See Also:



230
231
232
# File 'lib/typescript/monkey/transpiler.rb', line 230

def gem_assets_path
  gem_path_for("assets")
end

.gem_javascripts_pathPathname

Returns javascripts directory for this gem

Returns:

  • (Pathname)

    path to directory on success, otherwise nil

See Also:



240
241
242
# File 'lib/typescript/monkey/transpiler.rb', line 240

def gem_javascripts_path
  gem_path_for("javascripts")
end

.gem_lib_pathPathname

Returns lib directory for this gem

Returns:

  • (Pathname)

    path to directory on success, otherwise nil

See Also:



220
221
222
# File 'lib/typescript/monkey/transpiler.rb', line 220

def gem_lib_path
  gem_path_for("lib")
end

.gem_path_for(directory) ⇒ Pathname

Returns path for gem directory

The directory is the gem directory to resolve. Must be a value of:

+ root          - gem root directory
+ lib           - gem lib directory
+ assets        - gem lib/assets directory
+ javascripts   - gem lib/assets/javascripts directory
+ typescripts   - gem lib/assets/typescripts directory

Results are memoized.

Parameters:

  • directory (String)

    directory to discover

Returns:

  • (Pathname)

    path to directory on success, otherwise nil

Raises:

  • (ArgumentError)

    raises this exception if directory has not been supplied or is invalid.



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/typescript/monkey/transpiler.rb', line 170

def gem_path_for(directory)
  if directory.empty? || directory.nil?
    raise ArgumentError, "directory parameter required but not supplied"
  end
  if !["root", "lib", "assets", "javascripts", "typescripts"].include?(directory)
    raise ArgumentError, "invalid directory specified: #{directory}"
  end

  directory_sym = directory.downcase.to_sym
  @gem_paths ||= {}
  return @gem_paths[directory_sym] if @gem_paths.has_key?(directory_sym)

  # resolve gem root (top directory of this gem)
  gem_root = Pathname.new(File.expand_path("../../../../", __FILE__))

  # process shortcuts
  case directory_sym
  when :lib
    gem_path = gem_root.join("lib")
  when :assets
    gem_path = gem_root.join("lib/assets")
  when :javascripts
    gem_path = gem_root.join("lib/assets/javascripts")
  when :typescripts
    gem_path = gem_root.join("lib/assets/typescripts")
  when :root
    gem_path = gem_root
  else
    gem_path = Pathname.new("")
  end

  @gem_paths[directory_sym] = ((gem_path.directory?) ? gem_path : nil)
end

.gem_root_pathPathname

Returns top directory for this gem

Returns:

  • (Pathname)

    path to directory on success, otherwise nil

See Also:



210
211
212
# File 'lib/typescript/monkey/transpiler.rb', line 210

def gem_root_path
  gem_path_for("root")
end

.gem_typescripts_pathPathname

Returns typescripts directory for this gem

Returns:

  • (Pathname)

    path to directory on success, otherwise nil

See Also:



250
251
252
# File 'lib/typescript/monkey/transpiler.rb', line 250

def gem_typescripts_path
  gem_path_for("typescripts")
end