简体   繁体   English

Typescript+webpack: typescript emmited no output for index.d.ts

[英]Typescript+webpack: typescript emmited no output for index.d.ts

I followed this tutorial to setup typescript+webpack (no react) with success.我按照本教程成功设置了 typescript+webpack(无反应)。 It all works great until I add index.d.ts file my components folder, which I use to export all my modules, like:在我将 index.d.ts 文件添加到我的组件文件夹之前,一切都很好,我用它来导出我的所有模块,例如:

export * from "./MyClass1";
export * from "./MyClass2";
export * from "./MyClass2";

Then I import it:然后我导入它:

import * as MyLib from "./components";

Code hinting and everything works fine in sublime editor.代码提示和一切在 sublime 编辑器中工作正常。

Initially, when I run it, I've got:最初,当我运行它时,我有:

Cannot resolve 'file' or 'directory'./components无法解析“文件”或“目录”./components

So I added d.ts to extensions in webpack.config.js:所以我将 d.ts 添加到 webpack.config.js 中的扩展:

 resolve: {
        extensions: ["", ".webpack.js", ".web.js", ".ts", ".tsx", ".js", ".d.ts"]
    },

Now, when I run webpack, I get this error:现在,当我运行 webpack 时,出现以下错误:

Typescript emitted no output for [...]\index.d.ts Typescript 没有发出 output [...]\index.d.ts

How should I solve this problem?我应该如何解决这个问题?

In this particular question the content of index.d.ts was not a definition but a module and should have been moved into index.ts file. 在这个特定的问题中, index.d.ts的内容不是定义而是模块,应该已经移入index.ts文件中。 I've also ran into error "typescript emmited no output for index.d.ts" but with valid declaration files. 我也遇到了错误“typescript emmited no output for index.d.ts”但是有效的声明文件。

It seems ts-loader tries to add .d.ts files to final bundle but finds nothing to add since they contain only declarations needed for type-checking during build. 似乎ts-loader尝试将.d.ts文件添加到最终的bundle但是没有发现任何东西要添加,因为它们只包含构建期间类型检查所需的声明。

Working solution for me is not to pass .d.ts files to ts-loader but to some loader that does nothing, eg ignore-loader . 对我来说,工作解决方案不是将.d.ts文件传递给ts-loader而是传递给一些什么都不做的加载器 ,例如ignore-loader Corresponding rules in my webpack.config.js are: 我的webpack.config.js中的相应规则是:

{
    test: /\.tsx?$/,
    loader: 'ts-loader',
    exclude: /node_modules|\.d\.ts$/
},
{
    test: /\.d\.ts$/,
    loader: 'ignore-loader'
},

ts-loader can be configured slightly differently if you use ES2018, where negative lookbehind for regular expressions was added: 如果使用ES2018,可以对ts-loader进行略微不同的配置,其中添加了正则表达式的负向lookbehind:

{
    test: /(?<!\.d)\.tsx?$/,
    loader: 'ts-loader',
    exclude: /node_modules/
},
//same ignore-loader config here

index.d.ts index.d.ts

This is a declaration file . 这是一个声明文件 A declaration file has no javascript emit. 声明文件没有javascript发射。

Fix 固定

instead of adding .d.ts as a resolvable extension (don't do that!), add the .d.ts into the compilation context, using something like tsconfig.json 而不是添加.d.ts作为可解析的扩展(不要这样做!),使用像tsconfig.json这样的东西将.d.ts添加到编译上下文中

More 更多

https://alm-tools.gitbooks.io/alm/content/config/tsconfig.html https://alm-tools.gitbooks.io/alm/content/config/tsconfig.html

Declaration files should be imported using the type keyword.应使用type关键字导入声明文件。

import type * as MyLib from "./components"

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

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