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

feat: base without trailing slash #10723

Merged
merged 22 commits into from Nov 12, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
0cbe2fb
chore: added tests for base without trailing slash (e.g., "/foo")
BenediktAllendorf Oct 30, 2022
62c588a
chore: remove enforcing trailing slash for base
BenediktAllendorf Oct 30, 2022
cbe04d5
chore: use joinUrlSegments() where necessary
BenediktAllendorf Oct 30, 2022
3cfa091
chore: add stripBase() for handling bases with and without trailing s…
BenediktAllendorf Oct 30, 2022
02b9ba8
Update packages/vite/src/node/plugins/importAnalysis.ts
BenediktAllendorf Nov 3, 2022
6613ab1
Update packages/vite/src/node/plugins/importAnalysis.ts
BenediktAllendorf Nov 3, 2022
3ea8891
chore: use stripBase more and fix bad calls to path.posix.join
benmccann Nov 11, 2022
0a8ea2a
chore: don't duplicate test file
benmccann Nov 12, 2022
99e0e9d
chore: merge main
benmccann Nov 12, 2022
d93cb6b
chore: leave trailing slash in resolved config per bluwy's comment
benmccann Nov 12, 2022
4d78d6a
fix: get a couple tests passing
benmccann Nov 12, 2022
3ce3ac3
fix: update package.json scripts
benmccann Nov 12, 2022
d1e9fea
fix: remove newly added broken tests
benmccann Nov 12, 2022
a4237c4
chore: cleanup package.json
benmccann Nov 12, 2022
a5efea6
feat: add rawBase field
benmccann Nov 12, 2022
3d95beb
feat: use rawBase in base middleware
benmccann Nov 12, 2022
777c81e
fix: use rawBase for tests and CLI output
benmccann Nov 12, 2022
64edcfd
fix: make test more robust
benmccann Nov 12, 2022
91f7023
chore: mark rawBase as internal
benmccann Nov 12, 2022
301ff0b
docs: update docs
benmccann Nov 12, 2022
fa03d8c
chore: merge main
benmccann Nov 12, 2022
f47f642
fix: update recently added test
benmccann Nov 12, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/vite/src/node/build.ts
Expand Up @@ -1124,7 +1124,7 @@ export function toOutputFilePathWithoutRuntime(
if (relative && !config.build.ssr) {
return toRelative(filename, hostId)
} else {
return config.base + filename
return joinUrlSegments(config.base, filename)
}
}

Expand Down
8 changes: 0 additions & 8 deletions packages/vite/src/node/config.ts
Expand Up @@ -827,14 +827,6 @@ export function resolveBaseUrl(
}
}

// ensure ending slash
if (!base.endsWith('/')) {
logger.warn(
colors.yellow(colors.bold(`(!) "base" option should end with a slash.`))
)
base += '/'
}

return base
}

Expand Down
6 changes: 4 additions & 2 deletions packages/vite/src/node/plugins/importAnalysis.ts
Expand Up @@ -34,10 +34,12 @@ import {
isDataUrl,
isExternalUrl,
isJSRequest,
joinUrlSegments,
moduleListContains,
normalizePath,
prettifyUrl,
removeImportQuery,
stripBase,
stripBomTag,
timeFrom,
transformStableResult,
Expand Down Expand Up @@ -377,7 +379,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
}

// prepend base (dev base is guaranteed to have ending slash)
BenediktAllendorf marked this conversation as resolved.
Show resolved Hide resolved
url = base + url.replace(/^\//, '')
url = joinUrlSegments(base, url.replace(/^\//, ''))
BenediktAllendorf marked this conversation as resolved.
Show resolved Hide resolved
}

return [url, resolved.id]
Expand Down Expand Up @@ -538,7 +540,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {

// record for HMR import chain analysis
// make sure to unwrap and normalize away base
const hmrUrl = unwrapId(url.replace(base, '/'))
const hmrUrl = unwrapId(stripBase(url, base))
benmccann marked this conversation as resolved.
Show resolved Hide resolved
importedUrls.add(hmrUrl)

if (enablePartialAccept && importedBindings) {
Expand Down
13 changes: 7 additions & 6 deletions packages/vite/src/node/server/middlewares/base.ts
@@ -1,24 +1,22 @@
import type { Connect } from 'dep-types/connect'
import type { ViteDevServer } from '..'
import { joinUrlSegments } from '../../utils'
import { joinUrlSegments, stripBase } from '../../utils'

// this middleware is only active when (config.base !== '/')

export function baseMiddleware({
config
}: ViteDevServer): Connect.NextHandleFunction {
const devBase = config.base.endsWith('/') ? config.base : config.base + '/'

// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
return function viteBaseMiddleware(req, res, next) {
const url = req.url!
const parsed = new URL(url, 'http://vitejs.dev')
const path = parsed.pathname || '/'

if (path.startsWith(devBase)) {
if (path.startsWith(config.base)) {
// rewrite url to remove base. this ensures that other middleware does
// not need to consider base being prepended or not
req.url = url.replace(devBase, '/')
req.url = stripBase(url, config.base)
return next()
}

Expand All @@ -36,7 +34,10 @@ export function baseMiddleware({
return
} else if (req.headers.accept?.includes('text/html')) {
// non-based page visit
const redirectPath = joinUrlSegments(config.base, url)
const redirectPath =
url + '/' !== config.base
? joinUrlSegments(config.base, url)
: config.base
res.writeHead(404, {
'Content-Type': 'text/html'
})
Expand Down
6 changes: 6 additions & 0 deletions packages/vite/src/node/utils.ts
Expand Up @@ -1204,3 +1204,9 @@ export function joinUrlSegments(a: string, b: string): string {
}
return a + b
}

export function stripBase(path: string, base: string): string {
const devBase = base.endsWith('/') ? base.slice(0, -1) : base

return path.replace(RegExp(devBase + '/+'), '/')
benmccann marked this conversation as resolved.
Show resolved Hide resolved
}
@@ -0,0 +1 @@
module.exports = require('../../vite.config-without-trailing-slash')

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion playground/assets/package.json
Expand Up @@ -12,6 +12,9 @@
"preview:relative-base": "vite --config ./vite.config-relative-base.js preview",
"dev:runtime-base": "vite --config ./vite.config-runtime-base.js dev",
"build:runtime-base": "vite --config ./vite.config-runtime-base.js build",
"preview:runtime-base": "vite --config ./vite.config-runtime-base.js preview"
"preview:runtime-base": "vite --config ./vite.config-runtime-base.js preview",
"dev:without-trailing-slash": "vite --config ./vite.config-without-trailing-slash.js dev",
"build:without-trailing-slash": "vite --config ./vite.config-without-trailing-slash.js build",
"preview:without-trailing-slash": "vite --config ./vite.config-without-trailing-slash.js preview"
}
}
13 changes: 13 additions & 0 deletions playground/assets/vite.config-without-trailing-slash.js
@@ -0,0 +1,13 @@
const baseConfig = require('./vite.config.js')

/**
* @type {import('vite').UserConfig}
*/
module.exports = {
...baseConfig,
base: '/foo', // overwrite the original base: '/foo/'
build: {
...baseConfig.build,
outDir: 'dist/without-trailing-slash'
}
}