From aebfd9c3c5e83557b3da0f320170edee12e52ec3 Mon Sep 17 00:00:00 2001 From: patak Date: Sun, 18 Sep 2022 09:57:47 +0200 Subject: [PATCH 1/9] docs: remove heroku instructions (#10155) --- docs/guide/static-deploy.md | 54 ------------------------------------- 1 file changed, 54 deletions(-) diff --git a/docs/guide/static-deploy.md b/docs/guide/static-deploy.md index 57b7ba4b538d45..35909a960d4565 100644 --- a/docs/guide/static-deploy.md +++ b/docs/guide/static-deploy.md @@ -276,60 +276,6 @@ You can also add custom domains and handle custom build settings on Pages. Learn You can also deploy to a [custom domain](http://surge.sh/help/adding-a-custom-domain) by adding `surge dist yourdomain.com`. -## Heroku - -1. Install [Heroku CLI](https://devcenter.heroku.com/articles/heroku-cli). - -2. Create a Heroku account by [signing up](https://signup.heroku.com). - -3. Run `heroku login` and fill in your Heroku credentials: - - ```bash - $ heroku login - ``` - -4. Create a file called `static.json` in the root of your project with the below content: - - `static.json`: - - ```json - { - "root": "./dist" - } - ``` - - This is the configuration of your site; read more at [heroku-buildpack-static](https://github.com/heroku/heroku-buildpack-static). - -5. Set up your Heroku git remote: - - ```bash - # version change - $ git init - $ git add . - $ git commit -m "My site ready for deployment." - - # creates a new app with a specified name - $ heroku apps:create example - ``` - -6. Set buildpacks. We use `heroku/nodejs` to build the project and `heroku-buildpack-static` to serve it. - - ```bash - # set buildpacks - $ heroku buildpacks:set heroku/nodejs - $ heroku buildpacks:add https://github.com/heroku/heroku-buildpack-static.git - ``` - -7. Deploy your site: - - ```bash - # publish site - $ git push heroku main - - # opens a browser to view the Dashboard version of Heroku CI - $ heroku open - ``` - ## Azure Static Web Apps You can quickly deploy your Vite app with Microsoft Azure [Static Web Apps](https://aka.ms/staticwebapps) service. You need: From fc5310f3e3f500412aaf5e45a5871088a662d0e5 Mon Sep 17 00:00:00 2001 From: patak Date: Sun, 18 Sep 2022 11:41:51 +0200 Subject: [PATCH 2/9] fix: esbuildOutputFromId for symlinked root (#10154) --- packages/vite/src/node/optimizer/index.ts | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index 891d2dd709833c..eb80be2c3744a4 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -911,12 +911,22 @@ function esbuildOutputFromId( id: string, cacheDirOutputPath: string ): any { + const cwd = process.cwd() const flatId = flattenId(id) + '.js' - return outputs[ - normalizePath( - path.relative(process.cwd(), path.join(cacheDirOutputPath, flatId)) - ) - ] + const normalizedOutputPath = normalizePath( + path.relative(cwd, path.join(cacheDirOutputPath, flatId)) + ) + const output = outputs[normalizedOutputPath] + if (output) { + return output + } + // If the root dir was symlinked, esbuild could return output keys as `../cwd/` + // Normalize keys to support this case too + for (const [key, value] of Object.entries(outputs)) { + if (normalizePath(path.relative(cwd, key)) === normalizedOutputPath) { + return value + } + } } export async function extractExportsData( From 414d2ef6e53a8030be68388952f28e1b42d2bb8e Mon Sep 17 00:00:00 2001 From: chlorine Date: Sun, 18 Sep 2022 21:46:51 +0800 Subject: [PATCH 3/9] chore(create-vite): remove sourcemap, move --noEmit to tsconfig (#10150) --- packages/create-vite/template-vanilla-ts/tsconfig.json | 1 - packages/create-vite/template-vue-ts/package.json | 2 +- packages/create-vite/template-vue-ts/tsconfig.json | 4 ++-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/create-vite/template-vanilla-ts/tsconfig.json b/packages/create-vite/template-vanilla-ts/tsconfig.json index fbd022532d3096..eac16d14a6f2e2 100644 --- a/packages/create-vite/template-vanilla-ts/tsconfig.json +++ b/packages/create-vite/template-vanilla-ts/tsconfig.json @@ -6,7 +6,6 @@ "lib": ["ESNext", "DOM"], "moduleResolution": "Node", "strict": true, - "sourceMap": true, "resolveJsonModule": true, "isolatedModules": true, "esModuleInterop": true, diff --git a/packages/create-vite/template-vue-ts/package.json b/packages/create-vite/template-vue-ts/package.json index 02b6d15a989b4d..4c01d8a80bc384 100644 --- a/packages/create-vite/template-vue-ts/package.json +++ b/packages/create-vite/template-vue-ts/package.json @@ -5,7 +5,7 @@ "type": "module", "scripts": { "dev": "vite", - "build": "vue-tsc --noEmit && vite build", + "build": "vue-tsc && vite build", "preview": "vite preview" }, "dependencies": { diff --git a/packages/create-vite/template-vue-ts/tsconfig.json b/packages/create-vite/template-vue-ts/tsconfig.json index d4aefa2c929dc2..b557c4047cac64 100644 --- a/packages/create-vite/template-vue-ts/tsconfig.json +++ b/packages/create-vite/template-vue-ts/tsconfig.json @@ -6,12 +6,12 @@ "moduleResolution": "Node", "strict": true, "jsx": "preserve", - "sourceMap": true, "resolveJsonModule": true, "isolatedModules": true, "esModuleInterop": true, "lib": ["ESNext", "DOM"], - "skipLibCheck": true + "skipLibCheck": true, + "noEmit": true }, "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"], "references": [{ "path": "./tsconfig.node.json" }] From a13a7eb4165d38ce0ab6eefd4e4d38104ce63699 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Sun, 18 Sep 2022 23:09:43 +0900 Subject: [PATCH 4/9] fix(plugin-legacy): force set `build.target` (#10072) --- packages/plugin-legacy/build.config.ts | 3 ++- packages/plugin-legacy/package.json | 1 + packages/plugin-legacy/src/index.ts | 23 +++++++++++++++++++++++ pnpm-lock.yaml | 2 ++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/plugin-legacy/build.config.ts b/packages/plugin-legacy/build.config.ts index 6dcf1a5a2dc0b3..12afbbc54d1efd 100644 --- a/packages/plugin-legacy/build.config.ts +++ b/packages/plugin-legacy/build.config.ts @@ -5,6 +5,7 @@ export default defineBuildConfig({ clean: true, declaration: true, rollup: { - emitCJS: true + emitCJS: true, + inlineDependencies: true } }) diff --git a/packages/plugin-legacy/package.json b/packages/plugin-legacy/package.json index 6ef18878d2e080..077e3604151b8d 100644 --- a/packages/plugin-legacy/package.json +++ b/packages/plugin-legacy/package.json @@ -47,6 +47,7 @@ }, "devDependencies": { "@babel/core": "^7.19.0", + "picocolors": "^1.0.0", "vite": "workspace:*" } } diff --git a/packages/plugin-legacy/src/index.ts b/packages/plugin-legacy/src/index.ts index 4d51d1cecaf493..1e9850f8127b9f 100644 --- a/packages/plugin-legacy/src/index.ts +++ b/packages/plugin-legacy/src/index.ts @@ -19,6 +19,7 @@ import type { RenderedChunk } from 'rollup' import type { PluginItem as BabelPlugin } from '@babel/core' +import colors from 'picocolors' import type { Options } from './types' // lazy load babel since it's not used during dev @@ -156,6 +157,7 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] { }) } + let overriddenBuildTarget = false const legacyConfigPlugin: Plugin = { name: 'vite:legacy-config', @@ -173,6 +175,18 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] { // So targeting `chrome61` suffices to fix the compatibility issue. config.build.cssTarget = 'chrome61' } + + // Vite's default target browsers are **not** the same. + // See https://github.com/vitejs/vite/pull/10052#issuecomment-1242076461 + overriddenBuildTarget = config.build.target !== undefined + // browsers supporting ESM + dynamic import + import.meta + config.build.target = [ + 'es2020', + 'edge79', + 'firefox67', + 'chrome64', + 'safari11.1' + ] } return { @@ -183,6 +197,15 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] { : legacyEnvVarMarker } } + }, + configResolved(config) { + if (overriddenBuildTarget) { + config.logger.warn( + colors.yellow( + `plugin-legacy overrode 'build.target'. You should pass 'targets' as an option to this plugin with the list of legacy browsers to support instead.` + ) + ) + } } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c13b7b003ad21e..837648b0bc6362 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -138,6 +138,7 @@ importers: '@babel/standalone': ^7.19.0 core-js: ^3.25.1 magic-string: ^0.26.3 + picocolors: ^1.0.0 regenerator-runtime: ^0.13.9 systemjs: ^6.12.6 vite: workspace:* @@ -149,6 +150,7 @@ importers: systemjs: 6.12.6 devDependencies: '@babel/core': 7.19.0 + picocolors: 1.0.0 vite: link:../vite packages/plugin-react: From 62ff7887870392f0cce2a40b3cc5d1b7c48a9a47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Sun, 18 Sep 2022 23:53:52 +0900 Subject: [PATCH 5/9] docs(plugin-legacy): fix Vite default target (#10158) --- packages/plugin-legacy/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/plugin-legacy/README.md b/packages/plugin-legacy/README.md index b5c3631ab2ac60..708ba6059561f1 100644 --- a/packages/plugin-legacy/README.md +++ b/packages/plugin-legacy/README.md @@ -1,6 +1,6 @@ # @vitejs/plugin-legacy [![npm](https://img.shields.io/npm/v/@vitejs/plugin-legacy.svg)](https://npmjs.com/package/@vitejs/plugin-legacy) -Vite's default browser support baseline is [Native ESM](https://caniuse.com/es6-module). This plugin provides support for legacy browsers that do not support native ESM when building for production. +Vite's default browser support baseline is [Native ESM](https://caniuse.com/es6-module), [native ESM dynamic import](https://caniuse.com/es6-module-dynamic-import), and [`import.meta`](https://caniuse.com/mdn-javascript_operators_import_meta). This plugin provides support for legacy browsers that do not support those features when building for production. By default, this plugin will: From 4c8aa80f325700866765cf3ced8445d08d6f58ca Mon Sep 17 00:00:00 2001 From: patak-dev Date: Mon, 19 Sep 2022 10:18:39 +0200 Subject: [PATCH 6/9] release: plugin-legacy@2.2.0 --- packages/plugin-legacy/CHANGELOG.md | 9 +++++++++ packages/plugin-legacy/package.json | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/plugin-legacy/CHANGELOG.md b/packages/plugin-legacy/CHANGELOG.md index 61c7e6f4a1386c..3bf33a1488de08 100644 --- a/packages/plugin-legacy/CHANGELOG.md +++ b/packages/plugin-legacy/CHANGELOG.md @@ -1,3 +1,12 @@ +## 2.2.0 (2022-09-19) + +* docs(plugin-legacy): fix Vite default target (#10158) ([62ff788](https://github.com/vitejs/vite/commit/62ff788)), closes [#10158](https://github.com/vitejs/vite/issues/10158) +* fix(deps): update all non-major dependencies (#10077) ([caf00c8](https://github.com/vitejs/vite/commit/caf00c8)), closes [#10077](https://github.com/vitejs/vite/issues/10077) +* fix(deps): update all non-major dependencies (#9985) ([855f2f0](https://github.com/vitejs/vite/commit/855f2f0)), closes [#9985](https://github.com/vitejs/vite/issues/9985) +* fix(plugin-legacy): force set `build.target` (#10072) ([a13a7eb](https://github.com/vitejs/vite/commit/a13a7eb)), closes [#10072](https://github.com/vitejs/vite/issues/10072) + + + ## 2.1.0 (2022-09-05) diff --git a/packages/plugin-legacy/package.json b/packages/plugin-legacy/package.json index 077e3604151b8d..99b1a36f843e5b 100644 --- a/packages/plugin-legacy/package.json +++ b/packages/plugin-legacy/package.json @@ -1,6 +1,6 @@ { "name": "@vitejs/plugin-legacy", - "version": "2.1.0", + "version": "2.2.0", "license": "MIT", "author": "Evan You", "files": [ From c948e7d7fc5ad62262b24f89694e2b95a573a16d Mon Sep 17 00:00:00 2001 From: lsdsjy Date: Mon, 19 Sep 2022 18:19:37 +0800 Subject: [PATCH 7/9] fix(lib): respect `rollupOptions.input` in lib mode (#10116) --- packages/vite/src/node/build.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/build.ts b/packages/vite/src/node/build.ts index 21cdce31180e5d..2d279bce3d8795 100644 --- a/packages/vite/src/node/build.ts +++ b/packages/vite/src/node/build.ts @@ -373,7 +373,7 @@ async function doBuild( const resolve = (p: string) => path.resolve(config.root, p) const input = libOptions - ? resolve(libOptions.entry) + ? options.rollupOptions?.input || resolve(libOptions.entry) : typeof options.ssr === 'string' ? resolve(options.ssr) : options.rollupOptions?.input || resolve('index.html') From 71f08e766ca2fd1eb7e1913f86b14796fc6df93f Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Mon, 19 Sep 2022 20:34:50 +0800 Subject: [PATCH 8/9] fix(hmr): dedupe virtual modules in module graph (#10144) --- .../vite/src/node/plugins/importAnalysis.ts | 25 ++++++++----------- .../src/node/server/middlewares/indexHtml.ts | 12 +++------ packages/vite/src/node/ssr/ssrModuleLoader.ts | 5 ++-- packages/vite/src/node/utils.ts | 21 +++++++++++++--- playground/hmr/__tests__/hmr.spec.ts | 11 ++++++++ playground/hmr/hmr.ts | 3 +++ playground/hmr/importedVirtual.js | 1 + playground/hmr/index.html | 1 + playground/hmr/vite.config.ts | 13 ++++++++++ .../ssr-html/__tests__/ssr-html.spec.ts | 18 ++++++++++++- playground/ssr-html/index.html | 1 + playground/ssr-html/server.js | 17 ++++++++++++- playground/ssr-html/src/app.js | 8 ++++++ playground/ssr-html/src/importedVirtual.js | 1 + 14 files changed, 106 insertions(+), 31 deletions(-) create mode 100644 playground/hmr/importedVirtual.js create mode 100644 playground/ssr-html/src/importedVirtual.js diff --git a/packages/vite/src/node/plugins/importAnalysis.ts b/packages/vite/src/node/plugins/importAnalysis.ts index 80204c17a65091..86bad0b75ac001 100644 --- a/packages/vite/src/node/plugins/importAnalysis.ts +++ b/packages/vite/src/node/plugins/importAnalysis.ts @@ -15,9 +15,7 @@ import { CLIENT_DIR, CLIENT_PUBLIC_PATH, DEP_VERSION_RE, - FS_PREFIX, - NULL_BYTE_PLACEHOLDER, - VALID_ID_PREFIX + FS_PREFIX } from '../constants' import { debugHmr, @@ -42,7 +40,8 @@ import { stripBomTag, timeFrom, transformStableResult, - unwrapId + unwrapId, + wrapId } from '../utils' import type { ResolvedConfig } from '../config' import type { Plugin } from '../plugin' @@ -330,8 +329,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { // prefix it to make it valid. We will strip this before feeding it // back into the transform pipeline if (!url.startsWith('.') && !url.startsWith('/')) { - url = - VALID_ID_PREFIX + resolved.id.replace('\0', NULL_BYTE_PLACEHOLDER) + url = wrapId(resolved.id) } // make the URL browser-valid if not SSR @@ -361,7 +359,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { try { // delay setting `isSelfAccepting` until the file is actually used (#7870) const depModule = await moduleGraph.ensureEntryFromUrl( - url, + unwrapId(url), ssr, canSkipImportAnalysis(url) ) @@ -536,9 +534,9 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { } // record for HMR import chain analysis - // make sure to normalize away base - const urlWithoutBase = url.replace(base, '/') - importedUrls.add(urlWithoutBase) + // make sure to unwrap and normalize away base + const hmrUrl = unwrapId(url.replace(base, '/')) + importedUrls.add(hmrUrl) if (enablePartialAccept && importedBindings) { extractImportedBindings( @@ -551,7 +549,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { if (!isDynamicImport) { // for pre-transforming - staticImportedUrls.add({ url: urlWithoutBase, id: resolvedId }) + staticImportedUrls.add({ url: hmrUrl, id: resolvedId }) } } else if (!importer.startsWith(clientDir)) { if (!importer.includes('node_modules')) { @@ -712,10 +710,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { // by the deps optimizer if (config.server.preTransformRequests && staticImportedUrls.size) { staticImportedUrls.forEach(({ url, id }) => { - url = unwrapId(removeImportQuery(url)).replace( - NULL_BYTE_PLACEHOLDER, - '\0' - ) + url = removeImportQuery(url) transformRequest(url, server, { ssr }).catch((e) => { if (e?.code === ERR_OUTDATED_OPTIMIZED_DEP) { // This are expected errors diff --git a/packages/vite/src/node/server/middlewares/indexHtml.ts b/packages/vite/src/node/server/middlewares/indexHtml.ts index 3206734862baf0..1fc921573c0cbe 100644 --- a/packages/vite/src/node/server/middlewares/indexHtml.ts +++ b/packages/vite/src/node/server/middlewares/indexHtml.ts @@ -19,19 +19,15 @@ import { } from '../../plugins/html' import type { ResolvedConfig, ViteDevServer } from '../..' import { send } from '../send' -import { - CLIENT_PUBLIC_PATH, - FS_PREFIX, - NULL_BYTE_PLACEHOLDER, - VALID_ID_PREFIX -} from '../../constants' +import { CLIENT_PUBLIC_PATH, FS_PREFIX } from '../../constants' import { cleanUrl, ensureWatchedFile, fsPathFromId, injectQuery, normalizePath, - processSrcSetSync + processSrcSetSync, + wrapId } from '../../utils' import type { ModuleGraph } from '../moduleGraph' @@ -144,7 +140,7 @@ const devHtmlHook: IndexHtmlTransformHook = async ( // and ids are properly handled const validPath = `${htmlPath}${trailingSlash ? 'index.html' : ''}` proxyModulePath = `\0${validPath}` - proxyModuleUrl = `${VALID_ID_PREFIX}${NULL_BYTE_PLACEHOLDER}${validPath}` + proxyModuleUrl = wrapId(proxyModulePath) } const s = new MagicString(html) diff --git a/packages/vite/src/node/ssr/ssrModuleLoader.ts b/packages/vite/src/node/ssr/ssrModuleLoader.ts index 8f61125c134d8c..c3e4a4f6253744 100644 --- a/packages/vite/src/node/ssr/ssrModuleLoader.ts +++ b/packages/vite/src/node/ssr/ssrModuleLoader.ts @@ -12,7 +12,6 @@ import { transformRequest } from '../server/transformRequest' import type { InternalResolveOptions } from '../plugins/resolve' import { tryNodeResolve } from '../plugins/resolve' import { hookNodeResolve } from '../plugins/ssrRequireHook' -import { NULL_BYTE_PLACEHOLDER } from '../constants' import { ssrDynamicImportKey, ssrExportAllKey, @@ -38,7 +37,7 @@ export async function ssrLoadModule( urlStack: string[] = [], fixStacktrace?: boolean ): Promise { - url = unwrapId(url).replace(NULL_BYTE_PLACEHOLDER, '\0') + url = unwrapId(url) // when we instantiate multiple dependency modules in parallel, they may // point to shared modules. We need to avoid duplicate instantiation attempts @@ -138,7 +137,7 @@ async function instantiateModule( return nodeImport(dep, mod.file!, resolveOptions) } // convert to rollup URL because `pendingImports`, `moduleGraph.urlToModuleMap` requires that - dep = unwrapId(dep).replace(NULL_BYTE_PLACEHOLDER, '\0') + dep = unwrapId(dep) if (!isCircular(dep) && !pendingImports.get(dep)?.some(isCircular)) { pendingDeps.push(dep) if (pendingDeps.length === 1) { diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index d63691f3cdc922..032ec10da8dabe 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -25,6 +25,7 @@ import { DEFAULT_EXTENSIONS, ENV_PUBLIC_PATH, FS_PREFIX, + NULL_BYTE_PLACEHOLDER, OPTIMIZABLE_ENTRY_RE, VALID_ID_PREFIX, loopbackHosts, @@ -53,10 +54,24 @@ export function slash(p: string): string { return p.replace(/\\/g, '/') } -// Strip valid id prefix. This is prepended to resolved Ids that are -// not valid browser import specifiers by the importAnalysis plugin. +/** + * Prepend `/@id/` and replace null byte so the id is URL-safe. + * This is prepended to resolved ids that are not valid browser + * import specifiers by the importAnalysis plugin. + */ +export function wrapId(id: string): string { + return id.startsWith(VALID_ID_PREFIX) + ? id + : VALID_ID_PREFIX + id.replace('\0', NULL_BYTE_PLACEHOLDER) +} + +/** + * Undo {@link wrapId}'s `/@id/` and null byte replacements. + */ export function unwrapId(id: string): string { - return id.startsWith(VALID_ID_PREFIX) ? id.slice(VALID_ID_PREFIX.length) : id + return id.startsWith(VALID_ID_PREFIX) + ? id.slice(VALID_ID_PREFIX.length).replace(NULL_BYTE_PLACEHOLDER, '\0') + : id } export const flattenId = (id: string): string => diff --git a/playground/hmr/__tests__/hmr.spec.ts b/playground/hmr/__tests__/hmr.spec.ts index 3858719b772a37..ef8def29a389a5 100644 --- a/playground/hmr/__tests__/hmr.spec.ts +++ b/playground/hmr/__tests__/hmr.spec.ts @@ -627,4 +627,15 @@ if (!isBuild) { btn = await page.$('button') expect(await btn.textContent()).toBe('Compteur 0') }) + + test('handle virtual module updates', async () => { + await page.goto(viteTestUrl) + const el = await page.$('.virtual') + expect(await el.textContent()).toBe('[success]') + editFile('importedVirtual.js', (code) => code.replace('[success]', '[wow]')) + await untilUpdated(async () => { + const el = await page.$('.virtual') + return await el.textContent() + }, '[wow]') + }) } diff --git a/playground/hmr/hmr.ts b/playground/hmr/hmr.ts index 97330f05f29f64..dc3c22eac9d56e 100644 --- a/playground/hmr/hmr.ts +++ b/playground/hmr/hmr.ts @@ -1,3 +1,5 @@ +// @ts-ignore +import { virtual } from 'virtual:file' import { foo as depFoo, nestedFoo } from './hmrDep' import './importing-updated' @@ -5,6 +7,7 @@ export const foo = 1 text('.app', foo) text('.dep', depFoo) text('.nested', nestedFoo) +text('.virtual', virtual) if (import.meta.hot) { import.meta.hot.accept(({ foo }) => { diff --git a/playground/hmr/importedVirtual.js b/playground/hmr/importedVirtual.js new file mode 100644 index 00000000000000..8b0b417bc3113d --- /dev/null +++ b/playground/hmr/importedVirtual.js @@ -0,0 +1 @@ +export const virtual = '[success]' diff --git a/playground/hmr/index.html b/playground/hmr/index.html index aafeaea5b565d4..28f08014036ade 100644 --- a/playground/hmr/index.html +++ b/playground/hmr/index.html @@ -19,6 +19,7 @@
+
diff --git a/playground/hmr/vite.config.ts b/playground/hmr/vite.config.ts index 2ee03c28228ade..d68c0ed84e7135 100644 --- a/playground/hmr/vite.config.ts +++ b/playground/hmr/vite.config.ts @@ -19,6 +19,19 @@ export default defineConfig({ client.send('custom:remote-add-result', { result: a + b }) }) } + }, + { + name: 'virtual-file', + resolveId(id) { + if (id === 'virtual:file') { + return '\0virtual:file' + } + }, + load(id) { + if (id === '\0virtual:file') { + return 'import { virtual } from "/importedVirtual.js"; export { virtual };' + } + } } ] }) diff --git a/playground/ssr-html/__tests__/ssr-html.spec.ts b/playground/ssr-html/__tests__/ssr-html.spec.ts index f29b0ac33cf5d8..94e64bdb5d8b8b 100644 --- a/playground/ssr-html/__tests__/ssr-html.spec.ts +++ b/playground/ssr-html/__tests__/ssr-html.spec.ts @@ -1,7 +1,7 @@ import fetch from 'node-fetch' import { describe, expect, test } from 'vitest' import { port } from './serve' -import { page } from '~utils' +import { editFile, isServe, page, untilUpdated } from '~utils' const url = `http://localhost:${port}` @@ -39,3 +39,19 @@ describe('injected inline scripts', () => { } }) }) + +describe.runIf(isServe)('hmr', () => { + test('handle virtual module updates', async () => { + await page.goto(url) + const el = await page.$('.virtual') + expect(await el.textContent()).toBe('[success]') + editFile('src/importedVirtual.js', (code) => + code.replace('[success]', '[wow]') + ) + await page.waitForNavigation() + await untilUpdated(async () => { + const el = await page.$('.virtual') + return await el.textContent() + }, '[wow]') + }) +}) diff --git a/playground/ssr-html/index.html b/playground/ssr-html/index.html index 995c828caae1a8..cca83257565a95 100644 --- a/playground/ssr-html/index.html +++ b/playground/ssr-html/index.html @@ -12,5 +12,6 @@

SSR Dynamic HTML

+
diff --git a/playground/ssr-html/server.js b/playground/ssr-html/server.js index 6c6ddca9addc1d..7549fbf07cc388 100644 --- a/playground/ssr-html/server.js +++ b/playground/ssr-html/server.js @@ -48,7 +48,22 @@ export async function createServer(root = process.cwd(), hmrPort) { port: hmrPort } }, - appType: 'custom' + appType: 'custom', + plugins: [ + { + name: 'virtual-file', + resolveId(id) { + if (id === 'virtual:file') { + return '\0virtual:file' + } + }, + load(id) { + if (id === '\0virtual:file') { + return 'import { virtual } from "/src/importedVirtual.js"; export { virtual };' + } + } + } + ] }) // use vite's connect instance as middleware app.use(vite.middlewares) diff --git a/playground/ssr-html/src/app.js b/playground/ssr-html/src/app.js index 5b0175bb863d70..8612afffaea5ba 100644 --- a/playground/ssr-html/src/app.js +++ b/playground/ssr-html/src/app.js @@ -1,3 +1,11 @@ +import { virtual } from 'virtual:file' + const p = document.createElement('p') p.innerHTML = '✅ Dynamically injected script from file' document.body.appendChild(p) + +text('.virtual', virtual) + +function text(el, text) { + document.querySelector(el).textContent = text +} diff --git a/playground/ssr-html/src/importedVirtual.js b/playground/ssr-html/src/importedVirtual.js new file mode 100644 index 00000000000000..8b0b417bc3113d --- /dev/null +++ b/playground/ssr-html/src/importedVirtual.js @@ -0,0 +1 @@ +export const virtual = '[success]' From dfa22ca52de8025c5590b05ecf28ef09b32e3afe Mon Sep 17 00:00:00 2001 From: patak-dev Date: Mon, 19 Sep 2022 14:37:27 +0200 Subject: [PATCH 9/9] release: v3.1.3 --- packages/vite/CHANGELOG.md | 8 ++++++++ packages/vite/package.json | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/vite/CHANGELOG.md b/packages/vite/CHANGELOG.md index eaac863360feb9..12250b4fd3826f 100644 --- a/packages/vite/CHANGELOG.md +++ b/packages/vite/CHANGELOG.md @@ -1,3 +1,11 @@ +## 3.1.3 (2022-09-19) + +* fix: esbuildOutputFromId for symlinked root (#10154) ([fc5310f](https://github.com/vitejs/vite/commit/fc5310f)), closes [#10154](https://github.com/vitejs/vite/issues/10154) +* fix(hmr): dedupe virtual modules in module graph (#10144) ([71f08e7](https://github.com/vitejs/vite/commit/71f08e7)), closes [#10144](https://github.com/vitejs/vite/issues/10144) +* fix(lib): respect `rollupOptions.input` in lib mode (#10116) ([c948e7d](https://github.com/vitejs/vite/commit/c948e7d)), closes [#10116](https://github.com/vitejs/vite/issues/10116) + + + ## 3.1.2 (2022-09-17) * fix: use isOptimizable to ensure version query (#10141) ([23a51c6](https://github.com/vitejs/vite/commit/23a51c6)), closes [#10141](https://github.com/vitejs/vite/issues/10141) diff --git a/packages/vite/package.json b/packages/vite/package.json index 0a4ea3aceb5245..a4cdc9e83c4482 100644 --- a/packages/vite/package.json +++ b/packages/vite/package.json @@ -1,6 +1,6 @@ { "name": "vite", - "version": "3.1.2", + "version": "3.1.3", "type": "module", "license": "MIT", "author": "Evan You",