I'm using create-react-app with react-app-rewired to enable making changes to webpack/babel without ejecting.
I've added the following config-overrides.js file (a react-app-rewired requirement):
module.exports = function override(config, env) {
let babelLoader;
const checkRule = rule => rule.loader && rule.loader.indexOf('babel-loader') > 0;
config.module.rules.every(rule => {
if (rule.oneOf) {
babelLoader = rule.oneOf.find(checkRule);
} else if (checkRule(rule)) {
babelLoader = rule.loader;
}
return !babelLoader;
});
babelLoader.options.babelrc = true;
return config
}
This make sure to load the .babelrc file with the needed presets:
{
"plugins": [
"syntax-dynamic-import"
],
"env": {
"test": {
"plugins": [
"dynamic-import-node"
]
}
},
"presets": [
"env",
"react",
"lingui-react"
]
}
While this approach is working, extract is failing with the following exception:
Extracting messages from source files:
C:\Development\partner_portal\client\config-overrides.js
C:\Users\ofirhe\AppData\Local\Yarn\config\global\node_modules\babel-core\lib\transformation\file\index.js:590
throw err;
^
SyntaxError: C:/Development/partner_portal/client/node_modules/.bin/sha.js: Unexpected token, expected , (2:18)
1 | #!/bin/sh
> 2 | basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
| ^
3 |
4 | case `uname` in
5 | *CYGWIN*) basedir=`cygpath -w "$basedir"`;;
at Parser.pp$5.raise (C:\Users\ofirhe\AppData\Local\Yarn\config\global\node_modules\babylon\lib\index.js:4454:13)
at Parser.pp.unexpected (C:\Users\ofirhe\AppData\Local\Yarn\config\global\node_modules\babylon\lib\index.js:1761:8)
at Parser.pp.expect (C:\Users\ofirhe\AppData\Local\Yarn\config\global\node_modules\babylon\lib\index.js:1749:33)
at Parser.pp$3.parseCallExpressionArguments (C:\Users\ofirhe\AppData\Local\Yarn\config\global\node_modules\babylon\lib\index.js:3564:12)
at Parser.pp$3.parseSubscripts (C:\Users\ofirhe\AppData\Local\Yarn\config\global\node_modules\babylon\lib\index.js:3533:31)
at Parser.pp$3.parseExprSubscripts (C:\Users\ofirhe\AppData\Local\Yarn\config\global\node_modules\babylon\lib\index.js:3504:15)
at Parser.pp$3.parseMaybeUnary (C:\Users\ofirhe\AppData\Local\Yarn\config\global\node_modules\babylon\lib\index.js:3474:19)
at Parser.pp$3.parseExprOps (C:\Users\ofirhe\AppData\Local\Yarn\config\global\node_modules\babylon\lib\index.js:3404:19)
at Parser.pp$3.parseMaybeConditional (C:\Users\ofirhe\AppData\Local\Yarn\config\global\node_modules\babylon\lib\index.js:3381:19)
at Parser.pp$3.parseMaybeAssign (C:\Users\ofirhe\AppData\Local\Yarn\config\global\node_modules\babylon\lib\index.js:3344:19)
Expected:
While I think this exception should be addressed, I think it would also be beneficial to allow specifying the files to include/exclude from the extraction process
Hello @herzaso,
there's already srcPathIgnorePatterns configuration (https://lingui.gitbooks.io/js/ref/cli.html), but the default value is /node_modules/ so I wonder, why it tries to extract messages from node_modules.
I'm not familiar with react-app-rewired, I'll take a look.
It may be caused that /node_modules/ isn't treated correctly as a regexp, but rather unix-style directory, while you're on windows so it doesn't match. Just my first guess.
Could you please try to add this section to your package.json to verify it?
{
"lingui": {
"srcPathIgnorePatterns": ["node_modules"]
}
}
@tricoder42 you are right, it was caused by the wrong path delimiter (maybe consider changing the regexp to support windows delimiter also).
Other than that, I know have another problem:
C:\Users\ofirhe\AppData\Local\Yarn\config\global\node_modules\babel-core\lib\transformation\file\index.js:590
throw err;
^
SyntaxError: C:/Development/partner_portal/client/src/components/Form/Form.js: Unexpected token (41:10)
39 | uiSchema = Object.assign(uiSchema, schema.uiSchema)
40 |
> 41 | return {...schema, uiSchema}
| ^
42 | }
43 |
Do I understand it correctly, that you actually have this .babelrc in root of your repository?
{
"plugins": [
"syntax-dynamic-import"
],
"env": {
"test": {
"plugins": [
"dynamic-import-node"
]
}
},
"presets": [
"env",
"react",
"lingui-react"
]
}
All what you do in config-overrides.js is to tell webpack to use .babelrc, right? (babelLoader.options.babelrc = true;)
Yes. I have added the es2015 preset just to make sure (although create-react-app should have it by default), but the result is the same
Nevermind, I have to add the "transform-object-rest-spread" plugin ...
Ah, you're right! That plugin isn't part of env preset! Good job! 馃憤
Thank you for reporting the bug with ignore patterns. I'll fix it soon.
It should be fixed on latest release of [email protected]. Could you please confirm it? Simply remove srcPathIgnorePatterns to test the default works correctly.
Running the cli did produce the table with number of labels although it didn't show any file being collected (whereas before it did log the file paths that were collected).
Since I wasn't sure, I removed the locale directory and ran lingui add-locale en, but got the following error:
fs.js:641
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^
Error: ENOENT: no such file or directory, open 'C:\Development\partner_portal\client\locale\en\messages.json'
at Error (native)
at Object.fs.openSync (fs.js:641:18)
at Object.fs.writeFileSync (fs.js:1347:33)
at Object.write (C:\Users\ofirhe\AppData\Roaming\npm\node_modules\lingui-cli\dist\api\formats\lingui.js:60:20)
at Object.addLocale (C:\Users\ofirhe\AppData\Roaming\npm\node_modules\lingui-cli\dist\api\formats\lingui.js:167:14)
at C:\Users\ofirhe\AppData\Roaming\npm\node_modules\lingui-cli\dist\lingui-add-locale.js:29:36
at Array.map (native)
at command (C:\Users\ofirhe\AppData\Roaming\npm\node_modules\lingui-cli\dist\lingui-add-locale.js:28:25)
at Object.<anonymous> (C:\Users\ofirhe\AppData\Roaming\npm\node_modules\lingui-cli\dist\lingui-add-locale.js:67:3)
at Module._compile (module.js:570:32)
I made the output more silent. Running lingui extract --verbose gives the full output as before.
The error is, however, weird. I'll take a look