Skip to content

Commit

Permalink
Merge branch 'main' into feat/vite-fs-disallow
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Oct 27, 2021
2 parents 946cfa2 + ebd4027 commit 2280d24
Show file tree
Hide file tree
Showing 34 changed files with 326 additions and 109 deletions.
14 changes: 14 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ To development and test the core `vite` package:
- Run `pnpm link vite` in another Vite project to use the locally built Vite;
- Use the `vite` binary anywhere.

If your project has `vite` as a nested dependency, you can customize the dependency resolution instead depending on the package manager used. For pnpm, add this in your project's root `package.json`:

```json
{
"pnpm": {
"overrides": {
"vite": "link:../path/to/vite/packages/vite"
}
}
}
```

And re-run `pnpm install` to link the package.

## Running Tests

Each package under `packages/playground/` contains a `__tests__` directory. The tests are run using [Jest](https://jestjs.io/) + [Playwright](https://playwright.dev/) with custom integrations to make writing tests simple. The detailed setup is inside `jest.config.js` and `scripts/jest*` files.
Expand Down
1 change: 1 addition & 0 deletions docs/guide/api-hmr.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface ImportMeta {
accept(dep: string, cb: (mod: any) => void): void
accept(deps: string[], cb: (mods: any[]) => void): void

prune(cb: () => void): void
dispose(cb: (data: any) => void): void
decline(): void
invalidate(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ test('handle nested package', async () => {
expect(await page.textContent('.side-c')).toBe(c)
expect(await page.textContent('.d')).toBe('D@1.0.0')
expect(await page.textContent('.nested-d')).toBe('D-nested@1.0.0')
expect(await page.textContent('.nested-e')).toBe('1')
})
6 changes: 6 additions & 0 deletions packages/playground/nested-deps/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ <h2>direct dependency D</h2>
<h2>nested dependency nested-D (dep of D)</h2>
<pre class="nested-d"></pre>

<h2>exclude dependency of pre-bundled dependency</h2>
<div>nested module instance count: <span class="nested-e"></span></div>

<script type="module">
import A from 'test-package-a'
import B, { A as nestedA } from 'test-package-b'
import C from 'test-package-c'
import { C as sideC } from 'test-package-c/side'
import D, { nestedD } from 'test-package-d'
import { testExcluded } from 'test-package-e'

text('.a', A)
text('.b', B)
Expand All @@ -36,6 +40,8 @@ <h2>nested dependency nested-D (dep of D)</h2>
text('.d', D)
text('.nested-d', nestedD)

text('.nested-e', testExcluded())

function text(sel, text) {
document.querySelector(sel).textContent = text
}
Expand Down
3 changes: 2 additions & 1 deletion packages/playground/nested-deps/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"test-package-a": "link:./test-package-a",
"test-package-b": "link:./test-package-b",
"test-package-c": "link:./test-package-c",
"test-package-d": "link:./test-package-d"
"test-package-d": "link:./test-package-d",
"test-package-e": "link:./test-package-e"
}
}
2 changes: 2 additions & 0 deletions packages/playground/nested-deps/test-package-e/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { testIncluded } from 'test-package-e-included'
export { testExcluded } from 'test-package-e-excluded'
9 changes: 9 additions & 0 deletions packages/playground/nested-deps/test-package-e/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "test-package-e",
"version": "0.1.0",
"main": "index.js",
"dependencies": {
"test-package-e-excluded": "link:./test-package-e-excluded",
"test-package-e-included": "link:./test-package-e-included"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const key = '$$excludedDependencyInstanceCount'

if (!(key in window)) {
window[key] = 0
}

++window[key]

export function testExcluded() {
return window[key]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "test-package-e-excluded",
"version": "0.1.0",
"main": "index.js"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { testExcluded } from 'test-package-e-excluded'

export function testIncluded() {
return testExcluded()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "test-package-e-included",
"version": "0.1.0",
"main": "index.js",
"dependencies": {
"test-package-e-excluded": "link:../test-package-e-excluded"
}
}
5 changes: 3 additions & 2 deletions packages/playground/nested-deps/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ module.exports = {
'test-package-b',
'test-package-c',
'test-package-c/side',
'test-package-d > test-package-d-nested'
'test-package-d > test-package-d-nested',
'test-package-e-included'
],
exclude: ['test-package-d']
exclude: ['test-package-d', 'test-package-e-excluded']
}
}
9 changes: 9 additions & 0 deletions packages/plugin-react/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## [1.0.6](https://github.com/vitejs/vite/compare/plugin-react@1.0.5...plugin-react@1.0.6) (2021-10-25)


### Bug Fixes

* **plugin-react:** account for querystring in transform hook ([#5333](https://github.com/vitejs/vite/issues/5333)) ([13c3813](https://github.com/vitejs/vite/commit/13c381368caf8302a0c5b7cec07dfc0eb344bede))



## [1.0.5](https://github.com/vitejs/vite/compare/plugin-react@1.0.4...plugin-react@1.0.5) (2021-10-18)


Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vitejs/plugin-react",
"version": "1.0.5",
"version": "1.0.6",
"license": "MIT",
"author": "Evan You",
"contributors": [
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
ast: !isReasonReact,
root: projectRoot,
filename: id,
sourceFileName: id,
parserOpts: {
...opts.babel?.parserOpts,
sourceType: 'module',
Expand Down
9 changes: 9 additions & 0 deletions packages/plugin-vue/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## [1.9.4](https://github.com/vitejs/vite/compare/plugin-vue@1.9.3...plugin-vue@1.9.4) (2021-10-27)


### Bug Fixes

* **plugin-vue:** exclude direct css request from hmr target ([#5422](https://github.com/vitejs/vite/issues/5422)) ([4331c26](https://github.com/vitejs/vite/commit/4331c26a5e5d7a9efc08a8b7bf7056785a1bcd94))



## [1.9.3](https://github.com/vitejs/vite/compare/plugin-vue@1.9.2...plugin-vue@1.9.3) (2021-10-05)


Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-vue/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vitejs/plugin-vue",
"version": "1.9.3",
"version": "1.9.4",
"license": "MIT",
"author": "Evan You",
"files": [
Expand Down
5 changes: 4 additions & 1 deletion packages/plugin-vue/src/handleHotUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { ResolvedOptions } from '.'

const debug = _debug('vite:hmr')

const directRequestRE = /(\?|&)direct\b/

/**
* Vite-specific HMR handling
*/
Expand Down Expand Up @@ -92,7 +94,8 @@ export async function handleHotUpdate(
const mod = modules.find(
(m) =>
m.url.includes(`type=style&index=${i}`) &&
m.url.endsWith(`.${next.lang || 'css'}`)
m.url.endsWith(`.${next.lang || 'css'}`) &&
!directRequestRE.test(m.url)
)
if (mod) {
affectedModules.add(mod)
Expand Down
39 changes: 39 additions & 0 deletions packages/vite/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,42 @@
## [2.6.13](https://github.com/vitejs/vite/compare/v2.6.12...v2.6.13) (2021-10-27)


### Bug Fixes

* **css:** ?inline cannot self-accept ([#5433](https://github.com/vitejs/vite/issues/5433)) ([d283d9b](https://github.com/vitejs/vite/commit/d283d9b7d6231d296cad36ebb0bcce338769c975))



## [2.6.12](https://github.com/vitejs/vite/compare/v2.6.11...v2.6.12) (2021-10-26)


### Bug Fixes

* allowed files logic (fix [#5416](https://github.com/vitejs/vite/issues/5416)) ([#5420](https://github.com/vitejs/vite/issues/5420)) ([414bc45](https://github.com/vitejs/vite/commit/414bc45693762c330efbe1f3c8c97829cc05695a))



## [2.6.11](https://github.com/vitejs/vite/compare/v2.6.9...v2.6.11) (2021-10-25)


### Bug Fixes

* **build:** let top-level `this` refer to `globalThis` ([#5312](https://github.com/vitejs/vite/issues/5312)) ([7e25429](https://github.com/vitejs/vite/commit/7e254291e7870bdc621b71c3817f001efe9d648c))
* bundle ws types ([#5340](https://github.com/vitejs/vite/issues/5340)) ([bc4a96c](https://github.com/vitejs/vite/commit/bc4a96c883e849cf4dbd74356d4240763e713aef))
* **client:** fix typo in overlay config hint ([#5343](https://github.com/vitejs/vite/issues/5343)) ([96591bf](https://github.com/vitejs/vite/commit/96591bf9989529de839ba89958755eafe4c445ae))
* consider deep imports in isBuiltIn ([#5248](https://github.com/vitejs/vite/issues/5248)) ([269a1b6](https://github.com/vitejs/vite/commit/269a1b672bf954ed68d19d4541b9bdb471fc1937))
* ensure server.host is passed in preview-mode (fix [#5387](https://github.com/vitejs/vite/issues/5387)) ([#5389](https://github.com/vitejs/vite/issues/5389)) ([61b4b39](https://github.com/vitejs/vite/commit/61b4b39acd4c122b26a6c91c45bb0727728da7a3))
* load-fallback catch ([#5412](https://github.com/vitejs/vite/issues/5412)) ([e73281c](https://github.com/vitejs/vite/commit/e73281c806276740c337aea69a233e39235f5a0b))
* restrict static middleware fs access ([#5361](https://github.com/vitejs/vite/issues/5361)) ([1f4723b](https://github.com/vitejs/vite/commit/1f4723bbd82e234e779ee4cbc3a51b85c24463e0))
* **ssr:** ssrTransfrom with function declaration in scope, fix [#4306](https://github.com/vitejs/vite/issues/4306) ([#5376](https://github.com/vitejs/vite/issues/5376)) ([5306632](https://github.com/vitejs/vite/commit/5306632603fb5bb6d93f06e2412416394166e371))


### Performance Improvements

* minify css only when needed ([#5178](https://github.com/vitejs/vite/issues/5178)) ([7970239](https://github.com/vitejs/vite/commit/79702392874d81819e090a4a235313df83a7515c))



## [2.6.10](https://github.com/vitejs/vite/compare/v2.6.9...v2.6.10) (2021-10-18)


Expand Down
2 changes: 1 addition & 1 deletion packages/vite/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vite",
"version": "2.6.10",
"version": "2.6.13",
"license": "MIT",
"author": "Evan You",
"description": "Native-ESM powered web dev build tool",
Expand Down
10 changes: 9 additions & 1 deletion packages/vite/src/node/optimizer/esbuildDepPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
isRunningWithYarnPnp,
flattenId,
normalizePath,
isExternalUrl
isExternalUrl,
moduleListContains
} from '../utils'
import { browserExternalId } from '../plugins/resolve'
import { ExportsData } from '.'
Expand Down Expand Up @@ -99,6 +100,13 @@ export function esbuildDepPlugin(
build.onResolve(
{ filter: /^[\w@][^:]/ },
async ({ path: id, importer, kind }) => {
if (moduleListContains(config.optimizeDeps?.exclude, id)) {
return {
path: id,
external: true
}
}

// ensure esbuild uses our resolved entries
let entry: { path: string; namespace: string } | undefined
// if this is an entry, return entry namespace resolve result
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/optimizer/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
normalizePath,
isObject,
cleanUrl,
moduleListContains,
externalRE,
dataUrlRE,
multilineCommentsRE,
Expand Down Expand Up @@ -302,7 +303,7 @@ function esbuildScanPlugin(
filter: /^[\w@][^:]/
},
async ({ path: id, importer }) => {
if (exclude?.some((e) => e === id || id.startsWith(e + '/'))) {
if (moduleListContains(exclude, id)) {
return externalUnlessEntry({ path: id })
}
if (depImports[id]) {
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export function cssPlugin(config: ResolvedConfig): Plugin {
const thisModule = moduleGraph.getModuleById(id)
if (thisModule) {
// CSS modules cannot self-accept since it exports values
const isSelfAccepting = !modules
const isSelfAccepting = !modules && !inlineRE.test(id)
if (deps) {
// record deps in the module graph so edits to @import css can trigger
// main import to hot update
Expand Down
25 changes: 22 additions & 3 deletions packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import {
timeFrom,
normalizePath,
removeImportQuery,
unwrapId
unwrapId,
moduleListContains
} from '../utils'
import {
debugHmr,
Expand Down Expand Up @@ -179,13 +180,31 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
url = url.replace(base, '/')
}

const resolved = await this.resolve(url, importer)
let importerFile = importer
if (
moduleListContains(config.optimizeDeps?.exclude, url) &&
server._optimizeDepsMetadata
) {
// if the dependency encountered in the optimized file was excluded from the optimization
// the dependency needs to be resolved starting from the original source location of the optimized file
// because starting from node_modules/.vite will not find the dependency if it was not hoisted
// (that is, if it is under node_modules directory in the package source of the optimized file)
for (const optimizedModule of Object.values(
server._optimizeDepsMetadata.optimized
)) {
if (optimizedModule.file === importerModule.file) {
importerFile = optimizedModule.src
}
}
}

const resolved = await this.resolve(url, importerFile)

if (!resolved) {
this.error(
`Failed to resolve import "${url}" from "${path.relative(
process.cwd(),
importer
importerFile
)}". Does the file exist?`,
pos
)
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/loadFallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function loadFallbackPlugin(): Plugin {
name: 'vite:load-fallback',
async load(id) {
try {
return fs.readFile(cleanUrl(id), 'utf-8')
return await fs.readFile(cleanUrl(id), 'utf-8')
} catch (e) {
return fs.readFile(id, 'utf-8')
}
Expand Down
16 changes: 6 additions & 10 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import {
isDataUrl,
cleanUrl,
slash,
nestedResolveFrom
nestedResolveFrom,
isFileReadable
} from '../utils'
import { ViteDevServer, SSROptions } from '..'
import { createFilter } from '@rollup/pluginutils'
Expand Down Expand Up @@ -372,15 +373,10 @@ function tryResolveFile(
tryPrefix?: string,
skipPackageJson?: boolean
): string | undefined {
let isReadable = false
try {
// #2051 if we don't have read permission on a directory, existsSync() still
// works and will result in massively slow subsequent checks (which are
// unnecessary in the first place)
fs.accessSync(file, fs.constants.R_OK)
isReadable = true
} catch (e) {}
if (isReadable) {
// #2051 if we don't have read permission on a directory, existsSync() still
// works and will result in massively slow subsequent checks (which are
// unnecessary in the first place)
if (isFileReadable(file)) {
if (!fs.statSync(file).isDirectory()) {
return getRealPath(file, preserveSymlinks) + postfix
} else if (tryIndex) {
Expand Down
Loading

0 comments on commit 2280d24

Please sign in to comment.