Skip to content

Commit 36f4068

Browse files
authored
Merge pull request #48 from mjfaga/generate_ts_file
Add typescript template generator (not defaulted)
2 parents 2c8c006 + e4fa4ea commit 36f4068

File tree

11 files changed

+121
-4
lines changed

11 files changed

+121
-4
lines changed

README.md

+17
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,23 @@ Configurations:
4343
Typescript::Rails::Compiler.default_options = [ ... ]
4444
```
4545

46+
## Default Javascript Compilation
47+
48+
Add this line to your `config/application.rb` as show below, above the `config.assets.enabled = true`:
49+
50+
```ruby
51+
config.app_generators.javascript_engine :typescript
52+
53+
# Enable the asset pipeline
54+
config.assets.enabled = true
55+
```
56+
57+
If you don't want it to be the default javascript engine, you can also use it adhoc as show below:
58+
59+
```ruby
60+
rails g controller MyController --javascript_engine=typescript
61+
```
62+
4663
## Referenced TypeScript dependencies
4764

4865
`typescript-rails` recurses through all [TypeScript-style](https://github.com/teppeis/typescript-spec-md/blob/master/en/ch11.md#1111-source-files-dependencies) referenced files and tells its [`Sprockets::Context`](https://github.com/sstephenson/sprockets/blob/master/lib/sprockets/context.rb) that the TS file being processed [`depend`s`_on`](https://github.com/sstephenson/sprockets#the-depend_on-directive) each file listed as a reference. This activates Sprocket’s cache-invalidation behavior when any of the descendant references of the root TS file is changed.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require "rails/generators/named_base"
2+
3+
module Typescript
4+
module Generators
5+
class AssetsGenerator < ::Rails::Generators::NamedBase
6+
source_root File.expand_path("../templates", __FILE__)
7+
8+
def copy_typescript
9+
template "javascript.ts", File.join('app/assets/javascripts', class_path, "#{file_name}.ts")
10+
end
11+
end
12+
end
13+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Place all the behaviors and hooks related to the matching controller here.
2+
// All this logic will automatically be available in application.js.
3+
// You can use TypeScript in this file: www.typescriptlang.org

lib/typescript/rails/engine.rb

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
require 'rails/engine'
2+
require 'rails/generators'
3+
require 'typescript/rails/js_hook'
24

35
class Typescript::Rails::Engine < Rails::Engine
4-
# For now, let's not be the default generator ...
5-
# config.app_generators.javascript_engine :ts
6-
end
6+
# To become the default generator...
7+
# config.app_generators.javascript_engine :typescript
8+
9+
if config.respond_to?(:annotations)
10+
config.annotations.register_extensions(".ts") { |annotation| /#\s*(#{annotation}):?\s*(.*)$/ }
11+
end
12+
13+
initializer 'override js_template hook' do |app|
14+
if app.config.generators.rails[:javascript_engine] == :typescript
15+
::Rails::Generators::NamedBase.send :include, Typescript::Rails::JsHook
16+
end
17+
end
18+
end

lib/typescript/rails/js_hook.rb

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Typescript
2+
module Rails
3+
module JsHook
4+
extend ActiveSupport::Concern
5+
6+
included do
7+
no_tasks do
8+
redefine_method :js_template do |source, destination|
9+
template(source + '.ts', destination + '.ts')
10+
end
11+
end
12+
end
13+
end
14+
end
15+
end

lib/typescript/rails/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Typescript
22
module Rails
3-
VERSION = '0.6.2.3'
3+
VERSION = '0.6.2.4'
44
end
55
end

test/assets_generator_test.rb

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'test_helper'
2+
require 'rails/generators/typescript/assets/assets_generator'
3+
4+
class AssetGeneratorTest < Rails::Generators::TestCase
5+
tests Typescript::Generators::AssetsGenerator
6+
7+
destination File.expand_path("../tmp", __FILE__)
8+
setup :prepare_destination
9+
10+
def test_assets
11+
run_generator %w(posts)
12+
assert_no_file "app/assets/javascripts/posts.js"
13+
assert_file "app/assets/javascripts/posts.ts"
14+
end
15+
end

test/controller_generator_test.rb

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'test_helper'
2+
require 'rails/generators/rails/controller/controller_generator'
3+
require 'rails/generators/typescript/assets/assets_generator'
4+
5+
class ControllerGeneratorTest < Rails::Generators::TestCase
6+
tests Rails::Generators::ControllerGenerator
7+
8+
destination File.expand_path("../tmp", __FILE__)
9+
setup do
10+
prepare_destination
11+
copy_routes
12+
end
13+
14+
def test_assets
15+
run_generator %w(posts --javascript-engine=typescript --orm=false)
16+
assert_no_file "app/assets/javascripts/posts.js"
17+
assert_file "app/assets/javascripts/posts.ts"
18+
end
19+
end

test/scaffold_generator_test.rb

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'test_helper'
2+
require 'rails/generators/rails/scaffold/scaffold_generator'
3+
require 'rails/generators/typescript/assets/assets_generator'
4+
5+
class ScaffoldGeneratorTest < Rails::Generators::TestCase
6+
tests Rails::Generators::ScaffoldGenerator
7+
8+
destination File.expand_path("../tmp", __FILE__)
9+
setup do
10+
prepare_destination
11+
copy_routes
12+
end
13+
14+
def test_assets
15+
run_generator %w(posts --javascript-engine=typescript --orm=false)
16+
assert_no_file "app/assets/javascripts/posts.js"
17+
assert_file "app/assets/javascripts/posts.ts"
18+
end
19+
end

test/support/routes.rb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# routes dummy file

test/test_helper.rb

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
require 'rails/test_help'
1919
require 'minitest-power_assert'
2020

21+
# For generators
22+
require 'rails/generators/test_case'
23+
2124
def copy_routes
2225
routes = File.expand_path('../support/routes.rb', __FILE__)
2326
destination = File.join(destination_root, 'config')

0 commit comments

Comments
 (0)