Typescript:如何在同时针对ES2015和COMMONJS时导入javascript方法

Typescript:如何在同时针对ES2015和COMMONJS时导入javascript方法,javascript,angular,typescript,ecmascript-6,commonjs,Javascript,Angular,Typescript,Ecmascript 6,Commonjs,在typescript源文件中,我使用库xml格式化程序: import format = require('xml-formatter') ... let formatted = format( myXml ) function format( xml, options ) { ... } module.exports = format; 由于xml格式化程序库没有typescript定义文件,我在tsconfig.json中设置了noImplicitAny=false 我的应用程序在针对

在typescript源文件中,我使用库xml格式化程序:

import format = require('xml-formatter')
...
let formatted = format( myXml )
function format( xml, options ) { ... }
module.exports = format;
由于
xml格式化程序
库没有typescript定义文件,我在tsconfig.json中设置了
noImplicitAny=false

我的应用程序在针对COMMONJS时编译并工作。但是,当将ES2015作为捆绑我的应用程序的目标时,使用命令ngc-p tsconfig aot.json编译时返回以下错误:

C:/Projets/IMASentinel/app/history app处出错。ts:5:1:当目标模块运行时,无法使用导入分配。考虑使用从“mod”“导入”为“ns”,从“mod”导入“{a}”,从“mod”导入“d”,而不是“./p>”格式。 我尝试使用错误中描述的语法:

import xmlformatter from "xml-formatter" 
or  import * as xmlformatter from "xml-formatter" 
or  import format from "xml-formatter" 
...

let formatted = xmlformatter.format( myXml )
。。但是在运行时找不到xmlformatter.format方法:因为方法Experted是唯一没有xmlformatter命名空间的格式

在xml格式化程序库中,方法格式按以下方式导出:

import format = require('xml-formatter')
...
let formatted = format( myXml )
function format( xml, options ) { ... }
module.exports = format;

您可以使用cjs-to-es6转换器:(我没有尝试过,但它看起来很有希望)为什么不改用简单的旧语法:
const format=require('xml-formatter')
?正如你所说,你没有任何定义。是的,这就是我现在找到的解决方案。在开发环境中,我使用systemjs,但在prod环境中,对应的是我需要在index.html中包含整个“xml格式化程序”脚本文件及其依赖项。如果我可以使用import语句,rollup将绑定bundle.js应用程序文件中使用的方法。您可以使用cjs-to-es6转换器:(我还没有尝试过,但看起来很有希望)为什么不改用简单的旧语法:
const format=require('xml-formatter')
?正如你所说,你没有任何定义。是的,这就是我现在找到的解决方案。在开发环境中,我使用systemjs,但在prod环境中,对应的是我需要在index.html中包含整个“xml格式化程序”脚本文件及其依赖项。如果我可以使用import语句,rollup将绑定bundle.js应用程序文件中使用的方法。