Skip to content

Commit

Permalink
Ensure generated paths in split() matches JS
Browse files Browse the repository at this point in the history
  • Loading branch information
rtsao committed Aug 9, 2023
1 parent 3a7b7f3 commit 82f13ab
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
2 changes: 1 addition & 1 deletion fixtures/split-no-ssr/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ Object.defineProperties(import("./foo/baz"), {
value: []
},
__MODULE_ID: {
value: "virtual:fusion-vite-split-loader?importer=%2Fpath%2Fto%2Ffile%2Ejs&specifier=%2E%2Ffoo%2Fbaz"
value: "virtual:fusion-vite-split-loader?importer=%2Fpath%2Fto%2Ffile.js&specifier=.%2Ffoo%2Fbaz"
},
});
4 changes: 2 additions & 2 deletions fixtures/split/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ Object.defineProperties(import("./foo/baz"), {
value: []
},
__MODULE_ID: {
value: "virtual:fusion-vite-split-loader?importer=%2Fpath%2Fto%2Ffile%2Ejs&specifier=%2E%2Ffoo%2Fbaz"
value: "virtual:fusion-vite-split-loader?importer=%2Fpath%2Fto%2Ffile.js&specifier=.%2Ffoo%2Fbaz"
},
__FUSION_DYNAMIC_IMPORT_METADATA__: {
value: {
version: 0,
moduleId: "virtual:fusion-vite-split-loader?importer=%2Fpath%2Fto%2Ffile%2Ejs&specifier=%2E%2Ffoo%2Fbaz"
moduleId: "virtual:fusion-vite-split-loader?importer=%2Fpath%2Fto%2Ffile.js&specifier=.%2Ffoo%2Fbaz"
}
}
});
43 changes: 36 additions & 7 deletions packages/fusion/transform/src/visitors/split.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::rc::Rc;

use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
use swc_core::{
common::{errors::HANDLER, FileName, DUMMY_SP},
ecma::{
Expand All @@ -16,6 +16,25 @@ pub fn split(config: Rc<Config>, file_name: FileName) -> impl VisitMut + Fold {
as_folder(SplitVisitor { config, file_name })
}

// https://url.spec.whatwg.org/#query-percent-encode-set
const QUERY: &AsciiSet = &CONTROLS.add(b' ').add(b'"').add(b'#').add(b'<').add(b'>');
// https://url.spec.whatwg.org/#path-percent-encode-set
const PATH: &AsciiSet = &QUERY.add(b'?').add(b'`').add(b'{').add(b'}');
// https://url.spec.whatwg.org/#userinfo-percent-encode-set
const USERINFO: &AsciiSet = &PATH
.add(b'/')
.add(b':')
.add(b';')
.add(b'=')
.add(b'@')
.add(b'[')
.add(0x005c)
.add(0x005d)
.add(b'^')
.add(b'|');
// https://url.spec.whatwg.org/#component-percent-encode-set
const COMPONENT: &AsciiSet = &USERINFO.add(b'$').add(0x0025).add(b'&').add(b'+').add(b',');

fn get_member_props(module_id: String, ssr: bool) -> Vec<PropOrSpread> {
let mut member_props = vec![
PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
Expand Down Expand Up @@ -128,15 +147,13 @@ impl VisitMut for SplitVisitor {
type_args: Default::default(),
});

let encoded_file_name = utf8_percent_encode(
&self.file_name.to_string(),
NON_ALPHANUMERIC,
)
.to_string();
let encoded_file_name =
utf8_percent_encode(&self.file_name.to_string(), COMPONENT)
.to_string();

let encoded_import = utf8_percent_encode(
&lit_str.value.clone().to_string(),
NON_ALPHANUMERIC,
COMPONENT,
)
.to_string();

Expand Down Expand Up @@ -192,3 +209,15 @@ impl VisitMut for SplitVisitor {
}
}
}

#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_encode_uri_component() {
assert_eq!(
utf8_percent_encode("!@#$%^&*()-_+={}[]\\|'\";:+ ,.<>?/~`", COMPONENT,).to_string(),
"!%40%23%24%25%5E%26*()-_%2B%3D%7B%7D%5B%5D%5C%7C'%22%3B%3A%2B%20%2C.%3C%3E%3F%2F~%60"
);
}
}

0 comments on commit 82f13ab

Please sign in to comment.