From e4ea08a05bcd2477aa028f0d16527110447dff82 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Mon, 26 Feb 2024 21:01:01 +0900 Subject: [PATCH] Add `--no-content-hash` flag to avoid using hashes in the output file names --- CHANGELOG.md | 4 ++++ Sources/CartonCLI/Commands/Bundle.swift | 7 +++++-- .../CartonCommandTests/BundleCommandTests.swift | 16 ++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c171b28..dcca06e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,10 @@ sequenceDiagram CartonDevPlugin->>CartonFrontend: Reload browsers ``` +**Additional changes:** + +- `--no-content-hash` option is added to `carton bundle` command. This option disables the use of content hash in the output file name. + # 0.20.1 (25 Jan 2024) This release fixes a bug in `carton test` where it reports missing `sock_accept` syscall. diff --git a/Sources/CartonCLI/Commands/Bundle.swift b/Sources/CartonCLI/Commands/Bundle.swift index 4fc542c..a2ab5b5 100644 --- a/Sources/CartonCLI/Commands/Bundle.swift +++ b/Sources/CartonCLI/Commands/Bundle.swift @@ -65,6 +65,9 @@ struct Bundle: AsyncParsableCommand { ) var wasmOptimizations: WasmOptimizations = .size + @Flag(inversion: .prefixedNo, help: "Use a content hash for the output file names.") + var contentHash: Bool = true + @Option var output: String @@ -160,7 +163,7 @@ struct Bundle: AsyncParsableCommand { ) throws { // Rename the final binary to use a part of its hash to bust browsers and CDN caches. let wasmFileHash = try localFileSystem.readFileContents(wasmOutputFilePath).hexChecksum - let mainModuleName = "\(wasmFileHash).wasm" + let mainModuleName = contentHash ? "\(wasmFileHash).wasm" : URL(fileURLWithPath: mainWasmPath).lastPathComponent let mainModulePath = try AbsolutePath(validating: mainModuleName, relativeTo: bundleDirectory) try localFileSystem.move(from: wasmOutputFilePath, to: mainModulePath) @@ -174,7 +177,7 @@ struct Bundle: AsyncParsableCommand { with: mainModuleName ) ) - let entrypointName = "\(entrypoint.hexChecksum).js" + let entrypointName = contentHash ? "\(entrypoint.hexChecksum).js" : "index.js" try localFileSystem.writeFileContents( AbsolutePath(validating: entrypointName, relativeTo: bundleDirectory), bytes: entrypoint diff --git a/Tests/CartonCommandTests/BundleCommandTests.swift b/Tests/CartonCommandTests/BundleCommandTests.swift index 0c6ac1a..6018e48 100644 --- a/Tests/CartonCommandTests/BundleCommandTests.swift +++ b/Tests/CartonCommandTests/BundleCommandTests.swift @@ -60,6 +60,22 @@ final class BundleCommandTests: XCTestCase { } } + func testWithoutContentHash() throws { + try withFixture("EchoExecutable") { packageDirectory in + let result = try swiftRun( + ["carton", "bundle", "--no-content-hash", "--wasm-optimizations", "none"], packageDirectory: packageDirectory.url + ) + result.assertZeroExit() + + let bundleDirectory = packageDirectory.appending(component: "Bundle") + guard let wasmBinary = (bundleDirectory.ls().filter { $0.contains("wasm") }).first else { + XCTFail("No wasm binary found") + return + } + XCTAssertEqual(wasmBinary, "my-echo.wasm") + } + } + func testWasmOptimizationOptions() throws { try withFixture("EchoExecutable") { packageDirectory in func getFileSizeOfWasmBinary(wasmOptimizations: WasmOptimizations) throws -> UInt64 {