webpack-contrib / i18n-webpack-plugin

[DEPRECATED] Embed localization into your bundle
MIT License
317 stars 74 forks source link

Plugin doesn't work with webpack@2.2.0 #31

Open ma2ciek opened 7 years ago

ma2ciek commented 7 years ago

I'm using webpack@2.2.0 and wanted to take a look at this plugin while working on the https://github.com/ckeditor/ckeditor5/issues/387, but I got into few troubles.

compiler.parser.plugin is deprecated now. So I changed this line into something like this:

compiler.plugin("compilation", function(compilation, params) {
  params.normalModuleFactory.plugin("parser", (parser, options) => {
    parser.plugin("call " + functionName, (expr) => {
       console.log("it works");
    });
  });
});

But unfortunately it seems to be not working now... Could you provide some info how to fix it?

Regards, Maciek

codelegant commented 7 years ago

Although it has warning in webpack 2.2.0, but is still worked in my project.

ma2ciek commented 7 years ago

It turned out that the bug (or feature) was somewhere else.

Webpack parser runs "call fn" callback only when fn is not defined in the code (I'm not sure why). So when I have

const t = this.t;
t()

the parser doesn't see t invocation.

I have to use t() function later to provide multi-language support and to be able to transform this string later based on that string and additional words (translated string is used as a template).

rafaelmaruta commented 7 years ago

I'm with this problem too

josemigg commented 7 years ago

Either do I, anyone knows how to fix it?

ma2ciek commented 7 years ago

I had to use acorn to be able to create translation service https://github.com/ckeditor/ckeditor5-dev/blob/master/packages/ckeditor5-dev-utils/lib/translations/translationservice.js, but it would be much nicer if the webpack could handle it by itself.

timothylombrana commented 7 years ago

@ma2ciek @d3viant0ne noticed it had been a couple weeks since this was last commented on, was hoping to see if there was some kind of resolution? Thanks for the help!

ma2ciek commented 7 years ago

I had to implement my own solution with usage of the Acorn parser,

joshwiens commented 7 years ago

@alphapilgrim - There is a 1.0.0-beta.0 build on npm that will solve that particular issue. Keep in mind there is going to be churn on that beta dist-tag for a few weeks.

mightyaleksey commented 7 years ago

Webpack parser runs "call fn" callback only when fn is not defined in the code (I'm not sure why).

@ma2ciek looks like it's a current implementation of the webpack's Parser. See: https://github.com/webpack/webpack/blob/master/lib/Parser.js#L257 https://github.com/webpack/webpack/blob/master/lib/Parser.js#L1100

I'm not sure why you want to replace the calls of defined functions, but looks like to affect them you have to register another callback for the evaluate defined Identifier ${functionName} event and return an evaluated expression (instance of BasicEvaluatedExpression).

Smthing like this:

const BasicEvaluatedExpression = require('webpack/lib/BasicEvaluatedExpression');
// ...
parser.plugin(`evaluate defined Identifier ${name}`, function (expr) {
  return new BasicEvaluatedExpression().setIdentifier(expr.name).setRange(expr.range);
});