Skip to content

Commit a7ee472

Browse files
committedNov 25, 2014
Always build top level assets as ruby sources, irrespective of file extension
1 parent 358a9fc commit a7ee472

File tree

2 files changed

+39
-16
lines changed

2 files changed

+39
-16
lines changed
 

‎lib/opal/builder.rb

+38-15
Original file line numberDiff line numberDiff line change
@@ -75,29 +75,48 @@ def build(logical_path, options = {})
7575
end
7676

7777
def build_str(source, logical_path, options = {})
78-
filename = path_reader.expand(logical_path).to_s
79-
8078
options = options.merge(requirable: false)
81-
asset = build_asset(source, logical_path, filename, requirable: false)
79+
process_string(source, logical_path, options)
8280

8381
preload.each { |path| process_require path, options }
8482

85-
process_requires asset, filename, options
86-
@assets << asset
8783
self
8884
end
8985

90-
def build_require(logical_path, options = {})
91-
process_require(logical_path, options)
86+
# Build the given asset as a ruby source, ignoring any actual file
87+
# extension for asset.
88+
#
89+
# Handles a ruby file (on disk) as the entry point to the application.
90+
# This method ensures the file is compiled as a ruby source, irrelevant
91+
# of its actual file extension. This is useful in sprockets contexts
92+
# where a ruby file may be handled after it has been through other
93+
# engines, so its file extension might not identify it as a ruby
94+
# source.
95+
#
96+
def build_require(source, logical_path, options = {})
97+
process_string source, logical_path, options
98+
end
99+
100+
# @private
101+
#
102+
# Process the given string as a ruby source, ignoring the path for
103+
# processor type. Used to force a processor for the given path
104+
# compilation.
105+
def process_string(source, logical_path, options)
106+
filename = path_reader.expand(logical_path).to_s
107+
asset = build_asset(source, logical_path, default_processor, options)
108+
109+
process_requires(asset, filename, options)
110+
@assets << asset
92111
end
93112

94-
def process_require(logical_path, options)
113+
def process_require(logical_path, options, processor = nil)
95114
return if prerequired.include?(logical_path)
96115
return if processed.include?(logical_path)
97116
processed << logical_path
98117

99118
filename = path_reader.expand(logical_path).to_s
100-
asset = find_asset logical_path, options
119+
asset = find_asset logical_path, options, processor
101120

102121
process_requires asset, filename, options
103122
@assets << asset
@@ -109,7 +128,7 @@ def process_requires(asset, filename, options)
109128
end
110129
end
111130

112-
def find_asset(logical_path, options)
131+
def find_asset(logical_path, options, processor = nil)
113132
cached_asset(logical_path) do
114133
source = stub?(logical_path) ? '' : read(logical_path)
115134

@@ -121,15 +140,19 @@ def find_asset(logical_path, options)
121140
end
122141
end
123142

124-
filename = path_reader.expand(logical_path).to_s
143+
filename = path_reader.expand(logical_path).to_s
144+
processor ||= processor_for(filename)
125145

126-
build_asset(source, logical_path, filename, options.merge(requirable: true))
146+
build_asset(source, logical_path, processor, options.merge(requirable: true))
127147
end
128148
end
129149

130-
def build_asset(source, logical_path, filename, options)
131-
extname = File.extname(filename)
132-
processor = processors.fetch(extname) { default_processor }
150+
def processor_for(filename)
151+
extname = File.extname(filename)
152+
processors.fetch(extname) { default_processor }
153+
end
154+
155+
def build_asset(source, logical_path, processor, options)
133156
options = compiler_options.merge(options)
134157

135158
result = processor.new(source, logical_path, options)

‎lib/opal/sprockets/processor.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def evaluate(context, locals, &block)
7070
path = context.logical_path
7171

7272
builder = self.class.new_builder(context)
73-
builder.build_require(path)
73+
builder.build_require(data, path)
7474

7575
result = builder.to_s + "\nOpal.require(#{path.inspect});"
7676

0 commit comments

Comments
 (0)
Please sign in to comment.