Skip to content

Commit 38d01ef

Browse files
authored
feat: config accepts compilerBabelOptions (#906)
* chore: fixed catalogsMergePath warning on configuration
1 parent a3b5bcb commit 38d01ef

10 files changed

Lines changed: 104 additions & 30 deletions

File tree

docs/ref/conf.rst

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Default config:
2020
}],
2121
"compileNamespace": "cjs",
2222
"extractBabelOptions": {},
23+
"compilerBabelOptions": {},
2324
"fallbackLocales": {},
2425
"format": "po",
2526
"locales": [],
@@ -230,7 +231,37 @@ extracted. This is required when project doesn't use standard Babel config
230231
}
231232
}
232233
233-
.. config:: fallbackLocales
234+
.. config:: extractBabelOptions
235+
236+
compilerBabelOptions
237+
-------------------
238+
239+
Default: ``{
240+
"minified": true,
241+
"jsescOption": {
242+
"minimal": true
243+
}
244+
}``
245+
246+
247+
Specify extra babel options used to generate files when messages are being
248+
compiled. We use internaly ``@babel/generator`` that accepts some configuration for generating code with/out ASCII characters.
249+
These are all the options available: https://github.com/mathiasbynens/jsesc
250+
251+
.. code-block:: json
252+
253+
{
254+
"compilerBabelOptions": {
255+
"jsescOption": {
256+
"minimal": false
257+
}
258+
}
259+
}
260+
261+
This example configuration will compile with scaped ASCII characters. https://github.com/mathiasbynens/jsesc#minimal
262+
263+
.. config:: compilerBabelOptions
264+
234265

235266
fallbackLocales
236267
--------------

examples/create-react-app/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"start": "yarn extract && yarn compile && react-scripts start",
2121
"build": "yarn extract && yarn compile && react-scripts build",
2222
"test": "react-scripts test",
23+
"test:ci": "yarn extract && yarn compile && react-scripts test --watchAll=false",
2324
"eject": "react-scripts eject",
2425
"extract": "lingui extract",
2526
"compile": "lingui compile"

packages/cli/src/api/__snapshots__/compile.test.ts.snap

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
exports[`compile should report failed message on error 1`] = `Can't parse message. Please check correct syntax: "{value, plural, one {Book} other {Books"`;
44
5+
exports[`createCompiledCatalog options.compilerBabelOptions by default should return catalog without ASCII chars 1`] = `/*eslint-disable*/module.exports={messages:{"Hello":"Alohà"}};`;
6+
7+
exports[`createCompiledCatalog options.compilerBabelOptions should return catalog without ASCII chars 1`] = `/*eslint-disable*/module.exports={messages:{"Hello":"Aloh\\xE0"}};`;
8+
59
exports[`createCompiledCatalog options.namespace should compile with es 1`] = `/*eslint-disable*/export const messages={};`;
610
711
exports[`createCompiledCatalog options.namespace should compile with global 1`] = `/*eslint-disable*/global.test={messages:{}};`;
@@ -10,7 +14,7 @@ exports[`createCompiledCatalog options.namespace should compile with window 1`]
1014
1115
exports[`createCompiledCatalog options.namespace should error with invalid value 1`] = `Invalid namespace param: "global"`;
1216
13-
exports[`createCompiledCatalog options.pseudoLocale should return catalog with pseudolocalized messages 1`] = `/*eslint-disable*/module.exports={messages:{"Hello":"\\xC0\\u0125\\u014D\\u0134"}};`;
17+
exports[`createCompiledCatalog options.pseudoLocale should return catalog with pseudolocalized messages 1`] = `/*eslint-disable*/module.exports={messages:{"Hello":"ÀĥōĴ"}};`;
1418
1519
exports[`createCompiledCatalog options.pseudoLocale should return compiled catalog when pseudoLocale doesn't match current locale 1`] = `/*eslint-disable*/module.exports={messages:{"Hello":"Ahoj"}};`;
1620

packages/cli/src/api/compile.test.ts

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import generate from "@babel/generator"
22
import { compile, createCompiledCatalog } from "./compile"
33

4-
describe("compile", function () {
4+
describe("compile", () => {
55
const getSource = (message) =>
66
generate(compile(message) as any, {
77
compact: true,
88
minified: true,
99
}).code
1010

11-
it("should optimize string only messages", function () {
11+
it("should optimize string only messages", () => {
1212
expect(getSource("Hello World")).toEqual('"Hello World"')
1313
})
1414

@@ -17,21 +17,21 @@ describe("compile", function () {
1717
expect(getSource("''")).toEqual('"\'"')
1818
})
1919

20-
it("should compile arguments", function () {
20+
it("should compile arguments", () => {
2121
expect(getSource("{name}")).toEqual('[["name"]]')
2222

2323
expect(getSource("B4 {name} A4")).toEqual('["B4 ",["name"]," A4"]')
2424
})
2525

26-
it("should compile arguments with formats", function () {
26+
it("should compile arguments with formats", () => {
2727
expect(getSource("{name, number}")).toEqual('[["name","number"]]')
2828

2929
expect(getSource("{name, number, percent}")).toEqual(
3030
'[["name","number","percent"]]'
3131
)
3232
})
3333

34-
it("should compile plural", function () {
34+
it("should compile plural", () => {
3535
expect(getSource("{name, plural, one {Book} other {Books}}")).toEqual(
3636
'[["name","plural",{one:"Book",other:"Books"}]]'
3737
)
@@ -53,7 +53,7 @@ describe("compile", function () {
5353
)
5454
})
5555

56-
it("should compile select", function () {
56+
it("should compile select", () => {
5757
expect(getSource("{name, select, male {He} female {She}}")).toEqual(
5858
'[["name","select",{male:"He",female:"She"}]]'
5959
)
@@ -67,15 +67,15 @@ describe("compile", function () {
6767
)
6868
})
6969

70-
it("should report failed message on error", function () {
70+
it("should report failed message on error", () => {
7171
expect(() =>
7272
getSource("{value, plural, one {Book} other {Books")
7373
).toThrowErrorMatchingSnapshot()
7474
})
7575
})
7676

77-
describe("createCompiledCatalog", function () {
78-
describe("options.namespace", function () {
77+
describe("createCompiledCatalog", () => {
78+
describe("options.namespace", () => {
7979
const getCompiledCatalog = (namespace) =>
8080
createCompiledCatalog(
8181
"fr",
@@ -85,24 +85,24 @@ describe("createCompiledCatalog", function () {
8585
}
8686
)
8787

88-
it("should compile with es", function () {
88+
it("should compile with es", () => {
8989
expect(getCompiledCatalog("es")).toMatchSnapshot()
9090
})
9191

92-
it("should compile with window", function () {
92+
it("should compile with window", () => {
9393
expect(getCompiledCatalog("window.test")).toMatchSnapshot()
9494
})
9595

96-
it("should compile with global", function () {
96+
it("should compile with global", () => {
9797
expect(getCompiledCatalog("global.test")).toMatchSnapshot()
9898
})
9999

100-
it("should error with invalid value", function () {
100+
it("should error with invalid value", () => {
101101
expect(() => getCompiledCatalog("global")).toThrowErrorMatchingSnapshot()
102102
})
103103
})
104104

105-
describe("options.strict", function () {
105+
describe("options.strict", () => {
106106
const getCompiledCatalog = (strict) =>
107107
createCompiledCatalog(
108108
"cs",
@@ -115,16 +115,16 @@ describe("createCompiledCatalog", function () {
115115
}
116116
)
117117

118-
it("should return message key as a fallback translation", function () {
118+
it("should return message key as a fallback translation", () => {
119119
expect(getCompiledCatalog(false)).toMatchSnapshot()
120120
})
121121

122-
it("should't return message key as a fallback in strict mode", function () {
122+
it("should't return message key as a fallback in strict mode", () => {
123123
expect(getCompiledCatalog(true)).toMatchSnapshot()
124124
})
125125
})
126126

127-
describe("options.pseudoLocale", function () {
127+
describe("options.pseudoLocale", () => {
128128
const getCompiledCatalog = (pseudoLocale) =>
129129
createCompiledCatalog(
130130
"ps",
@@ -136,12 +136,37 @@ describe("createCompiledCatalog", function () {
136136
}
137137
)
138138

139-
it("should return catalog with pseudolocalized messages", function () {
139+
it("should return catalog with pseudolocalized messages", () => {
140140
expect(getCompiledCatalog("ps")).toMatchSnapshot()
141141
})
142142

143-
it("should return compiled catalog when pseudoLocale doesn't match current locale", function () {
143+
it("should return compiled catalog when pseudoLocale doesn't match current locale", () => {
144144
expect(getCompiledCatalog("en")).toMatchSnapshot()
145145
})
146146
})
147+
148+
describe("options.compilerBabelOptions", () => {
149+
const getCompiledCatalog = (opts = {}) =>
150+
createCompiledCatalog(
151+
"ru",
152+
{
153+
Hello: "Alohà",
154+
},
155+
opts
156+
)
157+
158+
it("by default should return catalog without ASCII chars", () => {
159+
expect(getCompiledCatalog()).toMatchSnapshot()
160+
})
161+
162+
it("should return catalog without ASCII chars", () => {
163+
expect(getCompiledCatalog({
164+
compilerBabelOptions: { 
165+
jsescOption: {
166+
minimal: false,
167+
}
168+
}
169+
})).toMatchSnapshot()
170+
})
171+
})
147172
})

packages/cli/src/api/compile.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ export type CreateCompileCatalogOptions = {
1515
strict?: boolean
1616
namespace?: CompiledCatalogNamespace
1717
pseudoLocale?: string
18+
compilerBabelOptions?: Record<string, unknown>
1819
}
1920

2021
export function createCompiledCatalog(
2122
locale: string,
2223
messages: CompiledCatalogType,
2324
options: CreateCompileCatalogOptions
2425
) {
25-
const { strict = false, namespace = "cjs", pseudoLocale } = options
26+
const { strict = false, namespace = "cjs", pseudoLocale, compilerBabelOptions = {} } = options
2627
const compiledMessages = R.keys(messages).map((key: string) => {
2728
// Don't use `key` as a fallback translation in strict mode.
2829
let translation = messages[key] || (!strict ? key : "")
@@ -41,8 +42,12 @@ export function createCompiledCatalog(
4142

4243
return (
4344
"/*eslint-disable*/" +
44-
generate(ast as any, {
45+
generate(ast, {
4546
minified: true,
47+
jsescOption: {
48+
minimal: true,
49+
},
50+
...compilerBabelOptions,
4651
}).code
4752
)
4853
}

packages/cli/src/lingui-compile.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as R from "ramda"
44
import program from "commander"
55
import * as plurals from "make-plural"
66

7-
import { getConfig } from "@lingui/conf"
7+
import { getConfig, LinguiConfig } from "@lingui/conf"
88

99
import { getCatalogs } from "./api/catalog"
1010
import { createCompiledCatalog } from "./api/compile"
@@ -16,7 +16,7 @@ const noMessages: (catalogs: Object[]) => boolean = R.pipe(
1616
R.all(R.equals<any>(true))
1717
)
1818

19-
function command(config, options) {
19+
function command(config: LinguiConfig, options) {
2020
const catalogs = getCatalogs(config)
2121

2222
if (noMessages(catalogs)) {
@@ -87,6 +87,7 @@ function command(config, options) {
8787
strict: false,
8888
namespace,
8989
pseudoLocale: config.pseudoLocale,
90+
compilerBabelOptions: config.compilerBabelOptions,
9091
})
9192

9293
const compiledPath = catalog.writeCompiled(

packages/conf/index.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ export declare type FallbackLocales = LocaleObject | DefaultLocaleObject
2323
export declare type LinguiConfig = {
2424
catalogs: CatalogConfig[];
2525
compileNamespace: string;
26-
extractBabelOptions: Object;
26+
extractBabelOptions: Record<string, unknown>;
27+
compilerBabelOptions: Record<string, unknown>;
2728
fallbackLocales: FallbackLocales;
2829
format: CatalogFormat;
2930
prevFormat: CatalogFormat;
3031
formatOptions: CatalogFormatOptions;
3132
localeDir: string;
3233
locales: string[];
33-
catalogsMergePath?: string;
34+
catalogsMergePath: string;
3435
orderBy: OrderBy;
3536
pseudoLocale: string;
3637
rootDir: string;
@@ -51,6 +52,7 @@ export declare const configValidation: {
5152
plugins: string[];
5253
presets: string[];
5354
};
55+
compilerBabelOptions: Record<string, unknown>;
5456
catalogs: CatalogConfig[];
5557
compileNamespace: string;
5658
fallbackLocales: FallbackLocales;

packages/conf/src/__snapshots__/index.test.ts.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ Object {
108108
path: ./locale/{locale}/messages,
109109
},
110110
],
111+
catalogsMergePath: ,
111112
compileNamespace: cjs,
113+
compilerBabelOptions: Object {},
112114
extractBabelOptions: Object {
113115
plugins: Array [],
114116
presets: Array [],

packages/conf/src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ type ModuleSource = [string, string?];
3232
export type LinguiConfig = {
3333
catalogs: CatalogConfig[]
3434
compileNamespace: string
35-
extractBabelOptions: Object
35+
extractBabelOptions: Record<string, unknown>
36+
compilerBabelOptions: Record<string, unknown>
3637
fallbackLocales?: FallbackLocales
3738
format: CatalogFormat
3839
formatOptions: CatalogFormatOptions
3940
locales: string[]
40-
catalogsMergePath?: string
41+
catalogsMergePath: string
4142
orderBy: OrderBy
4243
pseudoLocale: string
4344
rootDir: string
@@ -62,7 +63,9 @@ export const defaultConfig: LinguiConfig = {
6263
exclude: ["*/node_modules/*"],
6364
},
6465
],
66+
catalogsMergePath: "",
6567
compileNamespace: "cjs",
68+
compilerBabelOptions: {},
6669
extractBabelOptions: { plugins: [], presets: [] },
6770
fallbackLocales: {},
6871
format: "po",

scripts/verdaccio-integration.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ async function main() {
1818

1919
spinner.start("Running tests")
2020
try {
21-
await exec('yarn test --watchAll=false', OPTS)
21+
await exec('yarn test:ci', OPTS)
2222
spinner.succeed()
2323
} catch (error) {
2424
spinner.fail()

0 commit comments

Comments
 (0)