Skip to content
This repository has been archived by the owner on Jul 29, 2022. It is now read-only.

Commit

Permalink
refactor: conact modules into one file
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed May 6, 2022
1 parent 0335156 commit c567af3
Show file tree
Hide file tree
Showing 16 changed files with 491 additions and 491 deletions.
46 changes: 25 additions & 21 deletions test/fixture.test.ts → src/__tests__/fixture.test.ts
Expand Up @@ -2,7 +2,7 @@ import { resolve } from 'path'
import { promises as fs } from 'fs'
import { describe, expect, it } from 'vitest'
import { transformWithEsbuild } from 'vite'
import { transform } from '../src/transform'
import { transform } from '../plugin'

describe('fixture', async () => {
const resolveId = (id: string) => id
Expand Down Expand Up @@ -61,23 +61,26 @@ describe('fixture', async () => {
\\"./sibling.ts\\": () => import(\\"./sibling.ts?foo=bar&raw=true&lang.ts\\")
};
export const parent = {
\\"../../playground/src/main.ts\\": () => import(\\"../../playground/src/main.ts?url&lang.ts\\").then(m => m[\\"default\\"])
};
export const rootMixedRelative = {
\\"/build.config.ts\\": () => import(\\"../../build.config.ts?url&lang.ts\\").then(m => m[\\"default\\"]),
\\"/client.d.ts\\": () => import(\\"../../client.d.ts?url&lang.ts\\").then(m => m[\\"default\\"]),
\\"/playground/package.json\\": () => import(\\"../../playground/package.json?url&lang.json\\").then(m => m[\\"default\\"]),
\\"/takeover.d.ts\\": () => import(\\"../../takeover.d.ts?url&lang.ts\\").then(m => m[\\"default\\"]),
\\"/types.ts\\": () => import(\\"../../types.ts?url&lang.ts\\").then(m => m[\\"default\\"])
\\"/build.config.ts\\": () => import(\\"../../../build.config.ts?url&lang.ts\\").then(m => m[\\"default\\"]),
\\"/client.d.ts\\": () => import(\\"../../../client.d.ts?url&lang.ts\\").then(m => m[\\"default\\"]),
\\"/src/__tests__/fixture.test.ts\\": () => import(\\"../fixture.test.ts?url&lang.ts\\").then(m => m[\\"default\\"]),
\\"/src/__tests__/parse.test.ts\\": () => import(\\"../parse.test.ts?url&lang.ts\\").then(m => m[\\"default\\"]),
\\"/src/__tests__/utils.test.ts\\": () => import(\\"../utils.test.ts?url&lang.ts\\").then(m => m[\\"default\\"]),
\\"/takeover.d.ts\\": () => import(\\"../../../takeover.d.ts?url&lang.ts\\").then(m => m[\\"default\\"]),
\\"/types.ts\\": () => import(\\"../../../types.ts?url&lang.ts\\").then(m => m[\\"default\\"])
};
export const cleverCwd1 = {
\\"./node_modules/framework/pages/hello.page.js\\": () => import(\\"./node_modules/framework/pages/hello.page.js\\")
};
export const cleverCwd2 = {
\\"../../playground/src/fixtures/a.ts\\": () => import(\\"../../playground/src/fixtures/a.ts\\"),
\\"../../playground/src/fixtures/b.ts\\": () => import(\\"../../playground/src/fixtures/b.ts\\"),
\\"../fixture.test.ts\\": () => import(\\"../fixture.test.ts\\"),
\\"./modules/a.ts\\": () => import(\\"./modules/a.ts\\"),
\\"./modules/b.ts\\": () => import(\\"./modules/b.ts\\")
\\"./modules/b.ts\\": () => import(\\"./modules/b.ts\\"),
\\"../parse.test.ts\\": () => import(\\"../parse.test.ts\\"),
\\"../utils.test.ts\\": () => import(\\"../utils.test.ts\\")
};
"
`)
Expand All @@ -87,20 +90,21 @@ describe('fixture', async () => {
const root = resolve(__dirname, './fixtures')
const code = [
'import.meta.glob(\'/modules/*.ts\')',
'import.meta.glob([\'/../../playground/src/fixtures/*.ts\'])',
'import.meta.glob([\'/../*.ts\'])',
].join('\n')
expect((await transform(code, 'virtual:module', root, resolveId, options))?.s.toString())
.toMatchInlineSnapshot(`
"{
\\"/modules/a.ts\\": () => import(\\"/modules/a.ts\\"),
\\"/modules/b.ts\\": () => import(\\"/modules/b.ts\\"),
\\"/modules/index.ts\\": () => import(\\"/modules/index.ts\\")
}
{
\\"/../../playground/src/fixtures/a.ts\\": () => import(\\"/../../playground/src/fixtures/a.ts\\"),
\\"/../../playground/src/fixtures/b.ts\\": () => import(\\"/../../playground/src/fixtures/b.ts\\"),
\\"/../../playground/src/fixtures/index.ts\\": () => import(\\"/../../playground/src/fixtures/index.ts\\")
}"`,
"{
\\"/modules/a.ts\\": () => import(\\"/modules/a.ts\\"),
\\"/modules/b.ts\\": () => import(\\"/modules/b.ts\\"),
\\"/modules/index.ts\\": () => import(\\"/modules/index.ts\\")
}
{
\\"/../fixture.test.ts\\": () => import(\\"/../fixture.test.ts\\"),
\\"/../parse.test.ts\\": () => import(\\"/../parse.test.ts\\"),
\\"/../utils.test.ts\\": () => import(\\"/../utils.test.ts\\")
}"
`,
)

try {
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions test/fixtures/index.ts → src/__tests__/fixtures/index.ts
Expand Up @@ -42,7 +42,7 @@ export const parent = import.meta.glob('../../playground/src/*.ts', { as: 'url'

export const rootMixedRelative = import.meta.glob([
'/*.ts',
'../../playground/*.json',
'../*.ts',
], { as: 'url' })

export const cleverCwd1 = import.meta.glob(
Expand All @@ -51,7 +51,7 @@ export const cleverCwd1 = import.meta.glob(

export const cleverCwd2 = import.meta.glob([
'./modules/*.ts',
'../../playground/src/fixtures/*.ts',
'../*.ts',
'!**/index.ts',
],
)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion test/parse.test.ts → src/__tests__/parse.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { parseImportGlob } from '../src/parse'
import { parseImportGlob } from '../plugin'

async function run(input: string) {
const items = await parseImportGlob(input, process.cwd(), process.cwd(), id => id)
Expand Down
2 changes: 1 addition & 1 deletion test/utils.test.ts → src/__tests__/utils.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { getCommonBase } from '../src/utils'
import { getCommonBase } from '../plugin'

describe('getCommonBase()', async () => {
it('basic', () => {
Expand Down
29 changes: 0 additions & 29 deletions src/glob.ts

This file was deleted.

82 changes: 2 additions & 80 deletions src/index.ts
@@ -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
170 changes: 0 additions & 170 deletions src/parse.ts

This file was deleted.

0 comments on commit c567af3

Please sign in to comment.