-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new package with vue extractor
- Loading branch information
Christoffer Jahren
committed
Mar 31, 2023
1 parent
f2961fc
commit bc10f9d
Showing
13 changed files
with
375 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ node_modules/ | |
.yalc/ | ||
yalc.lock | ||
dist | ||
build | ||
|
||
npm-debug.log | ||
yarn-error.log | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Vue extractor | ||
|
||
This package contains a custom extractors that handles Vue files. It supports extracting messages from script and setup scripts. | ||
|
||
## Installation | ||
|
||
```sh | ||
npm install @lingui/extractor-vue | ||
``` | ||
|
||
## Usage | ||
|
||
This custom extractor requires that you use typescript for your lingui configuration. | ||
|
||
```ts | ||
import { vueExtractor } from "@lingui/extractor-vue" | ||
import babel from "@lingui/cli/api/extractors/babel" | ||
|
||
const linguiConfig = { | ||
locales: ["en", "nb"], | ||
sourceLocale: "en", | ||
catalogs: [ | ||
{ | ||
path: "<rootDir>/src/{locale}", | ||
include: ["<rootDir>/src"], | ||
}, | ||
], | ||
extractors: [babel, vueExtractor], | ||
} | ||
export default linguiConfig | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"name": "@lingui/extractor-vue", | ||
"version": "4.0.0-next.4", | ||
"description": "Custom vue extractor to be used with CLI tool", | ||
"main": "./dist/index.cjs", | ||
"module": "./dist/index.mjs", | ||
"types": "./dist/index.d.ts", | ||
"keywords": [ | ||
"cli", | ||
"i18n", | ||
"internationalization", | ||
"i10n", | ||
"localization", | ||
"i9n", | ||
"translation" | ||
], | ||
"repository": "lingui/js-lingui", | ||
"bugs": "https://github.com/lingui/js-lingui/issues", | ||
"license": "MIT", | ||
"author": { | ||
"name": "Christoffer Jahren", | ||
"email": "christoffer@jahren.it" | ||
}, | ||
"scripts": { | ||
"build": "unbuild", | ||
"stub": "unbuild --stub" | ||
}, | ||
"engines": { | ||
"node": ">=16.0.0" | ||
}, | ||
"dependencies": { | ||
"@babel/core": "^7.21.0", | ||
"@lingui/cli": "^4.0.0-next.4", | ||
"@lingui/conf": "^4.0.0-next.4", | ||
"@vue/compiler-sfc": "^3.2.47" | ||
}, | ||
"devDependencies": { | ||
"@lingui/babel-plugin-extract-messages": "^4.0.0-next.4", | ||
"unbuild": "^1.1.2" | ||
}, | ||
"gitHead": "f2961fcb319265b08ddeda3aa2549d104fc4c6d4" | ||
} |
29 changes: 29 additions & 0 deletions
29
packages/extractor-vue/src/__test__/__snapshots__/extractor.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`vue extractor should extract from vue script 1`] = ` | ||
[ | ||
{ | ||
comment: undefined, | ||
context: undefined, | ||
id: SVG Title, | ||
message: undefined, | ||
}, | ||
{ | ||
comment: undefined, | ||
context: undefined, | ||
id: custom.id, | ||
message: SVG description, | ||
}, | ||
] | ||
`; | ||
|
||
exports[`vue extractor should extract from vue setup script 1`] = ` | ||
[ | ||
{ | ||
comment: undefined, | ||
context: undefined, | ||
id: Loading..., | ||
message: undefined, | ||
}, | ||
] | ||
`; |
15 changes: 15 additions & 0 deletions
15
packages/extractor-vue/src/__test__/example-script-setup.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<template> | ||
<span role="progressbar" aria-valuenow="0" :aria-valuetext="ariaValueText" /> | ||
</template> | ||
|
||
<script> | ||
export default { name: "fButton" } | ||
</script> | ||
|
||
<script setup> | ||
import { i18n } from "@lingui/core" | ||
const props = { | ||
ariaValueText: { type: String, default: i18n._("Loading...") }, | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<template> | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
width="16" | ||
height="16" | ||
fill="none" | ||
viewBox="0 0 16 16" | ||
> | ||
<title>{{ svgTitle }}</title> | ||
<desc>{{ svgDescription }}</desc> | ||
</svg> | ||
</template> | ||
|
||
<script> | ||
import { i18n } from "@lingui/core" | ||
export default { | ||
name: "fAffix", | ||
setup(props) { | ||
const svgTitle = i18n._("SVG Title") | ||
const svgDescription = i18n._( | ||
/*i18n*/ { id: "custom.id", message: "SVG description" } | ||
) | ||
return { svgTitle, svgDescription } | ||
}, | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { makeConfig } from "@lingui/conf" | ||
import fs from "fs" | ||
import path from "path" | ||
import { vueExtractor } from "../vue-extractor" | ||
import type { ExtractedMessage } from "@lingui/babel-plugin-extract-messages" | ||
|
||
/** | ||
* Remove origin from snapshot to avoid | ||
* issues with snapshot tests failing on CI | ||
* @param snapshot | ||
*/ | ||
function removeOriginFromSnapshot(snapshot: ExtractedMessage[]) { | ||
snapshot.forEach((entry) => { | ||
if ("origin" in entry) { | ||
delete entry.origin | ||
} | ||
}) | ||
} | ||
|
||
describe("vue extractor", () => { | ||
const linguiConfig = makeConfig({ | ||
locales: ["en", "nb"], | ||
sourceLocale: "en", | ||
rootDir: ".", | ||
catalogs: [ | ||
{ | ||
path: "<rootDir>/{locale}", | ||
include: ["<rootDir>"], | ||
exclude: [], | ||
}, | ||
], | ||
extractorParserOptions: { | ||
tsExperimentalDecorators: false, | ||
flow: false, | ||
}, | ||
}) | ||
|
||
it("should extract from vue script", async () => { | ||
const filePath = path.resolve(__dirname, "example-script.vue") | ||
const code = fs.readFileSync(filePath, "utf-8") | ||
|
||
let messages: ExtractedMessage[] = [] | ||
|
||
await vueExtractor.extract( | ||
"test.vue", | ||
code, | ||
(res) => { | ||
messages.push(res) | ||
}, | ||
{ | ||
linguiConfig, | ||
} | ||
) | ||
|
||
removeOriginFromSnapshot(messages) | ||
|
||
expect(messages).toMatchSnapshot() | ||
}) | ||
|
||
it("should extract from vue setup script", async () => { | ||
const filePath = path.resolve(__dirname, "example-script-setup.vue") | ||
const code = fs.readFileSync(filePath, "utf-8") | ||
|
||
let setupMessages: ExtractedMessage[] = [] | ||
|
||
await vueExtractor.extract( | ||
"test.vue", | ||
code, | ||
(res) => { | ||
setupMessages.push(res) | ||
}, | ||
{ | ||
linguiConfig, | ||
} | ||
) | ||
|
||
removeOriginFromSnapshot(setupMessages) | ||
|
||
expect(setupMessages).toMatchSnapshot() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { vueExtractor } from "./vue-extractor" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import * as vueCompiler from "@vue/compiler-sfc" | ||
// @ts-expect-error issue with typings, will be fixed shortly | ||
import babel from "@lingui/cli/api/extractors/babel" | ||
import type { ExtractorCtx, ExtractorType } from "@lingui/conf" | ||
|
||
export const vueExtractor: ExtractorType = { | ||
match(filename: string) { | ||
return filename.endsWith(".vue") | ||
}, | ||
extract( | ||
filename: string, | ||
code: string, | ||
onMessageExtracted, | ||
ctx: ExtractorCtx | ||
) { | ||
const { | ||
descriptor: { script, scriptSetup }, | ||
} = vueCompiler.parse(code, { filename, ignoreEmpty: true }) | ||
|
||
const finalCode = `${script?.content} ${scriptSetup?.content}` | ||
|
||
return babel.extract(filename, finalCode ?? code, onMessageExtracted, { | ||
sourcemaps: false, | ||
...ctx, | ||
}) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.