我正在使用Dinero.js和DefinitelyTyped库@ types / dinero.js提供类型。
我像这样使用图书馆:
import Dinero = require("dinero.js");
export class Money implements Dinero.DineroObject {
static fromAmount(amount: number): Money {
console.log("fromAmount")
console.log(Dinero)
let money = new Money()
money.amount = amount
money.currency = Money.currency
money.precision = Money.precision
console.log(Dinero(money).getAmount())
return money
}
}
Money.fromAmount(10)
使用ts-node运行脚本时,它可以正常工作:
fromAmount
{ [Function: Dinero]
defaultAmount: 0,
defaultCurrency: 'ZAR',
defaultPrecision: 2,
globalLocale: 'en-ZA',
globalFormat: '$0,0.00',
globalRoundingMode: 'HALF_EVEN',
globalFormatRoundingMode: 'HALF_AWAY_FROM_ZERO',
globalExchangeRatesApi:
{ endpoint: undefined,
headers: undefined,
propertyPath: undefined },
normalizePrecision: [Function: normalizePrecision],
minimum: [Function: minimum],
maximum: [Function: maximum] }
10
但是,用Webpack打包后,我得到:
fromAmount
Object [Module] {
default:
{ [Function: Dinero]
defaultAmount: 0,
defaultCurrency: 'USD',
defaultPrecision: 2,
globalLocale: 'en-US',
globalFormat: '$0,0.00',
globalRoundingMode: 'HALF_EVEN',
globalFormatRoundingMode: 'HALF_AWAY_FROM_ZERO',
globalExchangeRatesApi:
{ endpoint: undefined,
headers: undefined,
propertyPath: undefined },
normalizePrecision: [Function: normalizePrecision],
minimum: [Function: minimum],
maximum: [Function: maximum] },
defaultCurrency: 'ZAR',
defaultPrecision: 2,
globalLocale: 'en-ZA' }
webpack:///./src/models/*.ts?:131
console.log(Dinero(money).getAmount());
^
TypeError: Dinero is not a function
at Function.fromAmount (webpack:///./src/models/userscan.ts?:131:21)
at eval (webpack:///./src/models/transaction.ts?:24:58)
at Object../src/models/transaction.ts (/home/**/.serverless/**/*.js:18580:1)
at __webpack_require__ (/home/**/.serverless/**/src/*.js:20:30)
at eval (webpack:///./src/models/*.ts?:25:23)
at Object../src/models/payout.ts (/home/**/.serverless/**/*.js:18544:1)
at __webpack_require__ (/home/**/.serverless/**/*.js:20:30)
at eval (webpack:///./src/models/scan.ts?:29:18)
at Object../src/models/scan.ts (/home/**/.serverless/**/*.js:18556:1)
[请说明为什么webpack更改了Dinero的定义以及如何解决它。
这种导入对我来说似乎很奇怪,介于es6 import和commonJS之间。您可能想要选择其中一个,而不要混淆。
import Dinero = require("dinero.js")
我想webpack对此有点疯狂。尝试使用规范的commonjs方法要求它,然后如果可行,则使用es6 import。最后不要使用.js
,请参见下文。
const Dinero = require("dinero")