Sandman
Sandman

Reputation: 1569

svelte-i18n - Svelte Rollup compiler warning: `this` has been rewritten to `undefined`

Since I installed the svelte-i18n library on my Svelte application I have had the following warning when compiling Rollup.

(!) `this` has been rewritten to `undefined`

I also insert the whole error stack trace because maybe it can be useful:

https://rollupjs.org/guide/en/#error-this-is-undefined
node_modules\intl-messageformat\lib\core.js
4: See the accompanying LICENSE file for terms.
5: */
6: var __assign = (this && this.__assign) || function () {
                   ^
7:     __assign = Object.assign || function(t) {
8:         for (var s, i = 1, n = arguments.length; i < n; i++) {
...and 1 other occurrence
node_modules\intl-messageformat\lib\formatters.js
1: var __extends = (this && this.__extends) || (function () {
                    ^
2:     var extendStatics = function (d, b) {
3:         extendStatics = Object.setPrototypeOf ||
...and 3 other occurrences
node_modules\intl-format-cache\lib\index.js
4: See the accompanying LICENSE file for terms.
5: */
6: var __spreadArrays = (this && this.__spreadArrays) || function () {
                         ^
7:     for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
8:     for (var r = Array(s), k = 0, i = 0; i < il; i++)
...and 1 other occurrence

...and 3 other files
created public\build\bundle.js in 1.3s

I believe this error is very similar if not the same described here: https://github.com/kaisermann/svelte-i18n/blob/master/docs/FAQ.md#this-keyword-is-equivalent-to-undefined

The libraries mentioned in this application have the following versions:

"svelte": 3.22.2
"svelte-i18n": 3.0.3

It seems that this warning is not a problem, however, is tremendously annoying for me. I don't really like the solution they give in this case ("learn to live with that warning and ignore it"). Is there any way to instruct the compiler to ignore that error or someone already managed to fix it?

EDIT:

My problem was on windows (therefore back slash) and the libraries were the following:

'node_modules\\intl-messageformat\\lib\\core.js',
'node_modules\\intl-messageformat\\lib\\compiler.js',
'node_modules\\intl-messageformat\\lib\\formatters.js',
'node_modules\\intl-format-cache\\lib\\index.js',
'node_modules\\intl-messageformat-parser\\lib\\skeleton.js',
'node_modules\\intl-messageformat-parser\\lib\\normalize.js',
'node_modules\\intl-messageformat-parser\\lib\\parser.js',

Upvotes: 2

Views: 2453

Answers (1)

gottlike
gottlike

Reputation: 316

I found a solution in an article where they linked the necessary Rollup config. Basically you need to add this to rollup.config.js:

moduleContext: (id) => {
  // In order to match native module behaviour, Rollup sets `this`
  // as `undefined` at the top level of modules. Rollup also outputs
  // a warning if a module tries to access `this` at the top level.
  // The following modules use `this` at the top level and expect it
  // to be the global `window` object, so we tell Rollup to set
  // `this = window` for these modules.
  const thisAsWindowForModules = [
    'node_modules/intl-messageformat/lib/core.js',
    'node_modules/intl-messageformat/lib/compiler.js'
  ];

  if (thisAsWindowForModules.some(id_ => id.trimRight().endsWith(id_))) {
    return 'window';
  }
}

Upvotes: 3

Related Questions