简体   繁体   English

在Javascript Webpack项目中过渡到Typescript(如何导出模块)

[英]Transitioning to Typescript in a Javascript Webpack project (how to export modules)

I'm currently trying to begin the process of converting a Javascript Webpack project to work in Typescript, so that everything is nicely typed. 我目前正在尝试开始将Javascript Webpack项目转换为可以在Typescript中工作的过程,以便可以很好地键入所有内容。 My configuration as it is, however, seems to fail to recognize the one file that I've changed to Typescript. 但是,我的配置似乎无法识别我已更改为Typescript的一个文件。

The project compiles, but I get this error at runtime: 该项目编译,但在运行时出现此错误:

TypeError: Angle.cyclic3dAxis is not a function

Angle is the (previously Javascript) file I rewrote in Typescript, consisting of 2 small static functions, in this format: Angle是我在Typescript中重写的(以前的Javascript)文件,由2个小型静态函数组成,格式为:

export class Angle
{
    public static cyclic3dAxis(i: number): number{ /* function defined here */ }
    public static anotherFunction(): number{/*defined here*/}
}

Original Javascript file (that I didn't write, but works), which I replaced with the TS one: 原始Javascript文件(我没有写过,但是可以使用),我用TS替换了它:

define([],function()
{
var Angle = { };
Angle.cyclic3dAxis = function(i) { /* function defined here */ };
Angle.anotherFunction = function() { /* defined here */ };
return Angle;
});

The functions contain identical code. 这些函数包含相同的代码。

Relevant parts of the webpack.config.json: webpack.config.json的相关部分:

/* some vars declared here truncated for brevity */
module.exports = function(env)
{
/* more stuff here */

resolve : {
    alias: {
        Cesium: path.resolve(__dirname, "scripts/Cesium/Cesium.js")
    },
    extensions: [".ts", ".tsx", ".js", ".json"]
},
module:  {
    rules : [
        {
            test: /\.js$/,
            exclude: /(Cesium|node_modules)/,
            use: [
                {
                    loader: 'babel-loader',
                    options: {
                        presets: ['es2015'],

                    }
                }
            ]
        },
        {
            test: /\.tsx?$/,
            exclude: /(Cesium|node_modules)/,
            use: [
                {
                    loader: 'awesome-typescript-loader'
                }
            ]
        },
/* and more */

and my tsconfig.json: 和我的tsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "sourceMap": true
    }
}

The entire project is quite large so if I set "allowJs" to true, I get an error about the Javascript heap being out of memory. 整个项目非常大,因此如果我将“ allowJs”设置为true,则会收到有关Javascript堆内存不足的错误。

Angle is referenced by other Javascript files in the project, like this: 项目中的其他Javascript文件引用了Angle,如下所示:

define([
    './Angle',
    ],
function(
    Angle
}
{
/* example function call */
functionName = function(i) {
    return Angle.cyclic3dAxis(i);
};
});

Please let me know if there is anything I'm doing wrong, or anything I need to add. 请让我知道我做错了什么,或者需要添加什么。 Appreciate your help! 感谢您的帮助!

Structure of exports in converted file doesn't match exports in original JS file. 转换后的文件中的导出结构与原始JS文件中的导出不匹配。 Module should look like this: 模块应如下所示:

export function cyclic3dAxis(i: number): number{ /* function defined here */ }

export function anotherFunction(): number{/*defined here*/}

Or if you need to have class with static methods it should be exported with export = Angle; 或者,如果您需要使用静态方法的类,则应使用export = Angle;导出该类export = Angle; :

class Angle
{
    public static cyclic3dAxis(i: number): number{ /* function defined here */ }
    public static anotherFunction(): number{/*defined here*/}
}

export = Angle;

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

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