typescript-monkey 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.coveralls.yml +1 -0
- data/.editorconfig +27 -0
- data/.gitignore +52 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +27 -0
- data/.vscode/tasks.json +40 -0
- data/CHANGES.md +48 -0
- data/Gemfile +16 -0
- data/LICENSE.txt +22 -0
- data/README.md +351 -0
- data/Rakefile +13 -0
- data/contrib/example_package.json +10 -0
- data/contrib/example_typescript.rb +14 -0
- data/gulpfile.js +67 -0
- data/lib/assets/javascripts/transpiler.ts.erb +1 -0
- data/lib/assets/javascripts/transpiler_pkg.js.erb +2 -0
- data/lib/assets/javascripts/typescript.js.erb +1 -0
- data/lib/assets/typescripts/transpile_once.ts +13 -0
- data/lib/assets/typescripts/transpiler.ts +203 -0
- data/lib/rails/generators/typescript/assets/assets_generator.rb +13 -0
- data/lib/rails/generators/typescript/assets/templates/javascript.ts +3 -0
- data/lib/typescript-monkey.rb +8 -0
- data/lib/typescript/monkey.rb +9 -0
- data/lib/typescript/monkey/cli.rb +22 -0
- data/lib/typescript/monkey/compiler.rb +177 -0
- data/lib/typescript/monkey/configuration.rb +49 -0
- data/lib/typescript/monkey/engine.rb +18 -0
- data/lib/typescript/monkey/js_hook.rb +15 -0
- data/lib/typescript/monkey/package.rb +239 -0
- data/lib/typescript/monkey/railtie.rb +20 -0
- data/lib/typescript/monkey/template.rb +32 -0
- data/lib/typescript/monkey/template_handler.rb +24 -0
- data/lib/typescript/monkey/transformer.rb +20 -0
- data/lib/typescript/monkey/transpiler.rb +256 -0
- data/lib/typescript/monkey/version.rb +3 -0
- data/package.json +32 -0
- data/test/assets_generator_test.rb +15 -0
- data/test/assets_test.rb +41 -0
- data/test/controller_generator_test.rb +19 -0
- data/test/fixtures/assets/javascripts/hello.js.ts +3 -0
- data/test/fixtures/assets/javascripts/included.ts +4 -0
- data/test/fixtures/assets/javascripts/reference.ts +2 -0
- data/test/fixtures/references/ref1_1.js.ts +3 -0
- data/test/fixtures/references/ref1_2.js.ts +3 -0
- data/test/fixtures/references/ref2_1.d.ts +1 -0
- data/test/fixtures/references/ref2_2.js.ts +3 -0
- data/test/fixtures/references/ref3_1.js.ts +5 -0
- data/test/fixtures/references/ref3_2.ts +3 -0
- data/test/fixtures/references/ref3_3.ts +3 -0
- data/test/fixtures/routes.rb +0 -0
- data/test/fixtures/site/es5.js.ts +7 -0
- data/test/fixtures/site/index.js.ts +1 -0
- data/test/fixtures/site/script_tags.html.erb +23 -0
- data/test/fixtures/sprockets/ref1_1.js.ts +3 -0
- data/test/fixtures/sprockets/ref1_2.js.ts +3 -0
- data/test/fixtures/sprockets/ref1_manifest.js.ts +2 -0
- data/test/references_test.rb +48 -0
- data/test/scaffold_generator_test.rb +19 -0
- data/test/sprockets_test.rb +36 -0
- data/test/support/routes.rb +1 -0
- data/test/template_handler_test.rb +35 -0
- data/test/test_helper.rb +111 -0
- data/tsconfig.json +23 -0
- data/typescript-monkey.gemspec +34 -0
- metadata +175 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 427a6a30bc837fb2b3600d1f6540bcdffd3573b4
|
4
|
+
data.tar.gz: ede7bb0989b12ff88229f928d3a088bc028089a0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d93b9f102f8b58147d15dc905bf62cbefd588661b4a73a0d35d6439a1e7737f0c5536807f98edab0ba02ce9f5d0d3b7ce3fe9c34b5c76763f31fe5eebfd73b66
|
7
|
+
data.tar.gz: e17b79826473486f468a140d4448d0d47423efdc19b24cc34219c64868f1643e63a92659349b63ce3eae28d027a22f9f492e2d460e4f1716c3d0940a307d1c21
|
data/.coveralls.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
service_name: travis-ci
|
data/.editorconfig
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
root = true
|
2
|
+
|
3
|
+
[*]
|
4
|
+
indent_style = space
|
5
|
+
indent_size = 4
|
6
|
+
insert_final_newline = true
|
7
|
+
|
8
|
+
[*.{js,json}]
|
9
|
+
indent_size = 2
|
10
|
+
|
11
|
+
[*.{ts}]
|
12
|
+
indent_size = 4
|
13
|
+
|
14
|
+
[*.{rb,rake}]
|
15
|
+
indent_size = 2
|
16
|
+
|
17
|
+
[*.html]
|
18
|
+
indent_size = 2
|
19
|
+
|
20
|
+
[*.{css,scss}]
|
21
|
+
indent_size = 2
|
22
|
+
|
23
|
+
[{Gemfile}]
|
24
|
+
indent_size = 2
|
25
|
+
|
26
|
+
[*.yml]
|
27
|
+
indent_size = 2
|
data/.gitignore
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# See http://help.github.com/ignore-files/ for more about ignoring files.
|
2
|
+
#
|
3
|
+
# If you find yourself ignoring temporary files generated by your text editor
|
4
|
+
# or operating system, you probably want to add a global ignore instead:
|
5
|
+
# git config --global core.excludesfile ~/.gitignore_global
|
6
|
+
|
7
|
+
# Ignore bundler config
|
8
|
+
/.bundle
|
9
|
+
|
10
|
+
# Ignore secret environment configs
|
11
|
+
.env
|
12
|
+
|
13
|
+
# Ignore all logfiles and tempfiles.
|
14
|
+
/log/*.log
|
15
|
+
/tmp
|
16
|
+
/test/tmp
|
17
|
+
|
18
|
+
# Ignore some ruby files
|
19
|
+
.rvmrc
|
20
|
+
Gemfile.lock
|
21
|
+
|
22
|
+
# Ignore yardoc object cache and generated docs
|
23
|
+
.yardopts
|
24
|
+
.yardoc
|
25
|
+
|
26
|
+
# Ignore ydoc output files
|
27
|
+
doc/yard
|
28
|
+
|
29
|
+
# Ignore rdoc output files
|
30
|
+
doc/rdoc_converted
|
31
|
+
doc/rdoc
|
32
|
+
|
33
|
+
# Ignore generated gem files
|
34
|
+
/*.gem
|
35
|
+
|
36
|
+
# Ignore generated assets
|
37
|
+
/lib/assets/javascripts/dyrt*.js
|
38
|
+
/lib/assets/javascripts/transpile*.js
|
39
|
+
|
40
|
+
# Ignore coveralls output
|
41
|
+
/coverage
|
42
|
+
|
43
|
+
# Ignore local node packages
|
44
|
+
/node_modules
|
45
|
+
|
46
|
+
# Ignore other unneeded files
|
47
|
+
database.yml
|
48
|
+
*.swp
|
49
|
+
*~
|
50
|
+
.project
|
51
|
+
.DS_Store
|
52
|
+
.byebug_history
|
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
typescript-monkey
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.3.0
|
data/.travis.yml
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
language: ruby
|
2
|
+
|
3
|
+
before_install:
|
4
|
+
- "npm install"
|
5
|
+
- "gem update --system 2.6.12"
|
6
|
+
- "gem update bundler"
|
7
|
+
|
8
|
+
script:
|
9
|
+
- bundle exec rake test
|
10
|
+
|
11
|
+
rvm:
|
12
|
+
- 2.0.0
|
13
|
+
- 2.3.4
|
14
|
+
- 2.4.1
|
15
|
+
- ruby-head
|
16
|
+
|
17
|
+
sudo: false
|
18
|
+
|
19
|
+
cache: bundler
|
20
|
+
|
21
|
+
matrix:
|
22
|
+
allow_failures:
|
23
|
+
- rvm: ruby-head
|
24
|
+
fast_finish: true
|
25
|
+
|
26
|
+
notifications:
|
27
|
+
email: false
|
data/.vscode/tasks.json
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
{
|
2
|
+
// See https://go.microsoft.com/fwlink/?LinkId=733558
|
3
|
+
// for the documentation about the tasks.json format
|
4
|
+
"version": "0.1.0",
|
5
|
+
"command": "node_modules/.bin/gulp",
|
6
|
+
"isShellCommand": true,
|
7
|
+
"args": ["--no-color"],
|
8
|
+
"tasks": [
|
9
|
+
{
|
10
|
+
"taskName": "all",
|
11
|
+
"isBuildCommand": true,
|
12
|
+
"showOutput": "always"
|
13
|
+
},
|
14
|
+
{
|
15
|
+
"taskName": "transpile",
|
16
|
+
"isBuildCommand": true,
|
17
|
+
"showOutput": "always"
|
18
|
+
},
|
19
|
+
{
|
20
|
+
"taskName": "transpile-min",
|
21
|
+
"isBuildCommand": true,
|
22
|
+
"showOutput": "always"
|
23
|
+
},
|
24
|
+
{
|
25
|
+
"taskName": "transpile-once",
|
26
|
+
"isBuildCommand": true,
|
27
|
+
"showOutput": "always"
|
28
|
+
},
|
29
|
+
{
|
30
|
+
"taskName": "transpile-once-min",
|
31
|
+
"isBuildCommand": true,
|
32
|
+
"showOutput": "always"
|
33
|
+
},
|
34
|
+
{
|
35
|
+
"taskName": "watch",
|
36
|
+
"isBuildCommand": true,
|
37
|
+
"showOutput": "always"
|
38
|
+
}
|
39
|
+
]
|
40
|
+
}
|
data/CHANGES.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
## v0.6.2.4 2016-09-12 13:48:00+0300
|
2
|
+
|
3
|
+
* Add typescript template generator (not defaulted). Thanks to @mjfaga
|
4
|
+
|
5
|
+
## v0.6.2.3 2016-07-27 17:25:00+0300
|
6
|
+
|
7
|
+
* Fixed incorrect value of register_mime_type (@mihai-dinculescu)
|
8
|
+
|
9
|
+
## v0.6.2.1 2016-07-26 18:27:00+0300
|
10
|
+
|
11
|
+
* Added Sprockets 4 support. Thanks to @mihai-dinculescu
|
12
|
+
|
13
|
+
## v0.6.2 2015-07-27 21:52:50+0300
|
14
|
+
|
15
|
+
* Updated to TS v1.6.2
|
16
|
+
|
17
|
+
## v0.6.1 2015-07-27 21:52:50+0300
|
18
|
+
|
19
|
+
* Now code raises exception with correct file name when TS compilation error occurs
|
20
|
+
|
21
|
+
## v0.6.0 2015-07-06 22:36:25+0300
|
22
|
+
|
23
|
+
* Updated version to 0.6.0 for using Typescript source 1.4.1.3
|
24
|
+
|
25
|
+
## v0.5.0 2015-03-25 10:46:29+0900
|
26
|
+
|
27
|
+
* Expire root TS cache in response to change in referenced `.ts` files (#24)
|
28
|
+
|
29
|
+
## v0.4.2 2014-11-26 07:30:12+0900
|
30
|
+
|
31
|
+
* Fix newlines (#15)
|
32
|
+
* Mention to `default_options` in README (#18)
|
33
|
+
|
34
|
+
## v0.4.1 2014-08-12 00:05:17+0900
|
35
|
+
|
36
|
+
* Fix a runtime error
|
37
|
+
|
38
|
+
## v0.4.0 2014-08-11 23:04:20+0900
|
39
|
+
|
40
|
+
* Set `--noImplicitAny` by default to tsc compiler
|
41
|
+
* Typescript::Rails::Compiler.default_options to provide tsc compiler with otpions
|
42
|
+
* default values: `--target ES5 --noImplicitAny`
|
43
|
+
* A lot of refactoring
|
44
|
+
|
45
|
+
## v0.3.0 2014-08-09 10:27:54+0900
|
46
|
+
|
47
|
+
* Compiles TypeScript files in ES5 mode
|
48
|
+
|
data/Gemfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in typescript-monkey.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
group :test do
|
7
|
+
gem 'rails', '~> 4.0'
|
8
|
+
gem 'sprockets-rails', '> 2.0'
|
9
|
+
gem 'minitest-power_assert'
|
10
|
+
gem 'coveralls'
|
11
|
+
gem 'simplecov'
|
12
|
+
gem 'thor', '0.19.1'
|
13
|
+
gem 'tzinfo-data', platforms: [:mingw, :mswin]
|
14
|
+
gem "byebug", '~> 9.0.6'
|
15
|
+
end
|
16
|
+
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (C) 2017 Mark Eissler. All rights reserved.
|
4
|
+
Portions Copyright (C) 2012-2016 Klaus Zanders et al.
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
8
|
+
in the Software without restriction, including without limitation the rights
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
11
|
+
furnished to do so, subject to the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
14
|
+
copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,351 @@
|
|
1
|
+
# TypeScript Monkey [![Build Status](https://api.travis-ci.org/markeissler/typescript-monkey.svg?branch=feature/typescript-monkey)](https://travis-ci.org/markeissler/typescript-monkey) [![Coverage Status](https://coveralls.io/repos/github/markeissler/typescript-monkey/badge.svg?branch=feature%2Ftypescript-monkey)](https://coveralls.io/github/markeissler/typescript-monkey?branch=feature%2Ftypescript-monkey)
|
2
|
+
|
3
|
+
This is a TypeScript transpiler engine for the Rails asset pipeline.
|
4
|
+
|
5
|
+
>BETA: TypeScript Monkey is currently in pre-release. That doesn't mean it's not ready for production, it just means it
|
6
|
+
hasn't been tested by a large audience yet. The more the merrier and the faster we get to v1.0. Install it, open issues
|
7
|
+
if you find bugs.
|
8
|
+
|
9
|
+
## Overview
|
10
|
+
|
11
|
+
TypeScript Monkey let's you use TypeScript (files with a `.ts` extension) wherever you can use JavaScript (`.js`) files.
|
12
|
+
This includes support for pre-processing other template formats. For instance, you can also create `.ts.erb` files. In
|
13
|
+
essence, TypeScript is a first class citizen on Rails!
|
14
|
+
|
15
|
+
For more information on TypeScript visit the [TypeScript](http://www.typescriptlang.org/) homepage.
|
16
|
+
|
17
|
+
## Requirements
|
18
|
+
|
19
|
+
### Node.js
|
20
|
+
|
21
|
+
One of the goals of __TypeScript Monkey__ is to reduce the dependence on third-party gems which aren't under active
|
22
|
+
development. This gem does require that [Node.js](https://nodejs.org/) has been installed either globally or locally. Specifically,
|
23
|
+
TypeScript support (language and compiler) is provided by the TypeScript package for Node. <u>If installed locally,
|
24
|
+
it is not recommended that you commit your `node_modules` directory to source control.</u>
|
25
|
+
|
26
|
+
If you're not familiar with Node.js don't worry, it's not that scary.
|
27
|
+
|
28
|
+
To install Node.js support, our recommendation is to use an appropriate package manager:
|
29
|
+
|
30
|
+
[Installing Node.js via package manager](https://nodejs.org/en/download/package-manager/)
|
31
|
+
|
32
|
+
#### Recommended package managers
|
33
|
+
|
34
|
+
For MacOS, our recommendation is [Homebrew](https://brew.sh/); for Windows your best bet is probably [Chocolatey](http://chocolatey.org/).
|
35
|
+
Go and install a package manager first, then install node, then come back here. If you're on Linux, well, the assumption
|
36
|
+
is that you probably know what you're doing already!
|
37
|
+
|
38
|
+
>NOTE: Another obvious choice for Windows is [NuGet](https://www.nuget.org/) but it appears that the Node package there is stale.
|
39
|
+
|
40
|
+
## Installation
|
41
|
+
|
42
|
+
### Adding a Node Package Definition (package.json) File
|
43
|
+
|
44
|
+
The first step is to create a Node.js `package.json` file at the root of your Rails project. You may have created this
|
45
|
+
file already for other uses, if so, then you will need to merge in the following dependencies:
|
46
|
+
|
47
|
+
```json
|
48
|
+
"dependencies": {
|
49
|
+
"@types/jquery": "^2.0.41",
|
50
|
+
"@types/node": "^7.0.14",
|
51
|
+
"typescript": "^2.3.1"
|
52
|
+
}
|
53
|
+
```
|
54
|
+
|
55
|
+
If you don't have an existing `package.json` you can use the `example_package.json` file in the [contrib](./contrib/)
|
56
|
+
directory as a starting point. Copy the file to the `root` of your Rails project. __Be sure to rename the file as simply
|
57
|
+
"package.json".__
|
58
|
+
|
59
|
+
The second step is to install some TypeScript dependencies with the Node.js package manager (`npm`):
|
60
|
+
|
61
|
+
```sh
|
62
|
+
>npm install
|
63
|
+
```
|
64
|
+
|
65
|
+
The package manager will determine and install the dependencies noted above by reading the `package.json` file.
|
66
|
+
|
67
|
+
### Adding the Gem to your Rails Project's Gemfile
|
68
|
+
|
69
|
+
Update your Gemfile to include the following statement:
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
# typescript support in the asset pipeline!
|
73
|
+
gem 'typescript-monkey', '~> 0.9.0'
|
74
|
+
```
|
75
|
+
|
76
|
+
Then run bundler:
|
77
|
+
|
78
|
+
```sh
|
79
|
+
>bundle install
|
80
|
+
```
|
81
|
+
|
82
|
+
Finally, restart your Rails server to load the __Typescript Monkey__ gem.
|
83
|
+
|
84
|
+
## Usage
|
85
|
+
|
86
|
+
### TypeScript Triple-Slash Directives
|
87
|
+
|
88
|
+
__Typescript Monkey__ parses Typescript files and examines __Triple-Slash__ directives (also known as __reference__ tags
|
89
|
+
or comments). For each directive, a Sprockets dependency relationship will be created so that a change in a dependency
|
90
|
+
will trigger Sprockets to re-process the dependent file as well.
|
91
|
+
|
92
|
+
>NOTE: __TypeScript Monkey__ does not currently support _import_ directives. See [Features Not Yet Implemented (but on the way)](#pending-features)
|
93
|
+
for more information.
|
94
|
+
|
95
|
+
Learn more about __Triple-Slash__ directives by referring to the following secction of the __Typescript Spec__:
|
96
|
+
|
97
|
+
[Source Files Dependencies](https://github.com/teppeis/typescript-spec-md/blob/master/en/ch11.md#1111-source-files-dependencies)
|
98
|
+
|
99
|
+
### In the Asset Pipeline (Sprockets)
|
100
|
+
|
101
|
+
Typical usage is the same as with JavaScript. That is, you will need to add your TypeScript files to `.js` manifest
|
102
|
+
files just like you would with JavaScript. For instance, in the below example let's pretend that you have created the
|
103
|
+
following:
|
104
|
+
|
105
|
+
```sh
|
106
|
+
app/assets/javascripts/
|
107
|
+
├── application.js
|
108
|
+
├── my-typescript-files
|
109
|
+
└── superduper.ts
|
110
|
+
```
|
111
|
+
|
112
|
+
#### File example: superduper.ts
|
113
|
+
|
114
|
+
```ts
|
115
|
+
class SuperDuper {
|
116
|
+
public message(message: string): void {
|
117
|
+
const body: HTMLBodyElement = document.getElementsByTagName("body")[0];
|
118
|
+
const element = document.createElement("p");
|
119
|
+
element.innerHTML = `${message}`;
|
120
|
+
body.appendChild(element);
|
121
|
+
}
|
122
|
+
}
|
123
|
+
|
124
|
+
$(() => {
|
125
|
+
const superduper = new SuperDuper();
|
126
|
+
superduper.message("TypeScript at work.");
|
127
|
+
});
|
128
|
+
```
|
129
|
+
|
130
|
+
Then, your `application.js` file might look like this:
|
131
|
+
|
132
|
+
#### File example: application.js
|
133
|
+
|
134
|
+
```js
|
135
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
136
|
+
// listed below.
|
137
|
+
//
|
138
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
139
|
+
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
|
140
|
+
//
|
141
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
142
|
+
// compiled file.
|
143
|
+
//
|
144
|
+
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
|
145
|
+
// about supported directives.
|
146
|
+
//
|
147
|
+
//= require jquery
|
148
|
+
//= require jquery_ujs
|
149
|
+
//= require jquery-ui/core
|
150
|
+
//= require bootstrap-sprockets
|
151
|
+
//= require my-typescript-files/superduper
|
152
|
+
|
153
|
+
```
|
154
|
+
|
155
|
+
### Embedded in \<script\> tags
|
156
|
+
|
157
|
+
Yes, you can embeded TypeScript in your templates just like JavaScript! All that's required is __TypeScript Monkey__
|
158
|
+
DYnamic Runtime Transpiler (aka __"dyrt"__). For example, let's say you had the following snippet in your template:
|
159
|
+
|
160
|
+
```erb
|
161
|
+
<script type="text/typescript">
|
162
|
+
class Alert {
|
163
|
+
public alert(message: string): void {
|
164
|
+
alert(`An important message: ${message}`);
|
165
|
+
return;
|
166
|
+
}
|
167
|
+
}
|
168
|
+
let myAlert = new Alert();
|
169
|
+
myAlert.alert("TypeScript is awesome!");
|
170
|
+
</script>
|
171
|
+
```
|
172
|
+
|
173
|
+
The key is making sure your `<script>` tag type is set appropriately as above.
|
174
|
+
|
175
|
+
To transpile this script at runtime you will have to make __dyrt__ available at runtime. You will also need to make the
|
176
|
+
TypeScript library avaialable at runtime. Both of these dependencies can be resolved by adding the following to the end
|
177
|
+
of your `<body>` section:
|
178
|
+
|
179
|
+
```erb
|
180
|
+
<%= javascript_include_tag 'transpiler_pkg' %>
|
181
|
+
```
|
182
|
+
|
183
|
+
After the page loads, __dyrt__ will parse all `<script>` tags, transpile the TypeScript objects and then append them
|
184
|
+
to the end of the page.
|
185
|
+
|
186
|
+
#### Loading the transpiler with other scripts
|
187
|
+
|
188
|
+
Yes, you could (theoretically) load the __Typescript Monkey__ transpiler along with you other script assets. If you
|
189
|
+
were to do that, you would then have to make the following call to trigger transpilation after DOM load:
|
190
|
+
|
191
|
+
```js
|
192
|
+
$(function() {
|
193
|
+
var transpiler = new TypeScript::Monkey::Transpiler();
|
194
|
+
transpiler.transpile();
|
195
|
+
);
|
196
|
+
```
|
197
|
+
|
198
|
+
The `transpile()` method will parse the page, remove all previous transpiled scripts, transpile each TypeScript object
|
199
|
+
and then append these scripts to the end of the page.
|
200
|
+
|
201
|
+
Pretty neat right? __BUT__ it's important to state that using __TypeScript Monkey__ this way may not offer the best
|
202
|
+
peformance. Also, you're inline scripts will not benefit from compression or minification. (The `transpiler_pkg` will,
|
203
|
+
however, be minified by Sprockets as usual).
|
204
|
+
|
205
|
+
### Updating Files and Clearing the Sprockets Cache
|
206
|
+
|
207
|
+
You've updated some of your TypeScript files and for some reason the changes aren't appearing. Hate it when that
|
208
|
+
happens, right? Do this:
|
209
|
+
|
210
|
+
```sh
|
211
|
+
>rake tmp:clear
|
212
|
+
```
|
213
|
+
|
214
|
+
Then re-load your website. Sprockets will re-compile all of your TypeScript assets.
|
215
|
+
|
216
|
+
## Configuration
|
217
|
+
|
218
|
+
### App-level Configuration
|
219
|
+
|
220
|
+
The common application level configuration options that affect the __TypeScript Monkey__ gem are:
|
221
|
+
|
222
|
+
```txt
|
223
|
+
#
|
224
|
+
# Rails options...
|
225
|
+
#
|
226
|
+
config.app_generators.javascript_engine :typescript
|
227
|
+
#
|
228
|
+
# Sprockets options...
|
229
|
+
#
|
230
|
+
config.assets.enabled = true
|
231
|
+
config.assets.js_compressor = :uglifier
|
232
|
+
# set to "true" to disable Sprockets concatenation
|
233
|
+
config.assets.debug = false
|
234
|
+
# set to "true" to generate digests
|
235
|
+
config.assets.digest = true
|
236
|
+
```
|
237
|
+
|
238
|
+
Since the above configuration options appear at the "app" level, changing these settings can affect gems in addition to
|
239
|
+
the __Typescript Monkey__ gem.
|
240
|
+
|
241
|
+
#### Setting Typescript as the Default Javascript Engine
|
242
|
+
|
243
|
+
If you plan on using TypeScript in your project exclusively (instead of plain `.js` or `.coffee` files), then you might
|
244
|
+
want to configure rails so that generators produce TypeScript template files by default. Add the following lines to
|
245
|
+
your `config/application.rb` file.
|
246
|
+
|
247
|
+
```ruby
|
248
|
+
module MyRailsApp
|
249
|
+
class Application < Rails::Application
|
250
|
+
...
|
251
|
+
# enable typescript in the asset pipeline
|
252
|
+
config.app_generators.javascript_engine :typescript
|
253
|
+
# enable the asset pipeline (!! Make sure assets are enabled !!)
|
254
|
+
config.assets.enabled = true
|
255
|
+
...
|
256
|
+
end
|
257
|
+
end
|
258
|
+
```
|
259
|
+
|
260
|
+
Alternatively, you can specify the JavaScript engine as a parameter to the rails generator command line:
|
261
|
+
|
262
|
+
```sh
|
263
|
+
>rails generate controller MyController --javascript_engine=typescript
|
264
|
+
```
|
265
|
+
|
266
|
+
### Gem-level Configuration
|
267
|
+
|
268
|
+
The __TypeScript Monkey__ gem offers some level of configuration. These settings should be configured from within an
|
269
|
+
appropriate initializer. An `example_typescript.rb` initialization file can be found in the [contrib](./contrib/)
|
270
|
+
directory. Copy the file to the `app/config/initializers` directory of your Rails project. __Be sure to rename the file
|
271
|
+
as simply "typescript.rb".__
|
272
|
+
|
273
|
+
<a name="typescript-concatenation"></a>
|
274
|
+
|
275
|
+
#### Setting Traditional TypeScript Concatenation
|
276
|
+
|
277
|
+
During TypeScript transpilation (conversion of TypeScript to JavaScript), the TypeScript compiler will resolve
|
278
|
+
triple-slash directives (dependencies) and as a result transpile and concatenate the dependencies to each file as
|
279
|
+
needed. The upside is that each indivual file will contain its dependencies; the downside is that this usually results
|
280
|
+
in code duplication in a Rails environment.
|
281
|
+
|
282
|
+
__Typescript Monkey__ procedes with transpilation under the notion that all dependencies will be met at runtime. That
|
283
|
+
should be the case if you have setup your manifest files correctly (either the single `application.js` manifest file or
|
284
|
+
your own additional manifest files).
|
285
|
+
|
286
|
+
Still, if you have a reason to revert back to TypeScript's concatenation scheme then you can do so by enabling the
|
287
|
+
following __Typescript Monkey__ configuration in an initializer:
|
288
|
+
|
289
|
+
```ruby
|
290
|
+
Typescript::Monkey.configure do |config|
|
291
|
+
# Configure Typescript::Monkey concatenated compilation
|
292
|
+
config.compile = true
|
293
|
+
end
|
294
|
+
```
|
295
|
+
|
296
|
+
### Configuring a Logger
|
297
|
+
|
298
|
+
Adding a logger config is really only useful for debugging. Don't configure logging unless you fully understand the
|
299
|
+
paragraph that follows the configuraiton snippet:
|
300
|
+
|
301
|
+
```ruby
|
302
|
+
Typescript::Monkey.configure do |config|
|
303
|
+
# Configure Typescript::Monkey logging (for debugging your app build)
|
304
|
+
config.logger = Rails.logger
|
305
|
+
end
|
306
|
+
```
|
307
|
+
|
308
|
+
In general, you will see a lot of dependency "errors" generated by the logger. These "errors" (again, in quotes) are
|
309
|
+
passed through from the TypeScript compiler which doesn't understand that we're building a web site and that all of the
|
310
|
+
"missing" (also, in quotes) dependencies will be resolved at runtime!
|
311
|
+
|
312
|
+
<a name="pending-features"></a>
|
313
|
+
|
314
|
+
## Features Not Yet Implemented (but on the way)
|
315
|
+
|
316
|
+
This is a short-list of planned features:
|
317
|
+
|
318
|
+
1. Support for specifying compiler options from initializer.
|
319
|
+
2. Support for reading compiler options from `tsconfig.json`.
|
320
|
+
3. Support for "import" syntax.
|
321
|
+
4. Features I forgot to write down and can't think of right now...
|
322
|
+
|
323
|
+
## Contributing
|
324
|
+
|
325
|
+
1. Fork it
|
326
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
327
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
328
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
329
|
+
5. Create new Pull Request
|
330
|
+
|
331
|
+
## Authors
|
332
|
+
|
333
|
+
__TypeScript Monkey__ is the work of __Mark Eissler__ based on the original work of others as noted in the
|
334
|
+
[Attributions](#attributions) section below.
|
335
|
+
|
336
|
+
<a name=attributions></a>
|
337
|
+
|
338
|
+
## Attributions
|
339
|
+
|
340
|
+
__TypeScript Monkey__ is a fork of [typescript-rails](https://github.com/typescript-ruby/typescript-rails). A lot of
|
341
|
+
work has gone into producing __Typescript Monkey__ which offers better support for Sprockets and the way that Rails
|
342
|
+
works with assets to provide a more native and familiar experience. Still, without the prior work of __Klaus Zanders__
|
343
|
+
([klaus.zanders@gmail.com](klaus.zanders@gmail.com)) and __FUJI Goro__ ([gfuji@cpan.org](gfuji@cpan.org)) and also the
|
344
|
+
[coffee-rails](https://github.com/rails/coffee-rails) project, this gem would probably not exist.
|
345
|
+
|
346
|
+
## License
|
347
|
+
|
348
|
+
__Typescript Monkey__ is licensed under the MIT open source license.
|
349
|
+
|
350
|
+
---
|
351
|
+
Without open source, there would be no Internet as we know it today.
|