From 11016060f7b982d991da59b85b43268867178a6d Mon Sep 17 00:00:00 2001 From: Sergio Moreno Date: Mon, 18 Jan 2021 12:45:31 +0100 Subject: [PATCH] feat: Introduced @lingui/snowpack-loader --- packages/snowpack-loader/README.md | 42 +++++++++++++++ packages/snowpack-loader/index.js | 1 + packages/snowpack-loader/package.json | 33 ++++++++++++ packages/snowpack-loader/src/index.ts | 54 +++++++++++++++++++ .../test/__snapshots__/index.ts.snap | 18 +++++++ .../test/fixtures/lingui.config.js | 3 ++ .../test/fixtures/locale/cs/messages.po | 5 ++ .../test/fixtures/locale/en/messages.po | 5 ++ packages/snowpack-loader/test/index.ts | 35 ++++++++++++ scripts/build/bundles.js | 4 ++ yarn.lock | 7 +++ 11 files changed, 207 insertions(+) create mode 100644 packages/snowpack-loader/README.md create mode 100644 packages/snowpack-loader/index.js create mode 100644 packages/snowpack-loader/package.json create mode 100644 packages/snowpack-loader/src/index.ts create mode 100644 packages/snowpack-loader/test/__snapshots__/index.ts.snap create mode 100644 packages/snowpack-loader/test/fixtures/lingui.config.js create mode 100644 packages/snowpack-loader/test/fixtures/locale/cs/messages.po create mode 100644 packages/snowpack-loader/test/fixtures/locale/en/messages.po create mode 100644 packages/snowpack-loader/test/index.ts diff --git a/packages/snowpack-loader/README.md b/packages/snowpack-loader/README.md new file mode 100644 index 000000000..e5cea9cd3 --- /dev/null +++ b/packages/snowpack-loader/README.md @@ -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 diff --git a/packages/snowpack-loader/index.js b/packages/snowpack-loader/index.js new file mode 100644 index 000000000..c14e96c06 --- /dev/null +++ b/packages/snowpack-loader/index.js @@ -0,0 +1 @@ +export { default } from "./src" diff --git a/packages/snowpack-loader/package.json b/packages/snowpack-loader/package.json new file mode 100644 index 000000000..28d7d5c2b --- /dev/null +++ b/packages/snowpack-loader/package.json @@ -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" + } +} diff --git a/packages/snowpack-loader/src/index.ts b/packages/snowpack-loader/src/index.ts new file mode 100644 index 000000000..a30f8b396 --- /dev/null +++ b/packages/snowpack-loader/src/index.ts @@ -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 \ No newline at end of file diff --git a/packages/snowpack-loader/test/__snapshots__/index.ts.snap b/packages/snowpack-loader/test/__snapshots__/index.ts.snap new file mode 100644 index 000000000..0886ca903 --- /dev/null +++ b/packages/snowpack-loader/test/__snapshots__/index.ts.snap @@ -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, + ], + }, +} +`; diff --git a/packages/snowpack-loader/test/fixtures/lingui.config.js b/packages/snowpack-loader/test/fixtures/lingui.config.js new file mode 100644 index 000000000..56ad10839 --- /dev/null +++ b/packages/snowpack-loader/test/fixtures/lingui.config.js @@ -0,0 +1,3 @@ +module.exports = { + locales: ["en", "cs"] +} diff --git a/packages/snowpack-loader/test/fixtures/locale/cs/messages.po b/packages/snowpack-loader/test/fixtures/locale/cs/messages.po new file mode 100644 index 000000000..b4612b13a --- /dev/null +++ b/packages/snowpack-loader/test/fixtures/locale/cs/messages.po @@ -0,0 +1,5 @@ +msgid "Hello World" +msgstr "Hello World" + +msgid "My name is {name}" +msgstr "My name is {name}" diff --git a/packages/snowpack-loader/test/fixtures/locale/en/messages.po b/packages/snowpack-loader/test/fixtures/locale/en/messages.po new file mode 100644 index 000000000..b4612b13a --- /dev/null +++ b/packages/snowpack-loader/test/fixtures/locale/en/messages.po @@ -0,0 +1,5 @@ +msgid "Hello World" +msgstr "Hello World" + +msgid "My name is {name}" +msgstr "My name is {name}" diff --git a/packages/snowpack-loader/test/index.ts b/packages/snowpack-loader/test/index.ts new file mode 100644 index 000000000..2f0f73db6 --- /dev/null +++ b/packages/snowpack-loader/test/index.ts @@ -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() + }) +}) \ No newline at end of file diff --git a/scripts/build/bundles.js b/scripts/build/bundles.js index ae7cfee5b..107577cfa 100644 --- a/scripts/build/bundles.js +++ b/scripts/build/bundles.js @@ -33,6 +33,10 @@ const bundles = [ type: bundleTypes.NODE, entry: "@lingui/babel-plugin-extract-messages" }, + { + type: bundleTypes.NODE, + entry: "@lingui/snowpack-loader" + }, { type: bundleTypes.NODE, diff --git a/yarn.lock b/yarn.lock index 1de0e5ee4..b4847d649 100644 --- a/yarn.lock +++ b/yarn.lock @@ -991,6 +991,13 @@ core-js-pure "^3.0.0" regenerator-runtime "^0.13.4" +"@babel/runtime@^7": + version "7.12.5" + resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.12.5.tgz#410e7e487441e1b360c29be715d870d9b985882e" + integrity sha512-plcc+hbExy3McchJCEQG3knOsuh3HH+Prx1P6cLIkET/0dLuQDEnrT+s27Axgc9bqfsmNUNHfscgMUdBpC9xfg== + dependencies: + regenerator-runtime "^0.13.4" + "@babel/runtime@^7.10.2", "@babel/runtime@^7.10.3", "@babel/runtime@^7.11.2", "@babel/runtime@^7.7.2", "@babel/runtime@^7.8.4": version "7.11.2" resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.11.2.tgz#f549c13c754cc40b87644b9fa9f09a6a95fe0736"