我正在使用包裹,我想从生产中排除一些代码以节省尽可能多的空间。 我使用io-ts来验证来自后端的数据。我希望这种行为出现在测试和开发构建中,而不是生产中。
(我正在使用打字稿)
// this is how it should look in non-production builds
import * as t from 'io-ts';
const schema = t.array(t.string);
export function getData(): t.TypeOf<typeof schema> {
return fetch('/data').then(resp => resp.json()).then(data => {
if(schema.decode(data)._tag != 'Right') throw 'ERROR!!!';
return data;
});
}
// this is production
export function getData() {
return fetch('/data').then(resp => resp.json());
}
我试图将其分为两个模块...
// data-core.ts
export function getData() {
return fetch('/data').then(resp => resp.json());
}
// data.ts
import { getData as getData_ } from './data-core';
import * as t from 'io-ts';
const schema = t.array(t.string);
export function getData(): t.TypeOf<typeof schema> {
return getData_().then(data => {
if(schema.decode(data)._tag != 'Right') throw 'ERROR!!!';
return data;
});
}
...然后,如果NODE_ENV ===“ production”,那么可以使用许多不同的插件来导入“ data-core”而不是“ data”,但是我失败了。
我还尝试使用process.env.NODE_ENV使用真实的验证器或{decode: x => ({'_tag': 'Right'})}
,希望树摇晃走摆脱未使用的验证器,没有运气。