-
Notifications
You must be signed in to change notification settings - Fork 85
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
fix: dev server support "publicPath" #1398
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -111,6 +111,22 @@ exports.dev = async function (opts) { | |
|
||
const outputPath = path.resolve(opts.cwd, opts.config.outputPath || 'dist'); | ||
|
||
function processReqURL(publicPath, reqURL) { | ||
whyer11 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!publicPath.startsWith('/')) { | ||
publicPath = '/' + publicPath; | ||
} | ||
if (reqURL.startsWith(publicPath)) { | ||
return reqURL.slice(publicPath.length - 1); | ||
} else { | ||
return reqURL; | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 建议改进: 优化 目前的逻辑可以进一步简化和优化,以提高代码的可读性和处理边界情况的能力。 function processReqURL(publicPath, reqURL) {
if (!publicPath.startsWith('/')) {
publicPath = '/' + publicPath;
}
publicPath = publicPath.replace(/\/+$/, '') + '/';
if (reqURL.startsWith(publicPath)) {
return reqURL.slice(publicPath.length - 1);
}
return reqURL;
} 这样可以确保 |
||
if (opts.config.publicPath) { | ||
app.use((req, res, next) => { | ||
req.url = processReqURL(opts.config.publicPath, req.url); | ||
next(); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 建议改进: 提高中间件的可读性 目前的中间件逻辑可以通过解构 const { publicPath } = opts.config;
if (publicPath) {
app.use((req, res, next) => {
req.url = processReqURL(publicPath, req.url);
next();
});
} 这样可以使代码更加简洁和易读。 |
||
} | ||
// serve dist files | ||
app.use(express.static(outputPath)); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
建议改进: 优化字符串处理逻辑
目前的逻辑虽然正确,但可以使用标准库提供的方法来简化字符串的处理。例如,可以使用
strip_prefix
和strip_suffix
方法来处理路径。