Skip to content

Commit

Permalink
breaking: drop legacy compatible features for Vue 2 (#354)
Browse files Browse the repository at this point in the history
* breaking: drop legacy compatible features for Vue 2

* update lock file
  • Loading branch information
kazupon committed Mar 26, 2024
1 parent 5165c1d commit 4ecd69d
Show file tree
Hide file tree
Showing 16 changed files with 41 additions and 1,412 deletions.
1 change: 0 additions & 1 deletion packages/bundle-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"escodegen": "^2.1.0",
"estree-walker": "^2.0.2",
"jsonc-eslint-parser": "^2.3.0",
"magic-string": "^0.30.0",
"mlly": "^1.2.0",
"source-map-js": "^1.0.1",
"yaml-eslint-parser": "^1.2.2"
Expand Down
12 changes: 0 additions & 12 deletions packages/bundle-utils/src/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,6 @@ export function checkInstallPackage(
)
}

// eslint-disable-next-line @typescript-eslint/ban-types
export function checkVueI18nBridgeInstallPackage(debug: Function): boolean {
let ret = false
try {
debug(`vue-i18n-bridge load path: ${_require.resolve('vue-i18n-bridge')}`)
ret = true
} catch (e) {
debug(`cannot find 'vue-i18n-bridge'`, e)
}
return ret
}

type VueI18nVersion = '8' | '9' | 'unknown' | ''

export function getVueI18nVersion(debug: Function): VueI18nVersion {
Expand Down
6 changes: 1 addition & 5 deletions packages/bundle-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
export { generate as generateJSON } from './json'
export { generate as generateYAML } from './yaml'
export { generate as generateJavaScript } from './js'
export {
getVueI18nVersion,
checkInstallPackage,
checkVueI18nBridgeInstallPackage
} from './deps'
export { getVueI18nVersion, checkInstallPackage } from './deps'
export type { CodeGenOptions, CodeGenResult, DevEnv } from './codegen'
export type { InstalledPackage } from './deps'
43 changes: 6 additions & 37 deletions packages/bundle-utils/src/js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ export function generate(
targetSource: string | Buffer,
{
type = 'plain',
bridge = false,
exportESM = false,
filename = 'vue-i18n-loader.js',
inSourceMap = undefined,
locale = '',
Expand All @@ -44,8 +42,7 @@ export function generate(
useClassComponent = false,
allowDynamic = false,
jit = false
}: CodeGenOptions,
injector?: () => string
}: CodeGenOptions
): CodeGenResult<Node> {
const target = Buffer.isBuffer(targetSource)
? targetSource.toString()
Expand All @@ -54,8 +51,6 @@ export function generate(

const options = {
type,
bridge,
exportESM,
source: value,
sourceMap,
locale,
Expand Down Expand Up @@ -109,7 +104,7 @@ export function generate(
}
}

const codeMaps = _generate(generator, ast, options, injector)
const codeMaps = _generate(generator, ast, options)

const { code, map } = generator.context()
// if (map) {
Expand Down Expand Up @@ -155,25 +150,15 @@ function scanAst(ast: Node) {
function _generate(
generator: CodeGenerator,
node: Node,
options: CodeGenOptions = {},
injector?: () => string
options: CodeGenOptions = {}
): Map<string, RawSourceMap> {
const propsCountStack = [] as number[]
const pathStack = [] as string[]
const itemsCountStack = [] as number[]
const skipStack = [] as boolean[]
const { forceStringify } = generator.context()
const codeMaps = new Map<string, RawSourceMap>()
const {
type,
bridge,
exportESM,
sourceMap,
isGlobal,
locale,
useClassComponent,
jit
} = options
const { type, sourceMap, isGlobal, locale, useClassComponent, jit } = options

const codegenFn: CodeGenFunction = jit
? generateResourceAst
Expand All @@ -200,17 +185,11 @@ function _generate(
type === 'sfc' ? (!isGlobal ? '__i18n' : '__i18nGlobal') : ''
const localeName =
type === 'sfc' ? (locale != null ? locale : `""`) : ''
const exportSyntax = bridge
? exportESM
? `export default`
: `module.exports =`
: `export default`
const exportSyntax = 'export default'
generator.push(`${exportSyntax} function (Component) {`)
generator.indent()
// prettier-ignore
const componentVariable = bridge
? `Component.options || Component`
: useClassComponent
const componentVariable = useClassComponent
? `Component.__o || Component.__vccOpts || Component`
: `Component`
// prettier-ignore
Expand Down Expand Up @@ -383,16 +362,6 @@ function _generate(
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(`}`)
} else if (type === 'plain') {
Expand Down
65 changes: 7 additions & 58 deletions packages/bundle-utils/src/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ import {
traverseNodes,
getStaticJSONValue
} from 'jsonc-eslint-parser'
import { isString, friendlyJSONstringify } from '@intlify/shared'
import { isString } from '@intlify/shared'
import {
createCodeGenerator,
generateMessageFunction,
generateResourceAst,
mapLinesColumns,
excludeLocales
} from './codegen'
import { generateLegacyCode } from './legacy'
import MagicString from 'magic-string'

import type { RawSourceMap } from 'source-map-js'
import type { JSONProgram, JSONNode } from 'jsonc-eslint-parser/lib/parser/ast'
Expand All @@ -31,11 +29,7 @@ export function generate(
targetSource: string | Buffer,
{
type = 'plain',
legacy = false,
vueVersion = 'v2.6',
bridge = false,
onlyLocales = [],
exportESM = false,
filename = 'vue-i18n-loader.json',
inSourceMap = undefined,
locale = '',
Expand All @@ -48,8 +42,7 @@ export function generate(
escapeHtml = false,
useClassComponent = false,
jit = false
}: CodeGenOptions,
injector?: () => string
}: CodeGenOptions
): CodeGenResult<JSONProgram> {
let value = Buffer.isBuffer(targetSource)
? targetSource.toString()
Expand All @@ -60,8 +53,6 @@ export function generate(

const options = {
type,
bridge,
exportESM,
source: value,
sourceMap,
locale,
Expand Down Expand Up @@ -92,23 +83,7 @@ export function generate(
ast = parseJSON(value, { filePath: filename })
}

// for vue 2.x
if (legacy && type === 'sfc') {
const gen = () => friendlyJSONstringify(getStaticJSONValue(ast))
const code = generateLegacyCode({ isGlobal, vueVersion }, gen)
const s = new MagicString(code)
return {
ast,
code: s.toString(),
map: s.generateMap({
file: filename,
source: value,
includeContent: true
}) as unknown as RawSourceMap
}
}

const codeMaps = _generate(generator, ast, options, injector)
const codeMaps = _generate(generator, ast, options)

const { code, map } = generator.context()
// if (map) {
Expand All @@ -132,24 +107,14 @@ export function generate(
function _generate(
generator: CodeGenerator,
node: JSONProgram,
options: CodeGenOptions = {},
injector?: () => string
options: CodeGenOptions = {}
): Map<string, RawSourceMap> {
const propsCountStack = [] as number[]
const pathStack = [] as string[]
const itemsCountStack = [] as number[]
const { forceStringify } = generator.context()
const codeMaps = new Map<string, RawSourceMap>()
const {
type,
bridge,
exportESM,
sourceMap,
isGlobal,
locale,
useClassComponent,
jit
} = options
const { type, sourceMap, isGlobal, locale, useClassComponent, jit } = options

const codegenFn: CodeGenFunction = jit
? generateResourceAst
Expand All @@ -169,17 +134,11 @@ function _generate(
type === 'sfc' ? (!isGlobal ? '__i18n' : '__i18nGlobal') : ''
const localeName =
type === 'sfc' ? (locale != null ? locale : `""`) : ''
const exportSyntax = bridge
? exportESM
? `export default`
: `module.exports =`
: `export default`
const exportSyntax = 'export default'
generator.push(`${exportSyntax} function (Component) {`)
generator.indent()
// prettier-ignore
const componentVariable = bridge
? `Component.options || Component`
: useClassComponent
const componentVariable = useClassComponent
? `Component.__o || Component.__vccOpts || Component`
: `Component`
// prettier-ignore
Expand Down Expand Up @@ -297,16 +256,6 @@ function _generate(
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(`}`)
} else if (type === 'plain') {
Expand Down
24 changes: 0 additions & 24 deletions packages/bundle-utils/src/legacy.ts

This file was deleted.

0 comments on commit 4ecd69d

Please sign in to comment.