Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add typescript template generator (not defaulted) #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@ Configurations:
Typescript::Rails::Compiler.default_options = [ ... ]
```

## Default Javascript Compilation

Add this line to your `config/application.rb` as show below, above the `config.assets.enabled = true`:

```ruby
config.app_generators.javascript_engine :typescript

# Enable the asset pipeline
config.assets.enabled = true
```

If you don't want it to be the default javascript engine, you can also use it adhoc as show below:

```ruby
rails g controller MyController --javascript_engine=typescript
```

## Referenced TypeScript dependencies

`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.
Expand Down
13 changes: 13 additions & 0 deletions lib/rails/generators/typescript/assets/assets_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require "rails/generators/named_base"

module Typescript
module Generators
class AssetsGenerator < ::Rails::Generators::NamedBase
source_root File.expand_path("../templates", __FILE__)

def copy_typescript
template "javascript.ts", File.join('app/assets/javascripts', class_path, "#{file_name}.ts")
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
// You can use TypeScript in this file: www.typescriptlang.org
18 changes: 15 additions & 3 deletions lib/typescript/rails/engine.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
require 'rails/engine'
require 'rails/generators'
require 'typescript/rails/js_hook'

class Typescript::Rails::Engine < Rails::Engine
# For now, let's not be the default generator ...
# config.app_generators.javascript_engine :ts
end
# To become the default generator...
# config.app_generators.javascript_engine :typescript

if config.respond_to?(:annotations)
config.annotations.register_extensions(".ts") { |annotation| /#\s*(#{annotation}):?\s*(.*)$/ }
end

initializer 'override js_template hook' do |app|
if app.config.generators.rails[:javascript_engine] == :typescript
::Rails::Generators::NamedBase.send :include, Typescript::Rails::JsHook
end
end
end
15 changes: 15 additions & 0 deletions lib/typescript/rails/js_hook.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Typescript
module Rails
module JsHook
extend ActiveSupport::Concern

included do
no_tasks do
redefine_method :js_template do |source, destination|
template(source + '.ts', destination + '.ts')
end
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/typescript/rails/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Typescript
module Rails
VERSION = '0.6.2.3'
VERSION = '0.6.2.4'
end
end
15 changes: 15 additions & 0 deletions test/assets_generator_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'test_helper'
require 'rails/generators/typescript/assets/assets_generator'

class AssetGeneratorTest < Rails::Generators::TestCase
tests Typescript::Generators::AssetsGenerator

destination File.expand_path("../tmp", __FILE__)
setup :prepare_destination

def test_assets
run_generator %w(posts)
assert_no_file "app/assets/javascripts/posts.js"
assert_file "app/assets/javascripts/posts.ts"
end
end
19 changes: 19 additions & 0 deletions test/controller_generator_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'test_helper'
require 'rails/generators/rails/controller/controller_generator'
require 'rails/generators/typescript/assets/assets_generator'

class ControllerGeneratorTest < Rails::Generators::TestCase
tests Rails::Generators::ControllerGenerator

destination File.expand_path("../tmp", __FILE__)
setup do
prepare_destination
copy_routes
end

def test_assets
run_generator %w(posts --javascript-engine=typescript --orm=false)
assert_no_file "app/assets/javascripts/posts.js"
assert_file "app/assets/javascripts/posts.ts"
end
end
19 changes: 19 additions & 0 deletions test/scaffold_generator_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'test_helper'
require 'rails/generators/rails/scaffold/scaffold_generator'
require 'rails/generators/typescript/assets/assets_generator'

class ScaffoldGeneratorTest < Rails::Generators::TestCase
tests Rails::Generators::ScaffoldGenerator

destination File.expand_path("../tmp", __FILE__)
setup do
prepare_destination
copy_routes
end

def test_assets
run_generator %w(posts --javascript-engine=typescript --orm=false)
assert_no_file "app/assets/javascripts/posts.js"
assert_file "app/assets/javascripts/posts.ts"
end
end
1 change: 1 addition & 0 deletions test/support/routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# routes dummy file
3 changes: 3 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
require 'rails/test_help'
require 'minitest-power_assert'

# For generators
require 'rails/generators/test_case'

def copy_routes
routes = File.expand_path('../support/routes.rb', __FILE__)
destination = File.join(destination_root, 'config')
Expand Down