-
-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: generate IntelliJ ide helper file
- Loading branch information
1 parent
9dc7333
commit 423cb7b
Showing
8 changed files
with
118 additions
and
2 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,71 @@ | ||
import { dirname, relative, isAbsolute } from 'path' | ||
import { promises as fs, existsSync } from 'fs' | ||
import { notNullish, slash } from '@antfu/utils' | ||
import { Context } from './context' | ||
import { getVueVersion } from './options' | ||
import { getTransformedPath } from './utils' | ||
|
||
export async function generateIdeHelper(ctx: Context, root: string, filepath: string) { | ||
const imports: Record<string, string> = Object.fromEntries( | ||
Object.values({ | ||
...ctx.componentNameMap, | ||
...ctx.componentCustomMap, | ||
}) | ||
.map(({ path, name, importName }) => { | ||
if (!name) | ||
return undefined | ||
path = getTransformedPath(path, ctx) | ||
const related = isAbsolute(path) | ||
? `./${relative(dirname(filepath), path)}` | ||
: path | ||
|
||
let entry = 'import ' | ||
if (importName) | ||
entry += `{ ${importName} as ${name} }` | ||
else | ||
entry += name | ||
entry += ` from '${slash(related)}';` | ||
return [name, entry] | ||
}) | ||
.filter(notNullish), | ||
) | ||
|
||
if (!Object.keys(imports).length) | ||
return | ||
|
||
const originalContent = existsSync(filepath) ? await fs.readFile(filepath, 'utf-8') : '' | ||
|
||
const lines = Object.entries(imports) | ||
.sort((a, b) => a[0].localeCompare(b[0])) | ||
|
||
let code = `// generated by unplugin-vue-components | ||
// We suggest you to NOT commit this file into source control | ||
// Read more: https://github.com/antfu/unplugin-vue-components/issues/135 | ||
` | ||
|
||
if (getVueVersion() === 'vue3') { | ||
code += `import { createApp } from "vue"; | ||
${lines.map(line => line[1]).join('\n')} | ||
const app = createApp({}); | ||
${lines.map(line => `app.component('${line[0]}', ${line[0]})`).join('\n')} | ||
app.mount("body"); | ||
` | ||
} | ||
else { | ||
code += `import Vue from "vue"; | ||
${lines.map(line => line[1]).join('\n')} | ||
${lines.map(line => `Vue.component('${line[0]}', ${line[0]});\nVue.component('Lazy${line[0]}', ${line[0]});`).join('\n')} | ||
` | ||
} | ||
|
||
if (code !== originalContent) | ||
await fs.writeFile(filepath, code, 'utf-8') | ||
} |
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