Skip to content

Commit ade30e1

Browse files
committed
Fixed bug where Size Class names were not correctly added to ExportableImages
1 parent cd1d51a commit ade30e1

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

Sources/ResponsivePublishPlugin/ImageResource.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ struct ImageConfiguration {
6363
self.targetExtension = targetExtension
6464
self.targetSizes = targetSizes
6565
}
66+
67+
func fileName(for sizeClass: SizeClass) -> String {
68+
"\(self.fileName)-\(sizeClass.fileSuffix)"
69+
}
6670
}
6771

6872
struct ExportableImage {
@@ -137,7 +141,7 @@ func rewrites(from source: Path, to target: Path, for config: ImageConfiguration
137141
config.targetSizes.map { size in
138142
ImageRewrite(
139143
source: .init(path: source, fileName: config.fileName, extension: config.extension),
140-
target: .init(path: target, fileName: "\(config.fileName)-\(size.fileSuffix)", extension: config.targetExtension),
144+
target: .init(path: target, fileName: config.fileName(for: size), extension: config.targetExtension),
141145
targetSizeClass: size
142146
)
143147
}

Sources/ResponsivePublishPlugin/ResponsivePublishPlugin+Environment.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ struct Environment {
2222
return Environment { configs in
2323
return Parallel<[ExportableImage]> {
2424
let images = configs.reduce([ExportableImage]()) { current, config in
25-
current + config.targetSizes.compactMap { size in
26-
guard let image = Image(width: size.upperBound / 2, height: size.upperBound) else { return nil }
25+
current + config.targetSizes.compactMap { sizeClass in
26+
guard let image = Image(width: sizeClass.upperBound / 2, height: sizeClass.upperBound) else { return nil }
2727
return ExportableImage(
28-
name: config.fileName,
28+
name: config.fileName(for: sizeClass),
2929
extension: .webp,
3030
image: image
3131
)
@@ -46,7 +46,7 @@ fileprivate func reducedImaged(for configurations: [ImageConfiguration]) -> Para
4646
let targetSize = sizeThatFits(for: image.size, within: sizeClass.upperBound)
4747
guard let resizedImage = image.resizedTo(width: targetSize.width, height: targetSize.height) else { return nil }
4848
return ExportableImage(
49-
name: config.fileName,
49+
name: config.fileName(for: sizeClass),
5050
extension: config.targetExtension,
5151
image: resizedImage
5252
)

Tests/ResponsivePublishPluginTests/ResponsivePublishPluginTests.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,29 @@ final class ResponsivePublishPluginTests: XCTestCase {
179179

180180
XCTAssertEqual(output, expected)
181181
}
182+
183+
func testImagesForSizeClassesAreCreated() throws {
184+
try TestWebsite().publish(
185+
at: Self.testDirPath,
186+
using: [
187+
.copyResources(),
188+
.installPlugin(
189+
.generateOptimizedImages(
190+
from: resourcesFolderPath.appendingComponent("img"),
191+
at: Path("img-optimized"),
192+
rewriting: Path("css/styles.css")
193+
)
194+
)
195+
]
196+
)
197+
198+
let output = try? outputFolder?
199+
.subfolder(at: "img-optimized")
200+
.files
201+
.names()
202+
.sorted()
203+
204+
XCTAssertEqual(output?.count, 4)
205+
XCTAssertEqual(output, ["background-extra-small.webp", "background-large.webp", "background-normal.webp", "background-small.webp"])
206+
}
182207
}

0 commit comments

Comments
 (0)