-
Notifications
You must be signed in to change notification settings - Fork 0
Really want to use this but can't figure out how #29
Comments
I'll try to whip up an example tonight or tomorrow. To unblock you right now, for The tricky part is to write out that result object to a file once webpack's done. |
@longlho ah okay. So aggregate() works similiarly to array.reduce in that you pass it a accumulator? Or do you mean something different by “result object?” Also thank you for working on an example. I’m very much looking to accomplish the functionality of babel-plugin-react-intl, and am hoping this lib will make things easier |
Yes it works like an accumulator
…On Wed, Oct 3, 2018 at 11:42 PM Martin ***@***.***> wrote:
@longlho <https://github.com/longlho> ah okay. So aggregate() works
similiarly to array.reduce in that you pass it a accumulator? Or do you
mean something different by “result object?”
Also thank you for working on an example. I’m very much looking to
accomplish the functionality of babel-plugin-react-intl, and am hoping this
lib will make things easier
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#29 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAMGbwDara3Ujzld_Qc_fwCsCX8Ndhwhks5uhYORgaJpZM4XHBI0>
.
|
Been trying some other attempts. Most recent failed attempt was to abstract the transformer into my own file:
{
loader: require.resolve('ts-loader'),
options: {
// disable type checker - we will use it in fork plugin
transpileOnly: true,
getCustomTransformers: () => ({
before: [mergeMessages]
})
},
},
var fs = require("fs");
var ts = require('typescript');
var mkdirpSync = require("mkdirp").sync;
var intl = require('ts-transform-react-intl');
const outputDir = './build/';
var transformer = function (context) {
console.log('running script');
// aggregate all messages into one big object
var messages = {};
var intlTransformer = intl.transform({
idPrefix: "prefix",
onMsgExtracted: intl.aggregate(messages),
});
intlTransformer();
console.log(messages);
// Create a new directory that we want to write the aggregate messages to
mkdirpSync(outputDir);
// Write the messages to this directory
fs.writeFileSync('data.json', `{ "en": ${JSON.stringify(messages, null, 2)} }`);
}
exports["default"] = transformer; But the build errors out with |
yeah that's not gonna work. I initially did this w/ typescript compiler wrapper because it's synchronous & just run through the whole project. Some pointer would be https://webpack.js.org/api/compilation-hooks/ or https://webpack.js.org/api/plugins/#custom-hooks The flow for webpack would be something like:
|
Oh looks like |
those are some good thoughts. I'll try creating a plugin that can hook into after compile tonight |
Looks like hooking into the callbacks didn't work either. The messages object is always empty
'use strict';
var fs = require('fs');
var mkdirp = require('mkdirp');
const outputPath = '../build/messages/';
class GenerateMessageFile {
constructor(messageObj) {
this.messageObj = messageObj;
};
apply(compiler) {
compiler.plugin('done', (compilation) => {
console.log(this.messageObj);
// Create a new directory that we want to write the aggregate messages to
mkdirp.sync(outputPath);
// Write the messages to this directory
fs.writeFileSync(outputPath + 'messages.json', `{ "en": ${JSON.stringify(this.messageObj, null, 2)} }`);
})
}
}
module.exports = GenerateMessageFile;
{
test: /\.(ts|tsx)$/,
include: paths.appSrc,
use: [
{
loader: require.resolve('ts-loader'),
options: {
// disable type checker - we will use it in fork plugin
transpileOnly: true,
getCustomTransformers: () => ({
before: [
intl.transform({
idPrefix: "prefix",
onMsgExtracted: intl.aggregate(messages),
}),
]
})
},
},
],
},
plugins: [
new GenerateMessageFile(messages),
] |
Ah okay so it seems like this plugin will not pick up the text if you’re solely using the react-intl React Component FormattedMessage and that you need to use the API function. The issue i’m running into now is that the out-of-scope messages var is undefined by the time my webpack hook runs Would you mind if i just made a PR to allow for passing in a file path to write to? I think that functionality would be widely useful |
Yeah PR’s welcome :)
…On Fri, Oct 5, 2018 at 4:24 AM Martin ***@***.***> wrote:
Ah okay so it seems like this plugin will not pick up the text if you’re
solely using the React Component and that you need to use the API function.
Would you mind if i just made a PR to allow for passing in a file path to
write to? I think that functionality would be widely useful
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#29 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAMGb211OIkjENcWgjS8OANWlxZcUwSrks5uhxc2gaJpZM4XHBI0>
.
|
I'm using webpack with ts-loader and I'd like to extract my react-intl strings to JSON files.
But I can't figure out how to get this plugin working correctly. I've tried looking at other issues and still can't find any help.
For starters, how do I tell this plugin where to create the JSON file? Also, how am I supposed to be using the
onMsgExtracted
propertyThis is what I've tried so far:
webpack.config
MyComponent.tsx
The text was updated successfully, but these errors were encountered: