-
-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improvement: vue-i18n re-packaging (#222)
* improvement: vue-i18n re-packaging * fix: rollup config
- Loading branch information
Showing
81 changed files
with
562 additions
and
9,231 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
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
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,69 @@ | ||
import { warn, format, isBoolean } from '@intlify/shared' | ||
import { baseCompile, defaultOnError } from '@intlify/message-compiler' | ||
|
||
import type { CompileOptions, CompileError } from '@intlify/message-compiler' | ||
import type { MessageFunction, MessageFunctions } from '@intlify/runtime' | ||
|
||
const RE_HTML_TAG = /<\/?[\w\s="/.':;#-\/]+>/ | ||
const WARN_MESSAGE = `Detected HTML in '{source}' message. Recommend not using HTML messages to avoid XSS.` | ||
|
||
function checkHtmlMessage(source: string, options: CompileOptions): void { | ||
const warnHtmlMessage = isBoolean(options.warnHtmlMessage) | ||
? options.warnHtmlMessage | ||
: true | ||
if (warnHtmlMessage && RE_HTML_TAG.test(source)) { | ||
warn(format(WARN_MESSAGE, { source })) | ||
} | ||
} | ||
|
||
const defaultOnCacheKey = (source: string): string => source | ||
let compileCache: unknown = Object.create(null) | ||
|
||
/** @internal */ | ||
export function clearCompileCache(): void { | ||
compileCache = Object.create(null) | ||
} | ||
|
||
/** @internal */ | ||
export function compileToFunction<T = string>( | ||
source: string, | ||
options: CompileOptions = {} | ||
): MessageFunction<T> { | ||
if (__RUNTIME__) { | ||
__DEV__ && | ||
warn( | ||
`Runtime compilation is not supported in ${ | ||
__BUNDLE_FILENAME__ || 'N/A' | ||
}.` | ||
) | ||
return (() => source) as MessageFunction<T> | ||
} else { | ||
// check HTML message | ||
__DEV__ && checkHtmlMessage(source, options) | ||
|
||
// check caches | ||
const onCacheKey = options.onCacheKey || defaultOnCacheKey | ||
const key = onCacheKey(source) | ||
const cached = (compileCache as MessageFunctions<T>)[key] | ||
if (cached) { | ||
return cached | ||
} | ||
|
||
// compile error detecting | ||
let occured = false | ||
const onError = options.onError || defaultOnError | ||
options.onError = (err: CompileError): void => { | ||
occured = true | ||
onError(err) | ||
} | ||
|
||
// compile | ||
const { code } = baseCompile(source, options) | ||
|
||
// evaluate function | ||
const msg = new Function(`return ${code}`)() as MessageFunction<T> | ||
|
||
// if occured compile error, don't cache | ||
return !occured ? ((compileCache as MessageFunctions<T>)[key] = msg) : msg | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// NOTE: for runtime only buidling & vue-i18n direct inmporting | ||
export * from './context' | ||
export * from './compile' | ||
export * from './translate' | ||
export * from './datetime' | ||
export * from './number' | ||
export * from './debugger' | ||
export * from './warnings' | ||
export * from './errors' | ||
export * from './types' |
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
Oops, something went wrong.