Skip to content

Commit

Permalink
fix: remoteLoader should return an object instead of export
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio Moreno committed Jun 8, 2021
1 parent e134ac0 commit d461695
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 28 deletions.
24 changes: 12 additions & 12 deletions packages/cli/src/api/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ export type CreateCompileCatalogOptions = {
namespace?: CompiledCatalogNamespace
pseudoLocale?: string
compilerBabelOptions?: GeneratorOptions
pure?: boolean
}

export function createCompiledCatalog(
locale: string,
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 : "")
Expand All @@ -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) {
Expand Down
76 changes: 60 additions & 16 deletions packages/loader/src/remoteLoader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

Expand All @@ -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()
})

Expand All @@ -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()
})
})
Expand Down
2 changes: 2 additions & 0 deletions packages/loader/src/remoteLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export function remoteLoader<T>(locale: string, messages: RemoteLoaderMessages<T
...config,
namespace: config.compileNamespace,
pseudoLocale: config.pseudoLocale,
// important, because without it returns an export statement
pure: true,
})

}
Expand Down

1 comment on commit d461695

@vercel
Copy link

@vercel vercel bot commented on d461695 Jun 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.