-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Introduced @lingui/snowpack-loader
- Loading branch information
Sergio Moreno
committed
Jan 18, 2021
1 parent
8ac50ab
commit 1101606
Showing
11 changed files
with
207 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
[![License][badge-license]][license] | ||
[![Version][badge-version]][package] | ||
[![Downloads][badge-downloads]][package] | ||
|
||
# @lingui/snowpack-loader | ||
|
||
> Snowpack plugin which compiles on the fly the .po files for auto-refreshing. In summary, `lingui compile` command isn't required when using this plugin | ||
`@lingui/snowpack-loader` is part of [LinguiJS][linguijs]. See the [documentation][documentation] for all information, tutorials and examples. | ||
|
||
## Installation | ||
|
||
```sh | ||
npm install --save-dev @lingui/snowpack-loader | ||
# yarn add --dev @lingui/snowpack-loader | ||
``` | ||
|
||
## Usage | ||
|
||
### Via `snowpack.config.js` (Recommended) | ||
|
||
**snowpack.config.js** | ||
|
||
```js | ||
module.exports = { | ||
plugins: [ | ||
'@lingui/snowpack-loader', | ||
], | ||
} | ||
``` | ||
|
||
## License | ||
|
||
[MIT][license] | ||
|
||
[license]: https://github.com/lingui/js-lingui/blob/main/LICENSE | ||
[linguijs]: https://github.com/lingui/js-lingui | ||
[documentation]: https://lingui.js.org/ | ||
[package]: https://www.npmjs.com/package/@lingui/snowpack-loader | ||
[badge-downloads]: https://img.shields.io/npm/dw/@lingui/snowpack-loader.svg | ||
[badge-version]: https://img.shields.io/npm/v/@lingui/snowpack-loader.svg | ||
[badge-license]: https://img.shields.io/npm/l/@lingui/snowpack-loader.svg |
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 { default } from "./src" |
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,33 @@ | ||
{ | ||
"name": "@lingui/snowpack-loader", | ||
"version": "3.4.0", | ||
"description": "Snowpack plugin for Lingui message catalogs", | ||
"main": "index.js", | ||
"license": "MIT", | ||
"keywords": [ | ||
"snowpack-plugin", | ||
"i18n", | ||
"snowpack", | ||
"linguijs", | ||
"internationalization", | ||
"i10n", | ||
"localization", | ||
"i9n", | ||
"translation" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/lingui/js-lingui.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/lingui/js-lingui/issues" | ||
}, | ||
"engines": { | ||
"node": ">=10.0.0" | ||
}, | ||
"dependencies": { | ||
"@babel/runtime": "^7", | ||
"@lingui/cli": "^3.4.0", | ||
"@lingui/conf": "^3.4.0" | ||
} | ||
} |
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,54 @@ | ||
import path from "path" | ||
import { getConfig } from "@lingui/conf" | ||
import { createCompiledCatalog, getCatalogs, getCatalogForFile } from "@lingui/cli/api" | ||
|
||
type LinguiConfigOpts = { | ||
cwd?: string; | ||
configPath?: string; | ||
skipValidation?: boolean; | ||
} | ||
type SnowpackLoadOpts = { | ||
filePath: string | ||
} | ||
function extractLinguiMessages(_?, linguiConfig: LinguiConfigOpts = {}) { | ||
const strict = process.env.NODE_ENV !== 'production' | ||
const config = getConfig(linguiConfig) | ||
|
||
return { | ||
name: '@lingui/snowpack-loader', | ||
resolve: { | ||
input: ['.po'], | ||
output: ['.js'], | ||
}, | ||
async load({ filePath }: SnowpackLoadOpts) { | ||
const catalogRelativePath = path.resolve(config.rootDir, filePath) | ||
|
||
const fileCatalog = getCatalogForFile( | ||
catalogRelativePath, | ||
getCatalogs(config) | ||
) | ||
|
||
const { locale, catalog } = fileCatalog | ||
const catalogs = catalog.readAll() | ||
|
||
const messages = Object.keys(catalogs[locale]).reduce((acc, key) => { | ||
acc[key] = catalog.getTranslation(catalogs, locale, key, { | ||
fallbackLocales: config.fallbackLocales, | ||
sourceLocale: config.sourceLocale, | ||
}) | ||
|
||
return acc | ||
}, {}) | ||
|
||
const compiled = createCompiledCatalog(locale, messages, { | ||
strict, | ||
namespace: config.compileNamespace, | ||
pseudoLocale: config.pseudoLocale, | ||
}) | ||
|
||
return compiled | ||
} | ||
} | ||
} | ||
|
||
export default extractLinguiMessages |
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,18 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`snowpack-loader should return compiled catalog 1`] = `/*eslint-disable*/module.exports={messages:{"Hello World":"Hello World","My name is {name}":["My name is ",["name"]]}};`; | ||
|
||
exports[`snowpack-loader to match snapshot 1`] = ` | ||
Object { | ||
load: [Function], | ||
name: @lingui/snowpack-loader, | ||
resolve: Object { | ||
input: Array [ | ||
.po, | ||
], | ||
output: Array [ | ||
.js, | ||
], | ||
}, | ||
} | ||
`; |
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 @@ | ||
module.exports = { | ||
locales: ["en", "cs"] | ||
} |
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,5 @@ | ||
msgid "Hello World" | ||
msgstr "Hello World" | ||
|
||
msgid "My name is {name}" | ||
msgstr "My name is {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,5 @@ | ||
msgid "Hello World" | ||
msgstr "Hello World" | ||
|
||
msgid "My name is {name}" | ||
msgstr "My name is {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,35 @@ | ||
import path from "path" | ||
import snowpackPlugin from "../src" | ||
|
||
describe("snowpack-loader", () => { | ||
beforeEach(() => { | ||
process.env.LINGUI_EXTRACT = "1" | ||
process.env.LINGUI_CONFIG = path.join( | ||
__dirname, | ||
"fixtures", | ||
"lingui.config.js" | ||
) | ||
}) | ||
|
||
it("to match snapshot", () => { | ||
const p = snowpackPlugin(null, { | ||
configPath: process.env.LINGUI_CONFIG | ||
}); | ||
expect(p).toMatchSnapshot(); | ||
}) | ||
|
||
it("should return compiled catalog", async() => { | ||
const p = snowpackPlugin(null, { | ||
configPath: process.env.LINGUI_CONFIG | ||
}) | ||
const filePath = path.join( | ||
__dirname, | ||
"fixtures", | ||
"locale", | ||
"en", | ||
"messages.po" | ||
) | ||
const result = await p.load({ filePath }) | ||
expect(result).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
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