forked from vitejs/vite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Matt Jones <mattjones701@gmail.com>
- Loading branch information
1 parent
771a668
commit d9028db
Showing
7 changed files
with
56 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
playground/transform-plugin/__tests__/transform-plugin.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<div id="transform-count"></div> | ||
|
||
<script type="module" src="./index.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// 'TRANSFORM_COUNT' is injected by the transform plugin | ||
document.getElementById('transform-count').innerHTML = TRANSFORM_COUNT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// Empty file for detecting changes in tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] | ||
} |