Skip to content

Commit

Permalink
Merge branch 'main' into preload-on-system-format
Browse files Browse the repository at this point in the history
  • Loading branch information
Tal500 committed Oct 1, 2022
2 parents f153ff2 + 7a6d4bc commit 4b8a587
Show file tree
Hide file tree
Showing 78 changed files with 1,090 additions and 947 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ module.exports = defineConfig({
}
},
{
files: ['packages/vite/src/dep-types/**', '*.spec.ts'],
files: ['packages/vite/src/types/**', '*.spec.ts'],
rules: {
'node/no-extraneous-import': 'off'
}
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*.local
*.log
/.vscode/
/packages/vite/client/types.d.ts
/packages/vite/LICENSE
dist
dist-ssr
Expand Down
4 changes: 3 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,12 @@ Avoid deps with large transitive dependencies that result in bloated size compar

Vite aims to be fully usable as a dependency in a TypeScript project (e.g. it should provide proper typings for VitePress), and also in `vite.config.ts`. This means technically a dependency whose types are exposed needs to be part of `dependencies` instead of `devDependencies`. However, this also means we won't be able to bundle it.

To get around this, we inline some of these dependencies' types in `packages/vite/src/dep-types`. This way, we can still expose the typing but bundle the dependency's source code.
To get around this, we inline some of these dependencies' types in `packages/vite/src/types`. This way, we can still expose the typing but bundle the dependency's source code.

Use `pnpm run check-dist-types` to check that the bundled types do not rely on types in `devDependencies`. If you are adding `dependencies`, make sure to configure `tsconfig.check.json`.

For types shared between client and node, they should be added into `packages/vite/types`. These types are not bundled and are published as is (though they are still considered internal). Dependency types within this directory (e.g. `packages/vite/types/chokidar.d.ts`) are deprecated and should be added to `packages/vite/src/types` instead.

### Think Before Adding Yet Another Option

We already have many config options, and we should avoid fixing an issue by adding yet another one. Before adding an option, consider whether the problem:
Expand Down
4 changes: 2 additions & 2 deletions docs/config/build-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ Options to pass on to [@rollup/plugin-dynamic-import-vars](https://github.com/ro
## build.lib
- **Type:** `{ entry: string, name?: string, formats?: ('es' | 'cjs' | 'umd' | 'iife')[], fileName?: string | ((format: ModuleFormat) => string) }`
- **Type:** `{ entry: string | string[] | { [entryAlias: string]: string }, name?: string, formats?: ('es' | 'cjs' | 'umd' | 'iife')[], fileName?: string | ((format: ModuleFormat, entryName: string) => string) }`
- **Related:** [Library Mode](/guide/build#library-mode)
Build as a library. `entry` is required since the library cannot use HTML as entry. `name` is the exposed global variable and is required when `formats` includes `'umd'` or `'iife'`. Default `formats` are `['es', 'umd']`. `fileName` is the name of the package file output, default `fileName` is the name option of package.json, it can also be defined as function taking the `format` as an argument.
Build as a library. `entry` is required since the library cannot use HTML as entry. `name` is the exposed global variable and is required when `formats` includes `'umd'` or `'iife'`. Default `formats` are `['es', 'umd']`. `fileName` is the name of the package file output, default `fileName` is the name option of package.json, it can also be defined as function taking the `format` and `entryAlias` as arguments.
## build.manifest
Expand Down
14 changes: 13 additions & 1 deletion docs/guide/api-hmr.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,18 @@ Calling `import.meta.hot.decline()` indicates this module is not hot-updatable,
## `hot.invalidate()`
For now, calling `import.meta.hot.invalidate()` simply reloads the page.
A self-accepting module may realize during runtime that it can't handle a HMR update, and so the update needs to be forcefully propagated to importers. By calling `import.meta.hot.invalidate()`, the HMR server will invalidate the importers of the caller, as if the caller wasn't self-accepting.
Note that you should always call `import.meta.hot.accept` even if you plan to call `invalidate` immediately afterwards, or else the HMR client won't listen for future changes to the self-accepting module. To communicate your intent clearly, we recommend calling `invalidate` within the `accept` callback like so:
```ts
import.meta.hot.accept(module => {
// You may use the new module instance to decide whether to invalidate.
if (cannotHandleUpdate(module)) {
import.meta.hot.invalidate()
}
})
```
## `hot.on(event, cb)`
Expand All @@ -136,6 +147,7 @@ The following HMR events are dispatched by Vite automatically:
- `'vite:beforeUpdate'` when an update is about to be applied (e.g. a module will be replaced)
- `'vite:beforeFullReload'` when a full reload is about to occur
- `'vite:beforePrune'` when modules that are no longer needed are about to be pruned
- `'vite:invalidate'` when a module is invalidated with `import.meta.hot.invalidate()`
- `'vite:error'` when an error occurs (e.g. syntax error)
Custom HMR events can also be sent from plugins. See [handleHotUpdate](./api-plugin#handlehotupdate) for more details.
Expand Down
21 changes: 6 additions & 15 deletions docs/guide/api-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -595,21 +595,12 @@ It is possible to type custom events by extending the `CustomEventMap` interface
```ts
// events.d.ts
import 'vite'
import 'vite/client/types'
import 'vite/types/customEvent'

interface MyCustomEventMap {
'custom:foo': { msg: string }
// 'event-key': payload
}

// extend interface for server-side
declare module 'vite' {
interface CustomEventMap extends MyCustomEventMap {}
}

// extend interface for client-side
declare module 'vite/client/types' {
interface CustomEventMap extends MyCustomEventMap {}
declare module 'vite/types/customEvent' {
interface CustomEventMap {
'custom:foo': { msg: string }
// 'event-key': payload
}
}
```
23 changes: 23 additions & 0 deletions docs/guide/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ import { defineConfig } from 'vite'
export default defineConfig({
build: {
lib: {
// Could also be a dictionary or array of multiple entry points
entry: resolve(__dirname, 'lib/main.js'),
name: 'MyLib',
// the proper extensions will be added
Expand Down Expand Up @@ -185,6 +186,28 @@ Recommended `package.json` for your lib:
}
```

Or, if exposing multiple entry points:

```json
{
"name": "my-lib",
"type": "module",
"files": ["dist"],
"main": "./dist/my-lib.cjs",
"module": "./dist/my-lib.mjs",
"exports": {
".": {
"import": "./dist/my-lib.mjs",
"require": "./dist/my-lib.cjs"
},
"./secondary": {
"import": "./dist/secondary.mjs",
"require": "./dist/secondary.cjs"
}
}
}
```

::: tip Note
If the `package.json` does not contain `"type": "module"`, Vite will generate different file extensions for Node.js compatibility. `.js` will become `.mjs` and `.cjs` will become `.js`.
:::
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": "~2.78.0",
"rollup": "^2.79.1",
"rollup-plugin-license": "^2.8.1",
"semver": "^7.3.7",
"simple-git-hooks": "^2.8.0",
Expand Down
8 changes: 7 additions & 1 deletion packages/plugin-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ export interface Options {
* @default true
*/
jsxPure?: boolean
/**
* Toggles whether or not to throw an error if an XML namespaced tag name is used.
* @default true
*/
jsxThrowIfNamespace?: boolean
/**
* Babel configuration applied in both dev and prod.
*/
Expand Down Expand Up @@ -248,7 +253,8 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
{
runtime: 'automatic',
importSource: opts.jsxImportSource,
pure: opts.jsxPure !== false
pure: opts.jsxPure !== false,
throwIfNamespace: opts.jsxThrowIfNamespace
}
])

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.15",
"debug": "^4.3.4",
"rollup": "~2.78.0",
"rollup": "^2.79.1",
"slash": "^4.0.0",
"source-map": "^0.6.1",
"vite": "workspace:*",
Expand Down
54 changes: 0 additions & 54 deletions packages/vite/api-extractor.client.json

This file was deleted.

2 changes: 1 addition & 1 deletion packages/vite/client.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// <reference path="./import-meta.d.ts" />
/// <reference path="./types/importMeta.d.ts" />

// CSS modules
type CSSModuleClasses = { readonly [key: string]: string }
Expand Down
10 changes: 0 additions & 10 deletions packages/vite/import-meta.d.ts

This file was deleted.

33 changes: 14 additions & 19 deletions packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,16 @@
"./client": {
"types": "./client.d.ts"
},
"./import-meta": {
"types": "./import-meta.d.ts"
},
"./client/types": {
"types": "./client/types.d.ts"
},
"./dist/client/*": "./dist/client/*",
"./package.json": "./package.json"
},
"files": [
"bin",
"dist",
"client.d.ts",
"import-meta.d.ts",
"index.cjs",
"src/client",
"types",
"client/types.d.ts"
"types"
],
"engines": {
"node": "^14.18.0 || >=16.0.0"
Expand All @@ -55,13 +47,12 @@
"dev": "rimraf dist && pnpm run build-bundle -w",
"build": "rimraf dist && run-s build-bundle build-types",
"build-bundle": "rollup --config rollup.config.ts --configPlugin typescript",
"build-types": "run-p build-node-types build-client-types",
"build-node-types": "run-s build-node-types-temp build-node-types-patch build-node-types-roll build-node-types-check",
"build-node-types-temp": "tsc --emitDeclarationOnly --outDir temp/node -p src/node",
"build-node-types-patch": "tsx scripts/patchTypes.ts",
"build-node-types-roll": "api-extractor run && rimraf temp",
"build-node-types-check": "tsc --project tsconfig.check.json",
"build-client-types": "api-extractor run -c api-extractor.client.json",
"build-types": "run-s build-types-temp build-types-pre-patch build-types-roll build-types-post-patch build-types-check",
"build-types-temp": "tsc --emitDeclarationOnly --outDir temp/node -p src/node",
"build-types-pre-patch": "tsx scripts/prePatchTypes.ts",
"build-types-roll": "api-extractor run && rimraf temp",
"build-types-post-patch": "tsx scripts/postPatchTypes.ts",
"build-types-check": "tsc --project tsconfig.check.json",
"lint": "eslint --cache --ext .ts src/**",
"format": "prettier --write --cache --parser typescript \"src/**/*.ts\"",
"prepublishOnly": "npm run build"
Expand All @@ -71,7 +62,7 @@
"esbuild": "^0.15.9",
"postcss": "^8.4.16",
"resolve": "^1.22.1",
"rollup": "~2.78.0"
"rollup": "^2.79.1"
},
"optionalDependencies": {
"fsevents": "~2.3.2"
Expand Down Expand Up @@ -127,15 +118,16 @@
"strip-literal": "^0.4.2",
"tsconfck": "^2.0.1",
"tslib": "^2.4.0",
"dep-types": "link:./src/dep-types",
"types": "link:./src/types",
"dep-types": "link:./src/types",
"types": "link:./types",
"ufo": "^0.8.5",
"ws": "^8.9.0"
},
"peerDependencies": {
"less": "*",
"sass": "*",
"stylus": "*",
"sugarss": "*",
"terser": "^5.4.0"
},
"peerDependenciesMeta": {
Expand All @@ -148,6 +140,9 @@
"less": {
"optional": true
},
"sugarss": {
"optional": true
},
"terser": {
"optional": true
}
Expand Down

0 comments on commit 4b8a587

Please sign in to comment.