diff --git a/packages/cli/src/api/compile.ts b/packages/cli/src/api/compile.ts index 23dfea535..16664cda5 100644 --- a/packages/cli/src/api/compile.ts +++ b/packages/cli/src/api/compile.ts @@ -16,6 +16,7 @@ export type CreateCompileCatalogOptions = { namespace?: CompiledCatalogNamespace pseudoLocale?: string compilerBabelOptions?: GeneratorOptions + pure?: boolean } export function createCompiledCatalog( @@ -23,7 +24,7 @@ export function createCompiledCatalog( messages: CompiledCatalogType, options: CreateCompileCatalogOptions ) { - const { strict = false, namespace = "cjs", pseudoLocale, compilerBabelOptions = {} } = options + const { strict = false, namespace = "cjs", pseudoLocale, compilerBabelOptions = {}, pure = false } = options const compiledMessages = R.keys(messages).map((key: string) => { // Don't use `key` as a fallback translation in strict mode. let translation = messages[key] || (!strict ? key : "") @@ -35,21 +36,20 @@ export function createCompiledCatalog( return t.objectProperty(t.stringLiteral(key), compile(translation)) }) - const ast = buildExportStatement( + const ast = pure ? t.objectExpression(compiledMessages) : buildExportStatement( t.objectExpression(compiledMessages), namespace ) - return ( - "/*eslint-disable*/" + - generate(ast, { - minified: true, - jsescOption: { - minimal: true, - }, - ...compilerBabelOptions, - }).code - ) + const code = generate(ast, { + minified: true, + jsescOption: { + minimal: true, + }, + ...compilerBabelOptions, + }).code + + return pure ? JSON.parse(code) : ("/*eslint-disable*/" + code) } function buildExportStatement(expression, namespace: CompiledCatalogNamespace) { diff --git a/packages/loader/src/remoteLoader.test.ts b/packages/loader/src/remoteLoader.test.ts index e2c08748e..1e80f1aa2 100644 --- a/packages/loader/src/remoteLoader.test.ts +++ b/packages/loader/src/remoteLoader.test.ts @@ -6,18 +6,42 @@ describe("remote-loader", () => { it("should compile correctly JSON messages coming from the fly", async () => { const unlink = createConfig("minimal") const messages = await simulatedJsonResponse() - expect(remoteLoader("en", messages)).toMatchInlineSnapshot( - `/*eslint-disable*/module.exports={messages:{"property.key":"value","{0} Deposited":[["0"]," Deposited"],"{0} Strategy":[["0"]," Strategy"]}};` - ) + const remoteMessages = remoteLoader("en", messages) + expect(remoteMessages).toMatchInlineSnapshot(` + Object { + property.key: value, + {0} Deposited: Array [ + Array [ + 0, + ], + Deposited, + ], + {0} Strategy: Array [ + Array [ + 0, + ], + Strategy, + ], + } + `) + expect(remoteMessages["property.key"]).toEqual("value") unlink() }) it("should compile correctly .po messages coming from the fly", async () => { const unlink = createConfig("po") const messages = await simulatedPoResponse() - expect(remoteLoader("en", messages)).toMatchInlineSnapshot( - `/*eslint-disable*/module.exports={messages:{"Hello World":"Hello World","My name is {name}":["My name is ",["name"]]}};` - ) + expect(remoteLoader("en", messages)).toMatchInlineSnapshot(` + Object { + Hello World: Hello World, + My name is {name}: Array [ + My name is , + Array [ + name, + ], + ], + } + `) unlink() }) @@ -27,11 +51,24 @@ describe("remote-loader", () => { const messages = await simulatedJsonResponse(true) const fallbackMessages = await simulatedJsonResponse() - expect( - remoteLoader("en", messages, fallbackMessages) - ).toMatchInlineSnapshot( - `/*eslint-disable*/module.exports={messages:{"property.key":"value","{0} Deposited":[["0"]," Deposited"],"{0} Strategy":[["0"]," Strategy"]}};` - ) + expect(remoteLoader("en", messages, fallbackMessages)) + .toMatchInlineSnapshot(` + Object { + property.key: value, + {0} Deposited: Array [ + Array [ + 0, + ], + Deposited, + ], + {0} Strategy: Array [ + Array [ + 0, + ], + Strategy, + ], + } + `) unlink() }) @@ -40,11 +77,18 @@ describe("remote-loader", () => { const messages = await simulatedPoResponse("es") const fallbackMessages = await simulatedPoCompiledFile() - expect( - remoteLoader("en", messages, fallbackMessages) - ).toMatchInlineSnapshot( - `/*eslint-disable*/module.exports={messages:{"Hello World":"Hello World","My name is {name}":["My name is ",["name"]]}};` - ) + expect(remoteLoader("en", messages, fallbackMessages)) + .toMatchInlineSnapshot(` + Object { + Hello World: Hello World, + My name is {name}: Array [ + My name is , + Array [ + name, + ], + ], + } + `) unlink() }) }) diff --git a/packages/loader/src/remoteLoader.ts b/packages/loader/src/remoteLoader.ts index e89bd0420..56de77ae6 100644 --- a/packages/loader/src/remoteLoader.ts +++ b/packages/loader/src/remoteLoader.ts @@ -51,6 +51,8 @@ export function remoteLoader(locale: string, messages: RemoteLoaderMessages