From ec10ebdc219d67bdb80fddff0343ae66b3fe45ae Mon Sep 17 00:00:00 2001 From: inottn Date: Sat, 16 Nov 2024 18:10:00 +0800 Subject: [PATCH] fix --- .github/workflows/get-runner-labels.yml | 2 +- crates/rspack_plugin_asset/src/lib.rs | 21 ++++++++++++++++++- crates/rspack_plugin_schemes/src/data_uri.rs | 12 +++++------ .../asset-modules/data-url/index.js | 9 ++++---- .../input-data-url-encoding/test.filter.js | 2 -- 5 files changed, 30 insertions(+), 16 deletions(-) delete mode 100644 tests/webpack-test/configCases/asset-modules/input-data-url-encoding/test.filter.js diff --git a/.github/workflows/get-runner-labels.yml b/.github/workflows/get-runner-labels.yml index 1e9b9f5545e1..8afb665ca821 100644 --- a/.github/workflows/get-runner-labels.yml +++ b/.github/workflows/get-runner-labels.yml @@ -22,7 +22,7 @@ on: jobs: main: name: Get Runner Labels - runs-on: [self-hosted, Linux, ci] + runs-on: ubuntu-latest outputs: LINUX_RUNNER_LABELS: ${{ steps.run.outputs.LINUX_RUNNER_LABELS }} MACOS_RUNNER_LABELS: ${{ steps.run.outputs.MACOS_RUNNER_LABELS }} diff --git a/crates/rspack_plugin_asset/src/lib.rs b/crates/rspack_plugin_asset/src/lib.rs index 0dcaada3eed8..b5a56a3ca113 100644 --- a/crates/rspack_plugin_asset/src/lib.rs +++ b/crates/rspack_plugin_asset/src/lib.rs @@ -102,6 +102,20 @@ impl AssetParserAndGenerator { } } + fn decode_data_uri_content(encoding: &str, content: &str, source: &BoxSource) -> Vec { + if encoding == "base64" + && let Some(cleaned) = rspack_base64::clean_base64(content) + { + return rspack_base64::decode_to_vec(cleaned.as_bytes()) + .unwrap_or_else(|_| source.buffer().to_vec()); + } + + match urlencoding::decode(content) { + Ok(decoded_content) => decoded_content.as_bytes().to_vec(), + Err(_) => content.as_bytes().to_vec(), + } + } + fn hash_for_source( &self, source: &BoxSource, @@ -184,7 +198,12 @@ impl AssetParserAndGenerator { encoding: &str, source: &BoxSource, ) -> Result { - if let Some(encoded_content) = &resource_data.encoded_content { + if let Some(encoded_content) = &resource_data.encoded_content + && let Some(resource_encoding) = &resource_data.encoding + && resource_encoding == encoding + && AssetParserAndGenerator::decode_data_uri_content(encoding, encoded_content, source) + .eq(&source.buffer().to_vec()) + { return Ok(encoded_content.to_owned()); } if encoding.is_empty() { diff --git a/crates/rspack_plugin_schemes/src/data_uri.rs b/crates/rspack_plugin_schemes/src/data_uri.rs index e0fbf38c6a40..86e0675aa885 100644 --- a/crates/rspack_plugin_schemes/src/data_uri.rs +++ b/crates/rspack_plugin_schemes/src/data_uri.rs @@ -69,13 +69,11 @@ async fn read_resource(&self, resource_data: &ResourceData) -> Result Ok(Some(Content::String(resource_data.resource.to_string()))), }; } - if !body.is_ascii() { - return Ok(Some(Content::Buffer( - urlencoding::decode_binary(body.as_bytes()).into_owned(), - ))); - } else { - return Ok(Some(Content::Buffer(body.bytes().collect()))); - } + + return match urlencoding::decode(body) { + Ok(decoded_content) => Ok(Some(Content::Buffer(decoded_content.as_bytes().to_vec()))), + Err(_) => Ok(Some(Content::Buffer(body.as_bytes().to_vec()))), + }; } Ok(None) } diff --git a/tests/webpack-test/configCases/asset-modules/data-url/index.js b/tests/webpack-test/configCases/asset-modules/data-url/index.js index f998c7754967..ee46bb5c0446 100644 --- a/tests/webpack-test/configCases/asset-modules/data-url/index.js +++ b/tests/webpack-test/configCases/asset-modules/data-url/index.js @@ -24,9 +24,8 @@ it("should generate various data-url types", () => { expect(urlSvg2.href).toContain( "data:image/svg+xml;p=1;q=2,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3e%3cpath fill='none' stroke=\"%23343a40\" stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M2 5l6 6 6-6'/%3e%3c/svg%3e" ); - // TODO: fixme - // expect(helloWorld.href).toContain("data:text/plain,Hello%2C%20World%21"); - // expect(helloWorldBase64.href).toContain( - // "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==" - // ); + expect(helloWorld.href).toContain("data:text/plain,Hello%2C%20World%21"); + expect(helloWorldBase64.href).toContain( + "data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==" + ); }); diff --git a/tests/webpack-test/configCases/asset-modules/input-data-url-encoding/test.filter.js b/tests/webpack-test/configCases/asset-modules/input-data-url-encoding/test.filter.js deleted file mode 100644 index 042493e42a5b..000000000000 --- a/tests/webpack-test/configCases/asset-modules/input-data-url-encoding/test.filter.js +++ /dev/null @@ -1,2 +0,0 @@ -// TODO: Should create a issue for this test -module.exports = () => { return false }