From f1647b18e088f99cc7157a1936c68f297af96725 Mon Sep 17 00:00:00 2001 From: Matt Jones Date: Sun, 1 Aug 2021 18:05:54 +1000 Subject: [PATCH 1/6] test: add tests --- .../__tests__/transform-plugin.spec.ts | 8 +++++++ .../playground/transform-plugin/index.html | 3 +++ packages/playground/transform-plugin/index.js | 3 +++ .../playground/transform-plugin/package.json | 11 ++++++++++ .../playground/transform-plugin/plugin-dep.js | 1 + .../transform-plugin/vite.config.js | 22 +++++++++++++++++++ 6 files changed, 48 insertions(+) create mode 100644 packages/playground/transform-plugin/__tests__/transform-plugin.spec.ts create mode 100644 packages/playground/transform-plugin/index.html create mode 100644 packages/playground/transform-plugin/index.js create mode 100644 packages/playground/transform-plugin/package.json create mode 100644 packages/playground/transform-plugin/plugin-dep.js create mode 100644 packages/playground/transform-plugin/vite.config.js diff --git a/packages/playground/transform-plugin/__tests__/transform-plugin.spec.ts b/packages/playground/transform-plugin/__tests__/transform-plugin.spec.ts new file mode 100644 index 00000000000000..cea0e082de2baf --- /dev/null +++ b/packages/playground/transform-plugin/__tests__/transform-plugin.spec.ts @@ -0,0 +1,8 @@ +import { editFile, untilUpdated } from '../../testUtils' + +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/packages/playground/transform-plugin/index.html b/packages/playground/transform-plugin/index.html new file mode 100644 index 00000000000000..83450ae9ed2350 --- /dev/null +++ b/packages/playground/transform-plugin/index.html @@ -0,0 +1,3 @@ +
+ + diff --git a/packages/playground/transform-plugin/index.js b/packages/playground/transform-plugin/index.js new file mode 100644 index 00000000000000..8e71251af61c5b --- /dev/null +++ b/packages/playground/transform-plugin/index.js @@ -0,0 +1,3 @@ +// 'TRANSFORM_COUNT' is injected by the transform plugin +// eslint-disable-next-line no-undef +document.getElementById('transform-count').innerHTML = TRANSFORM_COUNT diff --git a/packages/playground/transform-plugin/package.json b/packages/playground/transform-plugin/package.json new file mode 100644 index 00000000000000..a1e12d4aa62a26 --- /dev/null +++ b/packages/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/packages/playground/transform-plugin/plugin-dep.js b/packages/playground/transform-plugin/plugin-dep.js new file mode 100644 index 00000000000000..266703d8da05b4 --- /dev/null +++ b/packages/playground/transform-plugin/plugin-dep.js @@ -0,0 +1 @@ +// Empty file for detecting changes in tests diff --git a/packages/playground/transform-plugin/vite.config.js b/packages/playground/transform-plugin/vite.config.js new file mode 100644 index 00000000000000..5aec6ca477f4a9 --- /dev/null +++ b/packages/playground/transform-plugin/vite.config.js @@ -0,0 +1,22 @@ +let transformCount = 1 + +const transformPlugin = { + name: 'transform', + transform(code, id) { + if (id === require.resolve('./index.js')) { + // Ensure `index.js` is reevaluated if 'plugin-dep.js' is changed + this.addWatchFile(require.resolve('./plugin-dep.js')) + + return ` + // Inject TRANSFORM_COUNT + let TRANSFORM_COUNT = ${transformCount++}; + + ${code} + ` + } + } +} + +module.exports = { + plugins: [transformPlugin] +} From b41bab5f50a316e321072ff0582612c3ce216319 Mon Sep 17 00:00:00 2001 From: mattcompiles Date: Sun, 1 Aug 2021 21:01:49 +1000 Subject: [PATCH 2/6] Update packages/playground/transform-plugin/vite.config.js Co-authored-by: patak --- packages/playground/transform-plugin/vite.config.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/playground/transform-plugin/vite.config.js b/packages/playground/transform-plugin/vite.config.js index 5aec6ca477f4a9..2da89e4a3db062 100644 --- a/packages/playground/transform-plugin/vite.config.js +++ b/packages/playground/transform-plugin/vite.config.js @@ -1,9 +1,10 @@ +const { normalizePath } = require('vite') let transformCount = 1 const transformPlugin = { name: 'transform', transform(code, id) { - if (id === require.resolve('./index.js')) { + if (id === normalizePath(require.resolve('./index.js'))) { // Ensure `index.js` is reevaluated if 'plugin-dep.js' is changed this.addWatchFile(require.resolve('./plugin-dep.js')) From d7805a227762bd87b0ca6472a418d110d94ded17 Mon Sep 17 00:00:00 2001 From: Em Zhan Date: Thu, 18 Aug 2022 08:10:25 -0500 Subject: [PATCH 3/6] refactor: move old test to new location --- .../transform-plugin/__tests__/transform-plugin.spec.ts | 3 ++- .../playground => playground}/transform-plugin/index.html | 0 {packages/playground => playground}/transform-plugin/index.js | 0 .../playground => playground}/transform-plugin/package.json | 0 .../playground => playground}/transform-plugin/plugin-dep.js | 0 .../playground => playground}/transform-plugin/vite.config.js | 0 6 files changed, 2 insertions(+), 1 deletion(-) rename {packages/playground => playground}/transform-plugin/__tests__/transform-plugin.spec.ts (74%) rename {packages/playground => playground}/transform-plugin/index.html (100%) rename {packages/playground => playground}/transform-plugin/index.js (100%) rename {packages/playground => playground}/transform-plugin/package.json (100%) rename {packages/playground => playground}/transform-plugin/plugin-dep.js (100%) rename {packages/playground => playground}/transform-plugin/vite.config.js (100%) diff --git a/packages/playground/transform-plugin/__tests__/transform-plugin.spec.ts b/playground/transform-plugin/__tests__/transform-plugin.spec.ts similarity index 74% rename from packages/playground/transform-plugin/__tests__/transform-plugin.spec.ts rename to playground/transform-plugin/__tests__/transform-plugin.spec.ts index cea0e082de2baf..2a56add2d62bae 100644 --- a/packages/playground/transform-plugin/__tests__/transform-plugin.spec.ts +++ b/playground/transform-plugin/__tests__/transform-plugin.spec.ts @@ -1,4 +1,5 @@ -import { editFile, untilUpdated } from '../../testUtils' +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') diff --git a/packages/playground/transform-plugin/index.html b/playground/transform-plugin/index.html similarity index 100% rename from packages/playground/transform-plugin/index.html rename to playground/transform-plugin/index.html diff --git a/packages/playground/transform-plugin/index.js b/playground/transform-plugin/index.js similarity index 100% rename from packages/playground/transform-plugin/index.js rename to playground/transform-plugin/index.js diff --git a/packages/playground/transform-plugin/package.json b/playground/transform-plugin/package.json similarity index 100% rename from packages/playground/transform-plugin/package.json rename to playground/transform-plugin/package.json diff --git a/packages/playground/transform-plugin/plugin-dep.js b/playground/transform-plugin/plugin-dep.js similarity index 100% rename from packages/playground/transform-plugin/plugin-dep.js rename to playground/transform-plugin/plugin-dep.js diff --git a/packages/playground/transform-plugin/vite.config.js b/playground/transform-plugin/vite.config.js similarity index 100% rename from packages/playground/transform-plugin/vite.config.js rename to playground/transform-plugin/vite.config.js From 0646c7a92a85ab83412e77e82eb9fb2f2520636b Mon Sep 17 00:00:00 2001 From: Em Zhan Date: Thu, 18 Aug 2022 08:39:54 -0500 Subject: [PATCH 4/6] refactor: avoid `[require-resolve-not-external]` warning --- playground/transform-plugin/vite.config.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/playground/transform-plugin/vite.config.js b/playground/transform-plugin/vite.config.js index 2da89e4a3db062..18cbcfffbe75d8 100644 --- a/playground/transform-plugin/vite.config.js +++ b/playground/transform-plugin/vite.config.js @@ -1,12 +1,14 @@ const { normalizePath } = require('vite') +const { resolve } = require('path') + let transformCount = 1 const transformPlugin = { name: 'transform', transform(code, id) { - if (id === normalizePath(require.resolve('./index.js'))) { + if (id === normalizePath(resolve(__dirname, 'index.js'))) { // Ensure `index.js` is reevaluated if 'plugin-dep.js' is changed - this.addWatchFile(require.resolve('./plugin-dep.js')) + this.addWatchFile('./plugin-dep.js') return ` // Inject TRANSFORM_COUNT From 18d0f636278b6ec82cf377346c4870219486929b Mon Sep 17 00:00:00 2001 From: Em Zhan Date: Thu, 18 Aug 2022 09:16:41 -0500 Subject: [PATCH 5/6] refactor: fix eslint warnings/errors --- playground/transform-plugin/index.js | 1 - playground/transform-plugin/vite.config.js | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/playground/transform-plugin/index.js b/playground/transform-plugin/index.js index 8e71251af61c5b..ac3da49609d222 100644 --- a/playground/transform-plugin/index.js +++ b/playground/transform-plugin/index.js @@ -1,3 +1,2 @@ // 'TRANSFORM_COUNT' is injected by the transform plugin -// eslint-disable-next-line no-undef document.getElementById('transform-count').innerHTML = TRANSFORM_COUNT diff --git a/playground/transform-plugin/vite.config.js b/playground/transform-plugin/vite.config.js index 18cbcfffbe75d8..0b9ddb97c34304 100644 --- a/playground/transform-plugin/vite.config.js +++ b/playground/transform-plugin/vite.config.js @@ -1,5 +1,5 @@ +const { resolve } = require('node:path') const { normalizePath } = require('vite') -const { resolve } = require('path') let transformCount = 1 From ce2c9e3e8a737dc04bfe02ff42c6e91f93a388c8 Mon Sep 17 00:00:00 2001 From: Em Zhan Date: Wed, 17 Aug 2022 21:06:19 -0500 Subject: [PATCH 6/6] fix: make `addWatchFile()` work --- packages/vite/src/node/plugins/importAnalysis.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) 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)) }