Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update watch mode #7132

Merged
merged 6 commits into from Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 21 additions & 1 deletion packages/playground/assets/__tests__/assets.spec.ts
Expand Up @@ -263,6 +263,7 @@ if (isBuild) {
}
})
}

describe('css and assets in css in build watch', () => {
if (isBuild) {
test('css will not be lost and css does not contain undefined', async () => {
Expand All @@ -271,7 +272,26 @@ describe('css and assets in css in build watch', () => {
const cssFile = findAssetFile(/index\.\w+\.css$/, 'foo')
expect(cssFile).not.toBe('')
expect(cssFile).not.toMatch(/undefined/)
watcher?.close()
})

test('import module.css', async () => {
expect(await getColor('#foo')).toBe('red')
editFile(
'css/foo.module.css',
(code) => code.replace('red', 'blue'),
true
)
await notifyRebuildComplete(watcher)
await page.reload()
expect(await getColor('#foo')).toBe('blue')
})

test('import with raw query', async () => {
expect(await page.textContent('.raw-query')).toBe('foo')
editFile('static/foo.txt', (code) => code.replace('foo', 'zoo'), true)
await notifyRebuildComplete(watcher)
await page.reload()
expect(await page.textContent('.raw-query')).toBe('zoo')
})
}
})
Expand Down
3 changes: 3 additions & 0 deletions packages/playground/assets/css/foo.module.css
@@ -0,0 +1,3 @@
.foo-module {
color: red;
}
10 changes: 10 additions & 0 deletions packages/playground/assets/index.html
Expand Up @@ -212,6 +212,10 @@ <h3 class="import-css">@import</h3>
<h3 class="foo-public">
@import CSS from publicDir should load (this should be red)
</h3>
<h3 id="foo">import module css</h3>

<h3 class="raw-query"></h3>

<style>
@import '/foo.css';
</style>
Expand Down Expand Up @@ -268,6 +272,12 @@ <h3 class="foo-public">
document.querySelector('.import-meta-url-img-comma-nl').src =
metaUrlWithCommaNL

import classNames from './css/foo.module.css'
document.querySelector('#foo').className = classNames['foo-module']

import someString from './static/foo.txt?raw'
document.querySelector('.raw-query').textContent = someString

const metaUrlNonExistent = new URL('non-existent', import.meta.url).pathname
text('.non-existent-import-meta-url', metaUrlNonExistent)

Expand Down
1 change: 1 addition & 0 deletions packages/playground/assets/static/foo.txt
@@ -0,0 +1 @@
foo
2 changes: 2 additions & 0 deletions packages/vite/src/node/build.ts
Expand Up @@ -39,6 +39,7 @@ import { getDepsCacheDir, findKnownImports } from './optimizer'
import { assetImportMetaUrlPlugin } from './plugins/assetImportMetaUrl'
import { loadFallbackPlugin } from './plugins/loadFallback'
import { watchPackageDataPlugin } from './packages'
import { ensureWatchPlugin } from './plugins/ensureWatch'

export interface BuildOptions {
/**
Expand Down Expand Up @@ -308,6 +309,7 @@ export function resolveBuildPlugins(config: ResolvedConfig): {

return {
pre: [
...(options.watch ? [ensureWatchPlugin()] : []),
watchPackageDataPlugin(config),
commonjsPlugin(options.commonjsOptions),
dataURIPlugin(),
Expand Down
17 changes: 17 additions & 0 deletions packages/vite/src/node/plugins/ensureWatch.ts
@@ -0,0 +1,17 @@
import type { Plugin } from '../plugin'
import { cleanUrl, queryRE } from '../utils'

/**
* plugin to ensure rollup can watch correctly.
*/
export function ensureWatchPlugin(): Plugin {
return {
name: 'vite:ensure-watch',
load(id) {
if (queryRE.test(id)) {
this.addWatchFile(cleanUrl(id))
}
return null
}
bluwy marked this conversation as resolved.
Show resolved Hide resolved
}
}
3 changes: 3 additions & 0 deletions packages/vite/src/node/plugins/index.ts
Expand Up @@ -17,6 +17,7 @@ import { preAliasPlugin } from './preAlias'
import { definePlugin } from './define'
import { ssrRequireHookPlugin } from './ssrRequireHook'
import { workerImportMetaUrlPlugin } from './workerImportMetaUrl'
import { ensureWatchPlugin } from './ensureWatch'
import { metadataPlugin } from './metadata'

export async function resolvePlugins(
Expand All @@ -26,12 +27,14 @@ export async function resolvePlugins(
postPlugins: Plugin[]
): Promise<Plugin[]> {
const isBuild = config.command === 'build'
const isWatch = isBuild && !!config.build.watch

const buildPlugins = isBuild
? (await import('../build')).resolveBuildPlugins(config)
: { pre: [], post: [] }

return [
isWatch ? ensureWatchPlugin() : null,
isBuild ? metadataPlugin() : null,
isBuild ? null : preAliasPlugin(),
aliasPlugin({ entries: config.resolve.alias }),
Expand Down
3 changes: 2 additions & 1 deletion scripts/jestPerTestSetup.ts
Expand Up @@ -175,6 +175,7 @@ afterAll(async () => {
global.serverLogs = []
await global.page?.close()
await server?.close()
global.watcher?.close()
const beforeAllErr = getBeforeAllError()
if (beforeAllErr) {
throw beforeAllErr
Expand All @@ -200,7 +201,7 @@ function startStaticServer(config?: InlineConfig): Promise<string> {
}

// start static file server
const serve = sirv(resolve(rootDir, 'dist'))
const serve = sirv(resolve(rootDir, 'dist'), { dev: !!config?.build?.watch })
const httpServer = (server = http.createServer((req, res) => {
if (req.url === '/ping') {
res.statusCode = 200
Expand Down