Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Unify the static server in bundler-mako and devServer #1468

Merged
merged 8 commits into from
Aug 29, 2024
24 changes: 16 additions & 8 deletions crates/mako/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ impl DevServer {
staticfile: hyper_staticfile::Static,
txws: broadcast::Sender<WsMessage>,
) -> Result<hyper::Response<Body>> {
debug!("> {} {}", req.method().to_string(), req.uri().path());

let mut path = req.uri().path().to_string();
let public_path = &context.config.public_path;
if !public_path.is_empty() && public_path.starts_with('/') && public_path != "/" {
Expand Down Expand Up @@ -171,14 +173,16 @@ impl DevServer {
// staticfile has 302 problems when modify tooooo fast in 1 second
// it will response 302 and we will get the old file
// TODO: fix the 302 problem?
if let Some(res) = context.get_static_content(path_without_slash_start) {
debug!("serve with context.get_static_content: {}", path);
if !context.config.write_to_disk {
if let Some(res) = context.get_static_content(path_without_slash_start) {
debug!("serve with context.get_static_content: {}", path);

return Ok(hyper::Response::builder()
.status(hyper::StatusCode::OK)
.header(CONTENT_TYPE, content_type)
.body(hyper::Body::from(res))
.unwrap());
return Ok(hyper::Response::builder()
.status(hyper::StatusCode::OK)
.header(CONTENT_TYPE, content_type)
.body(hyper::Body::from(res))
.unwrap());
}
}
// for cached dep
let abs_path = context
Expand All @@ -196,7 +200,11 @@ impl DevServer {
}

// for hmr files
debug!("serve with staticfile server: {}", path);
debug!("< static file serve: {}", path);
let req = hyper::Request::builder()
.uri(path)
.body(hyper::Body::empty())
.unwrap();
let res = staticfile.serve(req).await;
res.map_err(anyhow::Error::from)
}
Expand Down
26 changes: 8 additions & 18 deletions packages/bundler-mako/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,27 +111,17 @@ exports.dev = async function (opts) {
logLevel: 'silent',
});
app.use('/__/hmr-ws', wsProxy);

const outputPath = path.resolve(opts.cwd, opts.config.outputPath || 'dist');

function processReqURL(publicPath, reqURL) {
if (!publicPath.startsWith('/')) {
publicPath = '/' + publicPath;
app.use((req, res, next) => {
if (req.method !== 'GET' && req.method !== 'HEAD') {
return next();
}
return reqURL.startsWith(publicPath)
? reqURL.slice(publicPath.length - 1)
: reqURL;
}

if (opts.config.publicPath) {
app.use((req, _res, next) => {
req.url = processReqURL(opts.config.publicPath, req.url);
if (req.accepts().join('').includes('html')) {
return next();
}
wsProxy(req, res, () => {
next();
});
}

// serve dist files
app.use(express.static(outputPath));
});

if (process.env.SSU === 'true') {
// for ssu cache chunks
Expand Down
48 changes: 48 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading