static-rails 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.circleci/config.yml +34 -7
- data/Gemfile.lock +1 -1
- data/README.md +62 -26
- data/lib/generators/templates/static.rb +13 -5
- data/lib/static-rails/compile.rb +1 -1
- data/lib/static-rails/server.rb +1 -1
- data/lib/static-rails/site.rb +3 -0
- data/lib/static-rails/version.rb +1 -1
- data/lib/static-rails/waits_for_connection.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 59b735436f8beadbe02b488f6d9ba385beb4527cc3b1da082e4e07f2a4645dc9
|
4
|
+
data.tar.gz: 5c40b3b20ea44e52dabef6d8660aaf59784f198759baac9e909ba9283916a9f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2d222bec9bcc9c02e3c52adadcd8426290dccbd18aaff465723ed4b152147d42c4980bf34284669cbd58ec2b9794aaf02b3e719bb1b76324ffceb24547b6afbc
|
7
|
+
data.tar.gz: c42139bb13e835326dd49c37f569c816689eff8a490befba9f6b37491e0113992dcaf4f5395fb24aaea20143c7abeacd5db8b0276932748ae7fc1476566263e3
|
data/.circleci/config.yml
CHANGED
@@ -8,12 +8,10 @@ jobs:
|
|
8
8
|
steps:
|
9
9
|
- checkout
|
10
10
|
|
11
|
-
- run:
|
12
|
-
name: Add fake blog to the host
|
13
|
-
command: echo 127.0.0.1 blog.localhost | sudo tee -a /etc/hosts
|
11
|
+
- run: echo 127.0.0.1 blog.localhost | sudo tee -a /etc/hosts
|
14
12
|
|
15
13
|
# One of the apps needs hugo
|
16
|
-
- run: apt-get update && apt-get install -y --no-install-recommends hugo
|
14
|
+
- run: sudo apt-get update && sudo apt-get install -y --no-install-recommends hugo
|
17
15
|
|
18
16
|
# Bundle install dependencies
|
19
17
|
- type: cache-restore
|
@@ -57,7 +55,36 @@ jobs:
|
|
57
55
|
- ~/.cache
|
58
56
|
key: v2-yarn-{{ checksum "example/yarn.lock" }}
|
59
57
|
|
60
|
-
|
58
|
+
# Bundle install dependencies for jekyll app
|
59
|
+
- type: cache-restore
|
60
|
+
key: v1-jekyll-{{ checksum "example/static/docs/Gemfile.lock" }}
|
61
|
+
|
62
|
+
- run: |
|
63
|
+
cd example/static/docs
|
64
|
+
bundle install --path vendor/bundle
|
65
|
+
|
66
|
+
- type: cache-save
|
67
|
+
key: v1-jekyll-{{ checksum "example/static/docs/Gemfile.lock" }}
|
68
|
+
paths:
|
69
|
+
- example/static/docs/vendor/bundle
|
70
|
+
|
71
|
+
# Npm install for Eleventy app
|
72
|
+
- restore_cache:
|
73
|
+
keys:
|
74
|
+
- v1-eleventy-{{ checksum "example/static/blog-docs/package-lock.json" }}
|
75
|
+
# fallback to using the latest cache if no exact match is found
|
76
|
+
- v1-eleventy-
|
77
|
+
|
61
78
|
- run: |
|
62
|
-
|
63
|
-
|
79
|
+
cd example/static/blog-docs
|
80
|
+
npm install
|
81
|
+
|
82
|
+
- save_cache:
|
83
|
+
paths:
|
84
|
+
- example/static/blog-docs/node_modules
|
85
|
+
key: v1-eleventy-{{ checksum "example/static/blog-docs/package-lock.json" }}
|
86
|
+
|
87
|
+
- run: bundle exec rake standard:fix
|
88
|
+
- run: |
|
89
|
+
cd example
|
90
|
+
./script/test
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,44 +1,71 @@
|
|
1
1
|
# static-rails
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
[](https://circleci.com/gh/testdouble/static-rails)
|
4
|
+
|
5
|
+
## What is this thing?
|
6
|
+
|
7
|
+
**tl;dr in development, static-rails runs your static site generators &
|
8
|
+
proxies requests; in production, it compiles and serves the final assets**
|
9
|
+
|
10
|
+
Static site generators are hot right now. Maybe you're hip with "the
|
11
|
+
[Jamstack](https://jamstack.org)", or maybe your API documentation is generated
|
12
|
+
by [Hugo](https://gohugo.io), or maybe your marketing folks use
|
13
|
+
[Jekyll](https://jekyllrb.com) for the company blog.
|
14
|
+
|
15
|
+
Up until now, compiling static assets with any degree of sophistication beyond
|
16
|
+
dumping them in your app's `public/` directory represented a significant
|
17
|
+
deviation from the "Rails Way". But the alternative—keeping your static sites
|
18
|
+
wholly separate from your Rails app—raises myriad operational challenges, from
|
19
|
+
tracking multiple git repositories, to managing multiple server configurations,
|
20
|
+
to figuring out a way to share common JavaScript and CSS assets between them
|
21
|
+
all.
|
22
|
+
|
23
|
+
No longer! static-rails lets you use your static asset generators of choice
|
24
|
+
without forcing you to abandon your monolithic Rails architecture.
|
25
|
+
|
26
|
+
Here's what it does:
|
27
|
+
|
28
|
+
* In `development` and `test` environments, the gem will run launch each site's
|
29
|
+
`serve_command` and proxy to them any requests that match their configured
|
30
|
+
`url_subdomain` and `url_root_path` properties
|
31
|
+
|
32
|
+
* When running `rake assets:precompile` (typically performed during a deploy),
|
33
|
+
the `compile_command` you've configured will be executed for each of your
|
34
|
+
sites
|
35
|
+
|
36
|
+
* In `production`, the gem will host each of your sites' assets out of your
|
37
|
+
configured `compile_dir` using the same middleware code that Rails uses to
|
38
|
+
host assets out of `public/`. (Putting a performant CDN in front of everything
|
39
|
+
remains an exercise for the reader.)
|
5
40
|
|
6
41
|
## Install
|
7
42
|
|
8
|
-
Add this to
|
43
|
+
Add this to your Gemfile:
|
9
44
|
|
10
45
|
```
|
11
46
|
gem "static-rails"
|
12
47
|
```
|
13
48
|
|
14
|
-
|
49
|
+
Then run this generator to create a configuration file
|
50
|
+
`config/initializers/static.rb`:
|
15
51
|
|
16
52
|
```
|
17
53
|
$ rails g static_rails:initializer
|
18
54
|
```
|
19
55
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
Once installed, any `config.sites` you specify will be handled by static-rails
|
24
|
-
in the following way, by default:
|
25
|
-
|
26
|
-
* In `development` and `test`, static-rails will start each site's
|
27
|
-
`serve_command` and proxy any requests to the Rails server that match the
|
28
|
-
`url_subdomain` and `url_root_path` to that server
|
29
|
-
|
30
|
-
* When running `rake assets:precompile` (typically performed during a deploy),
|
31
|
-
static-rails will run the `compile_command` for each configured site
|
56
|
+
You can check out the configuration options in the [generated file's
|
57
|
+
comments](/lib/generators/templates/static.rb).
|
32
58
|
|
33
|
-
|
34
|
-
|
35
|
-
settings (note that this is the opposite default for Rails'
|
36
|
-
`config.public_file_server.enabled` setting, which is disabled by default)
|
37
|
-
|
38
|
-
For examples on how to configure your static site, read on!
|
59
|
+
Want an example of setting things up? You're in luck, there's an [example
|
60
|
+
app](/example) right in this repo!
|
39
61
|
|
40
62
|
## Configuring your static site generators
|
41
63
|
|
64
|
+
Assuming you won't be mounting your static site to your app's root `/` path,
|
65
|
+
you'll probably need to configure its base URL path somehow. Here are
|
66
|
+
some tips (and if your tool of choice isn't listed, we'd love a [pull
|
67
|
+
request](https://github.com/testdouble/static-rails/edit/master/README.md)!):
|
68
|
+
|
42
69
|
### Using Jekyll
|
43
70
|
|
44
71
|
If you have a Jekyll app that you plan on serving from a non-root path (say,
|
@@ -77,8 +104,8 @@ To mitigate this, there are a few things you can do:
|
|
77
104
|
base path with `{{ "/" | relURL }}` (given the above `baseURL`, this will
|
78
105
|
render `"/marketing/"`)
|
79
106
|
|
80
|
-
Also, because Hugo will serve `/livereload.js` from the root,
|
81
|
-
work in development when running through the static-rails proxy
|
107
|
+
Also, because Hugo will serve `/livereload.js` from the root, live-reloading probably
|
108
|
+
won't work in development when running through the static-rails proxy.
|
82
109
|
You might consider disabling it with `--disableLiveReload`.
|
83
110
|
|
84
111
|
A static-rails config for a Hugo configuration in `sites` might look like:
|
@@ -125,10 +152,10 @@ A static-rails config for an Eleventy configuration in `sites` might look like:
|
|
125
152
|
|
126
153
|
### Using Gatsby
|
127
154
|
|
128
|
-
|
155
|
+
<strong> ⚠️ Gatsby is unlikely to work in development mode, due to [this
|
129
156
|
issue](https://github.com/gatsbyjs/gatsby/issues/18143), wherein all the assets
|
130
157
|
are actually served over a socket.io WebSocket channel and not able to be
|
131
|
-
proxied effectively. ⚠️
|
158
|
+
proxied effectively. ⚠️ </strong>
|
132
159
|
|
133
160
|
If you're mounting a [Gatsby](https://www.gatsbyjs.org) site to a non-root path
|
134
161
|
(e.g. in static-rails, you've configured its `url_root_path` to, say,
|
@@ -160,3 +187,12 @@ A static-rails config for a Gatsby configuration in `sites` might look like:
|
|
160
187
|
compile_dir: "static/docs/public"
|
161
188
|
},
|
162
189
|
```
|
190
|
+
|
191
|
+
## Code of Conduct
|
192
|
+
|
193
|
+
This project follows Test Double's [code of
|
194
|
+
conduct](https://testdouble.com/code-of-conduct) for all community interactions,
|
195
|
+
including (but not limited to) one-on-one communications, public posts/comments,
|
196
|
+
code reviews, pull requests, and GitHub issues. If violations occur, Test Double
|
197
|
+
will take any action they deem appropriate for the infraction, up to and
|
198
|
+
including blocking a user from the organization's repositories.
|
@@ -21,7 +21,8 @@ StaticRails.config do |config|
|
|
21
21
|
# # File path to the static app relative to Rails root path
|
22
22
|
# source_dir: "static/blog",
|
23
23
|
#
|
24
|
-
# # Constrain static app to the following subdomain (omit or leave nil
|
24
|
+
# # Constrain static app to the following subdomain (omit or leave nil
|
25
|
+
# # if you aren't hosting the site on a subdomain)
|
25
26
|
# url_subdomain: "blog",
|
26
27
|
#
|
27
28
|
# # Mount the static site web hosting to a certain sub-path (e.g. "/docs")
|
@@ -30,10 +31,16 @@ StaticRails.config do |config|
|
|
30
31
|
# # Whether to run the local development/test server or not
|
31
32
|
# start_server: !Rails.env.production?,
|
32
33
|
#
|
33
|
-
# # If start_server is true, wait to proxy requests to the server until it
|
34
|
+
# # If start_server is true, wait to proxy requests to the server until it
|
35
|
+
# # can connect to server_host over TCP on server_port
|
34
36
|
# ping_server: true
|
35
37
|
#
|
36
|
-
# #
|
38
|
+
# # Any environment variables you need to pass to the server & compile
|
39
|
+
# # commands as a hash (e.g. `env: {"BUNDLE_PATH" => "vendor/bundle"}`)
|
40
|
+
# env: {},
|
41
|
+
#
|
42
|
+
# # The command to execute when running the static app in local
|
43
|
+
# # development/test environments
|
37
44
|
# server_command: "hugo server",
|
38
45
|
#
|
39
46
|
# # The host the local development/test server should be reached on
|
@@ -42,13 +49,14 @@ StaticRails.config do |config|
|
|
42
49
|
# # The port the local development/test server should be reached on
|
43
50
|
# server_port: "1313",
|
44
51
|
#
|
45
|
-
# # The root path on the local development/test server to which requests
|
52
|
+
# # The root path on the local development/test server to which requests
|
53
|
+
# # should be forwarded
|
46
54
|
# server_path: "/",
|
47
55
|
#
|
48
56
|
# # The command to execute when building the static app for production
|
49
57
|
# compile_command: "hugo",
|
50
58
|
#
|
51
|
-
# # The destination of
|
59
|
+
# # The destination of production-compiled assets, relative to Rails root
|
52
60
|
# compile_dir: "static/blog/dist"
|
53
61
|
# },
|
54
62
|
]
|
data/lib/static-rails/compile.rb
CHANGED
@@ -9,7 +9,7 @@ module StaticRails
|
|
9
9
|
Dir.chdir(config.app.root.join(site.source_dir)) do
|
10
10
|
Bundler.with_unbundled_env do
|
11
11
|
puts "=> Compiling static site \"#{site.name}\" to #{site.compile_dir}"
|
12
|
-
result = system site.compile_command
|
12
|
+
result = system(site.env, site.compile_command)
|
13
13
|
unless result == true
|
14
14
|
raise Error.new("Compilation of static site \"#{site.name}\" failed (in directory \"#{site.source_dir}\" with command: `#{site.compile_command}`)")
|
15
15
|
end
|
data/lib/static-rails/server.rb
CHANGED
@@ -46,7 +46,7 @@ module StaticRails
|
|
46
46
|
|
47
47
|
Rails.logger.info "=> Starting #{@site.name} static server"
|
48
48
|
Bundler.with_unbundled_env do
|
49
|
-
Process.spawn(
|
49
|
+
Process.spawn(@site.env, @site.server_command, options).tap do |pid|
|
50
50
|
Process.detach(pid)
|
51
51
|
end
|
52
52
|
end
|
data/lib/static-rails/site.rb
CHANGED
@@ -6,6 +6,7 @@ module StaticRails
|
|
6
6
|
:source_dir,
|
7
7
|
:start_server,
|
8
8
|
:ping_server,
|
9
|
+
:env,
|
9
10
|
:server_command,
|
10
11
|
:server_host,
|
11
12
|
:server_port,
|
@@ -19,11 +20,13 @@ module StaticRails
|
|
19
20
|
url_root_path: "/",
|
20
21
|
start_server: !Rails.env.production?,
|
21
22
|
ping_server: true,
|
23
|
+
env: {},
|
22
24
|
server_host: "localhost",
|
23
25
|
server_path: "/",
|
24
26
|
**other_kwargs
|
25
27
|
)
|
26
28
|
@start_server = start_server
|
29
|
+
@env = env
|
27
30
|
@server_host = server_host
|
28
31
|
@server_path = server_path
|
29
32
|
@ping_server = ping_server
|
data/lib/static-rails/version.rb
CHANGED
@@ -8,7 +8,7 @@ module StaticRails
|
|
8
8
|
loop do
|
9
9
|
Socket.tcp(site.server_host, site.server_port, connect_timeout: 5)
|
10
10
|
break
|
11
|
-
rescue Errno::ECONNREFUSED
|
11
|
+
rescue Errno::ECONNREFUSED, Errno::EADDRNOTAVAIL
|
12
12
|
elapsed = Time.new - start
|
13
13
|
if elapsed > timeout
|
14
14
|
raise Error.new("Static site server \"#{site.name}\" failed to start within #{timeout} seconds. You can change the timeout with `StaticRails.config.ping_server_timeout = 42`")
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: static-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Justin Searls
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-05-
|
11
|
+
date: 2020-05-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|