-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: transform
kirbyup.import
with Vite plugin
- Loading branch information
1 parent
0e74ac3
commit 2307b67
Showing
8 changed files
with
124 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Exports are ESM and not part of the kirbyup CLI, | ||
* but importable when writing a Kirby Panel plugin | ||
*/ | ||
|
||
/** Single entry of Vite's `import.meta.globEager` function */ | ||
type Module = Record<string, any> | ||
|
||
const getComponentName = (path: string) => | ||
path | ||
.split('/') | ||
.pop()! | ||
.replace(/\.vue$/, '') | ||
.toLowerCase() | ||
|
||
/* | ||
* Set of Utils for Kirby Plugins | ||
*/ | ||
export const kirbyup = Object.freeze({ | ||
/** | ||
* Auto-import Kirby Panel components, will be transformed by | ||
* kirbyup's auto import plugin for Vite | ||
* | ||
* @example | ||
* kirbyup.import('./components/blocks/*.vue') | ||
*/ | ||
import(modules: Record<string, Module>) { | ||
return Object.entries(modules).reduce((acc: Module, [path, component]) => { | ||
acc[getComponentName(path)] = component.default | ||
return acc | ||
}, {}) | ||
} | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import MagicString from 'magic-string' | ||
import { multilineCommentsRE, singlelineCommentsRE } from './utils' | ||
import type { Plugin, ResolvedConfig } from 'vite' | ||
|
||
/** | ||
* Transform `kirbyup.import(<path>)` to | ||
* `kirbyup.import(import.meta.globEager(<path>))` | ||
*/ | ||
export default function kirbyupAutoImportPlugin(): Plugin { | ||
let config: ResolvedConfig | ||
|
||
return { | ||
name: 'kirbyup:auto-import', | ||
|
||
configResolved(resolvedConfig) { | ||
config = resolvedConfig | ||
}, | ||
|
||
async transform(code, id) { | ||
if (code.includes('kirbyup.import')) { | ||
const kirbyupImportRE = | ||
/\bkirbyup\.import\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*\)/g | ||
const noCommentsCode = code | ||
.replace(multilineCommentsRE, (m) => ' '.repeat(m.length)) | ||
.replace(singlelineCommentsRE, (m) => ' '.repeat(m.length)) | ||
let s: MagicString | null = null | ||
let match: RegExpExecArray | null | ||
|
||
while ((match = kirbyupImportRE.exec(noCommentsCode))) { | ||
const { 0: exp, 1: rawPath, index } = match | ||
|
||
if (!s) s = new MagicString(code) | ||
|
||
const path = rawPath.slice(1, -1) | ||
s.overwrite( | ||
index, | ||
index + exp.length, | ||
`kirbyup.import(import.meta.globEager("${path}"))` | ||
) | ||
} | ||
|
||
if (s) { | ||
return { | ||
code: s.toString(), | ||
map: config.build.sourcemap ? s.generateMap({ hires: true }) : null | ||
} | ||
} | ||
} | ||
|
||
return null | ||
} | ||
} | ||
} |
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,2 @@ | ||
export const multilineCommentsRE = /\/\*(.|[\r\n])*?\*\//gm | ||
export const singlelineCommentsRE = /\/\/.*/g |
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