小さく薄くrails newする
HerokuでRubyのスクリプトのような何かを動かす時に、activerecord等は利用したいのでRuby単体ではなくてRailsとしての方が都合がいいのだけどViewやJS関係は特に必要ない。そんな時があります。そんな時がありますね。
そういうわけで、上記のような用途向けのView関連、JS関連のファイルの作成を全てスキップしたrails newのオプション構成を調べました。
確認環境は
です。
結論
結論は下記です。
rails new . --database=postgresql --skip-yarn --skip-git --skip-action-mailer --skip-active-storage --skip-action-cable --skip-sprockets --skip-javascript --skip-turbolinks --skip-test --api --skip-bundle
そして、下記は僕のコピペ用です。
docker-compose run --rm app rails new . --database=postgresql --skip-yarn --skip-git --skip-action-mailer --skip-active-storage --skip-action-cable --skip-sprockets --skip-javascript --skip-turbolinks --skip-test --api --skip-bundle
各オプションの解説
各オプションの概要は下記の通りです。
オプション | 効果 |
---|---|
--database=DATABASE | 使用するdatabaseを指定 |
--skip-yarn | Yarnを利用しない |
--skip-git | .gitignore を作成しない |
--skip-action-mailer | Action Mailer関連のファイルを作成しない |
--skip-active-storage | Active Storage関連のファイルを作成しない |
--skip-action-cable | Action Cable関連のファイルを作成しない |
--skip-sprockets | Sprockets関連のファイルを作成しない |
--skip-javascript | JavaScript関連のファイルを作成しない |
--skip-turbolinks | turbolinks gemを利用しない |
--skip-test | test関連ファイルを作成しない |
--api | APIとして利用するapp向けの小さい構成 |
--skip-bundle | bundle installを実行しない |
詳しくは
rails new --help
でどうぞ。
補足しておくと、
- 予め
.gitignore
は用意しているので デフォルトの.gitignore
は作成しない (-skip-git
) - どうせ後でRspecをインストールするので標準のtest関連ファイルは必要ない (
--skip-test
) - どうせ後でGemfileに必要なgemを追加して
bunde install
するのでbundle install
しない (--skip-bundle
)
こちらからは以上です。
参考情報
参考までに、上記オプション付与時にremoveされるファイルは下記の通りです。
ちゃんとどのファイルが削除されるのか表示されて便利だ……。
rails new . --database=postgresql --skip-yarn --skip-git --skip-action-mailer --skip-action-cable --skip-sprockets --skip-javascript --skip-turbolinks --skip-test --api --skip-bundle (中略) remove app/assets remove lib/assets remove tmp/cache/assets remove app/helpers remove test/helpers remove app/views remove public/404.html remove public/422.html remove public/500.html remove public/apple-touch-icon-precomposed.png remove public/apple-touch-icon.png remove public/favicon.ico remove app/assets/javascripts remove config/initializers/assets.rb remove app/views/layouts/mailer.html.erb remove app/views/layouts/mailer.text.erb remove app/mailers remove test/mailers remove app/assets/javascripts/cable.js remove app/channels remove config/initializers/cookies_serializer.rb remove config/initializers/content_security_policy.rb remove config/initializers/new_framework_defaults_5_2.rb remove bin/yarn
また、作成されるGemfileとデフォルトのGemfile( Gemfile.default
)の差分は下記となります。
デフォルトのGemfileと比べると、sass-rails
, coffee-rails
から始まって web-console
, またtestの capybara
, selenium-webdrive
に至るまでViewに関するgem一式が削除されていることがわかりますね。
(はてなブログのdiffのhighlight見づらいな……)
git diff --no-index -U10 Gemfile.default Gemfile
diff --git a/Gemfile.default b/Gemfile index e45e610..c5441b8 100644 --- a/Gemfile.default +++ b/Gemfile @@ -1,62 +1,40 @@ source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } ruby '2.6.0' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.2.2' # Use postgresql as the database for Active Record gem 'pg', '>= 0.18', '< 2.0' # Use Puma as the app server gem 'puma', '~> 3.11' -# Use SCSS for stylesheets -gem 'sass-rails', '~> 5.0' -# Use Uglifier as compressor for JavaScript assets -gem 'uglifier', '>= 1.3.0' -# See https://github.com/rails/execjs#readme for more supported runtimes -# gem 'mini_racer', platforms: :ruby - -# Use CoffeeScript for .coffee assets and views -gem 'coffee-rails', '~> 4.2' -# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks -gem 'turbolinks', '~> 5' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder -gem 'jbuilder', '~> 2.5' -# Use Redis adapter to run Action Cable in production -# gem 'redis', '~> 4.0' +# gem 'jbuilder', '~> 2.5' # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' -# Use ActiveStorage variant -# gem 'mini_magick', '~> 4.8' - # Use Capistrano for deployment # gem 'capistrano-rails', group: :development # Reduces boot times through caching; required in config/boot.rb gem 'bootsnap', '>= 1.1.0', require: false +# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible +# gem 'rack-cors' + group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] end group :development do - # Access an interactive console on exception pages or by calling 'console' anywhere in the code. - gem 'web-console', '>= 3.3.0' gem 'listen', '>= 3.0.5', '< 3.2' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' gem 'spring-watcher-listen', '~> 2.0.0' end -group :test do - # Adds support for Capybara system testing and selenium driver - gem 'capybara', '>= 2.15' - gem 'selenium-webdriver' - # Easy installation and use of chromedriver to run system tests with Chrome - gem 'chromedriver-helper' -end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]