diff --git a/CHANGELOG.md b/CHANGELOG.md index 94727f4..ad8bae1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ This changelog is generated by [GitHub Releases](https://github.com/intlify/vue- ###    🐞 Bug Fixes -- Fully cjs/esm dual pacakges  -  by @kazupon in https://github.com/intlify/vue-i18n-extensions/issues/222 [(05597)](https://github.com/intlify/vue-i18n-extensions/commit/05597cb) +- Fully cjs/esm dual packages  -  by @kazupon in https://github.com/intlify/vue-i18n-extensions/issues/222 [(05597)](https://github.com/intlify/vue-i18n-extensions/commit/05597cb) #####     [View changes on GitHub](https://github.com/intlify/vue-i18n-extensions/compare/v6.0.4...v6.0.5) diff --git a/src/transform.ts b/src/transform.ts index 098cff8..bf41a52 100644 --- a/src/transform.ts +++ b/src/transform.ts @@ -99,6 +99,8 @@ const enum ConstantTypes { CAN_STRINGIFY } +const GLOBAL_TRANSLATE_SIGNATURE = '$t' + /** * Transform `v-t` custom directive * @@ -166,7 +168,7 @@ export function transformVTDirective< : isArray(options.translationSignatures) ? options.translationSignatures : ['t'] - translationSignatures.push(`$t`) // fallback to global scope + translationSignatures.push(GLOBAL_TRANSLATE_SIGNATURE) // fallback to global scope return (dir, node, context) => { const { exp, loc } = dir @@ -407,9 +409,12 @@ function generateTranslationCallableSignatures( context: TransformContext, translationSignatures: string[] ): string { - const { prefixIdentifiers, bindingMetadata } = context + const { prefixIdentifiers, bindingMetadata, inline } = context return translationSignatures .map(signature => { + if (inline && signature !== GLOBAL_TRANSLATE_SIGNATURE) { + return signature + } const type = hasOwn(bindingMetadata, signature) && bindingMetadata[signature] const bindingContext = prefixIdentifiers ? (type && type.startsWith('setup')) || type === BindingTypes.LITERAL_CONST diff --git a/test/__snapshots__/transform.test.ts.snap b/test/__snapshots__/transform.test.ts.snap index 4bab886..5cdecc1 100644 --- a/test/__snapshots__/transform.test.ts.snap +++ b/test/__snapshots__/transform.test.ts.snap @@ -5839,3 +5839,304 @@ exports[`script setup >
2`] = ` "type": 0, } `; + +exports[`script setup inline >
1`] = ` +"(_ctx, _cache) => { + return (_openBlock(), _createElementBlock("div", null, _toDisplayString((t || _ctx.$t)('hello', { }, { })), 1 /* TEXT */)) +}" +`; + +exports[`script setup inline >
2`] = ` +{ + "cached": 0, + "children": [ + { + "children": [ + { + "content": { + "children": [ + { + "constType": 0, + "content": "(t || _ctx.$t)('hello', { }, { })", + "isStatic": false, + "loc": { + "end": { + "column": 19, + "line": 1, + "offset": 18, + }, + "source": "v-t="'hello'"", + "start": { + "column": 6, + "line": 1, + "offset": 5, + }, + }, + "type": 4, + }, + ], + "loc": { + "end": { + "column": 1, + "line": 1, + "offset": 0, + }, + "source": "", + "start": { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "type": 8, + }, + "type": 5, + }, + ], + "codegenNode": { + "children": { + "content": { + "children": [ + { + "constType": 0, + "content": "(t || _ctx.$t)('hello', { }, { })", + "isStatic": false, + "loc": { + "end": { + "column": 19, + "line": 1, + "offset": 18, + }, + "source": "v-t="'hello'"", + "start": { + "column": 6, + "line": 1, + "offset": 5, + }, + }, + "type": 4, + }, + ], + "loc": { + "end": { + "column": 1, + "line": 1, + "offset": 0, + }, + "source": "", + "start": { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "type": 8, + }, + "type": 5, + }, + "directives": undefined, + "disableTracking": false, + "dynamicProps": undefined, + "isBlock": true, + "isComponent": false, + "loc": { + "end": { + "column": 21, + "line": 1, + "offset": 20, + }, + "source": "
", + "start": { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "patchFlag": "1 /* TEXT */", + "props": undefined, + "tag": ""div"", + "type": 13, + }, + "isSelfClosing": true, + "loc": { + "end": { + "column": 21, + "line": 1, + "offset": 20, + }, + "source": "
", + "start": { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "ns": 0, + "props": [ + { + "arg": undefined, + "exp": { + "ast": Node { + "comments": [], + "end": 8, + "errors": [], + "extra": { + "parenStart": 0, + "parenthesized": true, + "raw": "'hello'", + "rawValue": "hello", + }, + "loc": SourceLocation { + "end": Position { + "column": 8, + "index": 8, + "line": 1, + }, + "filename": undefined, + "identifierName": undefined, + "start": Position { + "column": 1, + "index": 1, + "line": 1, + }, + }, + "start": 1, + "type": "StringLiteral", + "value": "hello", + }, + "constType": 3, + "content": "'hello'", + "identifiers": [], + "isStatic": false, + "loc": { + "end": { + "column": 18, + "line": 1, + "offset": 17, + }, + "source": "'hello'", + "start": { + "column": 11, + "line": 1, + "offset": 10, + }, + }, + "type": 4, + }, + "loc": { + "end": { + "column": 19, + "line": 1, + "offset": 18, + }, + "source": "v-t="'hello'"", + "start": { + "column": 6, + "line": 1, + "offset": 5, + }, + }, + "modifiers": [], + "name": "t", + "rawName": "v-t", + "type": 7, + }, + ], + "tag": "div", + "tagType": 0, + "type": 1, + }, + ], + "codegenNode": { + "children": { + "content": { + "children": [ + { + "constType": 0, + "content": "(t || _ctx.$t)('hello', { }, { })", + "isStatic": false, + "loc": { + "end": { + "column": 19, + "line": 1, + "offset": 18, + }, + "source": "v-t="'hello'"", + "start": { + "column": 6, + "line": 1, + "offset": 5, + }, + }, + "type": 4, + }, + ], + "loc": { + "end": { + "column": 1, + "line": 1, + "offset": 0, + }, + "source": "", + "start": { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "type": 8, + }, + "type": 5, + }, + "directives": undefined, + "disableTracking": false, + "dynamicProps": undefined, + "isBlock": true, + "isComponent": false, + "loc": { + "end": { + "column": 21, + "line": 1, + "offset": 20, + }, + "source": "
", + "start": { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "patchFlag": "1 /* TEXT */", + "props": undefined, + "tag": ""div"", + "type": 13, + }, + "components": [], + "directives": [], + "filters": [], + "helpers": Set { + Symbol(toDisplayString), + Symbol(openBlock), + Symbol(createElementBlock), + }, + "hoists": [], + "imports": [], + "loc": { + "end": { + "column": 21, + "line": 1, + "offset": 20, + }, + "source": "
", + "start": { + "column": 1, + "line": 1, + "offset": 0, + }, + }, + "source": "
", + "temps": 0, + "transformed": true, + "type": 0, +} +`; diff --git a/test/transform.test.ts b/test/transform.test.ts index 50553c9..f5197c2 100644 --- a/test/transform.test.ts +++ b/test/transform.test.ts @@ -135,6 +135,24 @@ test('script setup', () => { expect(ast).toMatchSnapshot(source) }) +test('script setup inline', () => { + const transformVT = transformVTDirective() + const source = `
` + const bindingMetadata = { + t: BindingTypes.SETUP_CONST + } + const { code, ast } = compile(source, { + mode: 'function', + hoistStatic: false, + prefixIdentifiers: true, + bindingMetadata, + inline: true, + directiveTransforms: { t: transformVT } + }) + expect(code).toMatchSnapshot(source) + expect(ast).toMatchSnapshot(source) +}) + describe('legacy', () => { test('path', () => { const transformVT = transformVTDirective({ mode: 'legacy' })