Skip to content

Commit 1db3314

Browse files
authored
Preserve bundle-size; backward-compatible with unpadded placeholder. (#4)
* Preserve bundle-size; backward-compatible with unpadded placeholder. * Preserve size of bundle in characters instead of bytes.
1 parent 7c02a59 commit 1db3314

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

lib/injectable_env.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
class InjectableEnv
55
DefaultVarMatcher = /^REACT_APP_/
6-
Placeholder='{{REACT_APP_VARS_AS_JSON}}'
6+
Placeholder = /\{\{REACT_APP_VARS_AS_JSON_*?\}\}/
77

88
def self.create(var_matcher=DefaultVarMatcher)
99
vars = ENV.find_all {|name,value| var_matcher===name }
@@ -25,10 +25,15 @@ def self.render(*args)
2525

2626
def self.replace(file, *args)
2727
injectee = IO.read(file)
28-
return unless injectee.index(Placeholder)
28+
return unless placeholder = injectee.match(Placeholder)
29+
placeholder_size = placeholder.to_s.size
2930

3031
env = create(*args)
32+
env_size = env.size
33+
new_padding = placeholder_size - env_size
34+
env = env + (' ' * [new_padding, 0].max)
3135
head,_,tail = injectee.partition(Placeholder)
36+
3237
injected = head + env + tail
3338
File.open(file, 'w') do |f|
3439
f.write(injected)

spec/injectable_env_spec.rb

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
RSpec.describe InjectableEnv do
77

8+
Placeholder = '{{REACT_APP_VARS_AS_JSON______________________________________________________________________________________________________}}'
9+
UnpaddedPlaceholder = '{{REACT_APP_VARS_AS_JSON}}'
10+
811
describe '.create' do
912
it "returns empty object" do
1013
expect(InjectableEnv.create).to eq('{}')
@@ -63,7 +66,7 @@
6366

6467
describe '.replace' do
6568
before do
66-
ENV['REACT_APP_HELLO'] = "Hello\n\"World\" we \\ prices today"
69+
ENV['REACT_APP_HELLO'] = "Hello\n\"World\" we \\ prices today 🌞"
6770
end
6871
after do
6972
ENV.delete 'REACT_APP_HELLO'
@@ -72,14 +75,61 @@
7275
it "writes into file" do
7376
begin
7477
file = Tempfile.new('injectable_env_test')
75-
file.write('var injected="{{REACT_APP_VARS_AS_JSON}}"')
78+
file.write(%{var injected="#{Placeholder}"})
7679
file.rewind
7780

7881
InjectableEnv.replace(file.path)
7982

80-
expected_value='var injected="{\\"REACT_APP_HELLO\\":\\"Hello\\\\n\\\\\"World\\\\\" we \\\\\\\\ prices today\\"}"'
83+
expected_value='var injected="{\\"REACT_APP_HELLO\\":\\"Hello\\\\n\\\\\"World\\\\\" we \\\\\\\\ prices today 🌞\\"}'
8184
actual_value=file.read
82-
expect(actual_value).to eq(expected_value)
85+
expect(actual_value.index(expected_value)).to eq(0)
86+
# Closing double-quote is padded out but still last char.
87+
actual_size = actual_value.size
88+
expect(actual_value.index(/\"\Z/)).to eq(actual_size-1)
89+
ensure
90+
if file
91+
file.close
92+
file.unlink
93+
end
94+
end
95+
end
96+
97+
it "matches unpadded placeholder" do
98+
begin
99+
file = Tempfile.new('injectable_env_test')
100+
file.write(%{var injected="#{UnpaddedPlaceholder}"})
101+
file.rewind
102+
103+
InjectableEnv.replace(file.path)
104+
105+
expected_value='var injected="{\\"REACT_APP_HELLO\\":\\"Hello\\\\n\\\\\"World\\\\\" we \\\\\\\\ prices today 🌞\\"}'
106+
actual_value=file.read
107+
expect(actual_value.index(expected_value)).to eq(0)
108+
# Closing double-quote is padded out but still last char.
109+
actual_size = actual_value.size
110+
expect(actual_value.index(/\"\Z/)).to eq(actual_size-1)
111+
ensure
112+
if file
113+
file.close
114+
file.unlink
115+
end
116+
end
117+
end
118+
119+
it "preserves character length of bundle" do
120+
begin
121+
placeholder_size = Placeholder.size
122+
file = Tempfile.new('injectable_env_test')
123+
file.write(Placeholder)
124+
file.rewind
125+
126+
InjectableEnv.replace(file.path)
127+
128+
expected_value = '{\\"REACT_APP_HELLO\\":\\"Hello\\\\n\\\\\"World\\\\\" we \\\\\\\\ prices today 🌞\\"}'
129+
actual_value = file.read
130+
replaced_size = actual_value.size
131+
expect(replaced_size).to eq(placeholder_size)
132+
expect(actual_value.index(expected_value)).to eq(0)
83133
ensure
84134
if file
85135
file.close

0 commit comments

Comments
 (0)