Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support vue i18n bridge #47

Merged
merged 4 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
}
},
"devDependencies": {
"@intlify/runtime": "^9.1.6",
"@intlify/runtime": "^9.1.7",
"@kazupon/lerna-changelog": "^4.3.0",
"@octokit/rest": "^18.6.0",
"@rollup/plugin-alias": "^3.1.2",
Expand All @@ -35,52 +35,54 @@
"@types/debug": "^4.1.5",
"@types/eslint": "^7.2.6",
"@types/eslint-visitor-keys": "^1.0.0",
"@types/jest": "^26.0.19",
"@types/jest": "^27.0.2",
"@types/jsdom": "^16.2.5",
"@types/js-yaml": "^4.0.3",
"@types/json5": "^2.2.0",
"@types/loader-utils": "^2.0.0",
"@types/memory-fs": "^0.3.2",
"@types/node": "^15.12.4",
"@types/semver": "^7.3.6",
"@types/webpack": "^4.41.26",
"@types/webpack-merge": "^4.1.5",
"@typescript-eslint/eslint-plugin": "^4.28.0",
"@typescript-eslint/parser": "^4.28.0",
"@typescript-eslint/eslint-plugin": "^4.30.0",
"@typescript-eslint/parser": "^4.30.0",
"@vitejs/plugin-vue": "^1.2.5",
"@vue/compiler-sfc": "^3.0.11",
"babel-loader": "^8.1.0",
"babel-loader": "^8.2.2",
"chalk": "^4.1.1",
"debug": "^4.1.1",
"eslint": "^7.30.0",
"eslint": "^7.31.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.0",
"eslint-plugin-vue": "^7.13.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-vue": "^7.18.0",
"eslint-plugin-vue-libs": "^4.0.0",
"esno": "^0.7.3",
"execa": "^5.1.1",
"jest": "^26.6.3",
"jest-puppeteer": "^5.0.0",
"jest": "^27.2.3",
"jest-puppeteer": "^6.0.0",
"jest-watch-typeahead": "^0.6.4",
"jsdom": "^16.4.0",
"lint-staged": "^11.0.0",
"memory-fs": "^0.5.0",
"minimist": "^1.2.5",
"npm-run-all": "^4.1.5",
"opener": "^1.5.2",
"prettier": "^2.3.2",
"prettier": "^2.4.1",
"prompts": "^2.4.1",
"puppeteer": "^9.0.0",
"puppeteer": "^10.4.0",
"rollup": "^2.53.1",
"rollup-plugin-vue": "^6.0.0",
"secretlint": "^3.2.0",
"serve": "^11.3.2",
"source-map": "^0.6.1",
"tiny-glob": "^0.2.9",
"ts-jest": "^26.5.0",
"ts-jest": "^27.0.5",
"typescript": "^4.2.3",
"typescript-eslint-language-service": "^4.1.3",
"vite": "^2.4.2",
"vue": "3.0.11",
"vue-i18n": "^9.1.6",
"vue-i18n": "^9.2.0-beta.9",
"vue-loader": "^16.3.0",
"webpack": "^4.46.0",
"webpack-cli": "^3.3.12",
Expand Down
1 change: 1 addition & 0 deletions packages/bundle-utils/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface SourceLocationable {
*/
export interface CodeGenOptions {
type?: 'plain' | 'sfc' | 'bare'
bridge?: boolean
source?: string
sourceMap?: boolean
filename?: string
Expand Down
31 changes: 24 additions & 7 deletions packages/bundle-utils/src/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ export function generate(
targetSource: string | Buffer,
{
type = 'plain',
bridge = false,
filename = 'vue-i18n-loader.json',
inSourceMap = undefined,
locale = '',
isGlobal = false,
sourceMap = false,
env = 'development',
forceStringify = false
}: CodeGenOptions
}: CodeGenOptions,
injector?: () => string
): CodeGenResult<JSONProgram> {
const target = Buffer.isBuffer(targetSource)
? targetSource.toString()
Expand All @@ -40,6 +42,7 @@ export function generate(

const options = {
type,
bridge,
source: value,
sourceMap,
locale,
Expand All @@ -52,7 +55,7 @@ export function generate(
const generator = createCodeGenerator(options)

const ast = parseJSON(value, { filePath: filename })
const codeMaps = generateNode(generator, ast, options)
const codeMaps = generateNode(generator, ast, options, injector)

const { code, map } = generator.context()
// if (map) {
Expand All @@ -75,13 +78,16 @@ export function generate(
function generateNode(
generator: CodeGenerator,
node: JSONProgram,
options: CodeGenOptions
options: CodeGenOptions,
injector?: () => string
): Map<string, RawSourceMap> {
const propsCountStack = [] as number[]
const itemsCountStack = [] as number[]
const { forceStringify } = generator.context()
const codeMaps = new Map<string, RawSourceMap>()
const { type, sourceMap, isGlobal, locale } = options
const { type, bridge, sourceMap, isGlobal, locale } = options

const componentNamespace = bridge ? `Component.options.` : `Component.`

traverseNodes(node, {
enterNode(node: JSONNode, parent: JSONNode) {
Expand All @@ -95,12 +101,13 @@ function generateNode(
type === 'sfc' ? (!isGlobal ? '__i18n' : '__i18nGlobal') : ''
const localeName =
type === 'sfc' ? (locale != null ? locale : `""`) : ''
generator.push(`export default function (Component) {`)
const exportSyntax = bridge ? `module.exports =` : `export default`
generator.push(`${exportSyntax} function (Component) {`)
generator.indent()
generator.pushline(
`Component.${variableName} = Component.${variableName} || []`
`${componentNamespace}${variableName} = ${componentNamespace}${variableName} || []`
)
generator.push(`Component.${variableName}.push({`)
generator.push(`${componentNamespace}${variableName}.push({`)
generator.indent()
generator.pushline(`"locale": ${JSON.stringify(localeName)},`)
generator.push(`"resource": `)
Expand Down Expand Up @@ -196,6 +203,16 @@ function generateNode(
if (type === 'sfc') {
generator.deindent()
generator.push(`})`)
if (bridge && injector) {
generator.newline()
generator.pushline(
`${componentNamespace}__i18nBridge = ${componentNamespace}__i18nBridge || []`
)
generator.pushline(
`${componentNamespace}__i18nBridge.push('${injector()}')`
)
generator.pushline(`delete ${componentNamespace}_Ctor`)
}
generator.deindent()
generator.pushline(`}`)
}
Expand Down
31 changes: 24 additions & 7 deletions packages/bundle-utils/src/yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,24 @@ export function generate(
targetSource: string | Buffer,
{
type = 'plain',
bridge = false,
filename = 'vue-i18n-loader.yaml',
inSourceMap = undefined,
locale = '',
isGlobal = false,
sourceMap = false,
env = 'development',
forceStringify = false
}: CodeGenOptions
}: CodeGenOptions,
injector?: () => string
): CodeGenResult<YAMLProgram> {
const value = Buffer.isBuffer(targetSource)
? targetSource.toString()
: targetSource

const options = {
type,
bridge,
source: value,
sourceMap,
locale,
Expand All @@ -48,7 +51,7 @@ export function generate(
const generator = createCodeGenerator(options)

const ast = parseYAML(value, { filePath: filename })
const codeMaps = generateNode(generator, ast, options)
const codeMaps = generateNode(generator, ast, options, injector)

const { code, map } = generator.context()
// prettier-ignore
Expand All @@ -65,13 +68,16 @@ export function generate(
function generateNode(
generator: CodeGenerator,
node: YAMLProgram,
options: CodeGenOptions
options: CodeGenOptions,
injector?: () => string
): Map<string, RawSourceMap> {
const propsCountStack = [] as number[]
const itemsCountStack = [] as number[]
const { forceStringify } = generator.context()
const codeMaps = new Map<string, RawSourceMap>()
const { type, sourceMap, isGlobal, locale } = options
const { type, bridge, sourceMap, isGlobal, locale } = options

const componentNamespace = bridge ? `Component.options.` : `Component.`

traverseNodes(node, {
enterNode(node: YAMLNode, parent: YAMLNode) {
Expand All @@ -84,12 +90,13 @@ function generateNode(
type === 'sfc' ? (!isGlobal ? '__i18n' : '__i18nGlobal') : ''
const localeName =
type === 'sfc' ? (locale != null ? locale : `""`) : ''
generator.push(`export default function (Component) {`)
const exportSyntax = bridge ? `module.exports =` : `export default`
generator.push(`${exportSyntax} function (Component) {`)
generator.indent()
generator.pushline(
`Component.${variableName} = Component.${variableName} || []`
`${componentNamespace}${variableName} = ${componentNamespace}${variableName} || []`
)
generator.push(`Component.${variableName}.push({`)
generator.push(`${componentNamespace}${variableName}.push({`)
generator.indent()
generator.pushline(`"locale": ${JSON.stringify(localeName)},`)
generator.push(`"resource": `)
Expand Down Expand Up @@ -185,6 +192,16 @@ function generateNode(
if (type === 'sfc') {
generator.deindent()
generator.push(`})`)
if (bridge && injector) {
generator.newline()
generator.pushline(
`${componentNamespace}__i18nBridge = ${componentNamespace}__i18nBridge || []`
)
generator.pushline(
`${componentNamespace}__i18nBridge.push('${injector()}')`
)
generator.pushline(`delete ${componentNamespace}_Ctor`)
}
generator.deindent()
generator.push(`}`)
}
Expand Down
Loading