forked from ackama/rails-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.rb
110 lines (89 loc) · 3.48 KB
/
template.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
require "json"
source_paths.unshift(File.dirname(__FILE__))
# Configure app/assets
remove_file "app/assets/config/manifest.js"
create_file "app/assets/config/manifest.js" do
<<~EO_CONTENT
// This file must exist for assets pipeline compatibility.
EO_CONTENT
end
remove_dir "app/assets/stylesheets"
remove_dir "app/assets/images"
# create a basic package.json which explicitly sets our preferred package manager
# for external tooling; shakapacker:install will add a _lot_ more for us later
File.write("./package.json", JSON.generate({ "packageManager" => "[email protected]" }))
run "rails shakapacker:install"
# this is added by shakapacker:install, but we've already got one (with some extra tags)
# in our template, so remove theirs otherwise the app will error when rendering this
gsub_file! "app/views/layouts/application.html.erb",
" <%= javascript_pack_tag \"application\" %>\n",
""
# Configure app/frontend
run "mv app/javascript app/frontend"
copy_file "config/webpack/webpack.config.js", force: true
gsub_file! "config/shakapacker.yml", "cache_path: tmp/shakapacker", "cache_path: tmp/cache/shakapacker"
gsub_file! "config/shakapacker.yml", "source_path: app/javascript", "source_path: app/frontend"
old_shakapacker_test_compile_snippet = <<~EO_OLD
test:
<<: *default
compile: true
EO_OLD
new_shakapacker_test_compile_snippet = <<~EO_NEW
test:
<<: *default
# We pre-compile assets before running system tests so we do not want
# Shakapacker to automatically compile in the test env
compile: false
EO_NEW
gsub_file!("config/shakapacker.yml", old_shakapacker_test_compile_snippet, new_shakapacker_test_compile_snippet, force: true)
empty_directory_with_keep_file "app/frontend/images"
copy_file "app/frontend/stylesheets/_elements.scss"
copy_file "app/frontend/stylesheets/_reset.scss"
copy_file "app/frontend/stylesheets/application.scss"
prepend_to_file "app/frontend/packs/application.js" do
<<~EO_CONTENT
import "../stylesheets/application.scss";
EO_CONTENT
end
images_disabled_chunk = <<~EO_IMAGES_DISABLED
// const images = require.context('./images', true)
// const imagePath = (name) => images(name, true)
EO_IMAGES_DISABLED
images_enabled_chunk = <<~EO_ENABLE_IMAGES
const images = require.context('../images', true);
// eslint-disable-next-line no-unused-vars
const imagePath = name => images(name, true);
EO_ENABLE_IMAGES
gsub_file! "app/frontend/packs/application.js", images_disabled_chunk, images_enabled_chunk, force: true
# Configure app/views
gsub_file! "app/views/layouts/application.html.erb",
"<%= stylesheet_link_tag(",
"<%= stylesheet_pack_tag(",
force: true
copy_file "app/frontend/images/example.png"
body_open_tag_with_img_example = <<~EO_IMG_EXAMPLE
<body>
<%
# An example of how to load an image via shakapacker. This image is in
# app/frontend/images/example.png
%>
<%= image_pack_tag "images/example.png", alt: "Example Image" %>
EO_IMG_EXAMPLE
gsub_file! "app/views/layouts/application.html.erb", "<body>", body_open_tag_with_img_example, force: true
# shakapacker will automatically configure webpack to use these so long as the dependencies are present
yarn_add_dependencies %w[
css-loader
css-minimizer-webpack-plugin
mini-css-extract-plugin
sass
sass-loader
]
# Setup Turbo
yarn_add_dependencies %w[
@hotwired/turbo-rails
]
prepend_to_file "app/frontend/packs/application.js" do
<<~EO_CONTENT
import "@hotwired/turbo-rails"
EO_CONTENT
end