【问题标题】:svelte-i18n - Svelte Rollup compiler warning: `this` has been rewritten to `undefined`svelte-i18n - Svelte Rollup 编译器警告:`this` 已被重写为 `undefined`
【发布时间】:2020-09-01 18:50:24
【问题描述】:

自从我在我的 Svelte 应用程序上安装了 svelte-i18n 库后,我在编译 Rollup 时收到了以下警告。

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

我还插入了整个错误堆栈跟踪,因为它可能有用:

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

如果与此处描述的不同,我相信此错误非常相似: https://github.com/kaisermann/svelte-i18n/blob/master/docs/FAQ.md#this-keyword-is-equivalent-to-undefined

本应用中提到的库有以下版本:

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

似乎这个警告不是问题,但是对我来说非常烦人。 我真的不喜欢他们在这种情况下给出的解决方案(“学会忍受那个警告并忽略它”)。有什么方法可以指示编译器忽略该错误或有人已经设法修复它?

编辑:

我的问题是在 windows 上(因此是反斜杠),库如下:

'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',

【问题讨论】:

    标签: svelte rollupjs


    【解决方案1】:

    我在article 中找到了一个解决方案,他们在其中链接了必要的汇总配置。基本上你需要把这个添加到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';
      }
    }
    

    【讨论】:

    • 谢谢,它成功了。在我的情况下,唯一的小修正是使其适用于 Windows 并添加其他导致问题的库。
    • 由于某种原因这对我不起作用我仍然收到警告:(
    猜你喜欢
    • 2021-12-25
    • 2022-09-23
    • 2020-10-20
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    • 2020-04-09
    • 1970-01-01
    • 2019-11-21
    相关资源
    最近更新 更多