Skip to content

Commit

Permalink
fix(optimizer): windows paths
Browse files Browse the repository at this point in the history
  • Loading branch information
wmertens committed Jul 25, 2024
1 parent f55dbf5 commit f6d9fba
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 77 deletions.
7 changes: 5 additions & 2 deletions packages/qwik/src/optimizer/core/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,10 @@ impl Emitter for ErrorBuffer {

pub fn transform_code(config: TransformCodeOptions) -> Result<TransformOutput, anyhow::Error> {
let source_map = Lrc::new(SourceMap::default());
let path_data = parse_path(config.relative_path, config.src_dir)?;
let path_data = parse_path(
config.relative_path.replace('\\', "/").as_str(),
config.src_dir,
)?;
let module = parse(
config.code,
&path_data,
Expand Down Expand Up @@ -470,7 +473,7 @@ pub fn transform_code(config: TransformCodeOptions) -> Result<TransformOutput, a
} else {
path_data.file_name
};
let path = path_data.rel_dir.join(a).to_string_lossy().to_string();
let path = path_data.rel_dir.join(a).to_slash_lossy().to_string();

let mut hasher = DefaultHasher::new();
hasher.write(path.as_bytes());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
source: packages/qwik/src/optimizer/core/src/test.rs
assertion_line: 1948
expression: output
---
==INPUT==


import { component$ } from '@builder.io/qwik';
export const Greeter = component$(() => <div/>)

============================= components/apps/greeter_component_0jjovx068y0.ts (ENTRY POINT)==

import { _jsxQ } from "@builder.io/qwik";
export const Greeter_component_0jjOvx068y0 = ()=>/*#__PURE__*/ _jsxQ("div", null, null, null, 3, "KD_0");


Some("{\"version\":3,\"sources\":[\"C:\\\\users\\\\apps/components/apps/apps.tsx\"],\"names\":[],\"mappings\":\";6CAEkC,kBAAM,MAAC\"}")
/*
{
"origin": "components/apps/apps.tsx",
"name": "Greeter_component_0jjOvx068y0",
"entry": null,
"displayName": "Greeter_component",
"hash": "0jjOvx068y0",
"canonicalFilename": "greeter_component_0jjovx068y0",
"path": "components/apps",
"extension": "ts",
"parent": null,
"ctxKind": "function",
"ctxName": "component$",
"captures": false,
"loc": [
83,
95
]
}
*/
============================= components/apps/apps.ts ==

import { componentQrl } from "@builder.io/qwik";
import { qrl } from "@builder.io/qwik";
export const Greeter = /*#__PURE__*/ componentQrl(/*#__PURE__*/ qrl(()=>import("./greeter_component_0jjovx068y0"), "Greeter_component_0jjOvx068y0"));


Some("{\"version\":3,\"sources\":[\"C:\\\\users\\\\apps/components/apps/apps.tsx\"],\"names\":[],\"mappings\":\";;AAEA,OAAO,MAAM,wBAAU,gHAAwB\"}")
== DIAGNOSTICS ==

[]
138 changes: 69 additions & 69 deletions packages/qwik/src/optimizer/core/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,6 @@
use super::*;
use serde_json::to_string_pretty;

macro_rules! test_input {
($input: expr) => {
let input = $input;
let strip_exports: Option<Vec<JsWord>> = input
.strip_exports
.map(|v| v.into_iter().map(|s| JsWord::from(s)).collect());

let reg_ctx_name: Option<Vec<JsWord>> = input
.reg_ctx_name
.map(|v| v.into_iter().map(|s| JsWord::from(s)).collect());

let strip_ctx_name: Option<Vec<JsWord>> = input
.strip_ctx_name
.map(|v| v.into_iter().map(|s| JsWord::from(s)).collect());

let res = transform_modules(TransformModulesOptions {
src_dir: input.src_dir,
root_dir: input.root_dir,
input: vec![TransformModuleInput {
code: input.code.clone(),
path: input.filename,
}],
source_maps: true,
minify: input.minify,
transpile_ts: input.transpile_ts,
transpile_jsx: input.transpile_jsx,
preserve_filenames: input.preserve_filenames,
explicit_extensions: input.explicit_extensions,
manual_chunks: input.manual_chunks,
entry_strategy: input.entry_strategy,
mode: input.mode,
scope: input.scope,
core_module: input.core_module,
strip_exports,
strip_ctx_name,
reg_ctx_name,
strip_event_handlers: input.strip_event_handlers,
is_server: input.is_server,
});
if input.snapshot {
let input = input.code.to_string();
let output = format!("==INPUT==\n\n{}", input);
snapshot_res!(&res, output);
}
drop(res)
};
}

macro_rules! snapshot_res {
($res: expr, $prefix: expr) => {
match $res {
Expand All @@ -68,8 +20,6 @@ macro_rules! snapshot_res {
let hook = to_string_pretty(&hook).unwrap();
output += &format!("\n/*\n{}\n*/", hook);
}
// let map = if let Some(map) = s.map { map } else { "".to_string() };
// output += format!("\n== MAP ==\n{}", map).as_str();
}
output += format!(
"\n== DIAGNOSTICS ==\n\n{}",
Expand All @@ -85,6 +35,56 @@ macro_rules! snapshot_res {
};
}

macro_rules! test_input {
($input: expr) => {{
let input = $input;
let code = input.code.to_string();
let snapshot = input.snapshot;
let res = test_input_fn(input);
if snapshot {
snapshot_res!(&res, format!("==INPUT==\n\n{}", code.to_string()));
}
res
}};
}

fn test_input_fn(input: TestInput) -> Result<TransformOutput, anyhow::Error> {
let strip_exports: Option<Vec<JsWord>> = input
.strip_exports
.map(|v| v.into_iter().map(|s| JsWord::from(s)).collect());
let reg_ctx_name: Option<Vec<JsWord>> = input
.reg_ctx_name
.map(|v| v.into_iter().map(|s| JsWord::from(s)).collect());
let strip_ctx_name: Option<Vec<JsWord>> = input
.strip_ctx_name
.map(|v| v.into_iter().map(|s| JsWord::from(s)).collect());

transform_modules(TransformModulesOptions {
src_dir: input.src_dir,
root_dir: input.root_dir,
input: vec![TransformModuleInput {
code: input.code.clone(),
path: input.filename,
}],
source_maps: true,
minify: input.minify,
transpile_ts: input.transpile_ts,
transpile_jsx: input.transpile_jsx,
preserve_filenames: input.preserve_filenames,
explicit_extensions: input.explicit_extensions,
manual_chunks: input.manual_chunks,
entry_strategy: input.entry_strategy,
mode: input.mode,
scope: input.scope,
core_module: input.core_module,
strip_exports,
strip_ctx_name,
reg_ctx_name,
strip_event_handlers: input.strip_event_handlers,
is_server: input.is_server,
})
}

#[test]
fn example_1() {
test_input!(TestInput {
Expand Down Expand Up @@ -1857,7 +1857,7 @@ export const Parent = component$(() => {
transpile_ts: true,
transpile_jsx: true,
entry_strategy: EntryStrategy::Inline,
strip_ctx_name: Some(vec!["useClientMount$".into(),]),
strip_ctx_name: Some(vec!["useClientMount$".into()]),
strip_event_handlers: true,
..TestInput::default()
});
Expand Down Expand Up @@ -1943,34 +1943,34 @@ export const Greeter = component$(() => {
});
}

#[cfg(target_os = "windows")]
#[test]
fn issue_188() {
let res = test_input!({
fn support_windows_paths() {
let res = test_input!(TestInput {
filename: r"components\apps\apps.tsx".to_string(),
src_dir: r"C:\users\apps".to_string(),
code: r#"
import { component$, $ } from '@builder.io/qwik';
export const Greeter = component$(() => {
return $(() => {
return (
<div/>
)
});
});
const d = $(()=>console.log('thing'));
import { component$ } from '@builder.io/qwik';
export const Greeter = component$(() => <div/>)
"#
.to_string(),
transpile_ts: true,
transpile_jsx: true,
snapshot: false,
is_server: Some(false),
entry_strategy: EntryStrategy::Hook,
..TestInput::default()
})
.unwrap();
let last_module = res.modules.last().unwrap();
assert_eq!(last_module.path, r"C:/users/apps/components/apps/apps.tsx")
// verify that none of the modules have a path that contains backslashes
for module in res.modules {
assert!(!module.path.contains('\\'));
}
}
// filler to retain assertion line numbers
//
//
//
//
//

#[test]
fn issue_476() {
test_input!(TestInput {
Expand Down
7 changes: 1 addition & 6 deletions packages/qwik/src/optimizer/core/src/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,7 @@ impl<'a> QwikTransform<'a> {

fn get_dev_location(&self, span: Span) -> ast::ExprOrSpread {
let loc = self.options.cm.lookup_char_pos(span.lo);
let file_name = self
.options
.path_data
.rel_path
.to_string_lossy()
.to_string();
let file_name = self.options.path_data.rel_path.to_slash_lossy().to_string();
ast::ExprOrSpread {
spread: None,
expr: Box::new(ast::Expr::Object(ast::ObjectLit {
Expand Down

0 comments on commit f6d9fba

Please sign in to comment.