@formatjs/ts-transformer
Version:
TS Compiler transformer for formatjs
123 lines (122 loc) • 4.12 kB
TypeScript
import * as typescript from "typescript";
//#region packages/ts-transformer/types.d.ts
interface MessageDescriptor {
id: string;
description?: string | object;
defaultMessage?: string;
file?: string;
start?: number;
end?: number;
}
interface Messages {
[key: string]: MessageDescriptor;
}
//#endregion
//#region packages/ts-transformer/transform.d.ts
type Extractor = (filePath: string, msgs: MessageDescriptor[]) => void;
type MetaExtractor = (filePath: string, meta: Record<string, string>) => void;
type InterpolateNameFn = (id?: MessageDescriptor["id"], defaultMessage?: MessageDescriptor["defaultMessage"], description?: MessageDescriptor["description"], filePath?: string) => string;
type TypeScript = typeof typescript;
interface Opts {
/**
* Parse specific additional custom pragma.
* This allows you to tag certain file with metadata such as `project`.
* For example with this file:
* ```tsx
* // @intl-meta project:my-custom-project
* import {FormattedMessage} from 'react-intl';
* <FormattedMessage defaultMessage="foo" id="bar" />;
* ```
* and with option `{pragma: "@intl-meta"}`,
* we'll parse out `// @intl-meta project:my-custom-project`
* into `{project: 'my-custom-project'}` in the result file.
*/
pragma?: string;
/**
* Whether the metadata about the location of the message in the source file
* should be extracted. If `true`, then `file`, `start`, and `end`
* fields will exist for each extracted message descriptors.
* Defaults to `false`.
*/
extractSourceLocation?: boolean;
/**
* Remove `defaultMessage` field in generated js after extraction.
*/
removeDefaultMessage?: boolean;
/**
* Additional component names to extract messages from,
* e.g: `['FormattedFooBarMessage']`.
*/
additionalComponentNames?: string[];
/**
* Additional function names to extract messages from,
* e.g: `['formatMessage', '$t']`
* Default to `['formatMessage']`
*/
additionalFunctionNames?: string[];
/**
* Callback function that gets called everytime we encountered something
* that looks like a MessageDescriptor
*
* @type {Extractor}
* @memberof Opts
*/
onMsgExtracted?: Extractor;
/**
* Callback function that gets called when we successfully parsed meta
* declared in pragma
*/
onMetaExtracted?: MetaExtractor;
/**
* webpack-style name interpolation.
* Can also be a string like '[sha512:contenthash:hex:6]'
*
* @type {(InterpolateNameFn | string)}
* @memberof Opts
*/
overrideIdFn?: InterpolateNameFn | string;
/**
* Whether to compile `defaultMessage` to AST.
* This is no-op if `removeDefaultMessage` is `true`
*/
ast?: boolean;
/**
* Whether to preserve whitespace and newlines.
*/
preserveWhitespace?: boolean;
/**
* Whether to hoist selectors & flatten sentences
*/
flatten?: boolean;
/**
* Whether to throw on errors extracting messages.
* When false, invalid messages are skipped with a warning.
* Defaults to true.
*/
throws?: boolean;
/**
* Callback for reporting errors when throws is false.
*/
onMsgError?: (filePath: string, error: Error) => void;
}
declare function transformWithTs(ts: TypeScript, opts: Opts): typescript.TransformerFactory<typescript.SourceFile>;
declare function transform(opts: Opts): typescript.TransformerFactory<typescript.SourceFile>;
//#endregion
//#region packages/ts-transformer/interpolate-name.d.ts
interface LoaderContext {
resourceQuery?: string;
resourcePath?: string;
options?: {
customInterpolateName(this: LoaderContext, url: string, name: string | NameFn, options: Options): string;
};
}
interface Options {
context?: string;
content?: string;
regExp?: RegExp;
}
type NameFn = (resourcePath?: string, resourceQuery?: string) => string;
declare function interpolateName(loaderContext: LoaderContext, name: string | NameFn, options: Options): string;
//#endregion
export { Extractor, InterpolateNameFn, LoaderContext, MessageDescriptor, Messages, MetaExtractor, NameFn, Options, Opts, interpolateName, transform, transformWithTs };
//# sourceMappingURL=index.d.ts.map