Skip to content

Commit

Permalink
chore: merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Nov 12, 2022
2 parents 301ff0b + ce24c7c commit fa03d8c
Show file tree
Hide file tree
Showing 24 changed files with 255 additions and 166 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ jobs:
run: pnpm run test-docs

lint:
if: github.repository == 'vitejs/vite'
timeout-minutes: 10
runs-on: ubuntu-latest
name: "Lint: node-16, ubuntu-latest"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ecosystem-ci-trigger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
jobs:
trigger:
runs-on: ubuntu-latest
if: github.event.issue.pull_request && startsWith(github.event.comment.body, '/ecosystem-ci run')
if: github.repository == 'vitejs/vite' && github.event.issue.pull_request && startsWith(github.event.comment.body, '/ecosystem-ci run')
steps:
- uses: actions/github-script@v6
with:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/issue-close-require.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:

jobs:
close-issues:
if: github.repository == 'vitejs/vite'
runs-on: ubuntu-latest
steps:
- name: need reproduction
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/issue-labeled.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:

jobs:
reply-labeled:
if: github.repository == 'vitejs/vite'
runs-on: ubuntu-latest
steps:
- name: contribution welcome
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/lock-closed-issues.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ permissions:

jobs:
action:
if: github.repository == 'vitejs/vite'
runs-on: ubuntu-latest
steps:
- uses: dessant/lock-threads@v3
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/release-tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ on:

jobs:
release:
if: github.repository == 'vitejs/vite'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/semantic-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:

jobs:
main:
if: github.repository == 'vitejs/vite'
runs-on: ubuntu-latest
name: Semantic Pull Request
steps:
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ To use breakpoints and explore code execution, you can use the ["Run and Debug"]

2. Click the "Run and Debug" icon in the activity bar of the editor, which opens the [_Run and Debug view_](https://code.visualstudio.com/docs/editor/debugging#_run-and-debug-view).

3. Click the "JavaScript Debug Termimal" button in the _Run and Debug view_, which opens a terminal in VS Code.
3. Click the "JavaScript Debug Terminal" button in the _Run and Debug view_, which opens a terminal in VS Code.

4. From that terminal, go to `playground/xxx`, and run `pnpm run dev`.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"prompts": "^2.4.2",
"resolve": "^1.22.1",
"rimraf": "^3.0.2",
"rollup": "~3.2.3",
"rollup": "~3.3.0",
"rollup-plugin-license": "^2.9.1",
"semver": "^7.3.8",
"simple-git-hooks": "^2.8.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-legacy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ function polyfillsPlugin(
load(id) {
if (id === polyfillId) {
return (
[...imports].map((i) => `import "${i}";`).join('') +
[...imports].map((i) => `import ${JSON.stringify(i)};`).join('') +
(excludeSystemJS ? '' : `import "systemjs/dist/s.min.js";`)
)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"@jridgewell/gen-mapping": "^0.3.2",
"@jridgewell/trace-mapping": "^0.3.17",
"debug": "^4.3.4",
"rollup": "~3.2.3",
"rollup": "~3.3.0",
"slash": "^5.0.0",
"source-map": "^0.6.1",
"vite": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"esbuild": "^0.15.9",
"postcss": "^8.4.18",
"resolve": "^1.22.1",
"rollup": "~3.2.3"
"rollup": "~3.3.0"
},
"optionalDependencies": {
"fsevents": "~2.3.2"
Expand Down
2 changes: 0 additions & 2 deletions packages/vite/src/node/optimizer/esbuildDepPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
normalizePath
} from '../utils'
import { browserExternalId, optionalPeerDepId } from '../plugins/resolve'
import type { ExportsData } from '.'

const externalWithConversionNamespace =
'vite:dep-pre-bundle:external-conversion'
Expand Down Expand Up @@ -44,7 +43,6 @@ const externalTypes = [

export function esbuildDepPlugin(
qualified: Record<string, string>,
exportsData: Record<string, ExportsData>,
external: string[],
config: ResolvedConfig,
ssr: boolean
Expand Down
4 changes: 1 addition & 3 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,9 +580,7 @@ export async function runOptimizeDeps(
if (external.length) {
plugins.push(esbuildCjsExternalPlugin(external))
}
plugins.push(
esbuildDepPlugin(flatIdDeps, flatIdToExports, external, config, ssr)
)
plugins.push(esbuildDepPlugin(flatIdDeps, external, config, ssr))

const start = performance.now()

Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { parse as parseJS } from 'acorn'
import type { Node } from 'estree'
import { findStaticImports, parseStaticImport } from 'mlly'
import { makeLegalIdentifier } from '@rollup/pluginutils'
import { getDepOptimizationConfig } from '..'
import type { ViteDevServer } from '..'
import {
CLIENT_DIR,
Expand Down Expand Up @@ -46,6 +45,7 @@ import {
unwrapId,
wrapId
} from '../utils'
import { getDepOptimizationConfig } from '../config'
import type { ResolvedConfig } from '../config'
import type { Plugin } from '../plugin'
import {
Expand Down Expand Up @@ -712,7 +712,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
// These requests will also be registered in transformRequest to be awaited
// by the deps optimizer
if (config.server.preTransformRequests && staticImportedUrls.size) {
staticImportedUrls.forEach(({ url, id }) => {
staticImportedUrls.forEach(({ url }) => {
url = removeImportQuery(url)
transformRequest(url, server, { ssr }).catch((e) => {
if (e?.code === ERR_OUTDATED_OPTIMIZED_DEP) {
Expand Down
31 changes: 17 additions & 14 deletions packages/vite/src/node/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import { openBrowser } from './server/openBrowser'
import compression from './server/middlewares/compression'
import { proxyMiddleware } from './server/middlewares/proxy'
import { resolveHostname, resolveServerUrls } from './utils'
import { resolveHostname, resolveServerUrls, shouldServe } from './utils'
import { printServerUrls } from './logger'
import { resolveConfig } from '.'
import type { InlineConfig, ResolvedConfig } from '.'
Expand Down Expand Up @@ -112,21 +112,24 @@ export async function preview(
// static assets
const distDir = path.resolve(config.root, config.build.outDir)
const headers = config.preview.headers
app.use(
previewBase,
sirv(distDir, {
etag: true,
dev: true,
single: config.appType === 'spa',
setHeaders(res) {
if (headers) {
for (const name in headers) {
res.setHeader(name, headers[name]!)
}
const assetServer = sirv(distDir, {
etag: true,
dev: true,
single: config.appType === 'spa',
setHeaders(res) {
if (headers) {
for (const name in headers) {
res.setHeader(name, headers[name]!)
}
}
})
)
}
})
app.use(previewBase, async (req, res, next) => {
if (shouldServe(req.url!, distDir)) {
return assetServer(req, res, next)
}
next()
})

// apply post server hooks from plugins
postHooks.forEach((fn) => fn && fn())
Expand Down
6 changes: 5 additions & 1 deletion packages/vite/src/node/server/middlewares/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
isInternalRequest,
isParentDirectory,
isWindows,
shouldServe,
slash
} from '../../utils'

Expand Down Expand Up @@ -52,7 +53,10 @@ export function servePublicMiddleware(
if (isImportRequest(req.url!) || isInternalRequest(req.url!)) {
return next()
}
serve(req, res, next)
if (shouldServe(req.url!, dir)) {
return serve(req, res, next)
}
next()
}
}

Expand Down
6 changes: 5 additions & 1 deletion packages/vite/src/node/server/middlewares/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
unwrapId
} from '../../utils'
import { send } from '../send'
import { transformRequest } from '../transformRequest'
import { ERR_LOAD_URL, transformRequest } from '../transformRequest'
import { isHTMLProxy } from '../../plugins/html'
import {
DEP_VERSION_RE,
Expand Down Expand Up @@ -224,6 +224,10 @@ export function transformMiddleware(
// error but a normal part of the missing deps discovery flow
return
}
if (e?.code === ERR_LOAD_URL) {
// Let other middleware handle if we can't load the url via transformRequest
return next()
}
return next(e)
}

Expand Down
25 changes: 14 additions & 11 deletions packages/vite/src/node/server/transformRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import { getDepsOptimizer } from '../optimizer'
import { injectSourcesContent } from './sourcemap'
import { isFileServingAllowed } from './middlewares/static'

export const ERR_LOAD_URL = 'ERR_LOAD_URL'
export const ERR_LOAD_PUBLIC_URL = 'ERR_LOAD_PUBLIC_URL'

const debugLoad = createDebugger('vite:load')
const debugTransform = createDebugger('vite:transform')
const debugCache = createDebugger('vite:cache')
Expand Down Expand Up @@ -215,18 +218,18 @@ async function loadAndTransform(
}
}
if (code == null) {
if (checkPublicFile(url, config)) {
throw new Error(
`Failed to load url ${url} (resolved id: ${id}). ` +
`This file is in /public and will be copied as-is during build without ` +
`going through the plugin transforms, and therefore should not be ` +
`imported from source code. It can only be referenced via HTML tags.`
)
} else {
return null
}
const isPublicFile = checkPublicFile(url, config)
const msg = isPublicFile
? `This file is in /public and will be copied as-is during build without ` +
`going through the plugin transforms, and therefore should not be ` +
`imported from source code. It can only be referenced via HTML tags.`
: `Does the file exist?`
const err: any = new Error(
`Failed to load url ${url} (resolved id: ${id}). ${msg}`
)
err.code = isPublicFile ? ERR_LOAD_PUBLIC_URL : ERR_LOAD_URL
throw err
}

// ensure module in graph after successful load
const mod = await moduleGraph.ensureEntryFromUrl(url, ssr)
ensureWatchedFile(watcher, mod.file, root)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// eslint-disable-next-line node/no-missing-import
import { foo } from './non-existent.js'

export const hello = 'world'
23 changes: 23 additions & 0 deletions packages/vite/src/node/ssr/__tests__/ssrLoadModule.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { fileURLToPath } from 'node:url'
import { expect, test } from 'vitest'
import { createServer } from '../../server'

const root = fileURLToPath(new URL('./', import.meta.url))

async function createDevServer() {
const server = await createServer({ configFile: false, root })
server.pluginContainer.buildStart({})
return server
}

test('ssrLoad', async () => {
expect.assertions(1)
const server = await createDevServer()
try {
await server.ssrLoadModule('/fixtures/modules/has-invalid-import.js')
} catch (e) {
expect(e.message).toBe(
'Failed to load url ./non-existent.js (resolved id: ./non-existent.js). Does the file exist?'
)
}
})
39 changes: 39 additions & 0 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,45 @@ export const isNonDriveRelativeAbsolutePath = (p: string): boolean => {
return windowsDrivePathPrefixRE.test(p)
}

/**
* Determine if a file is being requested with the correct case, to ensure
* consistent behaviour between dev and prod and across operating systems.
*/
export function shouldServe(url: string, assetsDir: string): boolean {
// viteTestUrl is set to something like http://localhost:4173/ and then many tests make calls
// like `await page.goto(viteTestUrl + '/example')` giving us URLs beginning with a double slash
const pathname = decodeURI(
new URL(url.startsWith('//') ? url.substring(1) : url, 'http://example.com')
.pathname
)
const file = path.join(assetsDir, pathname)
if (
!fs.existsSync(file) ||
(isCaseInsensitiveFS && // can skip case check on Linux
!fs.statSync(file).isDirectory() &&
!hasCorrectCase(file, assetsDir))
) {
return false
}
return true
}

/**
* Note that we can't use realpath here, because we don't want to follow
* symlinks.
*/
function hasCorrectCase(file: string, assets: string): boolean {
if (file === assets) return true

const parent = path.dirname(file)

if (fs.readdirSync(parent).includes(path.basename(file))) {
return hasCorrectCase(parent, assets)
}

return false
}

export function joinUrlSegments(a: string, b: string): string {
if (!a || !b) {
return a || b || ''
Expand Down
7 changes: 7 additions & 0 deletions playground/assets/__tests__/assets.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fetch from 'node-fetch'
import { describe, expect, test } from 'vitest'
import {
browserLogs,
Expand All @@ -12,6 +13,7 @@ import {
readFile,
readManifest,
untilUpdated,
viteTestUrl,
watcher
} from '~utils'

Expand All @@ -27,6 +29,11 @@ test('should have no 404s', () => {
})
})

test('should get a 404 when using incorrect case', async () => {
expect((await fetch(viteTestUrl + 'icon.png')).status).toBe(200)
expect((await fetch(viteTestUrl + 'ICON.png')).status).toBe(404)
})

describe('injected scripts', () => {
test('@vite/client', async () => {
const hasClient = await page.$(
Expand Down

0 comments on commit fa03d8c

Please sign in to comment.