简体   繁体   English

通过TypeScript导入和Webpack2使用DatePicker

[英]Using DatePicker via TypeScript import and Webpack2

I'm trying to test Ant Design components within a TypeScript + React + Webpack2 setup. 我正在尝试在TypeScript + React + Webpack2设置中测试Ant设计组件。

I'm importing the DatePicker component manually, so that webpack can grab it out and build it into my bundle. 我手动导入DatePicker组件,以便webpack可以抓取它并将其构建到我的包中。

import * as React from "react";
import * as moment from "moment";
import DatePicker from 'antd/lib/date-picker';
import "antd/lib/date-picker/style/css";

export default class TestComponent extends React.Component<any, any> {
    render(): JSX.Element {
        return <DatePicker value={moment(new Date())} format={"YYYY.mm.dd."}/>;
    }
}

But when I try to run the compiled code in the browser, the component won't render, as the particular import compiles into this final code. 但是当我尝试在浏览器中运行已编译的代码时,组件将不会呈现,因为特定的导入会编译到最终的代码中。

Object.defineProperty(exports, "__esModule", { value: true });
var React = __webpack_require__(4);
var moment = __webpack_require__(0);
var date_picker_1 = __webpack_require__(287);
__webpack_require__(289);
var TestComponent = (function (_super) {
    __extends(TestComponent, _super);
    function TestComponent() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    TestComponent.prototype.render = function () {
        return React.createElement(date_picker_1.default, { value: moment(new Date()), format: "YYYY.mm.dd." });
    };
    return TestComponent;
}(React.Component));
exports.default = TestComponent;

Please note the date_picker_1.default reference, which is undefined , and causes the error. 请注意date_picker_1.default引用,该引用undefined ,并导致错误。

Why is this happening? 为什么会这样? It shouldn't call default on the imported object, just use it, as it is the DateTimePicker component itself. 它不应该在导入的对象上调用default ,只需使用它,因为它是DateTimePicker组件本身。

I have the following setup. 我有以下设置。 TypeScript compiles into es5 using commonjs module system. TypeScript使用commonjs模块系统编译成es5 This is done by the awesome-typescript-loader webpack loader. 这是由awesome-typescript-loader typescript awesome-typescript-loader webpack加载器完成的。 The rest should be done by webpack itself. 其余的应该由webpack本身完成。

The DateTimePicker does have a default export (see lib/date-picker/index.js at the bottom), but when TypeScript compiles it to a commonjs module there is some sort of problem. DateTimePicker确实有一个默认导出(请参见底部的lib / date-picker / index.js ),但是当TypeScript将其编译为commonjs模块时会出现某种问题。

Webpack 2 handles ES modules out of the box, so you should leave the imports to webpack instead. Webpack 2开箱即可处理ES模块,因此您应该将导入保留为webpack。 Simply change the compiler option module to es6 . 只需将编译器选项module更改为es6

"module": "es6"

You can change that without having to change your target, so it still outputs es5 but doesn't use commonjs modules. 您可以更改它而无需更改目标,因此它仍然输出es5但不使用commonjs模块。 From the TypeScript - Compiler Options : TypeScript - 编译器选项

► "ES6" and "ES2015" values may be used when targeting "ES5" or lower. ►定位“ES5”或更低时,可以使用“ES6”和“ES2015”值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM