This repository has been archived by the owner on Jul 29, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: conact modules into one file
- Loading branch information
Showing
16 changed files
with
491 additions
and
491 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
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,3 @@ | ||
import type { ModuleNode, Plugin, ResolvedConfig, ViteDevServer } from 'vite' | ||
import mm from 'micromatch' | ||
import type { ParsedImportGlob, PluginOptions } from '../types' | ||
import { transform } from './transform' | ||
import { importGlobPlugin } from './plugin' | ||
|
||
export default function (options: PluginOptions = {}): Plugin { | ||
let server: ViteDevServer | undefined | ||
let config: ResolvedConfig | ||
const map = new Map<string, string[][]>() | ||
|
||
function updateMap(id: string, info: ParsedImportGlob[]) { | ||
const allGlobs = info.map(i => i.globsResolved) | ||
map.set(id, allGlobs) | ||
// add those allGlobs to the watcher | ||
server?.watcher.add(allGlobs.flatMap(i => i.filter(i => i[0] !== '!'))) | ||
} | ||
|
||
function getAffectedModules(file: string) { | ||
const modules: ModuleNode[] = [] | ||
for (const [id, allGlobs] of map) { | ||
if (allGlobs.some(glob => mm.isMatch(file, glob))) | ||
modules.push(...(server?.moduleGraph.getModulesByFile(id) || [])) | ||
} | ||
return modules | ||
} | ||
|
||
return { | ||
name: 'vite-plugin-glob', | ||
config() { | ||
return { | ||
server: { | ||
watch: { | ||
disableGlobbing: false, | ||
}, | ||
}, | ||
} | ||
}, | ||
configResolved(_config) { | ||
config = _config | ||
}, | ||
buildStart() { | ||
map.clear() | ||
}, | ||
configureServer(_server) { | ||
server = _server | ||
const handleFileAddUnlink = (file: string) => { | ||
const modules = getAffectedModules(file) | ||
_server.ws.send({ | ||
type: 'update', | ||
updates: modules.map((mod) => { | ||
_server.moduleGraph.invalidateModule(mod) | ||
return { | ||
acceptedPath: mod.id!, | ||
path: mod.id!, | ||
timestamp: Date.now(), | ||
type: 'js-update', | ||
} | ||
}), | ||
}) | ||
} | ||
server.watcher.on('unlink', handleFileAddUnlink) | ||
server.watcher.on('add', handleFileAddUnlink) | ||
}, | ||
async transform(code, id) { | ||
const result = await transform( | ||
code, | ||
id, | ||
config.root, | ||
im => this.resolve(im, id).then(i => i?.id || im), | ||
options, | ||
) | ||
if (result) { | ||
updateMap(id, result.matches) | ||
return { | ||
code: result.s.toString(), | ||
map: result.s.generateMap(), | ||
} | ||
} | ||
}, | ||
} | ||
} | ||
export default importGlobPlugin |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.