Skip to content

Commit a943f07

Browse files
authored
refactor: code organizing (#383)
1 parent d3585fb commit a943f07

File tree

9 files changed

+126
-118
lines changed

9 files changed

+126
-118
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const PKG_NAME = 'unplugin-vue-i18n'
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './options'
2+
export * from './resource'
3+
export * from './directive'

packages/unplugin-vue-i18n/src/core/resource.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { parse as parsePath } from 'pathe'
22
import createDebug from 'debug'
33
import fg from 'fast-glob'
4+
import { promises as fs } from 'node:fs'
45
import {
56
isArray,
67
isEmptyObject,
@@ -18,7 +19,6 @@ import {
1819
import { parse } from 'vue/compiler-sfc'
1920
import { parseVueRequest, getVueCompiler } from '../vue'
2021
import {
21-
getRaw,
2222
warn,
2323
error,
2424
raiseError,
@@ -717,3 +717,7 @@ function asVirtualId(
717717
) {
718718
return framework === 'vite' ? VIRTUAL_PREFIX + id : id
719719
}
720+
721+
async function getRaw(path: string): Promise<string> {
722+
return fs.readFile(path, { encoding: 'utf-8' })
723+
}

packages/unplugin-vue-i18n/src/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { createUnplugin } from 'unplugin'
22
import createDebug from 'debug'
33
import { raiseError, checkInstallPackage, resolveNamespace } from './utils'
4-
import { resolveOptions } from './core/options'
5-
import { resourcePlugin } from './core/resource'
6-
import { directivePlugin } from './core/directive'
4+
import { resolveOptions, resourcePlugin, directivePlugin } from './core'
75

86
import type { PluginOptions } from './types'
97

packages/unplugin-vue-i18n/src/utils.ts

Lines changed: 0 additions & 114 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './log'
2+
export * from './plugin'
3+
export * from './resolver'
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import pc from 'picocolors'
2+
import { PKG_NAME } from '../constants'
3+
4+
export function warn(...args: unknown[]) {
5+
console.warn(pc.yellow(pc.bold(`[${PKG_NAME}] `)), ...args)
6+
}
7+
8+
export function error(...args: unknown[]) {
9+
console.error(pc.red(pc.bold(`[${PKG_NAME}] `)), ...args)
10+
}
11+
12+
export function raiseError(message: string) {
13+
throw new Error(`[${PKG_NAME}] ${message}`)
14+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import path from 'node:path'
2+
import { PKG_NAME } from '../constants'
3+
import { error } from './log'
4+
5+
import type { VitePlugin, RollupPlugin } from 'unplugin'
6+
7+
export function resolveNamespace(name: string): string {
8+
return `${PKG_NAME}:${name}`
9+
}
10+
11+
// @ts-expect-error -- FIXME: plugin type
12+
type UserConfig = Parameters<VitePlugin['configResolved']>[0]
13+
14+
export function getVitePlugin(
15+
config: UserConfig,
16+
name: string
17+
): RollupPlugin | null {
18+
// vite plugin has compoaibility for rollup plugin
19+
return config.plugins.find(p => p.name === name) as RollupPlugin
20+
}
21+
22+
export function checkVuePlugin(vuePlugin: RollupPlugin | null): boolean {
23+
if (vuePlugin == null || !vuePlugin.api) {
24+
error(
25+
'`@vitejs/plugin-vue` plugin is not found or invalid version. Please install `@vitejs/plugin-vue` v4.3.4 or later version.'
26+
)
27+
return false
28+
}
29+
return true
30+
}
31+
32+
const isWindows = typeof process !== 'undefined' && process.platform === 'win32'
33+
34+
const windowsSlashRE = /\\/g
35+
function slash(p: string): string {
36+
return p.replace(windowsSlashRE, '/')
37+
}
38+
39+
export function normalizePath(id: string): string {
40+
return path.posix.normalize(isWindows ? slash(id) : id)
41+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import fs from 'node:fs'
2+
import createDebug from 'debug'
3+
import module from 'node:module'
4+
import path from 'node:path'
5+
6+
const SUPPORT_PACKAGES = ['vue-i18n', 'petite-vue-i18n'] as const
7+
8+
type SupportPackage = (typeof SUPPORT_PACKAGES)[number]
9+
10+
export type InstalledPackageInfo = {
11+
alias: string
12+
pkg: SupportPackage
13+
}
14+
15+
const _require = module.createRequire(import.meta.url)
16+
17+
export function checkInstallPackage(
18+
debug: createDebug.Debugger
19+
): InstalledPackageInfo {
20+
const pkgInfo =
21+
resolvePkgPath('vue-i18n', debug) ||
22+
resolvePkgPath('petite-vue-i18n', debug)
23+
if (!pkgInfo) {
24+
throw new Error(
25+
`requires 'vue-i18n' or 'petite-vue-i18n' to be present in the dependency tree.`
26+
)
27+
}
28+
29+
debug('installed package info:', pkgInfo)
30+
return pkgInfo
31+
}
32+
33+
function resolvePkgPath(
34+
id: string,
35+
debug: createDebug.Debugger
36+
): InstalledPackageInfo | null {
37+
try {
38+
/**
39+
* NOTE:
40+
* Assuming the case of using npm alias `npm:`,
41+
* get the installed package name from `package.json`
42+
*/
43+
const resolvedPath = _require.resolve(id)
44+
const pkgPath = path.dirname(resolvedPath)
45+
const pkgJson = JSON.parse(
46+
fs.readFileSync(path.join(pkgPath, 'package.json'), 'utf-8')
47+
) as { name: string }
48+
const pkgName: string = pkgJson.name.startsWith('vue-i18n')
49+
? 'vue-i18n'
50+
: pkgJson.name.startsWith('petite-vue-i18n')
51+
? 'petite-vue-i18n'
52+
: ''
53+
return pkgJson ? { alias: id, pkg: pkgName as SupportPackage } : null
54+
} catch (e) {
55+
debug(`cannot find '${id}'`, e)
56+
return null
57+
}
58+
}

0 commit comments

Comments
 (0)