Skip to content

Commit

Permalink
feat: generate IntelliJ ide helper file
Browse files Browse the repository at this point in the history
  • Loading branch information
superbiche committed Dec 9, 2021
1 parent 9dc7333 commit 423cb7b
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"local-pkg": "^0.4.0",
"magic-string": "^0.25.7",
"minimatch": "^3.0.4",
"pnpm": "^6.23.6",
"resolve": "^1.20.0",
"unplugin": "^0.2.21"
},
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions src/core/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import { pascalCase, getNameFromFilePath, resolveAlias, matchGlobs, parseId } fr
import { resolveOptions } from './options'
import { searchComponents } from './fs/glob'
import { generateDeclaration } from './declaration'
import { generateIdeHelper } from './ideHelper'
import transformer from './transformer'

const debug = {
components: Debug('unplugin-vue-components:context:components'),
search: Debug('unplugin-vue-components:context:search'),
hmr: Debug('unplugin-vue-components:context:hmr'),
decleration: Debug('unplugin-vue-components:decleration'),
ideHelper: Debug('unplugin-vue-components:ideHelper'),
env: Debug('unplugin-vue-components:env'),
}

Expand All @@ -37,6 +39,7 @@ export class Context {
) {
this.options = resolveOptions(rawOptions, this.root)
this.generateDeclaration = throttle(500, false, this.generateDeclaration.bind(this))
this.generateIdeHelper = throttle(500, false, this.generateIdeHelper.bind(this))
this.setTransformer(this.options.transformer)
}

Expand Down Expand Up @@ -130,6 +133,7 @@ export class Context {

onUpdate(path: string) {
this.generateDeclaration()
this.generateIdeHelper()

if (!this._server)
return
Expand Down Expand Up @@ -251,6 +255,14 @@ export class Context {
generateDeclaration(this, this.options.root, this.options.dts)
}

generateIdeHelper() {
if (!this.options.generateIdeHelper)
return

debug.ideHelper('generating')
generateIdeHelper(this, this.options.root, this.options.generateIdeHelper)
}

get componentNameMap() {
return this._componentNameMap
}
Expand Down
71 changes: 71 additions & 0 deletions src/core/ideHelper.ts
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')
}
14 changes: 13 additions & 1 deletion src/core/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export const defaultOptions: Omit<Required<Options>, 'include' | 'exclude' | 'tr
importPathTransform: v => v,

allowOverrides: false,

generateIdeHelper: false,
}

function normalizeResolvers(resolvers: (ComponentResolver | ComponentResolver[])[]): ComponentResolverObject[] {
Expand Down Expand Up @@ -60,6 +62,16 @@ export function resolveOptions(options: Options, root: string): ResolvedOptions
? options.dts
: 'components.d.ts',
)

resolved.generateIdeHelper = !options.generateIdeHelper
? false
: resolve(
root,
typeof options.generateIdeHelper === 'string'
? options.generateIdeHelper
: '.components.gen.js',
)

resolved.root = root
resolved.transformer = options.transformer || getVueVersion() || 'vue3'
resolved.directives = (typeof options.directives === 'boolean')
Expand All @@ -71,7 +83,7 @@ export function resolveOptions(options: Options, root: string): ResolvedOptions
return resolved
}

function getVueVersion() {
export function getVueVersion() {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const vue = require('vue')
Expand Down
2 changes: 1 addition & 1 deletion src/core/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface ResolveResult {
replace: (resolved: string) => void
}

export default function tranformer(ctx: Context, transformer: SupportedTransformer): Transformer {
export default function transformer(ctx: Context, transformer: SupportedTransformer): Transformer {
return async(code, id, path) => {
ctx.searchGlob()

Expand Down
2 changes: 2 additions & 0 deletions src/core/unplugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default createUnplugin<Options>((options = {}) => {
try {
const result = await ctx.transform(code, id)
ctx.generateDeclaration()
ctx.generateIdeHelper()
return result
}
catch (e) {
Expand All @@ -44,6 +45,7 @@ export default createUnplugin<Options>((options = {}) => {
if (options.dts) {
ctx.searchGlob()
ctx.generateDeclaration()
ctx.generateIdeHelper()
}

if (config.build.watch && config.command === 'build')
Expand Down
10 changes: 10 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ export interface Options {
* @default undefined
*/
directives?: boolean

/**
* Generate components.js helper for IntelliJ IDEs.
*
* Accept boolean or a path related to project root.
*
* @default false
*/
generateIdeHelper?: boolean | string
}

export type ResolvedOptions = Omit<
Expand All @@ -151,6 +160,7 @@ Required<Options>,
resolvedDirs: string[]
globs: string[]
dts: string | false
generateIdeHelper: string | false
root: string
}

Expand Down

0 comments on commit 423cb7b

Please sign in to comment.