Skip to content

Commit

Permalink
feat(bundle-utils): support vue 2.7 custom block (#331)
Browse files Browse the repository at this point in the history
* feat(bundle-utils): support vue 2.7 custom block

* fix: remove unneccesary snapshot
  • Loading branch information
kazupon committed Dec 13, 2023
1 parent 48f79ec commit 12e3029
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/bundle-utils/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export interface SourceLocationable {
export interface CodeGenOptions {
type?: 'plain' | 'sfc' | 'bare'
legacy?: boolean
vueVersion?: 'v2.6' | 'v2.7'
bridge?: boolean
exportESM?: boolean
onlyLocales?: string[]
Expand Down
3 changes: 2 additions & 1 deletion packages/bundle-utils/src/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export function generate(
{
type = 'plain',
legacy = false,
vueVersion = 'v2.6',
bridge = false,
onlyLocales = [],
exportESM = false,
Expand Down Expand Up @@ -94,7 +95,7 @@ export function generate(
// for vue 2.x
if (legacy && type === 'sfc') {
const gen = () => friendlyJSONstringify(getStaticJSONValue(ast))
const code = generateLegacyCode(options, gen)
const code = generateLegacyCode({ isGlobal, vueVersion }, gen)
const s = new MagicString(code)
return {
ast,
Expand Down
7 changes: 5 additions & 2 deletions packages/bundle-utils/src/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import { friendlyJSONstringify } from '@intlify/shared'
import type { CodeGenOptions } from './codegen'

export function generateLegacyCode(
{ isGlobal }: CodeGenOptions,
{
isGlobal = false,
vueVersion = 'v2.6'
}: Pick<CodeGenOptions, 'isGlobal' | 'vueVersion'>,
generator: () => string
) {
// prettier-ignore
const componentNamespace = `Component.options`
const componentNamespace = `Component${ vueVersion === 'v2.6' ? '.options' : ''}`
const variableName = !isGlobal ? '__i18n' : '__i18nGlobal'
const exportSyntax = 'export default'
const code = `${exportSyntax} function (Component) {
Expand Down
3 changes: 2 additions & 1 deletion packages/bundle-utils/src/yaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export function generate(
{
type = 'plain',
legacy = false,
vueVersion = 'v2.6',
bridge = false,
onlyLocales = [],
exportESM = false,
Expand Down Expand Up @@ -94,7 +95,7 @@ export function generate(
// for vue 2.x
if (legacy && type === 'sfc') {
const gen = () => friendlyJSONstringify(getStaticYAMLValue(ast))
const code = generateLegacyCode(options, gen)
const code = generateLegacyCode({ isGlobal, vueVersion }, gen)
const s = new MagicString(code)
return {
ast,
Expand Down
23 changes: 23 additions & 0 deletions packages/bundle-utils/test/legacy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { generateLegacyCode } from '../src/legacy'

test('not global', () => {
expect(
generateLegacyCode({ isGlobal: false }, () =>
JSON.stringify({ foo: 'bar' })
)
).toMatch('Component.options.__i18n =')
})

test('global', () => {
expect(
generateLegacyCode({ isGlobal: true }, () => JSON.stringify({ foo: 'bar' }))
).toMatch('Component.options.__i18nGlobal =')
})

test('vue 2.7', () => {
expect(
generateLegacyCode({ vueVersion: 'v2.7' }, () =>
JSON.stringify({ foo: 'bar' })
)
).toMatch('Component.__i18n =')
})

0 comments on commit 12e3029

Please sign in to comment.