Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macOS-latest] # todo: add 'windows-latest'
os: [ubuntu-latest, macOS-latest, windows-latest]
rust: [stable]
experimental: [false]
# Note: We're no longer reliant on nightly, so we can remove this.
Expand All @@ -37,6 +37,7 @@ jobs:
toolchain: ${{ matrix.rust }}
- uses: actions/checkout@master
- name: Install npm packages for examples & tests
shell: bash
run: |
(cd ./crates/vite-rs/test_projects && npm ci)
(cd ./crates/vite-rs-axum-0-8/test_projects/basic_usage_test/app && npm ci)
Expand All @@ -53,6 +54,7 @@ jobs:
cargo test -p vite-rs-axum-0-8
cargo test -p vite-rs-axum-0-8 --release
- name: Run/compile examples
shell: bash
run: |
# VITE-RS
cargo run -p vite-rs --example basic_usage
Expand Down
24 changes: 20 additions & 4 deletions crates/vite-rs-axum-0-8/tests/basic_usage_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,18 @@ async fn ensure_serves_index(app: axum::Router) {
let body_bytes = body::to_bytes(response.into_body(), 2048).await.unwrap();

#[cfg(all(debug_assertions, not(feature = "debug-prod")))]
assert_eq!(body_bytes, "<!DOCTYPE html>\n<html lang=\"en\">\n <head>\n <script type=\"module\">import { injectIntoGlobalHook } from \"/@react-refresh\";\ninjectIntoGlobalHook(window);\nwindow.$RefreshReg$ = () => {};\nwindow.$RefreshSig$ = () => (type) => type;</script>\n\n <script type=\"module\" src=\"/@vite/client\"></script>\n\n <title>Hello World</title>\n <link rel=\"stylesheet\" href=\"/test.css\" />\n </head>\n <body>\n <h1>Loading...</h1>\n <script type=\"module\" src=\"/script.tsx\"></script>\n </body>\n</html>\n");
if cfg!(windows) {
assert_eq!(body_bytes, "<!DOCTYPE html>\r\n<html lang=\"en\">\r\n <head>\n <script type=\"module\">import { injectIntoGlobalHook } from \"/@react-refresh\";\ninjectIntoGlobalHook(window);\nwindow.$RefreshReg$ = () => {};\nwindow.$RefreshSig$ = () => (type) => type;</script>\n\n <script type=\"module\" src=\"/@vite/client\"></script>\n\r\n <title>Hello World</title>\r\n <link rel=\"stylesheet\" href=\"/test.css\" />\r\n </head>\r\n <body>\r\n <h1>Loading...</h1>\r\n <script type=\"module\" src=\"/script.tsx\"></script>\r\n </body>\r\n</html>\r\n");
} else {
assert_eq!(body_bytes, "<!DOCTYPE html>\n<html lang=\"en\">\n <head>\n <script type=\"module\">import { injectIntoGlobalHook } from \"/@react-refresh\";\ninjectIntoGlobalHook(window);\nwindow.$RefreshReg$ = () => {};\nwindow.$RefreshSig$ = () => (type) => type;</script>\n\n <script type=\"module\" src=\"/@vite/client\"></script>\n\n <title>Hello World</title>\n <link rel=\"stylesheet\" href=\"/test.css\" />\n </head>\n <body>\n <h1>Loading...</h1>\n <script type=\"module\" src=\"/script.tsx\"></script>\n </body>\n</html>\n");
}

#[cfg(not(all(debug_assertions, not(feature = "debug-prod"))))]
assert_eq!(body_bytes, "<!DOCTYPE html>\n<html lang=\"en\">\n <head>\n <title>Hello World</title>\n <link rel=\"stylesheet\" href=\"/test.css\" />\n <script type=\"module\" crossorigin src=\"/assets/index-CgRBhnJL.js\"></script>\n </head>\n <body>\n <h1>Loading...</h1>\n </body>\n</html>\n");
if cfg!(windows) {
assert_eq!(body_bytes, "<!DOCTYPE html>\r\n<html lang=\"en\">\r\n <head>\r\n <title>Hello World</title>\r\n <link rel=\"stylesheet\" href=\"/test.css\" />\r\n <script type=\"module\" crossorigin src=\"/assets/index-CgRBhnJL.js\"></script>\n </head>\r\n <body>\r\n <h1>Loading...</h1>\r\r\n </body>\r\n</html>\r\n");
} else {
assert_eq!(body_bytes, "<!DOCTYPE html>\n<html lang=\"en\">\n <head>\n <title>Hello World</title>\n <link rel=\"stylesheet\" href=\"/test.css\" />\n <script type=\"module\" crossorigin src=\"/assets/index-CgRBhnJL.js\"></script>\n </head>\n <body>\n <h1>Loading...</h1>\n </body>\n</html>\n");
}
}

async fn ensure_serves_public_files(app: axum::Router) {
Expand All @@ -204,10 +212,18 @@ async fn ensure_serves_public_files(app: axum::Router) {
let body_bytes = body::to_bytes(response.into_body(), 2048).await.unwrap();

#[cfg(all(debug_assertions, not(feature = "debug-prod")))]
assert_eq!(body_bytes, "body {\n background-color: black;\n color: white;\n font-family: Arial, sans-serif;\n padding: 42px;\n}\n");
if cfg!(windows) {
assert_eq!(body_bytes, "body {\r\n background-color: black;\r\n color: white;\r\n font-family: Arial, sans-serif;\r\n padding: 42px;\r\n}\r\n");
} else {
assert_eq!(body_bytes, "body {\n background-color: black;\n color: white;\n font-family: Arial, sans-serif;\n padding: 42px;\n}\n");
}

#[cfg(not(all(debug_assertions, not(feature = "debug-prod"))))]
assert_eq!(body_bytes, "body {\n background-color: black;\n color: white;\n font-family: Arial, sans-serif;\n padding: 42px;\n}\n");
if cfg!(windows) {
assert_eq!(body_bytes, "body {\r\n background-color: black;\r\n color: white;\r\n font-family: Arial, sans-serif;\r\n padding: 42px;\r\n}\r\n");
} else {
assert_eq!(body_bytes, "body {\n background-color: black;\n color: white;\n font-family: Arial, sans-serif;\n padding: 42px;\n}\n");
}
}

async fn ensure_serves_imports(app: axum::Router) {
Expand Down
29 changes: 26 additions & 3 deletions crates/vite-rs-dev-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,30 @@ pub fn start_dev_server(

// println!("Starting dev server!");
// start ViteJS dev server
let child = Arc::new(Mutex::new(
let child = Arc::new(Mutex::new(if cfg!(target_os = "windows") {
std::process::Command::new("cmd")
.arg("/C")
.arg("npx")
.arg("vite")
.arg("--host")
.arg(host)
.arg("--port")
.arg(port.to_string())
.arg("--strictPort")
.arg("--clearScreen")
.arg("false")
// we don't want to send stdin to the dev server; this also
// hides the "press h + enter to show help" message that the dev server prints
.stdin(std::process::Stdio::null())
.current_dir(
absolute_root_dir, /*format!(
"{}/examples/basic_usage",
std::env::var("CARGO_MANIFEST_DIR").unwrap()
)*/
)
.group_spawn()
.expect("failed to start ViteJS dev server")
} else {
std::process::Command::new("npx")
.arg("vite")
.arg("--host")
Expand All @@ -125,8 +148,8 @@ pub fn start_dev_server(
)*/
)
.group_spawn()
.expect("failed to start ViteJS dev server"),
));
.expect("failed to start ViteJS dev server")
}));
set_dev_server(ViteProcess(child.clone()));

#[cfg(feature = "ctrlc")]
Expand Down
43 changes: 30 additions & 13 deletions crates/vite-rs-embed-macro/src/vite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub mod build {
.map(|entry| {
let path = entry.path();
let path = path.strip_prefix(absolute_output_path).unwrap();
let path = path.to_str().unwrap().to_string();
let path = path.to_str().unwrap().replace("\\", "/");

path
})
Expand Down Expand Up @@ -45,18 +45,35 @@ pub mod build {
p.to_str().unwrap().to_string()
};

let vite_build = std::process::Command::new("npx")
.arg("vite")
.arg("build")
.arg("--manifest") // force manifest generation to `.vite/manifest.json`
.arg("--outDir")
.arg(&absolute_output_path)
.current_dir(absolute_root_dir)
.spawn()
.expect("failed to build")
.wait()
.expect("failed to wait for build to complete")
.success();
let vite_build = if cfg!(target_os = "windows") {
std::process::Command::new("cmd")
.arg("/c")
.arg("npx")
.arg("vite")
.arg("build")
.arg("--manifest") // force manifest generation to `.vite/manifest.json`
.arg("--outDir")
.arg(&absolute_output_path)
.current_dir(absolute_root_dir)
.spawn()
.expect("failed to build")
.wait()
.expect("failed to wait for build to complete")
.success()
} else {
std::process::Command::new("npx")
.arg("vite")
.arg("build")
.arg("--manifest") // force manifest generation to `.vite/manifest.json`
.arg("--outDir")
.arg(&absolute_output_path)
.current_dir(absolute_root_dir)
.spawn()
.expect("failed to build")
.wait()
.expect("failed to wait for build to complete")
.success()
};

if !vite_build {
return Err(syn::Error::new(
Expand Down
22 changes: 20 additions & 2 deletions crates/vite-rs/examples/basic_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn main() {

println!("{}", file_content);

#[cfg(all(debug_assertions, not(feature = "debug-prod")))]
#[cfg(all(debug_assertions, not(feature = "debug-prod"), not(windows)))]
assert_eq!(
strip_space(file_content),
strip_space(
Expand All @@ -92,6 +92,24 @@ fn main() {
)
);

#[cfg(all(debug_assertions, not(feature = "debug-prod"), windows))]
assert_eq!(
strip_space(file_content),
strip_space(
r#"
const test = (() => {
console.log("This is a test");
const a = 3;
return a;
})();
const num = test;
console.log("NUM: ", num);

//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInBhY2sxLnRzIl0sInNvdXJjZXNDb250ZW50IjpbImNvbnN0IHRlc3QgPSAoKCkgPT4ge1xyXG4gIGNvbnNvbGUubG9nKCdUaGlzIGlzIGEgdGVzdCcpXHJcblxyXG4gIGNvbnN0IGE6IG51bWJlciA9IDNcclxuXHJcbiAgcmV0dXJuIGFcclxufSkoKVxyXG5cclxuY29uc3QgbnVtID0gdGVzdFxyXG5cclxuY29uc29sZS5sb2coJ05VTTogJywgbnVtKVxyXG4iXSwibWFwcGluZ3MiOiJBQUFBLE1BQU0sUUFBUSxNQUFNO0FBQ2xCLFVBQVEsSUFBSSxnQkFBZ0I7QUFFNUIsUUFBTSxJQUFZO0FBRWxCLFNBQU87QUFDVCxHQUFHO0FBRUgsTUFBTSxNQUFNO0FBRVosUUFBUSxJQUFJLFNBQVMsR0FBRzsiLCJuYW1lcyI6W119
"#
)
);

#[cfg(any(not(debug_assertions), feature = "debug-prod"))]
assert_eq!(
strip_space(file_content),
Expand All @@ -100,5 +118,5 @@ fn main() {
}

fn strip_space(s: &str) -> String {
s.trim().replace(" ", "")
s.trim().replace(" ", "").replace('\r', "")
}
2 changes: 1 addition & 1 deletion crates/vite-rs/examples/custom_ctrl_c_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,5 @@ fn main() {
}

fn strip_space(s: &str) -> String {
s.trim().replace(" ", "")
s.trim().replace(" ", "").replace("\r", "")
}
7 changes: 4 additions & 3 deletions crates/vite-rs/examples/vite-project-folder/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import path from 'path'

export default defineConfig({
plugins: [react()],
build: {
rollupOptions: {
input: ['app/index.html', 'app/pack1.ts'],
input: [path.resolve(__dirname, 'app/index.html'), path.resolve(__dirname, 'app/pack1.ts')],
},
manifest: true, // **IMPORTANT**: this is required.
outDir: './dist', // this is the default value
outDir: path.resolve(__dirname, './dist'),
},
publicDir: './public', // this is the default value
publicDir: path.resolve(__dirname, './public'),
})
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { defineConfig } from "vite";
import path from "path";
import { globSync } from "glob";

export default defineConfig(() => ({
build: {
rollupOptions: {
input: globSync(path.resolve(__dirname, "*.txt")),
input: globSync("*.txt"),
},
manifest: true,
},
Expand Down
69 changes: 51 additions & 18 deletions crates/vite-rs/tests/normal_usage_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,49 @@ fn ensure_html_entrypoint() {

assert_eq!(file.content_type, "text/html");
#[cfg(all(debug_assertions, not(feature = "debug-prod")))]
assert_eq!(file.content_length, 475);
if cfg!(windows) {
assert_eq!(file.content_length, 489);
} else {
assert_eq!(file.content_length, 475);
}
#[cfg(any(not(debug_assertions), feature = "debug-prod"))]
assert_eq!(file.content_length, 470);
if cfg!(windows) {
assert_eq!(file.content_length, 484);
} else {
assert_eq!(file.content_length, 470);
}

let content = std::str::from_utf8(&file.bytes).unwrap();

#[cfg(all(debug_assertions, not(feature = "debug-prod")))]
assert_eq!(
content.replace(" ", ""),
"<!DOCTYPEhtml>\n<htmllang=\"en\">\n<head>\n<scripttype=\"module\"src=\"/@vite/client\"></script>\n\n<metacharset=\"UTF-8\"/>\n<linkrel=\"icon\"type=\"image/svg+xml\"href=\"./vite.svg\"/>\n<linkrel=\"stylsheet\"type=\"text/css\"href=\"./index.css\"/>\n<metaname=\"viewport\"content=\"width=device-width,initial-scale=1.0\"/>\n<title>vite-rs</title>\n</head>\n<body>\n<divid=\"root\"></div>\n<scripttype=\"module\"src=\"./index.ts\"></script>\n</body>\n</html>\n"
.replace(" ", "")
);
if cfg!(windows) {
assert_eq!(
content.replace(" ", ""),
"<!DOCTYPEhtml>\r\n<htmllang=\"en\">\r\n<head>\n<scripttype=\"module\"src=\"/@vite/client\"></script>\n\r\n<metacharset=\"UTF-8\"/>\r\n<linkrel=\"icon\"type=\"image/svg+xml\"href=\"./vite.svg\"/>\r\n<linkrel=\"stylsheet\"type=\"text/css\"href=\"./index.css\"/>\r\n<metaname=\"viewport\"content=\"width=device-width,initial-scale=1.0\"/>\r\n<title>vite-rs</title>\r\n</head>\r\n<body>\r\n<divid=\"root\"></div>\r\n<scripttype=\"module\"src=\"./index.ts\"></script>\r\n</body>\r\n</html>\r\n"
.replace(" ", "")
);
} else {
assert_eq!(
content.replace(" ", ""),
"<!DOCTYPEhtml>\n<htmllang=\"en\">\n<head>\n<scripttype=\"module\"src=\"/@vite/client\"></script>\n\n<metacharset=\"UTF-8\"/>\n<linkrel=\"icon\"type=\"image/svg+xml\"href=\"./vite.svg\"/>\n<linkrel=\"stylsheet\"type=\"text/css\"href=\"./index.css\"/>\n<metaname=\"viewport\"content=\"width=device-width,initial-scale=1.0\"/>\n<title>vite-rs</title>\n</head>\n<body>\n<divid=\"root\"></div>\n<scripttype=\"module\"src=\"./index.ts\"></script>\n</body>\n</html>\n"
.replace(" ", "")
);
}

#[cfg(any(not(debug_assertions), feature = "debug-prod"))]
assert_eq!(
content.replace(" ", ""),
"<!DOCTYPEhtml>\n<htmllang=\"en\">\n<head>\n<metacharset=\"UTF-8\"/>\n<linkrel=\"icon\"type=\"image/svg+xml\"href=\"/assets/vite-DcBtz0py.svg\"/>\n<metaname=\"viewport\"content=\"width=device-width,initial-scale=1.0\"/>\n<title>vite-rs</title>\n<scripttype=\"module\"crossoriginsrc=\"/assets/index-BZiJcslM.js\"></script>\n<linkrel=\"stylesheet\"crossoriginhref=\"/assets/index-BPvgi06w.css\">\n</head>\n<body>\n<divid=\"root\"></div>\n</body>\n</html>\n"
.replace(" ", "")
);
if cfg!(windows) {
assert_eq!(
content.replace(" ", ""),
"<!DOCTYPEhtml>\r\n<htmllang=\"en\">\r\n<head>\r\n<metacharset=\"UTF-8\"/>\r\n<linkrel=\"icon\"type=\"image/svg+xml\"href=\"/assets/vite-DcBtz0py.svg\"/>\r\r\n<metaname=\"viewport\"content=\"width=device-width,initial-scale=1.0\"/>\r\n<title>vite-rs</title>\r\n<scripttype=\"module\"crossoriginsrc=\"/assets/index-BZiJcslM.js\"></script>\n<linkrel=\"stylesheet\"crossoriginhref=\"/assets/index-BPvgi06w.css\">\n</head>\r\n<body>\r\n<divid=\"root\"></div>\r\r\n</body>\r\n</html>\r\n"
.replace(" ", "")
);
} else {
assert_eq!(
content.replace(" ", ""),
"<!DOCTYPEhtml>\n<htmllang=\"en\">\n<head>\n<metacharset=\"UTF-8\"/>\n<linkrel=\"icon\"type=\"image/svg+xml\"href=\"/assets/vite-DcBtz0py.svg\"/>\n<metaname=\"viewport\"content=\"width=device-width,initial-scale=1.0\"/>\n<title>vite-rs</title>\n<scripttype=\"module\"crossoriginsrc=\"/assets/index-BZiJcslM.js\"></script>\n<linkrel=\"stylesheet\"crossoriginhref=\"/assets/index-BPvgi06w.css\">\n</head>\n<body>\n<divid=\"root\"></div>\n</body>\n</html>\n"
.replace(" ", "")
);
}
}

fn ensure_ts_entrypoint() {
Expand All @@ -105,12 +129,21 @@ fn ensure_ts_entrypoint() {

#[cfg(all(debug_assertions, not(feature = "debug-prod")))]
{
assert_eq!(file.content_type, "text/javascript");
assert_eq!(file.content_length, 656);
assert_eq!(
content.replace(" ", ""),
"consttest=(()=>{\nconsole.log(\"Thisisatest\");\nconsta=3;\nreturna;\n})();\nconstnum=test;\nconsole.log(\"NUM:\",num);\n\n//#sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInBhY2sxLnRzIl0sInNvdXJjZXNDb250ZW50IjpbImNvbnN0IHRlc3QgPSAoKCkgPT4ge1xuICBjb25zb2xlLmxvZygnVGhpcyBpcyBhIHRlc3QnKVxuXG4gIGNvbnN0IGE6IG51bWJlciA9IDNcblxuICByZXR1cm4gYVxufSkoKVxuXG5jb25zdCBudW0gPSB0ZXN0XG5cbmNvbnNvbGUubG9nKCdOVU06ICcsIG51bSlcbiJdLCJtYXBwaW5ncyI6IkFBQUEsTUFBTSxRQUFRLE1BQU07QUFDbEIsVUFBUSxJQUFJLGdCQUFnQjtBQUU1QixRQUFNLElBQVk7QUFFbEIsU0FBTztBQUNULEdBQUc7QUFFSCxNQUFNLE1BQU07QUFFWixRQUFRLElBQUksU0FBUyxHQUFHOyIsIm5hbWVzIjpbXX0="
);
if cfg!(windows) {
assert_eq!(file.content_type, "text/javascript");
assert_eq!(file.content_length, 684);
assert_eq!(
content.replace(" ", ""),
"consttest=(()=>{\nconsole.log(\"Thisisatest\");\nconsta=3;\nreturna;\n})();\nconstnum=test;\nconsole.log(\"NUM:\",num);\n\n//#sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInBhY2sxLnRzIl0sInNvdXJjZXNDb250ZW50IjpbImNvbnN0IHRlc3QgPSAoKCkgPT4ge1xyXG4gIGNvbnNvbGUubG9nKCdUaGlzIGlzIGEgdGVzdCcpXHJcblxyXG4gIGNvbnN0IGE6IG51bWJlciA9IDNcclxuXHJcbiAgcmV0dXJuIGFcclxufSkoKVxyXG5cclxuY29uc3QgbnVtID0gdGVzdFxyXG5cclxuY29uc29sZS5sb2coJ05VTTogJywgbnVtKVxyXG4iXSwibWFwcGluZ3MiOiJBQUFBLE1BQU0sUUFBUSxNQUFNO0FBQ2xCLFVBQVEsSUFBSSxnQkFBZ0I7QUFFNUIsUUFBTSxJQUFZO0FBRWxCLFNBQU87QUFDVCxHQUFHO0FBRUgsTUFBTSxNQUFNO0FBRVosUUFBUSxJQUFJLFNBQVMsR0FBRzsiLCJuYW1lcyI6W119"
);
} else {
assert_eq!(file.content_type, "text/javascript");
assert_eq!(file.content_length, 656);
assert_eq!(
content.replace(" ", ""),
"consttest=(()=>{\nconsole.log(\"Thisisatest\");\nconsta=3;\nreturna;\n})();\nconstnum=test;\nconsole.log(\"NUM:\",num);\n\n//#sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInBhY2sxLnRzIl0sInNvdXJjZXNDb250ZW50IjpbImNvbnN0IHRlc3QgPSAoKCkgPT4ge1xuICBjb25zb2xlLmxvZygnVGhpcyBpcyBhIHRlc3QnKVxuXG4gIGNvbnN0IGE6IG51bWJlciA9IDNcblxuICByZXR1cm4gYVxufSkoKVxuXG5jb25zdCBudW0gPSB0ZXN0XG5cbmNvbnNvbGUubG9nKCdOVU06ICcsIG51bSlcbiJdLCJtYXBwaW5ncyI6IkFBQUEsTUFBTSxRQUFRLE1BQU07QUFDbEIsVUFBUSxJQUFJLGdCQUFnQjtBQUU1QixRQUFNLElBQVk7QUFFbEIsU0FBTztBQUNULEdBQUc7QUFFSCxNQUFNLE1BQU07QUFFWixRQUFRLElBQUksU0FBUyxHQUFHOyIsIm5hbWVzIjpbXX0="
);
}
}

#[cfg(any(not(debug_assertions), feature = "debug-prod"))]
Expand Down
Loading