Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support custom modifiers for glob imports
- Loading branch information
Showing
6 changed files
with
73 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,39 @@ | ||
import path from 'path' | ||
import { defineConfig } from 'vite' | ||
import { defineConfig, Plugin } from 'vite' | ||
import { init, parse } from 'es-module-lexer' | ||
|
||
export default defineConfig({ | ||
resolve: { | ||
alias: { | ||
'@dir': path.resolve(__dirname, './dir/') | ||
} | ||
} | ||
}, | ||
plugins: [customModifierPlugin()] | ||
}) | ||
|
||
function customModifierPlugin() { | ||
const metaRE = /(\?|&)meta(?:&|$)/ | ||
return { | ||
name: 'custom-modifier', | ||
async transform(src, id) { | ||
if (!metaRE.test(id)) { | ||
return | ||
} | ||
|
||
const exportNames = await getExportNames(src) | ||
|
||
const code = `export const exportNames = ${JSON.stringify(exportNames)};` | ||
|
||
return { | ||
code, | ||
map: { mappings: '' } | ||
} | ||
} | ||
} as Plugin | ||
} | ||
|
||
async function getExportNames(src: string): Promise<readonly string[]> { | ||
await init | ||
const exportNames = parse(src)[1] | ||
return exportNames | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.