diff --git a/README.md b/README.md index d277580..ac3bff6 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/rails/generators/typescript/assets/assets_generator.rb b/lib/rails/generators/typescript/assets/assets_generator.rb new file mode 100644 index 0000000..1fb6e34 --- /dev/null +++ b/lib/rails/generators/typescript/assets/assets_generator.rb @@ -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 diff --git a/lib/rails/generators/typescript/assets/templates/javascript.ts b/lib/rails/generators/typescript/assets/templates/javascript.ts new file mode 100644 index 0000000..f05389f --- /dev/null +++ b/lib/rails/generators/typescript/assets/templates/javascript.ts @@ -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 \ No newline at end of file diff --git a/lib/typescript/rails/engine.rb b/lib/typescript/rails/engine.rb index 40b0e90..5159680 100644 --- a/lib/typescript/rails/engine.rb +++ b/lib/typescript/rails/engine.rb @@ -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 \ No newline at end of file + # 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 diff --git a/lib/typescript/rails/js_hook.rb b/lib/typescript/rails/js_hook.rb new file mode 100644 index 0000000..e8a7fe4 --- /dev/null +++ b/lib/typescript/rails/js_hook.rb @@ -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 diff --git a/lib/typescript/rails/version.rb b/lib/typescript/rails/version.rb index 69afc76..f372931 100644 --- a/lib/typescript/rails/version.rb +++ b/lib/typescript/rails/version.rb @@ -1,5 +1,5 @@ module Typescript module Rails - VERSION = '0.6.2.3' + VERSION = '0.6.2.4' end end diff --git a/test/assets_generator_test.rb b/test/assets_generator_test.rb new file mode 100644 index 0000000..699e7b0 --- /dev/null +++ b/test/assets_generator_test.rb @@ -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 diff --git a/test/controller_generator_test.rb b/test/controller_generator_test.rb new file mode 100644 index 0000000..c2db1cc --- /dev/null +++ b/test/controller_generator_test.rb @@ -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 diff --git a/test/scaffold_generator_test.rb b/test/scaffold_generator_test.rb new file mode 100644 index 0000000..92ba055 --- /dev/null +++ b/test/scaffold_generator_test.rb @@ -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 \ No newline at end of file diff --git a/test/support/routes.rb b/test/support/routes.rb new file mode 100644 index 0000000..6028740 --- /dev/null +++ b/test/support/routes.rb @@ -0,0 +1 @@ +# routes dummy file diff --git a/test/test_helper.rb b/test/test_helper.rb index ac145a0..7306cb5 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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') pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy