Skip to content

Commit e6f89aa

Browse files
committedJul 24, 2020
feat: rewrite snowpack.config to js format
1 parent 95ff03b commit e6f89aa

File tree

8 files changed

+70
-38
lines changed

8 files changed

+70
-38
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
require 'json'
2-
31
Snowpacker.configure do |snowpacker|
42
# Where to find the config file
5-
snowpacker.config_file = Rails.root.join('config', 'snowpack.config.json')
3+
snowpacker.config_file = Rails.root.join('config', 'snowpack.config.js')
64

5+
# Snowpacker will set these values at runtime via `config/snowpack.config.js`
6+
snowpacker.output_path = "snowpacks" # => /public/snowpacks
77
snowpacker.port = "4035"
8+
9+
# Currently, snowpack does not support the hostname option as of version 2.6.4
10+
# Support should be coming in 2.6.5, so until then, this is just a placeholder.
811
snowpacker.hostname = "localhost"
912
end
1013

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const output_path = process.env["SNOWPACKER_OUTPUT_PATH"]
2+
const port = process.env["SNOWPACKER_PORT"]
3+
4+
// not currently supported
5+
// const hostname = process.env["SNOWPACKER_HOSTNAME"]
6+
7+
const scripts = {
8+
"mount:web_modules": `mount web_modules --to /${output_path}`,
9+
"mount:__snowpack__": `mount __snowpack__ --to /${output_path}`,
10+
"mount:snowpacks": `mount app/javascript --to /${output_path}`
11+
}
12+
13+
const installOptions = {
14+
NODE_ENV: true
15+
}
16+
17+
const devOptions = {
18+
port: parseInt(port, 10),
19+
open: "none",
20+
out: "public"
21+
}
22+
23+
const buildOptions = {
24+
clean: true,
25+
baseUrl: "/"
26+
}
27+
28+
module.exports = {
29+
scripts,
30+
plugins: ["@snowpack/plugin-babel"],
31+
installOptions,
32+
devOptions,
33+
buildOptions
34+
}
35+

‎examples/railsapp/config/snowpack.config.json

-22
This file was deleted.

‎lib/snowpacker/configuration.rb

+2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ module Snowpacker
22
class Configuration
33
attr_accessor :config_file
44

5+
attr_accessor :output_path
56
attr_accessor :port, :hostname
67

78
def initialize
9+
@output_path = "snowpacks"
810
@port = "4035"
911
@hostname = "localhost"
1012
end

‎lib/snowpacker/env.rb

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Snowpacker
2+
class Env
3+
ENV_PREFIX = "SNOWPACKER".freeze
4+
5+
class << self
6+
def set_env_variables
7+
set_env("OUTPUT_PATH", Snowpacker.config.output_path)
8+
set_env("HOSTNAME", Snowpacker.config.hostname)
9+
set_env("PORT", Snowpacker.config.port)
10+
end
11+
12+
private
13+
14+
def set_env(env_var, value)
15+
ENV["#{ENV_PREFIX}_#{env_var}"] = value.to_s
16+
end
17+
end
18+
end
19+
end

‎lib/snowpacker/runner.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require 'socket'
4+
require 'snowpacker/env'
45

56
module Snowpacker
67
module Rails
@@ -9,6 +10,7 @@ class Runner
910

1011
def initialize
1112
@config_file = Snowpacker.config.config_file
13+
@env = Env.set_env_variables
1214

1315
detect_port!
1416

‎lib/snowpacker/snowpacker_proxy.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class SnowpackerProxy < Rack::Proxy
99
def perform_request(env)
1010
request = Rack::Request.new(env)
1111

12-
if request.path =~ %r{^/snowpacks} # && dev_server_running?
12+
if request.path =~ %r{^/snowpacks} && dev_server_running?
1313
env["HTTP_HOST"] = host_with_port
1414
env['HTTP_COOKIE'] = ''
1515
super(env)
@@ -25,10 +25,10 @@ def dev_server_running?
2525
port = Snowpacker.config.port
2626
connect_timeout = 0.01
2727

28-
socket.tcp(host, port, connect_timeout: connect_timeout).close
29-
puts "Dev server running"
28+
Socket.tcp(host, port, connect_timeout: connect_timeout).close
3029
true
31-
rescue StandardError
30+
rescue Errno::ECONNREFUSED
31+
puts "Snowpacker is not currently running on #{host_with_port}"
3232
false
3333
end
3434

‎lib/snowpacker/templates/snowpacker.rb

+2-9
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,7 @@
22
# Where to find the config file
33
snowpacker.config_file = Rails.root.join('config', 'snowpack.config.json')
44

5-
# Where your javascript is located
6-
snowpacker.source_path = Rails.root.join('app', 'javascript')
7-
snowpacker.entry_points = [File.join(snowpacker.source_path, 'snowpacks', 'application.js')]
8-
9-
# Where to output your files to
10-
snowpacker.out = Rails.root.join('snowpacks')
11-
snowpacker.javascript = File.join(snowpacker.out, 'javascript')
12-
snowpacker.stylesheets = File.join(snowpacker.out, 'stylesheets')
13-
snowpacker.assets = File.join(snowpacker.out, 'assets')
5+
snowpacker.port = "4035" # Port to run on
6+
snowpacker.hostname = "localhost" # hostname to use
147
end
158

0 commit comments

Comments
 (0)
Please sign in to comment.