【问题标题】:rollup.JS and "'this' keyword is equivalent to 'undefined'rollup.JS 和 "'this' 关键字等价于 'undefined'
【发布时间】:2017-09-19 07:06:42
【问题描述】:

我正在尝试使用 Rollup.js 捆绑 Angular2 模块。 这是我的 rollup.config.vendor.js 文件:

import typescript from 'rollup-plugin-typescript2';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';

export default {
    entry: 'vendor.ts',
    dest: './Bundle/vendor.js',
    format: 'iife',
    moduleName: 'vendor',
    plugins: [
        typescript(),
        resolve({
            jsnext: true,
            main: true,
            browser: true
        }),
        commonjs({
            include: 'node_modules/rxjs/**',
        }),
    ]
}

它创建了一个捆绑的js,但在这个过程中它不断打印这种消息:

The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten
https://github.com/rollup/rollup/wiki/Troubleshooting#this-is-undefined
node_modules\@angular\forms\@angular\forms.es5.js (1:25)
1: var __extends = (this && this.__extends) || function (d, b) {
                            ^
2:     for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
3:     function __() { this.constructor = d; }

这是什么意思?
我做错了什么还是它应该是这样?

【问题讨论】:

    标签: javascript angular rollupjs


    【解决方案1】:

    您可以通过将onwarn 属性添加到您的rollup-config.js 文件中来安全地忽略documentation 中解释的这些警告:

    onwarn: function(warning) {
        // Skip certain warnings
    
        // should intercept ... but doesn't in some rollup versions
        if ( warning.code === 'THIS_IS_UNDEFINED' ) { return; }
    
        // console.warn everything else
        console.warn( warning.message );
    }
    

    引用:

    它会覆盖默认的 onwarn 方法,以跳过关于 AOT 编译器使用 this 关键字的烦人消息。

    【讨论】:

      【解决方案2】:

      您可以使用context 选项并将其设置为this:它避免将this 重写为undefined(实际上this 被重写为...this)。

      查看汇总文档:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-18
        • 2014-03-08
        • 2023-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多