Skip to content

Commit

Permalink
Add namespace to configuration, command option and api. This will all…
Browse files Browse the repository at this point in the history
…ow user to specify the namespace of compiled js file: ex: export default or module.export or window.translations
  • Loading branch information
An Nguyen committed Aug 27, 2018
1 parent c587586 commit be4b503
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 20 deletions.
60 changes: 42 additions & 18 deletions packages/cli/src/api/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,37 @@ function processTokens(tokens, arg) {
)
}

function buildExportStatement(namespace: string = "cjs", expression) {
namespace = namespace.trim()
if (namespace === "es") {
return t.ExportDefaultDeclaration(expression)
} else {
let exportExpression = null
const matches = namespace.match(/^(window|global)\.([^.\s]+)$/)
if (namespace === "cjs") {
exportExpression = t.memberExpression(
t.identifier("module"),
t.identifier("exports")
)
} else if (matches) {
exportExpression = t.memberExpression(
t.identifier(matches[1]),
t.identifier(matches[2])
)
} else {
throw new Error(`Invalid namespace param: ${namespace}`)
}
return t.expressionStatement(
t.assignmentExpression("=", exportExpression, expression)
)
}
}

export function createCompiledCatalog(
locale: string,
messages: CatalogType,
strict: boolean = false
strict: boolean = false,
namespace: string = "cjs"
) {
const [language] = locale.split(/[_-]/)
const pluralRules = plurals[language]
Expand All @@ -116,23 +143,20 @@ export function createCompiledCatalog(
)
]

const ast = t.expressionStatement(
t.assignmentExpression(
"=",
t.memberExpression(t.identifier("module"), t.identifier("exports")),
t.objectExpression([
// language data
t.objectProperty(
t.identifier("languageData"),
t.objectExpression(languageData)
),
// messages
t.objectProperty(
t.identifier("messages"),
t.objectExpression(compiledMessages)
)
])
)
const ast = buildExportStatement(
namespace,
t.objectExpression([
// language data
t.objectProperty(
t.identifier("languageData"),
t.objectExpression(languageData)
),
// messages
t.objectProperty(
t.identifier("messages"),
t.objectExpression(compiledMessages)
)
])
)

return (
Expand Down
11 changes: 10 additions & 1 deletion packages/cli/src/lingui-compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ function command(config, options) {
}
}

const compiledCatalog = createCompiledCatalog(locale, messages)
const compiledCatalog = createCompiledCatalog(
locale,
messages,
false,
options.namespace || config.namespace
)
const compiledPath = catalog.writeCompiled(locale, compiledCatalog)
if (options.typescript) {
const typescriptPath = compiledPath.replace(/\.js$/, "") + ".d.ts"
Expand All @@ -114,6 +119,10 @@ if (require.main === module) {
.option("--verbose", "Verbose output")
.option("--format <format>", "Format of message catalog")
.option("--typescript", "Create Typescript definition for compiled bundle")
.option(
"--namespace",
"Specify namespace for compiled bundle. Ex: cjs(default) -> module.exports, window.test -> window.test"
)
.on("--help", function() {
console.log("\n Examples:\n")
console.log(
Expand Down
3 changes: 2 additions & 1 deletion packages/conf/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export const defaultConfig = {
extractBabelOptions: {
plugins: [],
presets: []
}
},
namespace: "cjs"
}

const deprecatedConfig = {
Expand Down

0 comments on commit be4b503

Please sign in to comment.