Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes --config option processing #509

Merged
merged 2 commits into from
May 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/cli/src/lingui-add-locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ if (require.main === module) {

if (!program.args.length) program.help()

const config = getConfig()
const config = getConfig({ configPath: program.config })
if (program.format) {
const msg =
"--format option is deprecated and will be removed in @lingui/[email protected]." +
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/lingui-compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ if (require.main === module) {
})
.parse(process.argv)

const config = getConfig()
const config = getConfig({ configPath: program.config })

if (program.format) {
const msg =
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/lingui-extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ if (require.main === module) {
)
.parse(process.argv)

const config = getConfig()
const config = getConfig({ configPath: program.config })

if (program.format) {
const msg =
Expand Down
24 changes: 6 additions & 18 deletions packages/conf/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,29 +75,17 @@ const configValidation = {
comment: "See https://lingui.js.org/ref/conf.html for a list of valid options"
}

function configFilePathFromArgs() {
const configIndex = process.argv.indexOf("--config")

if (
configIndex >= 0 &&
process.argv.length > configIndex &&
fs.existsSync(process.argv[configIndex + 1])
) {
return process.argv[configIndex + 1]
}

return null
function configExists(path) {
return path && fs.existsSync(path)
}

export function getConfig({ cwd } = {}) {
export function getConfig({ cwd, configPath } = {}) {
const configExplorer = cosmiconfig("lingui")
const defaultRootDir = cwd || process.cwd()
const configPath = configFilePathFromArgs()

const result =
configPath == null
? configExplorer.searchSync(defaultRootDir)
: configExplorer.loadSync(configPath)
const result = configExists(configPath)
? configExplorer.loadSync(configPath)
: configExplorer.searchSync(defaultRootDir)

const raw = { ...defaultConfig, ...(result ? result.config : {}) }

Expand Down
13 changes: 2 additions & 11 deletions packages/conf/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,9 @@ describe("lingui-conf", function() {
expect(cosmiconfig().searchSync).toHaveBeenCalled()
})

describe("with --config command line argument", function() {
beforeEach(function() {
process.argv.push("--config")
process.argv.push("./lingui/myconfig")
})

afterEach(function() {
process.argv.splice(process.argv.length - 2, 2)
})

describe("with configPath parameter", function() {
it("allows specific config file to be loaded", function() {
getConfig()
getConfig({ configPath: "./lingui/myconfig" })
expect(cosmiconfig().searchSync).not.toHaveBeenCalled()
expect(cosmiconfig().loadSync).toHaveBeenCalledWith("./lingui/myconfig")
})
Expand Down
3 changes: 2 additions & 1 deletion packages/loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@lingui/cli": "0.0.0-managed-by-release-script",
"@lingui/conf": "0.0.0-managed-by-release-script",
"babel-runtime": "^6.26.0",
"ramda": "^0.26.1"
"ramda": "^0.26.1",
"loader-utils": "^1.1.0"
}
}
16 changes: 14 additions & 2 deletions packages/loader/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from "path"
import * as R from "ramda"
import { getConfig } from "@lingui/conf"
import { createCompiledCatalog, configureCatalog } from "@lingui/cli/api"
import loaderUtils from "loader-utils"

// Check if JavascriptParser and JavascriptGenerator exists -> Webpack 4
let JavascriptParser
Expand All @@ -16,6 +17,8 @@ try {
}

export default function(source) {
const options = loaderUtils.getOptions(this)

// Webpack 4 uses json-loader automatically, which breaks this loader because it
// doesn't return JSON, but JS module. This is a temporary workaround before
// official API is added (https://github.com/webpack/webpack/issues/7057#issuecomment-381883220)
Expand All @@ -26,7 +29,10 @@ export default function(source) {
this._module.generator = new JavascriptGenerator()
}

const config = getConfig({ cwd: path.dirname(this.resourcePath) })
const config = getConfig({
configPath: options.config,
cwd: path.dirname(this.resourcePath)
})
const catalog = configureCatalog(config)

const locale = catalog.getLocale(this.resourcePath)
Expand All @@ -47,5 +53,11 @@ export default function(source) {
// of I18nProvider (React) or setupI18n (core) and therefore we need to get
// empty translations if missing.
const strict = process.env.NODE_ENV !== "production"
return createCompiledCatalog(locale, messages, strict)
return createCompiledCatalog(
locale,
messages,
strict,
config.compileNamespace,
config.pseudoLocale
)
}
3 changes: 2 additions & 1 deletion packages/loader/test/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export default (fixture, options = {}) => {
{
test: /\.json$/,
use: {
loader: path.resolve(__dirname, "../src/index.js")
loader: path.resolve(__dirname, "../src/index.js"),
options
}
}
]
Expand Down
4 changes: 4 additions & 0 deletions packages/loader/test/customconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"localeDir": "<rootDir>/locale",
"compileNamespace": "window.really_long_namespace"
}
12 changes: 12 additions & 0 deletions packages/loader/test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,16 @@ describe("lingui-loader", function() {
expect(output.errors).toEqual([])
expect(output.modules[0].source).toMatchSnapshot()
})

skipOnWindows("should allow config option", async () => {
const stats = await compiler(
path.join(".", "locale", "en", "messages.json"),
{ config: `${path.dirname(module.filename)}/customconfig` }
)

const output = stats.toJson()

// customconfig contains this namespace
expect(output.modules[0].source).toMatch(/window\.really_long_namespace=/)
})
})