From 33f96718dc5d827612c300fb6d0258f1c040f5a1 Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Mon, 21 Feb 2022 00:01:18 +0800 Subject: [PATCH 01/50] docs: update pre-bundling guide (#7008) --- docs/guide/dep-pre-bundling.md | 36 +++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/docs/guide/dep-pre-bundling.md b/docs/guide/dep-pre-bundling.md index 150584b1a6521c..a72e06e6aff944 100644 --- a/docs/guide/dep-pre-bundling.md +++ b/docs/guide/dep-pre-bundling.md @@ -3,10 +3,10 @@ When you run `vite` for the first time, you may notice this message: ``` -Optimizable dependencies detected: -react, react-dom -Pre-bundling them to speed up dev server page load... -(this will be run only when your dependencies have changed) +Pre-bundling dependencies: + react + react-dom +(this will be run only when your dependencies or config have changed) ``` ## The Why @@ -28,6 +28,8 @@ This is Vite performing what we call "dependency pre-bundling". This process ser By pre-bundling `lodash-es` into a single module, we now only need one HTTP request instead! +Note that this only applies in development mode. + ## Automatic Dependency Discovery If an existing cache is not found, Vite will crawl your source code and automatically discover dependency imports (i.e. "bare imports" that expect to be resolved from `node_modules`) and use these found imports as entry points for the pre-bundle. The pre-bundling is performed with `esbuild` so it's typically very fast. @@ -36,11 +38,27 @@ After the server has already started, if a new dependency import is encountered ## Monorepos and Linked Dependencies -In a monorepo setup, a dependency may be a linked package from the same repo. Vite automatically detects dependencies that are not resolved from `node_modules` and treats the linked dep as source code. It will not attempt to bundle the linked dep, and instead will analyze the linked dep's dependency list instead. +In a monorepo setup, a dependency may be a linked package from the same repo. Vite automatically detects dependencies that are not resolved from `node_modules` and treats the linked dep as source code. It will not attempt to bundle the linked dep, and will analyze the linked dep's dependency list instead. + +However, this requires the linked dep to be exported as ESM. If not, you can add the dependency to [`optimizeDeps.include`](/config/#optimizedeps-include) and [`build.commonjsOptions.include`](/config/#build-commonjsoptions) in your config. + +```js +export default defineConfig({ + optimizeDeps: { + include: ['linked-dep'] + }, + build: { + commonjsOptions: { + include: [/linked-dep/, /node_modules/] + } + } +}) +``` + +When making changes to the linked dep, restart the dev server with the `--force` command line option for the changes to take effect. -::: warning Note -Linked dependencies might not work properly in the final build due to differences in dependency resolution. -Use `npm pack` instead for all local dependencies to avoid issues in the final bundle. (The `npm pack` is only ever needed when the linked source code or package only exports CJS code. If it exports ESM code, then it is not needed.) +::: warning Deduping +Due to differences in linked dependency resolution, transitive dependencies can deduplicated incorrectly, causing issues when used in runtime. If you stumble on this issue, use `npm pack` on the linked dependency to fix it. ::: ## Customizing the Behavior @@ -57,7 +75,7 @@ Both `include` and `exclude` can be used to deal with this. If the dependency is Vite caches the pre-bundled dependencies in `node_modules/.vite`. It determines whether it needs to re-run the pre-bundling step based on a few sources: -- The `dependencies` list in your `package.json` +- The `dependencies` list in your `package.json`. - Package manager lockfiles, e.g. `package-lock.json`, `yarn.lock`, or `pnpm-lock.yaml`. - Relevant fields in your `vite.config.js`, if present. From 2debb9f4cbb6003e7d24444cf049b45582d82ff1 Mon Sep 17 00:00:00 2001 From: Jeff Yang <32727188+ydcjeff@users.noreply.github.com> Date: Wed, 23 Feb 2022 03:00:03 +0630 Subject: [PATCH 02/50] fix: allow optional trailing comma in asset `import.meta.url` (#6983) Co-authored-by: bluwy --- packages/playground/assets/index.html | 45 +++++++++++++++++++ .../src/node/plugins/assetImportMetaUrl.ts | 2 +- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/packages/playground/assets/index.html b/packages/playground/assets/index.html index f3b9a0b372a608..ef673053abad27 100644 --- a/packages/playground/assets/index.html +++ b/packages/playground/assets/index.html @@ -137,6 +137,14 @@

new URL('...', import.meta.url)

+

new URL('...', import.meta.url,) (with comma)

+ + + +

new URL('...', import.meta.url,) (with comma + new line)

+ + +

new URL(`./${dynamic}`, import.meta.url)

@@ -147,6 +155,16 @@

new URL(`./${dynamic}`, import.meta.url)

+

new URL(`./${dynamic}`, import.meta.url,) (with comma)

+

+ + +

+

+ + +

+

simple script tag import-expression

+ + diff --git a/packages/plugin-vue/src/handleHotUpdate.ts b/packages/plugin-vue/src/handleHotUpdate.ts index 2bce3950563367..7c2a7ef7ee2f1f 100644 --- a/packages/plugin-vue/src/handleHotUpdate.ts +++ b/packages/plugin-vue/src/handleHotUpdate.ts @@ -40,9 +40,14 @@ export async function handleHotUpdate( if (hasScriptChanged(prevDescriptor, descriptor)) { let scriptModule: ModuleNode | undefined - if (descriptor.script?.lang && !descriptor.script.src) { + if ( + (descriptor.scriptSetup?.lang && !descriptor.scriptSetup.src) || + (descriptor.script?.lang && !descriptor.script.src) + ) { const scriptModuleRE = new RegExp( - `type=script.*&lang\.${descriptor.script.lang}$` + `type=script.*&lang\.${ + descriptor.scriptSetup?.lang || descriptor.script?.lang + }$` ) scriptModule = modules.find((m) => scriptModuleRE.test(m.url)) } From f38654fd331316f496008f3a118d2628c65b071b Mon Sep 17 00:00:00 2001 From: Zheeeng Date: Thu, 24 Feb 2022 16:14:10 +0800 Subject: [PATCH 06/50] fix: typo (#7064) --- packages/vite/src/node/certificate.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/vite/src/node/certificate.ts b/packages/vite/src/node/certificate.ts index be3604f1dab378..d8ce82d6b5365d 100644 --- a/packages/vite/src/node/certificate.ts +++ b/packages/vite/src/node/certificate.ts @@ -43,13 +43,13 @@ import 'node-forge/lib/pki' // this RFC in section 4.1.2.2 requires serial numbers to be positive // http://www.ietf.org/rfc/rfc5280.txt function toPositiveHex(hexString: string) { - let mostSiginficativeHexAsInt = parseInt(hexString[0], 16) - if (mostSiginficativeHexAsInt < 8) { + let mostSignificativeHexAsInt = parseInt(hexString[0], 16) + if (mostSignificativeHexAsInt < 8) { return hexString } - mostSiginficativeHexAsInt -= 8 - return mostSiginficativeHexAsInt.toString() + hexString.substring(1) + mostSignificativeHexAsInt -= 8 + return mostSignificativeHexAsInt.toString() + hexString.substring(1) } export function createCertificate(): string { From 745ae2f07a1273339ce32dc209735608679d8515 Mon Sep 17 00:00:00 2001 From: Zheeeng Date: Thu, 24 Feb 2022 19:46:35 +0800 Subject: [PATCH 07/50] chore(types): use more reasonable ts checking annotation comment (#7063) --- packages/vite/src/client/client.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/client/client.ts b/packages/vite/src/client/client.ts index 736590e175529a..988fd45291983f 100644 --- a/packages/vite/src/client/client.ts +++ b/packages/vite/src/client/client.ts @@ -254,7 +254,7 @@ export function updateStyle(id: string, content: string): void { if (!style) { style = new CSSStyleSheet() style.replaceSync(content) - // @ts-ignore + // @ts-expect-error: using experimental API document.adoptedStyleSheets = [...document.adoptedStyleSheets, style] } else { style.replaceSync(content) @@ -281,7 +281,7 @@ export function removeStyle(id: string): void { const style = sheetsMap.get(id) if (style) { if (style instanceof CSSStyleSheet) { - // @ts-ignore + // @ts-expect-error: using experimental API document.adoptedStyleSheets = document.adoptedStyleSheets.filter( (s: CSSStyleSheet) => s !== style ) From a9dfce38108e796e0de0e3b43ced34d60883cef3 Mon Sep 17 00:00:00 2001 From: yoho Date: Thu, 24 Feb 2022 21:13:16 +0800 Subject: [PATCH 08/50] fix(hmr): hmr style tag no support in html (#7052) --- .../assets/__tests__/assets.spec.ts | 17 ++++- .../src/node/server/middlewares/indexHtml.ts | 67 ++++++++++--------- 2 files changed, 52 insertions(+), 32 deletions(-) diff --git a/packages/playground/assets/__tests__/assets.spec.ts b/packages/playground/assets/__tests__/assets.spec.ts index c51bb4094b7b8c..dfdd8099b062a2 100644 --- a/packages/playground/assets/__tests__/assets.spec.ts +++ b/packages/playground/assets/__tests__/assets.spec.ts @@ -8,7 +8,8 @@ import { readManifest, readFile, editFile, - notifyRebuildComplete + notifyRebuildComplete, + untilUpdated } from '../../testUtils' const assetMatch = isBuild @@ -37,7 +38,7 @@ describe('injected scripts', () => { test('html-proxy', async () => { const hasHtmlProxy = await page.$( - 'script[type="module"][src="/foo/index.html?html-proxy&index=0.js"]' + 'script[type="module"][src^="/foo/index.html?html-proxy"]' ) if (isBuild) { expect(hasHtmlProxy).toBeFalsy() @@ -254,3 +255,15 @@ describe('css and assets in css in build watch', () => { }) } }) + +if (!isBuild) { + test('@import in html style tag hmr', async () => { + await untilUpdated(() => getColor('.import-css'), 'rgb(0, 136, 255)') + editFile( + './css/import.css', + (code) => code.replace('#0088ff', '#00ff88'), + true + ) + await untilUpdated(() => getColor('.import-css'), 'rgb(0, 255, 136)') + }) +} diff --git a/packages/vite/src/node/server/middlewares/indexHtml.ts b/packages/vite/src/node/server/middlewares/indexHtml.ts index 2721502c408e28..3c76d94c526930 100644 --- a/packages/vite/src/node/server/middlewares/indexHtml.ts +++ b/packages/vite/src/node/server/middlewares/indexHtml.ts @@ -1,7 +1,7 @@ import fs from 'fs' import path from 'path' import MagicString from 'magic-string' -import type { AttributeNode } from '@vue/compiler-dom' +import type { AttributeNode, ElementNode } from '@vue/compiler-dom' import { NodeTypes } from '@vue/compiler-dom' import type { Connect } from 'types/connect' import type { IndexHtmlTransformHook } from '../../plugins/html' @@ -94,9 +94,39 @@ const devHtmlHook: IndexHtmlTransformHook = async ( const base = config.base || '/' const s = new MagicString(html) - let scriptModuleIndex = -1 + let inlineModuleIndex = -1 const filePath = cleanUrl(htmlPath) + const addInlineModule = (node: ElementNode, ext: 'js' | 'css') => { + inlineModuleIndex++ + + const url = filePath.replace(normalizePath(config.root), '') + + const contents = node.children + .map((child: any) => child.content || '') + .join('') + + // add HTML Proxy to Map + addToHTMLProxyCache(config, url, inlineModuleIndex, contents) + + // inline js module. convert to src="proxy" + const modulePath = `${ + config.base + htmlPath.slice(1) + }?html-proxy&index=${inlineModuleIndex}.${ext}` + + // invalidate the module so the newly cached contents will be served + const module = server?.moduleGraph.getModuleById(modulePath) + if (module) { + server?.moduleGraph.invalidateModule(module) + } + + s.overwrite( + node.loc.start.offset, + node.loc.end.offset, + `` + ) + } + await traverseHtml(html, htmlPath, (node) => { if (node.type !== NodeTypes.ELEMENT) { return @@ -105,41 +135,18 @@ const devHtmlHook: IndexHtmlTransformHook = async ( // script tags if (node.tag === 'script') { const { src, isModule } = getScriptInfo(node) - if (isModule) { - scriptModuleIndex++ - } if (src) { processNodeUrl(src, s, config, htmlPath, originalUrl, moduleGraph) } else if (isModule) { - const url = filePath.replace(normalizePath(config.root), '') - - const contents = node.children - .map((child: any) => child.content || '') - .join('') - - // add HTML Proxy to Map - addToHTMLProxyCache(config, url, scriptModuleIndex, contents) - - // inline js module. convert to src="proxy" - const modulePath = `${ - config.base + htmlPath.slice(1) - }?html-proxy&index=${scriptModuleIndex}.js` - - // invalidate the module so the newly cached contents will be served - const module = server?.moduleGraph.getModuleById(modulePath) - if (module) { - server?.moduleGraph.invalidateModule(module) - } - - s.overwrite( - node.loc.start.offset, - node.loc.end.offset, - `` - ) + addInlineModule(node, 'js') } } + if (node.tag === 'style' && node.children.length) { + addInlineModule(node, 'css') + } + // elements with [href/src] attrs const assetAttrs = assetAttrsConfig[node.tag] if (assetAttrs) { From 88ded7f16382d83603511de043785e01ee1e4a3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Fri, 25 Feb 2022 05:31:05 +0900 Subject: [PATCH 09/50] fix: import with query with exports field (#7073) --- packages/playground/resolve/__tests__/resolve.spec.ts | 4 ++++ packages/playground/resolve/exports-path/deep.json | 3 +++ packages/playground/resolve/exports-path/package.json | 1 + packages/playground/resolve/index.html | 7 +++++++ packages/vite/src/node/plugins/resolve.ts | 7 ++++++- 5 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 packages/playground/resolve/exports-path/deep.json diff --git a/packages/playground/resolve/__tests__/resolve.spec.ts b/packages/playground/resolve/__tests__/resolve.spec.ts index 97e4a5dd0add7b..b64da138033fc0 100644 --- a/packages/playground/resolve/__tests__/resolve.spec.ts +++ b/packages/playground/resolve/__tests__/resolve.spec.ts @@ -16,6 +16,10 @@ test('deep import with exports field', async () => { expect(await page.textContent('.exports-deep')).toMatch('[success]') }) +test('deep import with query with exports field', async () => { + expect(await page.textContent('.exports-deep-query')).not.toMatch('fail') +}) + test('deep import with exports field + exposed dir', async () => { expect(await page.textContent('.exports-deep-exposed-dir')).toMatch( '[success]' diff --git a/packages/playground/resolve/exports-path/deep.json b/packages/playground/resolve/exports-path/deep.json new file mode 100644 index 00000000000000..97e19265d6c843 --- /dev/null +++ b/packages/playground/resolve/exports-path/deep.json @@ -0,0 +1,3 @@ +{ + "foo": "json" +} diff --git a/packages/playground/resolve/exports-path/package.json b/packages/playground/resolve/exports-path/package.json index 045fc85db128d2..7355da2f63f616 100644 --- a/packages/playground/resolve/exports-path/package.json +++ b/packages/playground/resolve/exports-path/package.json @@ -8,6 +8,7 @@ "require": "./cjs.js" }, "./deep.js": "./deep.js", + "./deep.json": "./deep.json", "./dir/": "./dir/", "./dir-mapped/*": { "import": "./dir/*", diff --git a/packages/playground/resolve/index.html b/packages/playground/resolve/index.html index db0a4bc54f1ad7..71699035abdd53 100644 --- a/packages/playground/resolve/index.html +++ b/packages/playground/resolve/index.html @@ -12,6 +12,9 @@

Entry resolving with exports field

Deep import with exports field

fail

+

Deep import with query with exports field

+

fail

+

Deep import with exports field + exposed directory

fail

@@ -100,6 +103,10 @@

resolve package that contains # in path

import { msg as deepMsg } from 'resolve-exports-path/deep.js' text('.exports-deep', deepMsg) + // deep import w/ exports w/ query + import deepPath from 'resolve-exports-path/deep.json?url' + text('.exports-deep-query', deepPath) + // deep import w/ exposed dir import { msg as exposedDirMsg } from 'resolve-exports-path/dir/dir' text('.exports-deep-exposed-dir', exposedDirMsg) diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 55afd92df1a8fb..03a9026242f428 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -813,7 +813,12 @@ function resolveDeepImport( // map relative based on exports data if (exportsField) { if (isObject(exportsField) && !Array.isArray(exportsField)) { - relativeId = resolveExports(data, relativeId, options, targetWeb) + relativeId = resolveExports( + data, + cleanUrl(relativeId), + options, + targetWeb + ) } else { // not exposed relativeId = undefined From 67d164392e8e9081dc3f0338c4b4b8eea6c5f7da Mon Sep 17 00:00:00 2001 From: yoho <907415276@qq.com> Date: Fri, 25 Feb 2022 14:32:19 +0800 Subject: [PATCH 10/50] docs: add debugging document (#7082) --- CONTRIBUTING.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5b8f1fed7192c3..5a0e5153f4b8ab 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -14,6 +14,20 @@ To develop and test the core `vite` package: You can alternatively use [Vite.js Docker Dev](https://github.com/nystudio107/vitejs-docker-dev) for a containerized Docker setup for Vite.js development. +## Debugging + +If you want to use break point and explore code execution you can use the ["Run and debug"](https://code.visualstudio.com/docs/editor/debugging) feature from vscode. + +1. Add a `debugger` statement where you want to stop the code execution. + +2. Click on the "Run and Debug" icon in the activity bar of the editor. + +3. Click on the "Javascript Debug Terminal" button. + +4. It will open a terminal, then go to `packages/playground/xxx` and run `pnpm run dev`. + +5. The execution will stop and you'll use the [Debug toolbar](https://code.visualstudio.com/docs/editor/debugging#_debug-actions) to continue, step over, restart the process... + ## Testing Vite against external packages You may wish to test your locally-modified copy of Vite against another package that is built with Vite. For pnpm, after building Vite, you can use [`pnpm.overrides`](https://pnpm.io/package_json#pnpmoverrides). Please note that `pnpm.overrides` must be specified in the root `package.json` and you must first list the package as a dependency in the root `package.json`: From c703a3348adeaad9dc92d805a381866917f2a03b Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Fri, 25 Feb 2022 08:39:45 -0800 Subject: [PATCH 11/50] feat: add fixStacktrace option to ssrLoadModule (#7048) --- docs/guide/api-javascript.md | 2 +- packages/vite/src/node/server/index.ts | 15 +++++++++--- packages/vite/src/node/ssr/ssrModuleLoader.ts | 24 +++++++++++++++---- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/docs/guide/api-javascript.md b/docs/guide/api-javascript.md index 1651923c06cdcb..ddaa04279737f5 100644 --- a/docs/guide/api-javascript.md +++ b/docs/guide/api-javascript.md @@ -94,7 +94,7 @@ interface ViteDevServer { */ ssrLoadModule( url: string, - options?: { isolated?: boolean } + options?: { fixStacktrace?: boolean } ): Promise> /** * Fix ssr error stacktrace. diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index aeea862dbc173f..77e33cd31a0995 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -224,7 +224,10 @@ export interface ViteDevServer { /** * Load a given URL as an instantiated module for SSR. */ - ssrLoadModule(url: string): Promise> + ssrLoadModule( + url: string, + opts?: { fixStacktrace?: boolean } + ): Promise> /** * Fix ssr error stacktrace */ @@ -361,14 +364,20 @@ export async function createServer( return transformRequest(url, server, options) }, transformIndexHtml: null!, // to be immediately set - ssrLoadModule(url) { + ssrLoadModule(url, opts?: { fixStacktrace?: boolean }) { server._ssrExternals ||= resolveSSRExternal( config, server._optimizeDepsMetadata ? Object.keys(server._optimizeDepsMetadata.optimized) : [] ) - return ssrLoadModule(url, server) + return ssrLoadModule( + url, + server, + undefined, + undefined, + opts?.fixStacktrace + ) }, ssrFixStacktrace(e) { if (e.stack) { diff --git a/packages/vite/src/node/ssr/ssrModuleLoader.ts b/packages/vite/src/node/ssr/ssrModuleLoader.ts index f0eb1bbfc0339e..de31c6a20266c5 100644 --- a/packages/vite/src/node/ssr/ssrModuleLoader.ts +++ b/packages/vite/src/node/ssr/ssrModuleLoader.ts @@ -35,7 +35,8 @@ export async function ssrLoadModule( url: string, server: ViteDevServer, context: SSRContext = { global }, - urlStack: string[] = [] + urlStack: string[] = [], + fixStacktrace?: boolean ): Promise { url = unwrapId(url).replace(NULL_BYTE_PLACEHOLDER, '\0') @@ -48,7 +49,13 @@ export async function ssrLoadModule( return pending } - const modulePromise = instantiateModule(url, server, context, urlStack) + const modulePromise = instantiateModule( + url, + server, + context, + urlStack, + fixStacktrace + ) pendingModules.set(url, modulePromise) modulePromise .catch(() => { @@ -64,7 +71,8 @@ async function instantiateModule( url: string, server: ViteDevServer, context: SSRContext = { global }, - urlStack: string[] = [] + urlStack: string[] = [], + fixStacktrace?: boolean ): Promise { const { moduleGraph } = server const mod = await moduleGraph.ensureEntryFromUrl(url, true) @@ -132,7 +140,13 @@ async function instantiateModule( if (pendingDeps.length === 1) { pendingImports.set(url, pendingDeps) } - const mod = await ssrLoadModule(dep, server, context, urlStack) + const mod = await ssrLoadModule( + dep, + server, + context, + urlStack, + fixStacktrace + ) if (pendingDeps.length === 1) { pendingImports.delete(url) } else { @@ -188,7 +202,7 @@ async function instantiateModule( ssrExportAll ) } catch (e) { - if (e.stack) { + if (e.stack && fixStacktrace !== false) { const stacktrace = ssrRewriteStacktrace(e.stack, moduleGraph) rebindErrorStacktrace(e, stacktrace) server.config.logger.error( From 5818ac927861783ea2b05450761fed30f40e7399 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Mart=C3=ADn=20Seery?= Date: Sat, 26 Feb 2022 04:49:34 -0300 Subject: [PATCH 12/50] fix: ?html-proxy with trailing = added by some servers (#7093) --- packages/vite/src/node/plugins/html.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index e7ca5e63961253..7f8a750c72bf22 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -41,7 +41,7 @@ interface ScriptAssetsUrl { url: string } -const htmlProxyRE = /\?html-proxy[&inline\-css]*&index=(\d+)\.(js|css)$/ +const htmlProxyRE = /\?html-proxy=?[&inline\-css]*&index=(\d+)\.(js|css)$/ const inlineCSSRE = /__VITE_INLINE_CSS__([^_]+_\d+)__/g const inlineImportRE = /\bimport\s*\(("[^"]*"|'[^']*')\)/g export const isHTMLProxy = (id: string): boolean => htmlProxyRE.test(id) From 30125418b4c7ebda56555090b177ac34b29ffdc7 Mon Sep 17 00:00:00 2001 From: ygj6 <7699524+ygj6@users.noreply.github.com> Date: Sun, 27 Feb 2022 00:52:43 +0800 Subject: [PATCH 13/50] fix(ssrTransform): use appendLeft instead of appendRight (#6407) --- .../node/ssr/__tests__/ssrTransform.spec.ts | 34 ++++++++++++++++--- packages/vite/src/node/ssr/ssrTransform.ts | 2 +- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts index f91444b49636a1..e086365ee25f16 100644 --- a/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts +++ b/packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts @@ -304,8 +304,8 @@ test('should declare variable for imported super class', async () => { const Foo = __vite_ssr_import_0__.Foo; class A extends Foo {} class B extends Foo {} - Object.defineProperty(__vite_ssr_exports__, \\"default\\", { enumerable: true, value: A }); - Object.defineProperty(__vite_ssr_exports__, \\"B\\", { enumerable: true, configurable: true, get(){ return B }});" + Object.defineProperty(__vite_ssr_exports__, \\"B\\", { enumerable: true, configurable: true, get(){ return B }}); + Object.defineProperty(__vite_ssr_exports__, \\"default\\", { enumerable: true, value: A });" `) }) @@ -351,8 +351,8 @@ test('should handle default export variants', async () => { ).toMatchInlineSnapshot(` "class A {} class B extends A {} - Object.defineProperty(__vite_ssr_exports__, \\"default\\", { enumerable: true, value: A }); - Object.defineProperty(__vite_ssr_exports__, \\"B\\", { enumerable: true, configurable: true, get(){ return B }});" + Object.defineProperty(__vite_ssr_exports__, \\"B\\", { enumerable: true, configurable: true, get(){ return B }}); + Object.defineProperty(__vite_ssr_exports__, \\"default\\", { enumerable: true, value: A });" `) }) @@ -610,7 +610,7 @@ test('jsx', async () => { const code = ` import React from 'react' import { Foo, Slot } from 'foo' - + function Bar({ Slot = }) { return ( <> @@ -633,3 +633,27 @@ test('jsx', async () => { " `) }) + +test('continuous exports', async () => { + expect( + ( + await ssrTransform( + ` +export function fn1() { +}export function fn2() { +} + `, + null, + null + ) + ).code + ).toMatchInlineSnapshot(` + " + function fn1() { + } + Object.defineProperty(__vite_ssr_exports__, \\"fn1\\", { enumerable: true, configurable: true, get(){ return fn1 }});function fn2() { + } + Object.defineProperty(__vite_ssr_exports__, \\"fn2\\", { enumerable: true, configurable: true, get(){ return fn2 }}); + " + `) +}) diff --git a/packages/vite/src/node/ssr/ssrTransform.ts b/packages/vite/src/node/ssr/ssrTransform.ts index b4eee713e707bc..36496b8168ad36 100644 --- a/packages/vite/src/node/ssr/ssrTransform.ts +++ b/packages/vite/src/node/ssr/ssrTransform.ts @@ -66,7 +66,7 @@ export async function ssrTransform( } function defineExport(position: number, name: string, local = name) { - s.appendRight( + s.appendLeft( position, `\nObject.defineProperty(${ssrModuleExportsKey}, "${name}", ` + `{ enumerable: true, configurable: true, get(){ return ${local} }});` From 79428ad5b849455e14f95d1b439ae296ba231221 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Volf?= Date: Sun, 27 Feb 2022 05:02:14 +0100 Subject: [PATCH 14/50] fix(config): Warn about terserOptions in more cases (#7101) --- packages/vite/src/node/config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/config.ts b/packages/vite/src/node/config.ts index fbda8d5166cb1b..224c534c497ece 100644 --- a/packages/vite/src/node/config.ts +++ b/packages/vite/src/node/config.ts @@ -649,7 +649,7 @@ export async function resolveConfig( ) } - if (config.build?.terserOptions && config.build.minify === 'esbuild') { + if (config.build?.terserOptions && config.build.minify !== 'terser') { logger.warn( colors.yellow( `build.terserOptions is specified but build.minify is not set to use Terser. ` + From 7a1a552ba710bad5714ef0fbb16fdd29ac58ae0b Mon Sep 17 00:00:00 2001 From: yoho <907415276@qq.com> Date: Sun, 27 Feb 2022 19:56:00 +0800 Subject: [PATCH 15/50] fix: fileToBuiltUrl got undefined when file type is `.ico` (#7106) --- .../playground/assets/__tests__/assets.spec.ts | 4 ++++ packages/playground/assets/favicon.ico | Bin 0 -> 1150 bytes packages/playground/assets/index.html | 1 + packages/vite/src/node/plugins/asset.ts | 4 ++++ 4 files changed, 9 insertions(+) create mode 100644 packages/playground/assets/favicon.ico diff --git a/packages/playground/assets/__tests__/assets.spec.ts b/packages/playground/assets/__tests__/assets.spec.ts index dfdd8099b062a2..8781fec785f3f2 100644 --- a/packages/playground/assets/__tests__/assets.spec.ts +++ b/packages/playground/assets/__tests__/assets.spec.ts @@ -121,6 +121,10 @@ describe('css url() references', () => { const match = isBuild ? `data:image/png;base64` : `/foo/nested/icon.png` expect(await getBg('.css-url-base64-inline')).toMatch(match) expect(await getBg('.css-url-quotes-base64-inline')).toMatch(match) + const icoMatch = isBuild ? `data:image;base64` : `favicon.ico` + const el = await page.$(`link.ico`) + const herf = await el.getAttribute('href') + expect(herf).toMatch(icoMatch) }) test('multiple urls on the same line', async () => { diff --git a/packages/playground/assets/favicon.ico b/packages/playground/assets/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..d75d248ef0b15096a95054643a4d97f5d9b60846 GIT binary patch literal 1150 zcmaKqSxA*Z7>4KMS_;~x;8i!JU{-`tpyx!I(n2NFMU)w2L{WiMS3#Lcp@vrpA*5Yp zSy6|`AkYfDD(e{`n8yX0pLf20X1@3RmKkGw`Vte3=0)aUq%ldx zm^49K+Hw0b#^`KboP)QXJOwbuVUFxlAs{RfqJ+twGylWfOp{Hc$s#253LlN1nsFVc zKa>40?h5(7PTC6ltDx)(Y&Ze2xggCq(kK? zTA`;gAfKD!+uFjpxc_A3+Ma(L28W=z4Gvs@r*ECk`;c45=S#;=oA|abt`f&j5&uJO z3Dn+&^gZ%h4JidsaTR{{!_Y8PUx(-%PosPy2gi@qIvBMMYz;e3L1{f~mrd9RdB>pZ zD}4R|sk_C`;=cT&r)c=8u>7h9)u32*SbL`xiq3(pq5C^5-sSOw;<|fv@nfXfl&U`2 z81K5ExDp;bf#DISW%IY%k&2-noShOoz-;kb(u?5RFX-ro?87j3GZdCXrFc8bTx}jd zz_n@djWnxc*TbbCjEq80FPyG}1zQwvjq7R6ZSWuQ@_#A*LN5n<3$BI?X}q%iD!B-s zdSFcNp!EgpJr6CAK?klug4>=)Tv z+F#{yt>6EK)3NU=L&y_W3UNaC?Tg=6YE0)^V;(0Mb0$WJ7>7@Lg0~+3x9d)!Pd + diff --git a/packages/vite/src/node/plugins/asset.ts b/packages/vite/src/node/plugins/asset.ts index 7ec3a01543f9a4..0b859183f6f8f3 100644 --- a/packages/vite/src/node/plugins/asset.ts +++ b/packages/vite/src/node/plugins/asset.ts @@ -33,6 +33,10 @@ const emittedHashMap = new WeakMap>() export function assetPlugin(config: ResolvedConfig): Plugin { // assetHashToFilenameMap initialization in buildStart causes getAssetFilename to return undefined assetHashToFilenameMap.set(config, new Map()) + + // add own dictionary entry by directly assigning mrmine + // https://github.com/lukeed/mrmime/issues/3 + mrmime.mimes['ico'] = 'image' return { name: 'vite:asset', From 1bd54caa098d81f6ecec1cee7e99a6a7ac7b7f88 Mon Sep 17 00:00:00 2001 From: "renovate[bot]" <29139614+renovate[bot]@users.noreply.github.com> Date: Mon, 28 Feb 2022 09:23:05 +0100 Subject: [PATCH 16/50] chore(deps): update actions/setup-node action to v3 (#7113) --- .github/workflows/ci.yml | 4 ++-- .github/workflows/publish.yml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3954ca49c11479..69ba432050fa7e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -44,7 +44,7 @@ jobs: version: 6 - name: Set node version to ${{ matrix.node_version }} - uses: actions/setup-node@v2 + uses: actions/setup-node@v3 with: node-version: ${{ matrix.node_version }} cache: "pnpm" @@ -82,7 +82,7 @@ jobs: version: 6 - name: Set node version to 16 - uses: actions/setup-node@v2 + uses: actions/setup-node@v3 with: node-version: 16 cache: "pnpm" diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index eb8f4513707872..9e22af5aed6ee8 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -23,7 +23,7 @@ jobs: version: 6 - name: Set node version to 16.x - uses: actions/setup-node@v2 + uses: actions/setup-node@v3 with: node-version: 16.x registry-url: https://registry.npmjs.org/ From 065ceca5c7b8f1843e220fbdbe8a0da4cbb78935 Mon Sep 17 00:00:00 2001 From: yoho <907415276@qq.com> Date: Mon, 28 Feb 2022 18:45:20 +0800 Subject: [PATCH 17/50] fix: image -> image/x-icon (#7120) --- packages/playground/assets/__tests__/assets.spec.ts | 2 +- packages/vite/src/node/plugins/asset.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/playground/assets/__tests__/assets.spec.ts b/packages/playground/assets/__tests__/assets.spec.ts index 8781fec785f3f2..8efbf1fcc2f082 100644 --- a/packages/playground/assets/__tests__/assets.spec.ts +++ b/packages/playground/assets/__tests__/assets.spec.ts @@ -121,7 +121,7 @@ describe('css url() references', () => { const match = isBuild ? `data:image/png;base64` : `/foo/nested/icon.png` expect(await getBg('.css-url-base64-inline')).toMatch(match) expect(await getBg('.css-url-quotes-base64-inline')).toMatch(match) - const icoMatch = isBuild ? `data:image;base64` : `favicon.ico` + const icoMatch = isBuild ? `data:image/x-icon;base64` : `favicon.ico` const el = await page.$(`link.ico`) const herf = await el.getAttribute('href') expect(herf).toMatch(icoMatch) diff --git a/packages/vite/src/node/plugins/asset.ts b/packages/vite/src/node/plugins/asset.ts index 0b859183f6f8f3..3c7681ad6f0bb8 100644 --- a/packages/vite/src/node/plugins/asset.ts +++ b/packages/vite/src/node/plugins/asset.ts @@ -36,7 +36,7 @@ export function assetPlugin(config: ResolvedConfig): Plugin { // add own dictionary entry by directly assigning mrmine // https://github.com/lukeed/mrmime/issues/3 - mrmime.mimes['ico'] = 'image' + mrmime.mimes['ico'] = 'image/x-icon' return { name: 'vite:asset', From 66cafca61c8eefe67c8371249a2fc6e043b91e43 Mon Sep 17 00:00:00 2001 From: Haoqun Jiang Date: Tue, 1 Mar 2022 00:06:04 +0800 Subject: [PATCH 18/50] fix(vue-jsx): support jsx imports with queries (#7121) --- packages/playground/vue-jsx/Query.jsx | 12 ++++++++++ .../vue-jsx/__tests__/vue-jsx.spec.ts | 3 +++ packages/playground/vue-jsx/main.jsx | 4 ++++ packages/playground/vue-jsx/vite.config.js | 23 ++++++++++++++++++- packages/plugin-vue-jsx/index.js | 7 ++++-- 5 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 packages/playground/vue-jsx/Query.jsx diff --git a/packages/playground/vue-jsx/Query.jsx b/packages/playground/vue-jsx/Query.jsx new file mode 100644 index 00000000000000..60de93eafb7b1c --- /dev/null +++ b/packages/playground/vue-jsx/Query.jsx @@ -0,0 +1,12 @@ +import { defineComponent, ref } from 'vue' + +export default defineComponent(() => { + const count = ref(6) + const inc = () => count.value++ + + return () => ( + + ) +}) diff --git a/packages/playground/vue-jsx/__tests__/vue-jsx.spec.ts b/packages/playground/vue-jsx/__tests__/vue-jsx.spec.ts index 64327e64a5f263..999fdc19af51ec 100644 --- a/packages/playground/vue-jsx/__tests__/vue-jsx.spec.ts +++ b/packages/playground/vue-jsx/__tests__/vue-jsx.spec.ts @@ -7,6 +7,7 @@ test('should render', async () => { expect(await page.textContent('.default-tsx')).toMatch('3') expect(await page.textContent('.script')).toMatch('4') expect(await page.textContent('.src-import')).toMatch('5') + expect(await page.textContent('.jsx-with-query')).toMatch('6') expect(await page.textContent('.other-ext')).toMatch('Other Ext') }) @@ -23,6 +24,8 @@ test('should update', async () => { expect(await page.textContent('.script')).toMatch('5') await page.click('.src-import') expect(await page.textContent('.src-import')).toMatch('6') + await page.click('.jsx-with-query') + expect(await page.textContent('.jsx-with-query')).toMatch('7') }) if (!isBuild) { diff --git a/packages/playground/vue-jsx/main.jsx b/packages/playground/vue-jsx/main.jsx index 1a792ea55d2a3c..e304e7788e49e7 100644 --- a/packages/playground/vue-jsx/main.jsx +++ b/packages/playground/vue-jsx/main.jsx @@ -5,6 +5,9 @@ import OtherExt from './OtherExt.tesx' import JsxScript from './Script.vue' import JsxSrcImport from './SrcImport.vue' import JsxSetupSyntax from './setup-syntax-jsx.vue' +// eslint-disable-next-line +import JsxWithQuery from './Query.jsx?query=true' + function App() { return ( <> @@ -16,6 +19,7 @@ function App() { + ) } diff --git a/packages/playground/vue-jsx/vite.config.js b/packages/playground/vue-jsx/vite.config.js index 3ec89a003d79f4..d6eb84e05f4e4a 100644 --- a/packages/playground/vue-jsx/vite.config.js +++ b/packages/playground/vue-jsx/vite.config.js @@ -9,7 +9,28 @@ module.exports = { vueJsxPlugin({ include: [/\.tesx$/, /\.[jt]sx$/] }), - vuePlugin() + vuePlugin(), + { + name: 'jsx-query-plugin', + transform(code, id) { + if (id.includes('?query=true')) { + return ` +import { createVNode as _createVNode } from "vue"; +import { defineComponent, ref } from 'vue'; +export default defineComponent(() => { + const count = ref(6); + + const inc = () => count.value++; + + return () => _createVNode("button", { + "class": "jsx-with-query", + "onClick": inc + }, [count.value]); +}); +` + } + } + } ], build: { // to make tests faster diff --git a/packages/plugin-vue-jsx/index.js b/packages/plugin-vue-jsx/index.js index 56c50fe2ff5e6d..248270765d19a1 100644 --- a/packages/plugin-vue-jsx/index.js +++ b/packages/plugin-vue-jsx/index.js @@ -95,10 +95,13 @@ function vueJsxPlugin(options = {}) { } = options const filter = createFilter(include || /\.[jt]sx$/, exclude) + const [filepath] = id.split('?') - if (filter(id)) { + // use id for script blocks in Vue SFCs (e.g. `App.vue?vue&type=script&lang.jsx`) + // use filepath for plain jsx files (e.g. App.jsx) + if (filter(id) || filter(filepath)) { const plugins = [importMeta, [jsx, babelPluginOptions], ...babelPlugins] - if (id.endsWith('.tsx')) { + if (id.endsWith('.tsx') || filepath.endsWith('.tsx')) { plugins.push([ require('@babel/plugin-transform-typescript'), // @ts-ignore From 22a0381e5e791296ffa7758331d95ae8ca870acd Mon Sep 17 00:00:00 2001 From: patak-dev Date: Mon, 28 Feb 2022 17:35:18 +0100 Subject: [PATCH 19/50] release: v2.8.5 --- packages/vite/CHANGELOG.md | 29 +++++++++++++++++++++++++++++ packages/vite/package.json | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/packages/vite/CHANGELOG.md b/packages/vite/CHANGELOG.md index c2f9a3f62ed9f0..711fdfb3026df8 100644 --- a/packages/vite/CHANGELOG.md +++ b/packages/vite/CHANGELOG.md @@ -1,3 +1,32 @@ +## [2.8.5](https://github.com/vitejs/vite/compare/v2.8.4...v2.8.5) (2022-02-28) + + +### Bug Fixes + +* ?html-proxy with trailing = added by some servers ([#7093](https://github.com/vitejs/vite/issues/7093)) ([5818ac9](https://github.com/vitejs/vite/commit/5818ac927861783ea2b05450761fed30f40e7399)) +* allow optional trailing comma in asset `import.meta.url` ([#6983](https://github.com/vitejs/vite/issues/6983)) ([2debb9f](https://github.com/vitejs/vite/commit/2debb9f4cbb6003e7d24444cf049b45582d82ff1)) +* cannot reassign process.env.NODE_ENV in ssr ([#6989](https://github.com/vitejs/vite/issues/6989)) ([983feb2](https://github.com/vitejs/vite/commit/983feb2cdc5180dc46c3f5fc5b99baaa8d6b7078)) +* **config:** Warn about terserOptions in more cases ([#7101](https://github.com/vitejs/vite/issues/7101)) ([79428ad](https://github.com/vitejs/vite/commit/79428ad5b849455e14f95d1b439ae296ba231221)) +* don't override user config ([#7034](https://github.com/vitejs/vite/issues/7034)) ([8fd8f6e](https://github.com/vitejs/vite/commit/8fd8f6e0e501c9e46bc3e179c900d31fa5cafce1)) +* fileToBuiltUrl got undefined when file type is `.ico` ([#7106](https://github.com/vitejs/vite/issues/7106)) ([7a1a552](https://github.com/vitejs/vite/commit/7a1a552ba710bad5714ef0fbb16fdd29ac58ae0b)) +* **glob:** css imports injecting a ?used query to export the css string ([#6949](https://github.com/vitejs/vite/issues/6949)) ([0b3f4ef](https://github.com/vitejs/vite/commit/0b3f4ef231004e072bf1b037f63bc4ef169d938e)) +* **hmr:** hmr style tag no support in html ([#7052](https://github.com/vitejs/vite/issues/7052)) ([a9dfce3](https://github.com/vitejs/vite/commit/a9dfce38108e796e0de0e3b43ced34d60883cef3)) +* image -> image/x-icon ([#7120](https://github.com/vitejs/vite/issues/7120)) ([065ceca](https://github.com/vitejs/vite/commit/065ceca5c7b8f1843e220fbdbe8a0da4cbb78935)) +* import with query with exports field ([#7073](https://github.com/vitejs/vite/issues/7073)) ([88ded7f](https://github.com/vitejs/vite/commit/88ded7f16382d83603511de043785e01ee1e4a3a)) +* prebundle dep with colon ([#7006](https://github.com/vitejs/vite/issues/7006)) ([2136f2b](https://github.com/vitejs/vite/commit/2136f2bb960d1a81ac3b3ca04d9ebd89dba44661)) +* recycle serve to avoid preventing Node self-exit ([#6895](https://github.com/vitejs/vite/issues/6895)) ([d6b2c53](https://github.com/vitejs/vite/commit/d6b2c53c6f0bcc4ffa9cdf48375f9bbcc98f79f7)) +* resolve [@import](https://github.com/import) of the proxied