省略"require"和“出口"从TypeScript发出的AMD依赖项 [英] Omit "require" and "exports" from TypeScript emitted AMD dependencies

查看:100
本文介绍了省略"require"和“出口"从TypeScript发出的AMD依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下TypeScript文件

Given the following TypeScript file,

export = {};

tsc(带有"module": "amd")将发出:

define(["require", "exports"], function (require, exports) {
  "use strict";
   return {};
});

但是,我希望它发出

define([], function() {
    "use strict";
    return {};
});

...,并且如果我明确导入它们,则仅包含requireexports,即

... and only include require or exports if I explicitly import them, i.e.

import relativeRequire = require("require");

有没有办法告诉TypeScript不要在发出的AMD模块中发出requireexports(即要求它 not 使用

Is there any way to tell TypeScript not to emit require and exports in emitted AMD modules (i.e. ask it not to use the CommonJS simplified wrapping)?

注意:

  • The output I propose is fully compliant with the AMD spec.
  • An empty dependencies array is the only way for the module to have zero dependencies (as opposed to omitting the dependencies array, which implies the require, exports, and module dependencies).

2017年7月4日更新: 看起来这实际上是TypeScript GitHub存储库中的一个未解决的问题: https://github.com/Microsoft/TypeScript/issues/669

UPDATE 4 July 2017: Looks like this is actually an open issue in the TypeScript GitHub repo: https://github.com/Microsoft/TypeScript/issues/669

在实施此方案之前,是否有任何实用的解决方案的想法? (或者,实际上是否有某种方法可以使TypeScript做到这一点?)

Any ideas for a pragmatic workaround until this gets implemented? (Or, is there actually some way to make TypeScript do this?)

推荐答案

我认为您尝试执行的操作没有任何实质性优势.对于超过玩具应用程序的任何应用程序,通过删除未使用的依赖项节省的执行时间将与其余应用程序的执行时间相形见war. requireexports都是虚拟模块,实例化花费很少. (通过虚拟",我的意思是它们完全在您使用的AMD加载器内部,并且不需要从网络或磁盘上的文件中进行任何获取.)我看到

I see no substantial advantage in what you are trying to do. Whatever execution time is saved by removing the unused dependencies will be dwarfed by the execution time of the rest of the app, for any app that is more than a toy app. Both require and exports are virtual modules that cost very little to instantiate. (By "virtual" I mean they are completely internal to the AMD loader you use and do not entail any fetching from the network or a file on disk.) I see issue 669 you mention has been open since September 2014 and deemed "accepted" since April 2015. No one seems to be hurting so badly that they are rushing to produce a pull request.

我不知道TypeScript可以开箱即用地完成您想做的任何事情.我最近研究了TypeScript如何发出其define调用,因为我需要将名为"module"的虚拟模块添加到依赖项列表中. (如果使用Angular,则要使用module.id将当前模块的ID传递给Angular,以便它可以解决诸如相对模板路径之类的问题.您可以使用module.id,对CommonJS输出没有问题,但对于AMD输出module默认不包含在依赖项列表中.)我通过编写一个构建步骤来解决此问题,该步骤在tsc发出后修改tsc发出的代码.它使用一个regexp来修改依赖项列表以添加"module",并修改回调以添加相应的参数.这对我有用,因为我正在添加.对于您要执行的操作来说,这不是一个很好的方法,因为您想删除依赖关系,但是在某些情况下,删除依赖关系会导致无效代码.

I don't know of any way TypeScript will do what you want out of the box. I recently researched how TypeScript emits its define calls because I needed to add the virtual module named "module" to the list of dependencies. (If you use Angular, you want to use module.id to pass the id of the current module to Angular so that it can resolve things like relative template paths, etc. You can use module.id without issue with CommonJS output but with AMD output module is not included by default in the list of dependencies.) I worked around the issue by writing a build step that modifies the code that tsc emits, after tsc has emitted it. It uses a regexp that modifies the dependency list to add "module", and modifies the callback to add the corresponding argument. This works for me because I'm adding. It is not a good enough approach for what you are trying to do because you want to remove dependencies but there may be cases where removing them would result in invalid code.

对于解决方法,您可以使用 Esprima 来检查tsc,并且如果传递给define的工厂函数中的代码未使用模块"require""exports"的值,则从相关性列表中删除未使用的模块,并从传递的参数列表中删除相应的参数恢复出厂功能.这将是最通用的解决方案. (除其他事项外,它与使用AMD加载程序在工厂功能内提供的异步require调用兼容(形式为require([...], function (...) {})).)但是,对该逻辑进行编码可能与涉及到的编码一样复杂.产生拉取请求,首先让tsc发出您想要的代码.

For a workaround, you could use Esprima to examine the JavaScript generated by tsc and if the values of the modules "require" and "exports" are not used by the code inside the factory function passed to define, then remove the unused modules from the dependency list and the corresponding arguments from the argument list passed to the factory function. This would be the most general solution. (Among other things, it would be compatible with using the asynchronous require call that AMD loaders make available (of the form require([...], function (...) {})) inside the factory function.) But coding this logic may be just as involved as it would be to produce a pull request have tsc emit the code you want in the first place.

这篇关于省略"require"和“出口"从TypeScript发出的AMD依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆