简体   繁体   English

如何防止TypeScript在输出中包含.d.ts导入?

[英]How to prevent TypeScript from including .d.ts import in the output?

I have installed a .d.ts file (namely, type definitions for p2.js library) as external (non-global) typings module with the following command: 我使用以下命令将.d.ts文件(即p2.js库的类型定义)安装为外部(非全局) typings模块:

typings install p2=github:schteppe/p2.js/index.d.ts#9d56924

I then use it in my TypeScript code like this: 然后我在我的TypeScript代码中使用它,如下所示:

import * as p2 from 'p2';

This import appears in the compiled javascript: 此导入显示在已编译的javascript中:

var p2 = require('p2');

But, obviously, I do not need it there, since the p2 external module contains only type definitions, not actual code. 但是,显然,我不需要它,因为p2外部模块只包含类型定义,而不包含实际代码。 I then run webpack on the TypeScript's output and it fails to compile with 然后我在TypeScript的输出上运行webpack ,它无法编译

...Module not found: Error: Cannot resolve module 'p2'...

What is the right way to use this external module so that TypeScript and my IDE (Atom with atom-typescript plugin) see the definitions, but no require call for it gets included in the TypeScript output? 使用此外部模块的正确方法是什么,以便TypeScript和我的IDE(带有atom-typescript插件的Atom)看到定义,但是require调用它才能包含在TypeScript输出中?

webpack supports configuring externals. webpack支持配置外部。

In your case this would be 在你的情况下,这将是
externals: [ { "p2": true}]

More info on handling externals in the webpack documentation: webpack.github.io/docs/configuration.html#externals. 有关处理webpack文档中的外部的更多信息:webpack.github.io/docs/configuration.html#externals。

Edit: updated with new information from comments 编辑:使用评论中的新信息进行更新

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

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