jekyll-minibundle 2.1.2 → 2.2.0

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.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +96 -42
  3. data/LICENSE.txt +1 -1
  4. data/README.md +232 -52
  5. data/Rakefile +16 -0
  6. data/jekyll-minibundle.gemspec +7 -6
  7. data/lib/jekyll/minibundle/asset_bundle.rb +3 -3
  8. data/lib/jekyll/minibundle/asset_file_drop.rb +45 -0
  9. data/lib/jekyll/minibundle/asset_file_properties.rb +12 -8
  10. data/lib/jekyll/minibundle/asset_file_registry.rb +4 -4
  11. data/lib/jekyll/minibundle/asset_tag_markup.rb +21 -20
  12. data/lib/jekyll/minibundle/bundle_file.rb +6 -1
  13. data/lib/jekyll/minibundle/development_file.rb +2 -1
  14. data/lib/jekyll/minibundle/development_file_collection.rb +2 -2
  15. data/lib/jekyll/minibundle/environment.rb +13 -15
  16. data/lib/jekyll/minibundle/files.rb +16 -18
  17. data/lib/jekyll/minibundle/hashes.rb +10 -12
  18. data/lib/jekyll/minibundle/log.rb +5 -7
  19. data/lib/jekyll/minibundle/mini_bundle_block.rb +35 -12
  20. data/lib/jekyll/minibundle/mini_stamp_tag.rb +108 -16
  21. data/lib/jekyll/minibundle/stamp_file.rb +1 -0
  22. data/lib/jekyll/minibundle/variable_template.rb +145 -0
  23. data/lib/jekyll/minibundle/variable_template_registry.rb +19 -0
  24. data/lib/jekyll/minibundle/version.rb +1 -1
  25. data/test/fixture/site/_bin/with_count +1 -1
  26. data/test/integration/minibundle_development_mode_test.rb +146 -61
  27. data/test/integration/minibundle_production_mode_test.rb +271 -143
  28. data/test/integration/ministamp_development_mode_test.rb +66 -25
  29. data/test/integration/ministamp_production_mode_test.rb +129 -37
  30. data/test/integration/static_files_as_asset_sources_test.rb +10 -10
  31. data/test/support/assertions.rb +1 -1
  32. data/test/support/{static_file_api_config.rb → static_file_config.rb} +6 -3
  33. data/test/support/test_case.rb +7 -4
  34. data/test/unit/asset_bundle_test.rb +6 -6
  35. data/test/unit/asset_file_drop_test.rb +65 -0
  36. data/test/unit/asset_file_registry_test.rb +136 -98
  37. data/test/unit/asset_tag_markup_test.rb +11 -5
  38. data/test/unit/bundle_file_properties_test.rb +44 -23
  39. data/test/unit/bundle_file_writing_test.rb +50 -32
  40. data/test/unit/development_file_properties_test.rb +95 -0
  41. data/test/unit/development_file_writing_test.rb +15 -6
  42. data/test/unit/environment_test.rb +3 -3
  43. data/test/unit/files_test.rb +7 -7
  44. data/test/unit/hashes_test.rb +12 -12
  45. data/test/unit/jekyll_static_file_api_test.rb +91 -20
  46. data/test/unit/mini_bundle_block_test.rb +59 -9
  47. data/test/unit/mini_stamp_tag_test.rb +37 -6
  48. data/test/unit/stamp_file_properties_test.rb +47 -24
  49. data/test/unit/stamp_file_writing_test.rb +33 -24
  50. data/test/unit/variable_template_test.rb +121 -0
  51. metadata +27 -6
  52. data/test/unit/development_file_collection_properties_test.rb +0 -106
@@ -1,36 +1,120 @@
1
+ require 'cgi/util'
1
2
  require 'jekyll/minibundle/files'
2
3
  require 'jekyll/minibundle/asset_file_registry'
4
+ require 'jekyll/minibundle/variable_template_registry'
3
5
 
4
6
  module Jekyll::Minibundle
5
7
  class MiniStampTag < Liquid::Tag
6
8
  def initialize(tag_name, text, _tokens)
7
9
  super
10
+ @local_stamp_config =
11
+ MiniStampTag
12
+ .default_stamp_config
13
+ .merge(parse_arguments(text.strip))
14
+ end
15
+
16
+ def render(context)
17
+ site = context.registers.fetch(:site)
18
+
19
+ stamp_config = get_current_stamp_config(context)
20
+ source_path = stamp_config.fetch('source_path')
21
+ destination_path = stamp_config.fetch('destination_path')
22
+ render_basename_only = stamp_config.fetch('render_basename_only')
8
23
 
9
- @source_path, destination_path = text.split(/\s+/, 3)[0, 2]
24
+ file = register_asset_file(site, source_path, destination_path)
25
+ dst_path = Files.strip_dot_slash_from_path_start(file.destination_path_for_markup)
26
+
27
+ url =
28
+ if render_basename_only
29
+ File.basename(dst_path)
30
+ else
31
+ stamp_config.fetch('baseurl') + dst_path
32
+ end
33
+
34
+ CGI.escape_html(url)
35
+ end
10
36
 
11
- if !@source_path || @source_path.empty?
12
- raise ArgumentError, "No asset source for ministamp tag; pass value such as '_assets/site.css' as the first argument"
37
+ def self.default_stamp_config
38
+ {
39
+ 'source_path' => '',
40
+ 'destination_path' => '',
41
+ 'baseurl' => '',
42
+ 'render_basename_only' => false,
43
+ 'use_template' => false
44
+ }
45
+ end
46
+
47
+ private
48
+
49
+ def parse_arguments(args)
50
+ raise ArgumentError, 'Missing asset source and destination paths for ministamp tag; specify value such as "_assets/source.css assets/destination.css" as the argument' if args.empty?
51
+
52
+ structure = parse_structure(args)
53
+
54
+ case structure
55
+ when String
56
+ parse_string_argument(structure)
57
+ when Hash
58
+ parse_hash_argument(structure)
59
+ else
60
+ raise ArgumentError, "Unsupported ministamp tag argument type (#{structure.class}), only String and Hash are supported: #{args}"
13
61
  end
62
+ end
14
63
 
15
- if !destination_path || destination_path.empty?
16
- raise ArgumentError, "No asset destination for ministamp tag; pass value such as 'assets/site.css' as the second argument"
64
+ def parse_structure(args)
65
+ ::SafeYAML.load(args)
66
+ rescue => e
67
+ raise ArgumentError, "Failed parsing ministamp tag argument syntax as YAML: #{args.inspect}. Cause: #{e}"
68
+ end
69
+
70
+ def parse_string_argument(str)
71
+ source_path, destination_path = str.split(/\s+/, 3)[0, 2]
72
+
73
+ unless destination_path
74
+ raise ArgumentError, 'Missing asset destination path for ministamp tag; specify value such as "assets/destination.css" after asset source path argument, separated with a space'
17
75
  end
18
76
 
19
- @baseurl, @destination_path = normalize_destination_path(destination_path)
77
+ {
78
+ 'source_path' => source_path,
79
+ 'destination_path' => destination_path
80
+ }
20
81
  end
21
82
 
22
- def render(context)
23
- site = context.registers.fetch(:site)
24
- file =
25
- if Environment.development?(site)
26
- AssetFileRegistry.register_development_file(site, @source_path, @destination_path)
27
- else
28
- AssetFileRegistry.register_stamp_file(site, @source_path, @destination_path)
29
- end
30
- @baseurl + Files.strip_dot_slash_from_path_start(file.destination_path_for_markup)
83
+ def parse_hash_argument(hash)
84
+ source_path = hash.fetch('source_path', '').to_s
85
+ destination_path = hash.fetch('destination_path', '').to_s
86
+ render_basename_only = hash.fetch('render_basename_only', false)
87
+
88
+ raise ArgumentError, 'Missing asset source path for ministamp tag; specify Hash entry such as "source_path: _assets/site.css"' if source_path.empty?
89
+ raise ArgumentError, 'Missing asset destination path for ministamp tag; specify Hash entry such as "destination_path: assets/site.css"' if destination_path.empty?
90
+
91
+ {
92
+ 'source_path' => source_path,
93
+ 'destination_path' => destination_path,
94
+ 'render_basename_only' => render_basename_only,
95
+ 'use_template' => true
96
+ }
31
97
  end
32
98
 
33
- private
99
+ def get_current_stamp_config(context)
100
+ source_path = @local_stamp_config.fetch('source_path')
101
+ destination_path = @local_stamp_config.fetch('destination_path')
102
+ render_basename_only = @local_stamp_config.fetch('render_basename_only')
103
+
104
+ if @local_stamp_config.fetch('use_template')
105
+ source_path = VariableTemplateRegistry.register_template(source_path).render(context)
106
+ destination_path = VariableTemplateRegistry.register_template(destination_path).render(context)
107
+ end
108
+
109
+ baseurl, destination_path = normalize_destination_path(destination_path)
110
+
111
+ {
112
+ 'source_path' => source_path,
113
+ 'destination_path' => destination_path,
114
+ 'baseurl' => baseurl,
115
+ 'render_basename_only' => render_basename_only
116
+ }
117
+ end
34
118
 
35
119
  def normalize_destination_path(destination_path)
36
120
  if destination_path.start_with?('/')
@@ -39,6 +123,14 @@ module Jekyll::Minibundle
39
123
  ['', destination_path]
40
124
  end
41
125
  end
126
+
127
+ def register_asset_file(site, source_path, destination_path)
128
+ if Environment.development?(site)
129
+ AssetFileRegistry.register_development_file(site, source_path, destination_path)
130
+ else
131
+ AssetFileRegistry.register_stamp_file(site, source_path, destination_path)
132
+ end
133
+ end
42
134
  end
43
135
  end
44
136
 
@@ -13,6 +13,7 @@ module Jekyll::Minibundle
13
13
  def initialize(site, asset_source_path, asset_destination_path)
14
14
  @site = site
15
15
  @asset_source_path = File.join(@site.source, asset_source_path)
16
+ raise ArgumentError, "Stamp source file does not exist: #{@asset_source_path}" unless File.file?(@asset_source_path)
16
17
  @asset_destination_dir = File.dirname(asset_destination_path)
17
18
  @asset_destination_extension = File.extname(asset_destination_path)
18
19
  @asset_destination_filename_prefix = File.basename(asset_destination_path)[0..-(@asset_destination_extension.size + 1)]
@@ -0,0 +1,145 @@
1
+ require 'strscan'
2
+
3
+ module Jekyll::Minibundle
4
+ class VariableTemplate
5
+ OPEN_TAG = '{{'.freeze
6
+ CLOSE_TAG = '}}'.freeze
7
+ ESCAPE_CHAR = '\\'.freeze
8
+
9
+ def initialize(interpolation)
10
+ instance_eval("def render(variables) #{interpolation} end", __FILE__, __LINE__)
11
+ end
12
+
13
+ def self.compile(template)
14
+ new(Generator.compile(Parser.parse(template)))
15
+ end
16
+
17
+ class SyntaxError < ArgumentError
18
+ CURSOR = '@'.freeze
19
+
20
+ def initialize(message, template, position)
21
+ @message = message
22
+ @template = template
23
+ @position = position
24
+ end
25
+
26
+ def to_s
27
+ template_before_pos = @template[0, @position]
28
+ template_after_pos = @template[@position..-1]
29
+
30
+ <<-END
31
+ #{@message} at position #{@position} in template (position highlighted with "#{CURSOR}"):
32
+ #{template_before_pos}#{CURSOR}#{template_after_pos}
33
+ END
34
+ end
35
+ end
36
+
37
+ module Parser
38
+ # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
39
+ def self.parse(template)
40
+ raise ArgumentError, 'Nil template' if template.nil?
41
+
42
+ escape_or_open_regex = Parser.make_escape_sequence_or_open_tag_regexp
43
+ close_regex = Parser.make_close_tag_regexp
44
+
45
+ scanner = StringScanner.new(template)
46
+
47
+ tokens = []
48
+ text_buffer = ''
49
+ escape_or_open_match = scanner.scan_until(escape_or_open_regex)
50
+
51
+ while escape_or_open_match
52
+ escape_match = scanner[1]
53
+
54
+ # escape sequence
55
+ if escape_match
56
+ text_buffer += escape_or_open_match[0..-3]
57
+ text_buffer += escape_match[1, 1]
58
+ # open tag
59
+ else
60
+ text_buffer += escape_or_open_match[0..-(OPEN_TAG.size + 1)]
61
+ tokens << Token.text(text_buffer)
62
+ text_buffer = ''
63
+ close_match = scanner.scan_until(close_regex)
64
+ raise SyntaxError.new(%{Missing closing tag ("#{CLOSE_TAG}") for variable opening tag ("#{OPEN_TAG}")}, template, scanner.charpos) unless close_match
65
+ tokens << Token.variable(close_match[0..-(CLOSE_TAG.size + 1)].strip)
66
+ end
67
+
68
+ escape_or_open_match = scanner.scan_until(escape_or_open_regex)
69
+ end
70
+
71
+ text_buffer += scanner.rest unless scanner.eos?
72
+ tokens << Token.text(text_buffer) unless text_buffer.empty?
73
+
74
+ tokens
75
+ end
76
+ # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
77
+
78
+ def self.make_escape_sequence_regexp
79
+ escape_chars = (OPEN_TAG + CLOSE_TAG).chars.uniq
80
+ escape_chars << ESCAPE_CHAR
81
+ escape_chars.map { |c| Regexp.escape(ESCAPE_CHAR + c) }
82
+ end
83
+
84
+ def self.make_escape_sequence_or_open_tag_regexp
85
+ @_escape_sequence_or_open_tag_regexp ||=
86
+ begin
87
+ regexp = [make_escape_sequence_regexp.join('|'), Regexp.escape(OPEN_TAG)].map { |p| "(#{p})" }.join('|')
88
+ Regexp.compile(regexp)
89
+ end
90
+ end
91
+
92
+ def self.make_close_tag_regexp
93
+ @_close_tag_regexp ||= Regexp.compile(Regexp.escape(CLOSE_TAG))
94
+ end
95
+ end
96
+
97
+ class Token
98
+ attr_reader :value
99
+
100
+ def initialize(value, is_variable)
101
+ @value = value
102
+ @is_variable = is_variable
103
+ end
104
+
105
+ def variable?
106
+ @is_variable
107
+ end
108
+
109
+ def self.text(value)
110
+ new(value, false)
111
+ end
112
+
113
+ def self.variable(value)
114
+ new(value, true)
115
+ end
116
+ end
117
+
118
+ # Transforms array of tokens to Ruby interpolation string.
119
+ #
120
+ # Idea adapted from Mustache's
121
+ # [Generator](https://github.com/mustache/mustache/blob/master/lib/mustache/generator.rb).
122
+ module Generator
123
+ def self.compile(tokens)
124
+ result = '"'
125
+
126
+ tokens.each do |token|
127
+ result +=
128
+ if token.variable?
129
+ %(#\{variables["#{escape_token(token.value)}"].to_s})
130
+ else
131
+ escape_token(token.value)
132
+ end
133
+ end
134
+
135
+ result += '"'
136
+
137
+ result
138
+ end
139
+
140
+ def self.escape_token(token)
141
+ token.inspect[1..-2]
142
+ end
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,19 @@
1
+ require 'jekyll/minibundle/variable_template'
2
+
3
+ module Jekyll::Minibundle
4
+ module VariableTemplateRegistry
5
+ def self.clear
6
+ @_templates = {}
7
+ end
8
+
9
+ def self.register_template(template)
10
+ @_templates[template] ||= VariableTemplate.compile(template)
11
+ end
12
+
13
+ clear
14
+ end
15
+ end
16
+
17
+ ::Jekyll::Hooks.register(:site, :post_write) do
18
+ ::Jekyll::Minibundle::VariableTemplateRegistry.clear
19
+ end
@@ -1,5 +1,5 @@
1
1
  module Jekyll
2
2
  module Minibundle
3
- VERSION = '2.1.2'.freeze
3
+ VERSION = '2.2.0'.freeze
4
4
  end
5
5
  end
@@ -3,7 +3,7 @@
3
3
  filename = ARGV.first
4
4
 
5
5
  count =
6
- if File.exist?(filename)
6
+ if File.file?(filename)
7
7
  File.read(filename).strip.to_i
8
8
  else
9
9
  0
@@ -10,27 +10,27 @@ module Jekyll::Minibundle::Test
10
10
 
11
11
  def test_css_assets_have_tags_in_configured_order
12
12
  with_precompiled_site(:development) do
13
- assert_equal CSS_ASSET_DESTINATION_PATHS, find_css_paths_from_index
13
+ assert_equal(CSS_ASSET_DESTINATION_PATHS, find_css_paths_from_index)
14
14
  end
15
15
  end
16
16
 
17
17
  def test_js_assets_have_tags_in_configured_order
18
18
  with_precompiled_site(:development) do
19
- assert_equal JS_ASSET_DESTINATION_PATHS, find_js_paths_from_index
19
+ assert_equal(JS_ASSET_DESTINATION_PATHS, find_js_paths_from_index)
20
20
  end
21
21
  end
22
22
 
23
23
  def test_css_assets_have_configured_attributes
24
24
  with_precompiled_site(:development) do
25
25
  elements = find_css_elements_from_index.map { |el| [el['id'], el['media']] }.uniq
26
- assert_equal [['my-styles', 'projection']], elements
26
+ assert_equal([['my-styles', 'projection']], elements)
27
27
  end
28
28
  end
29
29
 
30
30
  def test_js_assets_have_configured_attributes
31
31
  with_precompiled_site(:development) do
32
32
  elements = find_js_elements_from_index.map { |el| [el['id'], el['async']] }.uniq
33
- assert_equal [['my-scripts', '']], elements
33
+ assert_equal([['my-scripts', '']], elements)
34
34
  end
35
35
  end
36
36
 
@@ -65,8 +65,8 @@ module Jekyll::Minibundle::Test
65
65
 
66
66
  generate_site(:development, clear_cache: false)
67
67
 
68
- assert_equal File.read(destination), File.read(source)
69
- assert_operator file_mtime_of(destination), :>, org_mtime
68
+ assert_equal(File.read(destination), File.read(source))
69
+ assert_operator(file_mtime_of(destination), :>, org_mtime)
70
70
  end
71
71
  end
72
72
  end
@@ -95,7 +95,7 @@ module Jekyll::Minibundle::Test
95
95
 
96
96
  generate_site(:development, clear_cache: false)
97
97
 
98
- assert_operator file_mtime_of(destination), :>, org_mtime
98
+ assert_operator(file_mtime_of(destination), :>, org_mtime)
99
99
  end
100
100
  end
101
101
 
@@ -122,10 +122,10 @@ module Jekyll::Minibundle::Test
122
122
 
123
123
  generate_site(:development, clear_cache: false)
124
124
 
125
- assert_equal [File.join(JS_BUNDLE_DESTINATION_PATH, 'dependency.js')], find_js_paths_from_index
125
+ assert_equal([File.join(JS_BUNDLE_DESTINATION_PATH, 'dependency.js')], find_js_paths_from_index)
126
126
 
127
127
  new_mtime = file_mtime_of(destination_path(JS_BUNDLE_DESTINATION_PATH, 'dependency.js'))
128
- assert_operator new_mtime, :>, org_mtime
128
+ assert_operator(new_mtime, :>, org_mtime)
129
129
  end
130
130
  end
131
131
 
@@ -133,8 +133,8 @@ module Jekyll::Minibundle::Test
133
133
  with_site_dir do
134
134
  generate_site(:development)
135
135
 
136
- assert File.exist?(destination_path(JS_BUNDLE_DESTINATION_PATH, 'dependency.js'))
137
- assert File.exist?(destination_path(JS_BUNDLE_DESTINATION_PATH, 'app.js'))
136
+ assert(File.file?(destination_path(JS_BUNDLE_DESTINATION_PATH, 'dependency.js')))
137
+ assert(File.file?(destination_path(JS_BUNDLE_DESTINATION_PATH, 'app.js')))
138
138
 
139
139
  ensure_file_mtime_changes do
140
140
  change_destination_path_in_minibundle_block('assets/site', 'assets/site2')
@@ -142,12 +142,12 @@ module Jekyll::Minibundle::Test
142
142
 
143
143
  generate_site(:development, clear_cache: false)
144
144
 
145
- refute File.exist?(destination_path(JS_BUNDLE_DESTINATION_PATH, 'dependency.js'))
146
- refute File.exist?(destination_path(JS_BUNDLE_DESTINATION_PATH, 'app.js'))
145
+ refute(File.file?(destination_path(JS_BUNDLE_DESTINATION_PATH, 'dependency.js')))
146
+ refute(File.file?(destination_path(JS_BUNDLE_DESTINATION_PATH, 'app.js')))
147
147
 
148
- assert_equal ['assets/site2/dependency.js', 'assets/site2/app.js'], find_js_paths_from_index
149
- assert File.exist?(destination_path('assets/site2/dependency.js'))
150
- assert File.exist?(destination_path('assets/site2/app.js'))
148
+ assert_equal(['assets/site2/dependency.js', 'assets/site2/app.js'], find_js_paths_from_index)
149
+ assert(File.file?(destination_path('assets/site2/dependency.js')))
150
+ assert(File.file?(destination_path('assets/site2/app.js')))
151
151
  end
152
152
  end
153
153
 
@@ -155,8 +155,8 @@ module Jekyll::Minibundle::Test
155
155
  with_site_dir do
156
156
  generate_site(:development)
157
157
 
158
- assert File.exist?(destination_path(JS_BUNDLE_DESTINATION_PATH, 'dependency.js'))
159
- assert File.exist?(destination_path(JS_BUNDLE_DESTINATION_PATH, 'app.js'))
158
+ assert(File.file?(destination_path(JS_BUNDLE_DESTINATION_PATH, 'dependency.js')))
159
+ assert(File.file?(destination_path(JS_BUNDLE_DESTINATION_PATH, 'app.js')))
160
160
 
161
161
  ensure_file_mtime_changes do
162
162
  change_destination_path_in_minibundle_block('assets/site', 'assets/site2')
@@ -164,12 +164,12 @@ module Jekyll::Minibundle::Test
164
164
 
165
165
  generate_site(:development, clear_cache: false)
166
166
 
167
- refute File.exist?(destination_path(JS_BUNDLE_DESTINATION_PATH, 'dependency.js'))
168
- refute File.exist?(destination_path(JS_BUNDLE_DESTINATION_PATH, 'app.js'))
167
+ refute(File.file?(destination_path(JS_BUNDLE_DESTINATION_PATH, 'dependency.js')))
168
+ refute(File.file?(destination_path(JS_BUNDLE_DESTINATION_PATH, 'app.js')))
169
169
 
170
- assert_equal ['assets/site2/dependency.js', 'assets/site2/app.js'], find_js_paths_from_index
171
- assert File.exist?(destination_path('assets/site2/dependency.js'))
172
- assert File.exist?(destination_path('assets/site2/app.js'))
170
+ assert_equal(['assets/site2/dependency.js', 'assets/site2/app.js'], find_js_paths_from_index)
171
+ assert(File.file?(destination_path('assets/site2/dependency.js')))
172
+ assert(File.file?(destination_path('assets/site2/app.js')))
173
173
 
174
174
  ensure_file_mtime_changes do
175
175
  change_destination_path_in_minibundle_block('assets/site2', 'assets/site')
@@ -177,12 +177,12 @@ module Jekyll::Minibundle::Test
177
177
 
178
178
  generate_site(:development, clear_cache: false)
179
179
 
180
- refute File.exist?(destination_path('assets/site2/dependency.js'))
181
- refute File.exist?(destination_path('assets/site2/app.js'))
180
+ refute(File.file?(destination_path('assets/site2/dependency.js')))
181
+ refute(File.file?(destination_path('assets/site2/app.js')))
182
182
 
183
- assert_equal ['assets/site/dependency.js', 'assets/site/app.js'], find_js_paths_from_index
184
- assert File.exist?(destination_path(JS_BUNDLE_DESTINATION_PATH, 'dependency.js'))
185
- assert File.exist?(destination_path(JS_BUNDLE_DESTINATION_PATH, 'app.js'))
183
+ assert_equal(['assets/site/dependency.js', 'assets/site/app.js'], find_js_paths_from_index)
184
+ assert(File.file?(destination_path(JS_BUNDLE_DESTINATION_PATH, 'dependency.js')))
185
+ assert(File.file?(destination_path(JS_BUNDLE_DESTINATION_PATH, 'app.js')))
186
186
  end
187
187
  end
188
188
 
@@ -195,11 +195,11 @@ module Jekyll::Minibundle::Test
195
195
  org_mtime_css = file_mtime_of(destination_css)
196
196
  org_mtime_js = file_mtime_of(destination_js)
197
197
 
198
- assert File.exist?(destination_css)
199
- assert File.exist?(destination_js)
198
+ assert(File.file?(destination_css))
199
+ assert(File.file?(destination_js))
200
200
 
201
- assert_equal 'assets/site/common.css', find_css_paths_from_index.last
202
- assert_equal 'assets/site/app.js', find_js_paths_from_index.last
201
+ assert_equal('assets/site/common.css', find_css_paths_from_index.last)
202
+ assert_equal('assets/site/app.js', find_js_paths_from_index.last)
203
203
 
204
204
  ensure_file_mtime_changes do
205
205
  find_and_gsub_in_file(
@@ -211,32 +211,84 @@ module Jekyll::Minibundle::Test
211
211
 
212
212
  generate_site(:development, clear_cache: false)
213
213
 
214
- assert File.exist?(destination_css)
215
- assert File.exist?(destination_js)
216
- assert_equal org_mtime_css, file_mtime_of(destination_css)
217
- assert_equal org_mtime_js, file_mtime_of(destination_js)
218
- assert_equal '/assets/site/common.css', find_css_paths_from_index.last
219
- assert_equal '/assets/site/app.js', find_js_paths_from_index.last
214
+ assert(File.file?(destination_css))
215
+ assert(File.file?(destination_js))
216
+ assert_equal(org_mtime_css, file_mtime_of(destination_css))
217
+ assert_equal(org_mtime_js, file_mtime_of(destination_js))
218
+ assert_equal('/assets/site/common.css', find_css_paths_from_index.last)
219
+ assert_equal('/assets/site/app.js', find_js_paths_from_index.last)
220
220
  end
221
221
  end
222
222
 
223
223
  def test_supports_baseurl
224
224
  with_site_dir do
225
+ merge_to_yaml_file(source_path('_config.yml'), 'baseurl' => '/root')
226
+
227
+ find_and_gsub_in_file(
228
+ source_path('_layouts/default.html'),
229
+ ' {% minibundle css %}',
230
+ <<-END
231
+ {% minibundle css %}
232
+ baseurl: '{{ site.baseurl }}/'
233
+ END
234
+ )
235
+
236
+ find_and_gsub_in_file(
237
+ source_path('_layouts/default.html'),
238
+ ' {% minibundle js %}',
239
+ <<-END
240
+ {% minibundle js %}
241
+ baseurl: {{ site.baseurl }}/js
242
+ END
243
+ )
244
+
225
245
  generate_site(:development)
226
246
 
227
- assert File.exist?(destination_path(CSS_BUNDLE_DESTINATION_PATH, 'common.css'))
228
- assert File.exist?(destination_path(JS_BUNDLE_DESTINATION_PATH, 'app.js'))
247
+ assert(File.file?(destination_path(CSS_BUNDLE_DESTINATION_PATH, 'common.css')))
248
+ assert(File.file?(destination_path(JS_BUNDLE_DESTINATION_PATH, 'app.js')))
249
+
250
+ assert_equal('/root/assets/site/common.css', find_css_paths_from_index.last)
251
+ assert_equal('/root/js/assets/site/app.js', find_js_paths_from_index.last)
252
+ end
253
+ end
229
254
 
230
- assert_equal 'assets/site/common.css', find_css_paths_from_index.last
231
- assert_equal 'assets/site/app.js', find_js_paths_from_index.last
255
+ def test_supports_destination_baseurl
256
+ with_site_dir do
257
+ merge_to_yaml_file(source_path('_config.yml'), 'cdn_baseurl' => 'https://cdn.example.com/?file=')
258
+
259
+ find_and_gsub_in_file(
260
+ source_path('_layouts/default.html'),
261
+ ' {% minibundle css %}',
262
+ <<-END
263
+ {% minibundle css %}
264
+ baseurl: /ignored
265
+ destination_baseurl: {{ site.cdn_baseurl }}css/
266
+ END
267
+ )
268
+
269
+ find_and_gsub_in_file(
270
+ source_path('_layouts/default.html'),
271
+ / #{Regexp.escape('{% minibundle js %}')}.*#{Regexp.escape('{% endminibundle %}')}/m,
272
+ <<-END
273
+ {% minibundle js %}
274
+ source_dir: _assets/scripts
275
+ destination_path: static
276
+ baseurl: /ignored
277
+ destination_baseurl: '{{ site.cdn_baseurl }}'
278
+ assets:
279
+ - dependency
280
+ - app
281
+ {% endminibundle %}
282
+ END
283
+ )
232
284
 
233
- find_and_gsub_in_file(source_path('_layouts/default.html'), '{% minibundle css %}', "{% minibundle css %}\n baseurl: /css-root")
234
- find_and_gsub_in_file(source_path('_layouts/default.html'), '{% minibundle js %}', "{% minibundle js %}\n baseurl: /js-root")
285
+ generate_site(:development)
235
286
 
236
- generate_site(:development, clear_cache: false)
287
+ assert(File.file?(destination_path(CSS_BUNDLE_DESTINATION_PATH, 'common.css')))
288
+ assert(File.file?(destination_path('static/app.js')))
237
289
 
238
- assert_equal '/css-root/assets/site/common.css', find_css_paths_from_index.last
239
- assert_equal '/js-root/assets/site/app.js', find_js_paths_from_index.last
290
+ assert_equal('https://cdn.example.com/?file=css/site/common.css', find_css_paths_from_index.last)
291
+ assert_equal('https://cdn.example.com/?file=static/app.js', find_js_paths_from_index.last)
240
292
  end
241
293
  end
242
294
 
@@ -244,8 +296,8 @@ module Jekyll::Minibundle::Test
244
296
  with_site_dir do
245
297
  generate_site(:development)
246
298
 
247
- assert File.exist?(destination_path(CSS_BUNDLE_DESTINATION_PATH, 'common.css'))
248
- assert File.exist?(destination_path(JS_BUNDLE_DESTINATION_PATH, 'app.js'))
299
+ assert(File.file?(destination_path(CSS_BUNDLE_DESTINATION_PATH, 'common.css')))
300
+ assert(File.file?(destination_path(JS_BUNDLE_DESTINATION_PATH, 'app.js')))
249
301
 
250
302
  find_and_gsub_in_file(source_path('_layouts/default.html'), 'id: my-styles', 'id: my-styles2')
251
303
  find_and_gsub_in_file(source_path('_layouts/default.html'), 'id: my-scripts', 'id: my-scripts2')
@@ -253,10 +305,10 @@ module Jekyll::Minibundle::Test
253
305
  generate_site(:development, clear_cache: false)
254
306
 
255
307
  css_ids = find_css_elements_from_index.map { |el| el['id'] }.uniq
256
- assert_equal ['my-styles2'], css_ids
308
+ assert_equal(['my-styles2'], css_ids)
257
309
 
258
310
  js_ids = find_js_elements_from_index.map { |el| el['id'] }.uniq
259
- assert_equal ['my-scripts2'], js_ids
311
+ assert_equal(['my-scripts2'], js_ids)
260
312
  end
261
313
  end
262
314
 
@@ -277,13 +329,13 @@ module Jekyll::Minibundle::Test
277
329
 
278
330
  generate_site(:development, clear_cache: false)
279
331
 
280
- assert_equal org_mtime, file_mtime_of(expected_js_path)
332
+ assert_equal(org_mtime, file_mtime_of(expected_js_path))
281
333
 
282
334
  ensure_file_mtime_changes { FileUtils.touch('index.html') }
283
335
 
284
336
  generate_site(:development, clear_cache: false)
285
337
 
286
- assert_equal org_mtime, file_mtime_of(expected_js_path)
338
+ assert_equal(org_mtime, file_mtime_of(expected_js_path))
287
339
  end
288
340
  end
289
341
 
@@ -300,11 +352,11 @@ module Jekyll::Minibundle::Test
300
352
 
301
353
  generate_site(:development, clear_cache: false)
302
354
 
303
- assert_equal org_mtime, file_mtime_of(expected_js_path)
355
+ assert_equal(org_mtime, file_mtime_of(expected_js_path))
304
356
  end
305
357
  end
306
358
 
307
- def test_does_not_rebundle_assets_when_changing_baseurl
359
+ def test_does_not_rewrite_destination_when_changing_baseurl
308
360
  with_site_dir do
309
361
  generate_site(:development)
310
362
 
@@ -312,12 +364,45 @@ module Jekyll::Minibundle::Test
312
364
  org_mtime = file_mtime_of(expected_js_path)
313
365
 
314
366
  ensure_file_mtime_changes do
315
- find_and_gsub_in_file(source_path('_layouts/default.html'), '{% minibundle js %}', "{% minibundle js %}\n baseurl: /js-root")
367
+ find_and_gsub_in_file(
368
+ source_path('_layouts/default.html'),
369
+ ' {% minibundle js %}',
370
+ <<-END
371
+ {% minibundle js %}
372
+ baseurl: /js-root
373
+ END
374
+ )
375
+ end
376
+
377
+ generate_site(:development, clear_cache: false)
378
+
379
+ assert_equal(org_mtime, file_mtime_of(expected_js_path))
380
+ assert_equal("/js-root/#{JS_BUNDLE_DESTINATION_PATH}/app.js", find_js_paths_from_index.last)
381
+ end
382
+ end
383
+
384
+ def test_does_not_rewrite_destination_when_changing_destination_baseurl
385
+ with_site_dir do
386
+ generate_site(:development)
387
+
388
+ expected_js_path = destination_path(JS_BUNDLE_DESTINATION_PATH, 'app.js')
389
+ org_mtime = file_mtime_of(expected_js_path)
390
+
391
+ ensure_file_mtime_changes do
392
+ find_and_gsub_in_file(
393
+ source_path('_layouts/default.html'),
394
+ ' {% minibundle js %}',
395
+ <<-END
396
+ {% minibundle js %}
397
+ destination_baseurl: /js-root/
398
+ END
399
+ )
316
400
  end
317
401
 
318
402
  generate_site(:development, clear_cache: false)
319
403
 
320
- assert_equal org_mtime, file_mtime_of(expected_js_path)
404
+ assert_equal(org_mtime, file_mtime_of(expected_js_path))
405
+ assert_equal('/js-root/site/app.js', find_js_paths_from_index.last)
321
406
  end
322
407
  end
323
408
 
@@ -325,7 +410,7 @@ module Jekyll::Minibundle::Test
325
410
  with_site_dir do
326
411
  merge_to_yaml_file('_config.yml', 'minibundle' => {'mode' => 'development'})
327
412
  generate_site(nil)
328
- assert File.exist?(destination_path(JS_BUNDLE_DESTINATION_PATH, 'app.js'))
413
+ assert(File.file?(destination_path(JS_BUNDLE_DESTINATION_PATH, 'app.js')))
329
414
  end
330
415
  end
331
416
 
@@ -333,7 +418,7 @@ module Jekyll::Minibundle::Test
333
418
  with_site_dir do
334
419
  merge_to_yaml_file('_config.yml', 'minibundle' => {'mode' => 'production'})
335
420
  generate_site(:development)
336
- assert File.exist?(destination_path(JS_BUNDLE_DESTINATION_PATH, 'app.js'))
421
+ assert(File.file?(destination_path(JS_BUNDLE_DESTINATION_PATH, 'app.js')))
337
422
  end
338
423
  end
339
424
 
@@ -360,8 +445,8 @@ module Jekyll::Minibundle::Test
360
445
  end
361
446
 
362
447
  def expect_file_exists_and_is_equal_to(actual, expected)
363
- assert File.exist?(actual)
364
- assert_equal File.read(expected), File.read(actual)
448
+ assert(File.file?(actual))
449
+ assert_equal(File.read(expected), File.read(actual))
365
450
  end
366
451
 
367
452
  def change_destination_path_in_minibundle_block(from, to)