Skip to content

Commit

Permalink
fix(conf): pass config path to getConfig explicitly (#509)
Browse files Browse the repository at this point in the history
Instead of processing process.argv, getConfig now accepts the path to a custom config file. Processing process.argv can cause problems when used in conjuction with @lingui/loader since the --config arg overlaps with webpack
  • Loading branch information
brandonc authored and tricoder42 committed May 22, 2019
1 parent 51e2279 commit 43873c9
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 36 deletions.
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/cli@3.0.0." +
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=/)
})
})

0 comments on commit 43873c9

Please sign in to comment.