From 34db08bb8aab61f63432122afdc9762a181b1fda Mon Sep 17 00:00:00 2001 From: Em Zhan Date: Wed, 16 Nov 2022 07:36:09 -0600 Subject: [PATCH] fix: make `addWatchFile()` work (fix #7024) (#9723) Co-authored-by: Matt Jones --- .../vite/src/node/plugins/importAnalysis.ts | 9 ++++--- .../__tests__/transform-plugin.spec.ts | 9 +++++++ playground/transform-plugin/index.html | 3 +++ playground/transform-plugin/index.js | 2 ++ playground/transform-plugin/package.json | 11 ++++++++ playground/transform-plugin/plugin-dep.js | 1 + playground/transform-plugin/vite.config.js | 25 +++++++++++++++++++ 7 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 playground/transform-plugin/__tests__/transform-plugin.spec.ts create mode 100644 playground/transform-plugin/index.html create mode 100644 playground/transform-plugin/index.js create mode 100644 playground/transform-plugin/package.json create mode 100644 playground/transform-plugin/plugin-dep.js create mode 100644 playground/transform-plugin/vite.config.js diff --git a/packages/vite/src/node/plugins/importAnalysis.ts b/packages/vite/src/node/plugins/importAnalysis.ts index 9dd0b028226396..8162f621988630 100644 --- a/packages/vite/src/node/plugins/importAnalysis.ts +++ b/packages/vite/src/node/plugins/importAnalysis.ts @@ -231,7 +231,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { throwOutdatedRequest(importer) } - if (!imports.length) { + if (!imports.length && !(this as any)._addedImports) { importerModule.isSelfAccepting = false isDebug && debug( @@ -263,7 +263,8 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { const normalizeUrl = async ( url: string, - pos: number + pos: number, + forceSkipImportAnalysis: boolean = false ): Promise<[string, string]> => { url = stripBase(url, base) @@ -364,7 +365,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { const depModule = await moduleGraph.ensureEntryFromUrl( unwrapId(url), ssr, - canSkipImportAnalysis(url) + canSkipImportAnalysis(url) || forceSkipImportAnalysis ) if (depModule.lastHMRTimestamp > 0) { url = injectQuery(url, `t=${depModule.lastHMRTimestamp}`) @@ -667,7 +668,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { if (pluginImports) { ;( await Promise.all( - [...pluginImports].map((id) => normalizeUrl(id, 0)) + [...pluginImports].map((id) => normalizeUrl(id, 0, true)) ) ).forEach(([url]) => importedUrls.add(url)) } diff --git a/playground/transform-plugin/__tests__/transform-plugin.spec.ts b/playground/transform-plugin/__tests__/transform-plugin.spec.ts new file mode 100644 index 00000000000000..2a56add2d62bae --- /dev/null +++ b/playground/transform-plugin/__tests__/transform-plugin.spec.ts @@ -0,0 +1,9 @@ +import { expect, test } from 'vitest' +import { editFile, page, untilUpdated } from '~utils' + +test('should re-run transform when plugin-dep file is edited', async () => { + expect(await page.textContent('#transform-count')).toBe('1') + + await editFile('plugin-dep.js', (str) => str) + await untilUpdated(() => page.textContent('#transform-count'), '2') +}) diff --git a/playground/transform-plugin/index.html b/playground/transform-plugin/index.html new file mode 100644 index 00000000000000..83450ae9ed2350 --- /dev/null +++ b/playground/transform-plugin/index.html @@ -0,0 +1,3 @@ +
+ + diff --git a/playground/transform-plugin/index.js b/playground/transform-plugin/index.js new file mode 100644 index 00000000000000..ac3da49609d222 --- /dev/null +++ b/playground/transform-plugin/index.js @@ -0,0 +1,2 @@ +// 'TRANSFORM_COUNT' is injected by the transform plugin +document.getElementById('transform-count').innerHTML = TRANSFORM_COUNT diff --git a/playground/transform-plugin/package.json b/playground/transform-plugin/package.json new file mode 100644 index 00000000000000..a1e12d4aa62a26 --- /dev/null +++ b/playground/transform-plugin/package.json @@ -0,0 +1,11 @@ +{ + "name": "test-transform-plugin", + "private": true, + "version": "0.0.0", + "scripts": { + "dev": "vite", + "build": "vite build", + "debug": "node --inspect-brk ../../vite/bin/vite", + "serve": "vite preview" + } +} diff --git a/playground/transform-plugin/plugin-dep.js b/playground/transform-plugin/plugin-dep.js new file mode 100644 index 00000000000000..266703d8da05b4 --- /dev/null +++ b/playground/transform-plugin/plugin-dep.js @@ -0,0 +1 @@ +// Empty file for detecting changes in tests diff --git a/playground/transform-plugin/vite.config.js b/playground/transform-plugin/vite.config.js new file mode 100644 index 00000000000000..0b9ddb97c34304 --- /dev/null +++ b/playground/transform-plugin/vite.config.js @@ -0,0 +1,25 @@ +const { resolve } = require('node:path') +const { normalizePath } = require('vite') + +let transformCount = 1 + +const transformPlugin = { + name: 'transform', + transform(code, id) { + if (id === normalizePath(resolve(__dirname, 'index.js'))) { + // Ensure `index.js` is reevaluated if 'plugin-dep.js' is changed + this.addWatchFile('./plugin-dep.js') + + return ` + // Inject TRANSFORM_COUNT + let TRANSFORM_COUNT = ${transformCount++}; + + ${code} + ` + } + } +} + +module.exports = { + plugins: [transformPlugin] +}