Skip to content

Commit

Permalink
refactor: code organizing (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
kazupon authored Jun 25, 2024
1 parent d3585fb commit a943f07
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 118 deletions.
1 change: 1 addition & 0 deletions packages/unplugin-vue-i18n/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const PKG_NAME = 'unplugin-vue-i18n'
3 changes: 3 additions & 0 deletions packages/unplugin-vue-i18n/src/core/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './options'
export * from './resource'
export * from './directive'
6 changes: 5 additions & 1 deletion packages/unplugin-vue-i18n/src/core/resource.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { parse as parsePath } from 'pathe'
import createDebug from 'debug'
import fg from 'fast-glob'
import { promises as fs } from 'node:fs'
import {
isArray,
isEmptyObject,
Expand All @@ -18,7 +19,6 @@ import {
import { parse } from 'vue/compiler-sfc'
import { parseVueRequest, getVueCompiler } from '../vue'
import {
getRaw,
warn,
error,
raiseError,
Expand Down Expand Up @@ -717,3 +717,7 @@ function asVirtualId(
) {
return framework === 'vite' ? VIRTUAL_PREFIX + id : id
}

async function getRaw(path: string): Promise<string> {
return fs.readFile(path, { encoding: 'utf-8' })
}
4 changes: 1 addition & 3 deletions packages/unplugin-vue-i18n/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { createUnplugin } from 'unplugin'
import createDebug from 'debug'
import { raiseError, checkInstallPackage, resolveNamespace } from './utils'
import { resolveOptions } from './core/options'
import { resourcePlugin } from './core/resource'
import { directivePlugin } from './core/directive'
import { resolveOptions, resourcePlugin, directivePlugin } from './core'

import type { PluginOptions } from './types'

Expand Down
114 changes: 0 additions & 114 deletions packages/unplugin-vue-i18n/src/utils.ts

This file was deleted.

3 changes: 3 additions & 0 deletions packages/unplugin-vue-i18n/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './log'
export * from './plugin'
export * from './resolver'
14 changes: 14 additions & 0 deletions packages/unplugin-vue-i18n/src/utils/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pc from 'picocolors'
import { PKG_NAME } from '../constants'

export function warn(...args: unknown[]) {
console.warn(pc.yellow(pc.bold(`[${PKG_NAME}] `)), ...args)
}

export function error(...args: unknown[]) {
console.error(pc.red(pc.bold(`[${PKG_NAME}] `)), ...args)
}

export function raiseError(message: string) {
throw new Error(`[${PKG_NAME}] ${message}`)
}
41 changes: 41 additions & 0 deletions packages/unplugin-vue-i18n/src/utils/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import path from 'node:path'
import { PKG_NAME } from '../constants'
import { error } from './log'

import type { VitePlugin, RollupPlugin } from 'unplugin'

export function resolveNamespace(name: string): string {
return `${PKG_NAME}:${name}`
}

// @ts-expect-error -- FIXME: plugin type
type UserConfig = Parameters<VitePlugin['configResolved']>[0]

export function getVitePlugin(
config: UserConfig,
name: string
): RollupPlugin | null {
// vite plugin has compoaibility for rollup plugin
return config.plugins.find(p => p.name === name) as RollupPlugin
}

export function checkVuePlugin(vuePlugin: RollupPlugin | null): boolean {
if (vuePlugin == null || !vuePlugin.api) {
error(
'`@vitejs/plugin-vue` plugin is not found or invalid version. Please install `@vitejs/plugin-vue` v4.3.4 or later version.'
)
return false
}
return true
}

const isWindows = typeof process !== 'undefined' && process.platform === 'win32'

const windowsSlashRE = /\\/g
function slash(p: string): string {
return p.replace(windowsSlashRE, '/')
}

export function normalizePath(id: string): string {
return path.posix.normalize(isWindows ? slash(id) : id)
}
58 changes: 58 additions & 0 deletions packages/unplugin-vue-i18n/src/utils/resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import fs from 'node:fs'
import createDebug from 'debug'
import module from 'node:module'
import path from 'node:path'

const SUPPORT_PACKAGES = ['vue-i18n', 'petite-vue-i18n'] as const

type SupportPackage = (typeof SUPPORT_PACKAGES)[number]

export type InstalledPackageInfo = {
alias: string
pkg: SupportPackage
}

const _require = module.createRequire(import.meta.url)

export function checkInstallPackage(
debug: createDebug.Debugger
): InstalledPackageInfo {
const pkgInfo =
resolvePkgPath('vue-i18n', debug) ||
resolvePkgPath('petite-vue-i18n', debug)
if (!pkgInfo) {
throw new Error(
`requires 'vue-i18n' or 'petite-vue-i18n' to be present in the dependency tree.`
)
}

debug('installed package info:', pkgInfo)
return pkgInfo
}

function resolvePkgPath(
id: string,
debug: createDebug.Debugger
): InstalledPackageInfo | null {
try {
/**
* NOTE:
* Assuming the case of using npm alias `npm:`,
* get the installed package name from `package.json`
*/
const resolvedPath = _require.resolve(id)
const pkgPath = path.dirname(resolvedPath)
const pkgJson = JSON.parse(
fs.readFileSync(path.join(pkgPath, 'package.json'), 'utf-8')
) as { name: string }
const pkgName: string = pkgJson.name.startsWith('vue-i18n')
? 'vue-i18n'
: pkgJson.name.startsWith('petite-vue-i18n')
? 'petite-vue-i18n'
: ''
return pkgJson ? { alias: id, pkg: pkgName as SupportPackage } : null
} catch (e) {
debug(`cannot find '${id}'`, e)
return null
}
}

0 comments on commit a943f07

Please sign in to comment.