Empty reactIntlMessages.json file.
@Ognian - thank you for writing this plugin. I think it's exactly what I was hoping for. That said, I tried to follow your workflow, but my reactIntlMessages.json file contents are [], indicating an empty array.
Here's what I've done so far:
- Installed react-intl, babel-plugin-react-intl, react-intl-webpack-plugin, reactIntlJson-loader.
- Installed the specific PR of
"babel-loader": "git://github.com/Ognian/babel-loader.git#passthru_metadata", - Created a context around my root element with
<IntlProvider>, imported from react-intl. - In the .jsx one of my react components, I added the import
import FormattedMessage from 'react-intl'; - In the same .jsx file, I added some FormattedMessage components:
<FormattedMessage id="sample_message_id" defaultMessage="Sample Message" /> - In my webpack.config.js file:
const ReactIntlPlugin=require('react-intl-webpack-plugin');
...
plugins: [
...
new ReactIntlPlugin(),
],
module: {
loaders: [
...
{
test: /\.jsx?$/, loader: 'babel-loader', exclude: /node_modules/,
query: {
"cacheDirectory": true,
"metadataSubscribers":[ReactIntlPlugin.metadataContextFunctionName],
"plugins": ["transform-runtime",
["react-intl", {
"enforceDescriptions": false
}]],
"presets": ['react', "es2015", "stage-1"]
}
},
],
},
When I build, the reactIntlMessages.json file is created, but there aren't any strings in it--just [].
Any hints on what I might be doing wrong or missing? Thanks in advance!
I guess it is because the option metadataSubscribers is not implemented by babel-loader. Check https://github.com/babel/babel-loader/pull/254
Thanks for trying it out. Lets find out where the problem is:
- add temporarily the
messagesDiroption to the react-intl babel plugin like this
test: /\.js?$/, exclude: /node_modules, loader: 'babel-loader',
query: {
"cacheDirectory": true,
"plugins": [
"transform-runtime",
["react-intl", {
"messagesDir": "dist/intl/extractedMessages",
"enforceDescriptions": false
}]
],
"presets": ['react', "es2015", "stage-1"]
}
},
and look if the corresponding id:"message" pairs the problem is with the react-intl plugin.
2. check that node_modules/babel-loader/index.js indeed contains my updated version of the loader. If you see function passMetadatainside the code, this should be the case. If not delete your node_modules directory and npm installagain, you may use the --force option too...
3. Keep in mind that as long as you use webpack-dev-serverall assets are generated in memory. To access those assets use either the browser an check http://localhost:devServerPort/reactIntlMessages.json or, like I do, add a script to package.json like "trans:refreshJsonDEV": "curl localhost:3100/reactIntlMessages.json >./dist/reactIntlMessages.json" or
start your webpack build to generate the assets into the build directory.
Thanks for your help!
- I have done as you suggested, and I am not seeing any
id: "message"pairs. It seems my usage of the babel-react-intl-plugin may be the problem. Is there something I need to do to register the plugin or include it? I have verified that it is installed. - I verified that I have your version of the loader.
- I am running a webpack build to generate the assets into the build directory. The reactIntlMessages.json shows up there.
Any thoughts? Thanks again in advance!
Hmm,
check the versions: babel-plugin-react-intl is "2.1.3", and react-intl is "2.1.2", "babel-core": "6.9.1",
the babel plugin is noting more than a function and it is registered in the "plugins" part of the "query" part of babel-loader. Do you have multiple test: /\.js?$/ entries? Are
One more thing:
see https://github.com/yahoo/babel-plugin-react-intl#usage
it say's This Babel plugin only visits ES6 modules which import React Intl.
so you need something like:
import {IntlProvider, addLocaleData} from 'react-intl';
look at https://github.com/yahoo/babel-plugin-react-intl/issues/47 too
I was able to get the id: "message" pairs to generate. The problem was with the "cacheDirectory": true. Each time I would make changes to the webpack.config.js file, nothing would happen because the files that were already cached weren't being processed. I had to point to a valid build folder like you mentioned in yahoo/babel-plugin-react-intl#47 for me, it ended up needing to be "./build/messages/" where the trailing backslash seemed to be important and the folder had to exist.
Once I got that working, I added the
"metadataSubscribers":[ReactIntlPlugin.metadataContextFunctionName]
back in. My json file is still producing an empty array.
One clue: I uncommented all of your comments in react-intl-webpack-plugin/index.js, and I'm getting lots and lots of
registering function: /Users/ben/dev/techcyte/techcyte-web/client/node_modules/react-intl-webpack-plugin in loader context
but I never see any
"module:",module,"collecting metadata:", metadata
I also added some logging to this function:
Object.keys(messages).map(function (e) {
messages[e].map(function (m) {
console.log("m.id:", m.id);
if (!idIndex[m.id]) {
idIndex[m.id] = e;
jsonMessages.push(m);
} else {
compilation.errors.push("ReactIntlPlugin -> duplicate id: '" + m.id + "'.Found in '" + idIndex[m.id] + "' and '" + e + "'.");
}
})
});
I never see any of the m.id: messages.
Any ideas? Your last ideas were very helpful.
You must see "module:",module,"collecting metadata:", metadata messages. If not it is clear why there are no m.id:messages. Go to the node_modules/babel-loader folder and edit the index.js file and uncomment the console.log messages in the passMetadata function:
function passMetadata(s, context, metadata) {
//console.log("s:",s,"metadata:",metadata,"context:",context);
if (context[s]) {
//console.log("context function:",context[s].toString());
context[s](metadata);
}
}
and check that the metadataSubscribers var is properly initialized by adding a console.log:
var cacheDirectory = options.cacheDirectory;
var cacheIdentifier = options.cacheIdentifier;
var metadataSubscribers = options.metadataSubscribers;
delete options.cacheDirectory;
delete options.cacheIdentifier;
delete options.metadataSubscribers;
console.log("metadataSubscribers:", metadataSubscribers);
this.cacheable();
var context = this;
Maybe you should set cacheDirectory to false for the time of testing too
Thanks! That was very helpful. Now that it's not caching and my changes were actually taking effect, it was easy to debug and figure out what I was doing wrong. I have made modifications that really only apply to my specific configuration, and I can see that you have already added the note about setting the "cacheDirectory": false option. All is working nicely now! Thanks again for a very useful tool!!!