-
Notifications
You must be signed in to change notification settings - Fork 397
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vite-plugin): support different extensions for catalogs. Improve…
… coverage (#1526)
- Loading branch information
1 parent
b4db627
commit 8e94c47
Showing
19 changed files
with
368 additions
and
250 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
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 |
---|---|---|
|
@@ -39,6 +39,6 @@ | |
"@lingui/conf": "4.0.0-next.1" | ||
}, | ||
"devDependencies": { | ||
"vite": "3.2.4" | ||
"vite": "4.1.4" | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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,21 @@ | ||
import { UserConfig } from "vite" | ||
import { lingui } from "../src/index" | ||
import path from "path" | ||
|
||
export function createDefaultViteConfig(dirname: string): UserConfig { | ||
return { | ||
build: { | ||
lib: { | ||
entry: path.resolve(dirname, "entrypoint.js"), | ||
fileName: "bundle", | ||
formats: ["cjs"], | ||
}, | ||
}, | ||
|
||
plugins: [ | ||
lingui({ | ||
cwd: dirname, | ||
}), | ||
], | ||
} | ||
} |
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 |
---|---|---|
@@ -1,15 +1,62 @@ | ||
import path from "path" | ||
import { lingui } from "../src" | ||
import { exec as _exec } from "child_process" | ||
import { mkdtempSync } from "fs" | ||
import os from "os" | ||
|
||
const skipOnWindows = os.platform() === "win32" ? it.skip : it | ||
|
||
describe("vite-plugin", () => { | ||
it("should return compiled catalog", async () => { | ||
const p = lingui({ | ||
configPath: path.resolve(__dirname, ".linguirc"), | ||
}) | ||
const result = await (p.transform as any)( | ||
"", | ||
path.join(__dirname, "locale", "en", "messages.po") | ||
) | ||
expect(result).toMatchSnapshot() | ||
skipOnWindows("should return compiled catalog", async () => { | ||
const mod = await runVite(`po-format/vite.config.ts`) | ||
expect((await mod.load()).messages).toMatchSnapshot() | ||
}) | ||
|
||
skipOnWindows("should return compiled catalog json", async () => { | ||
const mod = await runVite(`json-format/vite.config.ts`) | ||
|
||
expect((await mod.load()).messages).toMatchSnapshot() | ||
}) | ||
}) | ||
|
||
async function runVite(configPath: string) { | ||
// we could not use Vite directly using nodejs api because Vite is native ESM module | ||
// and Jest should be reconfigured completely to be able to require such modules | ||
const packageJsonPath = require.resolve("vite/package.json") | ||
const packageJson = require("vite/package.json") | ||
const viteExecutable = path.resolve( | ||
path.dirname(packageJsonPath), | ||
packageJson.bin.vite | ||
) | ||
|
||
const outDir = mkdtempSync( | ||
path.join(os.tmpdir(), `lingui-test-${process.pid}`) | ||
) | ||
const command = | ||
viteExecutable + | ||
` build -c ` + | ||
path.resolve(__dirname, configPath) + | ||
` --emptyOutDir --outDir ${outDir}` | ||
await exec(command) | ||
|
||
return await import(path.resolve(outDir, "bundle.js")) | ||
} | ||
|
||
function exec(cmd: string) { | ||
const _options = { | ||
env: process.env, | ||
} | ||
return new Promise((resolve, reject) => { | ||
_exec(cmd, _options, (error, stdout, stderr) => { | ||
stdout = stdout.trim() | ||
stderr = stderr.trim() | ||
|
||
if (error === null) { | ||
resolve({ stdout, stderr }) | ||
} else { | ||
reject({ error, stdout, stderr }) | ||
console.error(stdout) | ||
console.error(stderr) | ||
} | ||
}) | ||
}) | ||
} |
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,7 @@ | ||
{ | ||
"locales": ["en"], | ||
"catalogs": [{ | ||
"path": "<rootDir>/locale/{locale}" | ||
}], | ||
"format": "minimal" | ||
} |
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,3 @@ | ||
export async function load() { | ||
return await import("./locale/en.json?lingui") | ||
} |
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,4 @@ | ||
{ | ||
"key": "Message", | ||
"key2": "Hello {name}" | ||
} |
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,3 @@ | ||
import { createDefaultViteConfig } from "../default-vite.config" | ||
|
||
export default createDefaultViteConfig(__dirname) |
This file was deleted.
Oops, something went wrong.
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,7 @@ | ||
{ | ||
"locales": ["en"], | ||
"catalogs": [{ | ||
"path": "<rootDir>/locale/{locale}" | ||
}], | ||
"format": "po" | ||
} |
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,3 @@ | ||
export async function load() { | ||
return import("./locale/en.po") | ||
} |
File renamed without changes.
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,3 @@ | ||
import { createDefaultViteConfig } from "../default-vite.config" | ||
|
||
export default createDefaultViteConfig(__dirname) |
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.