Skip to content

Commit 50c6fea

Browse files
CopilotElMassimo
andauthored
refactor: simplify filtered hook handlers
Agent-Logs-Url: https://github.com/ElMassimo/iles/sessions/2f29b5ee-5bf6-4265-aed8-abd9fd19681f Co-authored-by: ElMassimo <1158253+ElMassimo@users.noreply.github.com>
1 parent 5c07b02 commit 50c6fea

5 files changed

Lines changed: 14 additions & 34 deletions

File tree

packages/iles/src/node/plugin/documents.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,14 @@ export default function documentsPlugin (config: AppConfig): Plugin {
3535
resolveId: {
3636
filter: { id: docsVirtualIdFilter },
3737
handler (id) {
38-
if (id.startsWith(DOCS_VIRTUAL_ID))
39-
return id
38+
return id
4039
},
4140
},
4241
// Extract frontmatter for each file in the matching pattern, and create a
4342
// module where the default export is an array with each matching document.
4443
load: {
4544
filter: { id: docsVirtualIdFilter },
46-
async handler (id, options) {
47-
if (!id.startsWith(DOCS_VIRTUAL_ID)) return
48-
45+
async handler (id, _options) {
4946
const { query: { pattern: rawPath } } = parseId(id)
5047

5148
// Extract pattern from the virtual module path, and resolve any alias.
@@ -121,7 +118,7 @@ export default function documentsPlugin (config: AppConfig): Plugin {
121118
filter: { id: fileCanUseDocuments },
122119
async handler (code, id) {
123120
// Replace each usage of useDocuments with an import of a virtual module.
124-
if (fileCanUseDocuments.test(id) && !definitionRegex.test(code)) {
121+
if (!definitionRegex.test(code)) {
125122
const paths: [string, string][] = []
126123
code = code.replace(usageRegex, (_, path) => {
127124
path = path.trim().slice(1, -1)

packages/iles/src/node/plugin/plugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ export default function IslandsPlugins (appConfig: AppConfig): PluginOption[] {
136136
},
137137
transform: {
138138
filter: { id: APP_COMPONENT_PATH },
139-
handler (code, id) {
140-
if (id === APP_COMPONENT_PATH && !isBuild && appConfig.debug)
139+
handler (code, _id) {
140+
if (!isBuild && appConfig.debug)
141141
return code.replace('const DebugPanel = () => null', () => `import DebugPanel from '${DEBUG_COMPONENT_PATH}'`)
142142
},
143143
},

packages/images/src/images.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,9 @@ export default function IlesImagePresets (presets: ImagePresets, options?: Optio
4848
name: '@islands/images:inject-mdx-component',
4949
transform: {
5050
filter: { id: /\/composables\/mdxComponents\.js/ },
51-
handler (code, id) {
52-
if (id.includes('/composables/mdxComponents.js')) {
53-
code = code.replace('inject(mdxComponentsKey)', '{ img: _Picture, ...inject(mdxComponentsKey) }')
54-
return `import _Picture from '${PICTURE_COMPONENT_PATH}'\n${code}`
55-
}
51+
handler (code, _id) {
52+
code = code.replace('inject(mdxComponentsKey)', '{ img: _Picture, ...inject(mdxComponentsKey) }')
53+
return `import _Picture from '${PICTURE_COMPONENT_PATH}'\n${code}`
5654
},
5755
},
5856
},

packages/mdx/src/mdx-vite-plugins.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { extname } from 'path'
2-
31
import type { Plugin, TransformResult } from 'vite'
42
import type { createFormatAwareProcessors } from '@mdx-js/mdx/internal-create-format-aware-processors'
53
import hash from 'hash-sum'
@@ -13,10 +11,6 @@ export default function IlesMdx (options: MarkdownOptions = {}): Plugin[] {
1311
let markdownProcessor: ReturnType<typeof createFormatAwareProcessors>
1412
let isDevelopment: boolean
1513

16-
function shouldTransform (path: string) {
17-
return markdownProcessor.extnames.includes(extname(path))
18-
}
19-
2014
async function createMdxProcessor (sourcemap: string | boolean) {
2115
const { createFormatAwareProcessors } = await import('@mdx-js/mdx/internal-create-format-aware-processors')
2216
markdownProcessor = createFormatAwareProcessors({
@@ -42,8 +36,6 @@ export default function IlesMdx (options: MarkdownOptions = {}): Plugin[] {
4236
transform: {
4337
filter: { id: markdownIdFilter },
4438
async handler (value, path) {
45-
if (!shouldTransform(path)) return
46-
4739
const compiled = await markdownProcessor.process({ value, path })
4840
return { code: String(compiled.value), map: compiled.map } as TransformResult
4941
},
@@ -55,8 +47,6 @@ export default function IlesMdx (options: MarkdownOptions = {}): Plugin[] {
5547
transform: {
5648
filter: { id: markdownIdFilter },
5749
async handler (code, path) {
58-
if (!shouldTransform(path)) return
59-
6050
return code.replace('export default function MDXContent', () => `
6151
import { defineComponent as $defineComponent } from 'iles/jsx-runtime'
6252
@@ -75,8 +65,6 @@ function MDXContent`)
7565
transform: {
7666
filter: { id: markdownIdFilter },
7767
handler (code: string, path: string) {
78-
if (!shouldTransform(path)) return
79-
8068
const hmrId = hash(`${path.split('?', 2)[0]}default`)
8169

8270
return `${code}

packages/pages/src/pages.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,20 @@ export default function IlesPages (): any {
6868
},
6969
resolveId: {
7070
filter: { id: moduleIdRegex },
71-
async handler (id) {
72-
if (moduleIdRegex.test(id))
73-
return MODULE_ID
71+
async handler (_id) {
72+
return MODULE_ID
7473
},
7574
},
7675
load: {
7776
filter: { id: moduleIdRegex },
78-
async handler (id) {
79-
if (moduleIdRegex.test(id))
80-
return generatedRoutes ||= await api.generateRoutesModule()
77+
async handler (_id) {
78+
return generatedRoutes ||= await api.generateRoutesModule()
8179
},
8280
},
8381
transform: {
8482
filter: { id: /vue&type=page/ },
85-
async handler (_code, id) {
86-
if (id.includes('vue&type=page'))
87-
return 'export default {};'
83+
async handler (_code, _id) {
84+
return 'export default {};'
8885
},
8986
},
9087
}

0 commit comments

Comments
 (0)