自从我安装了 svelte-i18n
在我的Svelte应用程序中,当编译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.comkaisermannsvelte-i18nblobmasterdocsFAQ.md#this-keyword-is-equivalent-undefined。
这个应用程序中提到的库有以下版本。
"svelte": 3.22.2
"svelte-i18n": 3.0.3
看起来这个警告不是问题,但是,对我来说是非常烦人的。我不太喜欢他们在这种情况下给出的解决方案("学会忍受这个警告并忽略它")。有什么方法可以指示编译器忽略这个错误,或者有人已经设法修复它?
EDIT:
我的问题是在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',
我找到了一个解决方案,在一个 文章 其中他们链接了必要的Rollup配置。基本上你需要把这个添加到 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';
}
}