Skip to content

Commit

Permalink
Merge pull request #208 from transifex/cli-detect-vue-script-setup
Browse files Browse the repository at this point in the history
@transifex/cli: Detect Vue3 script setup
  • Loading branch information
Nikos Vasileiou committed Jan 11, 2024
2 parents 831b71d + 05f7955 commit 54f10e5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
7 changes: 7 additions & 0 deletions packages/cli/src/api/parsers/vue.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,19 @@ function vueElementVisitor(HASHES, relativeFile, options) {
function extractVuePhrases(HASHES, source, relativeFile, options) {
// Use the vue-template-compiler API to parse content
const vueContent = vueTemplateCompiler.parse(source);

// Get the JS Content from the file and extract hashes/phrases with Babel
if (vueContent.descriptor.script && vueContent.descriptor.script.content) {
const script = vueContent.descriptor.script.content;
babelExtractPhrases(HASHES, script, relativeFile, options);
}

// Also detect Vue3 script setup
if (vueContent.descriptor.scriptSetup && vueContent.descriptor.scriptSetup.content) {
const script = vueContent.descriptor.scriptSetup.content;
babelExtractPhrases(HASHES, script, relativeFile, options);
}

// Get the template content from the file and extract hashes/phrases with
// custom traverse function
if (vueContent.descriptor.template && vueContent.descriptor.template.content) {
Expand Down
8 changes: 6 additions & 2 deletions packages/cli/test/api/extract.hashedkeys.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,8 +543,12 @@ describe('extractPhrases with hashed keys', () => {
string: 'Text 5',
meta: { context: [], tags: [], occurrences: ['vuejs.vue'] },
},
'39bcf931264f8a4de0d4c993ba8e7094': {
string: 'Text 6',
d42dd30c271a7dc6ecfaddd2cee7e457: {
string: 'Text in script',
meta: { context: [], tags: [], occurrences: ['vuejs.vue'] },
},
'2d97d5484416727f585501ee9f842ab1': {
string: 'Text in script setup',
meta: { context: [], tags: [], occurrences: ['vuejs.vue'] },
},
});
Expand Down
12 changes: 8 additions & 4 deletions packages/cli/test/api/extract.sourcekeys.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,10 +484,6 @@ describe('extractPhrases with source keys', () => {
string: 'Text 5',
meta: { context: [], tags: [], occurrences: ['vuejs.vue'] },
},
'Text 6': {
string: 'Text 6',
meta: { context: [], tags: [], occurrences: ['vuejs.vue'] },
},
'Text 7': {
meta: {
context: [],
Expand Down Expand Up @@ -528,6 +524,14 @@ describe('extractPhrases with source keys', () => {
},
string: 'Text 9 with siblings',
},
'Text in script': {
string: 'Text in script',
meta: { context: [], tags: [], occurrences: ['vuejs.vue'] },
},
'Text in script setup': {
string: 'Text in script setup',
meta: { context: [], tags: [], occurrences: ['vuejs.vue'] },
},
});
});
});
13 changes: 12 additions & 1 deletion packages/cli/test/fixtures/vuejs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,23 @@
/>
</div>
</template>

<script>
import { defineComponent } from '@vue/composition-api'
export default defineComponent({
setup() {
t('Text 6')
t('Text in script')
},
})
</script>

<script setup>
import { defineComponent } from '@vue/composition-api'
export default defineComponent({
setup() {
t('Text in script setup')
},
})
</script>

0 comments on commit 54f10e5

Please sign in to comment.