-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: temp commit * chore: temp commit * feat: support vite hmr * test: added unit test * docs: update readme * chore: release v1.3.3-beta.1
- Loading branch information
1 parent
29c19ea
commit 4399659
Showing
21 changed files
with
1,607 additions
and
1,353 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
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
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
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
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,95 @@ | ||
import { resolve } from 'path' | ||
import { beforeEach, describe, expect, test } from 'vitest' | ||
import { transformSymbol } from '@unplugin-vue-cssvars/utils' | ||
import { triggerSFCUpdate, updatedCSSModules } from '../hmr' | ||
|
||
const mockOption = { | ||
rootDir: resolve(), | ||
include: [/.vue/], | ||
includeCompile: ['**/**.scss', '**/**.css'], | ||
server: true, | ||
} | ||
const file = transformSymbol(`${resolve()}/packages/core/hmr/__test__/style/foo.css`) | ||
const mockModuleNode = new Set<any>() | ||
mockModuleNode.add({ id: 'foo.vue' }) | ||
|
||
const mockFileToModulesMap = new Map() | ||
mockFileToModulesMap.set('../D/test', mockModuleNode) | ||
|
||
let hmrModule = null | ||
const mockServer = { | ||
reloadModule: (m) => { | ||
hmrModule = m | ||
}, | ||
moduleGraph: { | ||
fileToModulesMap: mockFileToModulesMap, | ||
}, | ||
} | ||
beforeEach(() => { | ||
hmrModule = null | ||
}) | ||
describe('HMR', () => { | ||
test('HMR: updatedCSSModules', () => { | ||
const CSSFileModuleMap = new Map() | ||
CSSFileModuleMap.set(file, { | ||
importer: new Set(), | ||
vBindCode: ['foo'], | ||
}) | ||
updatedCSSModules(CSSFileModuleMap, mockOption, file) | ||
expect(CSSFileModuleMap.get(file).content).toBeTruthy() | ||
expect(CSSFileModuleMap.get(file).vBindCode).toMatchObject(['test']) | ||
}) | ||
|
||
test('HMR: triggerSFCUpdate basic', () => { | ||
const CSSFileModuleMap = new Map() | ||
CSSFileModuleMap.set(file, { | ||
importer: new Set(), | ||
vBindCode: ['foo'], | ||
sfcPath: new Set(['../D/test']), | ||
}) | ||
|
||
triggerSFCUpdate(CSSFileModuleMap, mockOption, { | ||
importer: new Set(), | ||
vBindCode: ['foo'], | ||
sfcPath: new Set(['../D/test']), | ||
} as any, file, mockServer as any) | ||
expect(CSSFileModuleMap.get(file).content).toBeTruthy() | ||
expect(CSSFileModuleMap.get(file).vBindCode).toMatchObject(['test']) | ||
expect(hmrModule).toMatchObject({ id: 'foo.vue' }) | ||
}) | ||
|
||
test('HMR: triggerSFCUpdate sfcPath is undefined', () => { | ||
const CSSFileModuleMap = new Map() | ||
CSSFileModuleMap.set(file, { | ||
importer: new Set(), | ||
vBindCode: ['foo'], | ||
sfcPath: new Set(['../D/test']), | ||
}) | ||
|
||
triggerSFCUpdate(CSSFileModuleMap, mockOption, { | ||
importer: new Set(), | ||
vBindCode: ['foo'], | ||
} as any, file, mockServer as any) | ||
expect(CSSFileModuleMap.get(file).content).not.toBeTruthy() | ||
expect(CSSFileModuleMap.get(file).vBindCode).toMatchObject(['foo']) | ||
expect(hmrModule).not.toBeTruthy() | ||
}) | ||
|
||
test('HMR: triggerSFCUpdate sfcPath is empty', () => { | ||
const CSSFileModuleMap = new Map() | ||
CSSFileModuleMap.set(file, { | ||
importer: new Set(), | ||
vBindCode: ['foo'], | ||
sfcPath: new Set(['../D/test']), | ||
}) | ||
|
||
triggerSFCUpdate(CSSFileModuleMap, mockOption, { | ||
importer: new Set(), | ||
vBindCode: ['foo'], | ||
sfcPath: new Set(), | ||
} as any, file, mockServer as any) | ||
expect(CSSFileModuleMap.get(file).content).not.toBeTruthy() | ||
expect(CSSFileModuleMap.get(file).vBindCode).toMatchObject(['foo']) | ||
expect(hmrModule).not.toBeTruthy() | ||
}) | ||
}) |
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,6 @@ | ||
#foo{ | ||
color: v-bind-m(test); | ||
background: #ffebf8; | ||
width: 200px; | ||
height: 30px; | ||
} |
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,65 @@ | ||
import { setTArray } from '@unplugin-vue-cssvars/utils' | ||
import { preProcessCSS } from '../runtime/pre-process-css' | ||
import type { ICSSFile, ICSSFileMap, Options } from '../types' | ||
import type { ViteDevServer } from 'vite' | ||
|
||
export function viteHMR( | ||
CSSFileModuleMap: ICSSFileMap, | ||
userOptions: Options, | ||
file: string, | ||
server: ViteDevServer, | ||
) { | ||
// 获取变化的样式文件的 CSSFileMap上有使用它的 | ||
const sfcModulesPathList = CSSFileModuleMap.get(file) | ||
triggerSFCUpdate(CSSFileModuleMap, userOptions, sfcModulesPathList, file, server) | ||
} | ||
|
||
/** | ||
* update CSSModules | ||
* @param CSSFileModuleMap | ||
* @param userOptions | ||
* @param file | ||
*/ | ||
|
||
export function updatedCSSModules( | ||
CSSFileModuleMap: ICSSFileMap, | ||
userOptions: Options, | ||
file: string) { | ||
const updatedCSSMS = preProcessCSS(userOptions, userOptions.alias, [file]).get(file) | ||
CSSFileModuleMap.set(file, updatedCSSMS) | ||
} | ||
|
||
// TODO: unit test | ||
/** | ||
* triggerSFCUpdate | ||
* @param CSSFileModuleMap | ||
* @param userOptions | ||
* @param sfcModulesPathList | ||
* @param file | ||
* @param server | ||
*/ | ||
export function triggerSFCUpdate( | ||
CSSFileModuleMap: ICSSFileMap, | ||
userOptions: Options, | ||
sfcModulesPathList: ICSSFile, | ||
file: string, | ||
server: ViteDevServer) { | ||
if (sfcModulesPathList && sfcModulesPathList.sfcPath) { | ||
// 变化的样式文件的 CSSFileMap上有使用它的 sfc 的信息 | ||
const ls = setTArray(sfcModulesPathList.sfcPath) | ||
ls.forEach((sfcp: string) => { | ||
const modules = server.moduleGraph.fileToModulesMap.get(sfcp) || new Set() | ||
|
||
// updatedCSSModules | ||
updatedCSSModules(CSSFileModuleMap, userOptions, file) | ||
|
||
// update sfc | ||
const modulesList = setTArray(modules) | ||
for (let i = 0; i < modulesList.length; i++) { | ||
// ⭐TODO: 只支持 .vue ? jsx, tsx, js, ts ? | ||
if (modulesList[i].id && (modulesList[i].id as string).endsWith('.vue')) | ||
server.reloadModule(modulesList[i]) | ||
} | ||
}) | ||
} | ||
} |
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
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
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
This file was deleted.
Oops, something went wrong.
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
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
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
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
Oops, something went wrong.