-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
126 additions
and
118 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const PKG_NAME = 'unplugin-vue-i18n' |
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,3 @@ | ||
export * from './options' | ||
export * from './resource' | ||
export * from './directive' |
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './log' | ||
export * from './plugin' | ||
export * from './resolver' |
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,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}`) | ||
} |
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,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) | ||
} |
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,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 | ||
} | ||
} |