From d46169598e4b43c5fb5cb686f9b1e8811292e9c6 Mon Sep 17 00:00:00 2001 From: Sergio Moreno Date: Wed, 9 Jun 2021 00:41:13 +0200 Subject: [PATCH] fix: remoteLoader should return an object instead of export --- packages/cli/src/api/compile.ts | 24 ++++---- packages/loader/src/remoteLoader.test.ts | 76 +++++++++++++++++++----- packages/loader/src/remoteLoader.ts | 2 + 3 files changed, 74 insertions(+), 28 deletions(-) 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