@formatjs/ts-transformer 4.1.0 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export * from './src/transform.js';
2
- export * from './src/types.js';
3
- export * from './src/interpolate-name.js';
1
+ export * from "./src/transform.js";
2
+ export * from "./src/types.js";
3
+ export * from "./src/interpolate-name.js";
package/index.js CHANGED
@@ -1,3 +1,3 @@
1
- export * from './src/transform.js';
2
- export * from './src/types.js';
3
- export * from './src/interpolate-name.js';
1
+ export * from "./src/transform.js";
2
+ export * from "./src/types.js";
3
+ export * from "./src/interpolate-name.js";
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@formatjs/ts-transformer",
3
3
  "description": "TS Compiler transformer for formatjs",
4
- "version": "4.1.0",
4
+ "version": "4.2.0",
5
5
  "license": "MIT",
6
6
  "author": "Long Ho <holevietlong@gmail.com>",
7
7
  "type": "module",
@@ -12,7 +12,7 @@
12
12
  "json-stable-stringify": "^1.3.0",
13
13
  "tslib": "^2.8.0",
14
14
  "typescript": "^5.6.0",
15
- "@formatjs/icu-messageformat-parser": "3.2.1"
15
+ "@formatjs/icu-messageformat-parser": "3.3.0"
16
16
  },
17
17
  "devDependencies": {
18
18
  "ts-jest": "^29"
@@ -1,26 +1,26 @@
1
- import * as chalkNs from 'chalk';
2
- import { format } from 'util';
1
+ import * as chalkNs from "chalk";
2
+ import { format } from "util";
3
3
  const chalk = chalkNs.default ?? chalkNs;
4
4
  const LEVEL_COLORS = {
5
- debug: chalk.green,
6
- warn: chalk.yellow,
7
- error: chalk.red,
5
+ debug: chalk.green,
6
+ warn: chalk.yellow,
7
+ error: chalk.red
8
8
  };
9
9
  function label(level, message) {
10
- return `[@formatjs/ts-transformer] [${LEVEL_COLORS[level](level.toUpperCase())}] ${message}`;
10
+ return `[@formatjs/ts-transformer] [${LEVEL_COLORS[level](level.toUpperCase())}] ${message}`;
11
11
  }
12
12
  export async function debug(message, ...args) {
13
- if (process.env.LOG_LEVEL !== 'debug') {
14
- return;
15
- }
16
- console.error(format(label('debug', message), ...args));
17
- console.error('\n');
13
+ if (process.env.LOG_LEVEL !== "debug") {
14
+ return;
15
+ }
16
+ console.error(format(label("debug", message), ...args));
17
+ console.error("\n");
18
18
  }
19
19
  export function warn(message, ...args) {
20
- console.error(format(label('warn', message), ...args));
21
- console.error('\n');
20
+ console.error(format(label("warn", message), ...args));
21
+ console.error("\n");
22
22
  }
23
23
  export function error(message, ...args) {
24
- console.error(format(label('error', message), ...args));
25
- console.error('\n');
24
+ console.error(format(label("error", message), ...args));
25
+ console.error("\n");
26
26
  }
@@ -1,14 +1,14 @@
1
1
  export interface LoaderContext {
2
- resourceQuery?: string;
3
- resourcePath?: string;
4
- options?: {
5
- customInterpolateName(this: LoaderContext, url: string, name: string | NameFn, options: Options): string;
6
- };
2
+ resourceQuery?: string;
3
+ resourcePath?: string;
4
+ options?: {
5
+ customInterpolateName(this: LoaderContext, url: string, name: string | NameFn, options: Options): string;
6
+ };
7
7
  }
8
8
  export interface Options {
9
- context?: string;
10
- content?: string;
11
- regExp?: RegExp;
9
+ context?: string;
10
+ content?: string;
11
+ regExp?: RegExp;
12
12
  }
13
13
  export type NameFn = (resourcePath?: string, resourceQuery?: string) => string;
14
14
  export declare function interpolateName(loaderContext: LoaderContext, name: string | NameFn, options: Options): string;
@@ -1,86 +1,71 @@
1
- import { createHash } from 'crypto';
2
- import * as path from 'path';
3
- function getHashDigest(content, hashType = 'md5', digestType = 'hex', length = 9999) {
4
- const hasher = createHash(hashType);
5
- hasher.update(content);
6
- return hasher.digest(digestType).slice(0, length);
1
+ import { createHash } from "crypto";
2
+ import * as path from "path";
3
+ function getHashDigest(content, hashType = "md5", digestType = "hex", length = 9999) {
4
+ const hasher = createHash(hashType);
5
+ hasher.update(content);
6
+ return hasher.digest(digestType).slice(0, length);
7
7
  }
8
8
  export function interpolateName(loaderContext, name, options) {
9
- let filename;
10
- const hasQuery = loaderContext.resourceQuery && loaderContext.resourceQuery.length > 1;
11
- if (typeof name === 'function') {
12
- filename = name(loaderContext.resourcePath, hasQuery ? loaderContext.resourceQuery : undefined);
13
- }
14
- else {
15
- filename = name || '[hash].[ext]';
16
- }
17
- const context = options.context;
18
- const content = options.content;
19
- const regExp = options.regExp;
20
- let ext = 'bin';
21
- let basename = 'file';
22
- let directory = '';
23
- let folder = '';
24
- let query = '';
25
- if (loaderContext.resourcePath) {
26
- const parsed = path.parse(loaderContext.resourcePath);
27
- let resourcePath = loaderContext.resourcePath;
28
- if (parsed.ext) {
29
- ext = parsed.ext.slice(1);
30
- }
31
- if (parsed.dir) {
32
- basename = parsed.name;
33
- resourcePath = parsed.dir + path.sep;
34
- }
35
- if (typeof context !== 'undefined') {
36
- directory = path
37
- .relative(context, resourcePath + '_')
38
- .replace(/\\/g, '/')
39
- .replace(/\.\.(\/)?/g, '_$1');
40
- directory = directory.slice(0, -1);
41
- }
42
- else {
43
- directory = resourcePath.replace(/\\/g, '/').replace(/\.\.(\/)?/g, '_$1');
44
- }
45
- if (directory.length === 1) {
46
- directory = '';
47
- }
48
- else if (directory.length > 1) {
49
- folder = path.basename(directory);
50
- }
51
- }
52
- if (loaderContext.resourceQuery && loaderContext.resourceQuery.length > 1) {
53
- query = loaderContext.resourceQuery;
54
- const hashIdx = query.indexOf('#');
55
- if (hashIdx >= 0) {
56
- query = query.slice(0, hashIdx);
57
- }
58
- }
59
- let url = filename;
60
- if (content) {
61
- // Match hash template
62
- url = url
63
- // `hash` and `contenthash` are same in `loader-utils` context
64
- // let's keep `hash` for backward compatibility
65
- .replace(/\[(?:([^:\]]+):)?(?:hash|contenthash)(?::([a-z]+\d*[a-z]*))?(?::(\d+))?\]/gi, (_, hashType, digestType, maxLength) => getHashDigest(content, hashType, digestType, parseInt(maxLength, 10)));
66
- }
67
- url = url
68
- .replace(/\[ext\]/gi, () => ext)
69
- .replace(/\[name\]/gi, () => basename)
70
- .replace(/\[path\]/gi, () => directory)
71
- .replace(/\[folder\]/gi, () => folder)
72
- .replace(/\[query\]/gi, () => query);
73
- if (regExp && loaderContext.resourcePath) {
74
- const match = loaderContext.resourcePath.match(new RegExp(regExp));
75
- if (match) {
76
- match.forEach((matched, i) => {
77
- url = url.replace(new RegExp('\\[' + i + '\\]', 'ig'), matched);
78
- });
79
- }
80
- }
81
- if (typeof loaderContext.options === 'object' &&
82
- typeof loaderContext.options.customInterpolateName === 'function') {
83
- url = loaderContext.options.customInterpolateName.call(loaderContext, url, name, options);
84
- }
85
- return url;
9
+ let filename;
10
+ const hasQuery = loaderContext.resourceQuery && loaderContext.resourceQuery.length > 1;
11
+ if (typeof name === "function") {
12
+ filename = name(loaderContext.resourcePath, hasQuery ? loaderContext.resourceQuery : undefined);
13
+ } else {
14
+ filename = name || "[hash].[ext]";
15
+ }
16
+ const context = options.context;
17
+ const content = options.content;
18
+ const regExp = options.regExp;
19
+ let ext = "bin";
20
+ let basename = "file";
21
+ let directory = "";
22
+ let folder = "";
23
+ let query = "";
24
+ if (loaderContext.resourcePath) {
25
+ const parsed = path.parse(loaderContext.resourcePath);
26
+ let resourcePath = loaderContext.resourcePath;
27
+ if (parsed.ext) {
28
+ ext = parsed.ext.slice(1);
29
+ }
30
+ if (parsed.dir) {
31
+ basename = parsed.name;
32
+ resourcePath = parsed.dir + path.sep;
33
+ }
34
+ if (typeof context !== "undefined") {
35
+ directory = path.relative(context, resourcePath + "_").replace(/\\/g, "/").replace(/\.\.(\/)?/g, "_$1");
36
+ directory = directory.slice(0, -1);
37
+ } else {
38
+ directory = resourcePath.replace(/\\/g, "/").replace(/\.\.(\/)?/g, "_$1");
39
+ }
40
+ if (directory.length === 1) {
41
+ directory = "";
42
+ } else if (directory.length > 1) {
43
+ folder = path.basename(directory);
44
+ }
45
+ }
46
+ if (loaderContext.resourceQuery && loaderContext.resourceQuery.length > 1) {
47
+ query = loaderContext.resourceQuery;
48
+ const hashIdx = query.indexOf("#");
49
+ if (hashIdx >= 0) {
50
+ query = query.slice(0, hashIdx);
51
+ }
52
+ }
53
+ let url = filename;
54
+ if (content) {
55
+ // Match hash template
56
+ url = url.replace(/\[(?:([^:\]]+):)?(?:hash|contenthash)(?::([a-z]+\d*[a-z]*))?(?::(\d+))?\]/gi, (_, hashType, digestType, maxLength) => getHashDigest(content, hashType, digestType, parseInt(maxLength, 10)));
57
+ }
58
+ url = url.replace(/\[ext\]/gi, () => ext).replace(/\[name\]/gi, () => basename).replace(/\[path\]/gi, () => directory).replace(/\[folder\]/gi, () => folder).replace(/\[query\]/gi, () => query);
59
+ if (regExp && loaderContext.resourcePath) {
60
+ const match = loaderContext.resourcePath.match(new RegExp(regExp));
61
+ if (match) {
62
+ match.forEach((matched, i) => {
63
+ url = url.replace(new RegExp("\\[" + i + "\\]", "ig"), matched);
64
+ });
65
+ }
66
+ }
67
+ if (typeof loaderContext.options === "object" && typeof loaderContext.options.customInterpolateName === "function") {
68
+ url = loaderContext.options.customInterpolateName.call(loaderContext, url, name, options);
69
+ }
70
+ return url;
86
71
  }
@@ -1,80 +1,80 @@
1
- import * as typescript from 'typescript';
2
- import { MessageDescriptor } from './types.js';
1
+ import * as typescript from "typescript";
2
+ import { type MessageDescriptor } from "./types.js";
3
3
  export type Extractor = (filePath: string, msgs: MessageDescriptor[]) => void;
4
4
  export type MetaExtractor = (filePath: string, meta: Record<string, string>) => void;
5
- export type InterpolateNameFn = (id?: MessageDescriptor['id'], defaultMessage?: MessageDescriptor['defaultMessage'], description?: MessageDescriptor['description'], filePath?: string) => string;
5
+ export type InterpolateNameFn = (id?: MessageDescriptor["id"], defaultMessage?: MessageDescriptor["defaultMessage"], description?: MessageDescriptor["description"], filePath?: string) => string;
6
6
  type TypeScript = typeof typescript;
7
7
  export interface Opts {
8
- /**
9
- * Parse specific additional custom pragma.
10
- * This allows you to tag certain file with metadata such as `project`.
11
- * For example with this file:
12
- * ```tsx
13
- * // @intl-meta project:my-custom-project
14
- * import {FormattedMessage} from 'react-intl';
15
- * <FormattedMessage defaultMessage="foo" id="bar" />;
16
- * ```
17
- * and with option `{pragma: "@intl-meta"}`,
18
- * we'll parse out `// @intl-meta project:my-custom-project`
19
- * into `{project: 'my-custom-project'}` in the result file.
20
- */
21
- pragma?: string;
22
- /**
23
- * Whether the metadata about the location of the message in the source file
24
- * should be extracted. If `true`, then `file`, `start`, and `end`
25
- * fields will exist for each extracted message descriptors.
26
- * Defaults to `false`.
27
- */
28
- extractSourceLocation?: boolean;
29
- /**
30
- * Remove `defaultMessage` field in generated js after extraction.
31
- */
32
- removeDefaultMessage?: boolean;
33
- /**
34
- * Additional component names to extract messages from,
35
- * e.g: `['FormattedFooBarMessage']`.
36
- */
37
- additionalComponentNames?: string[];
38
- /**
39
- * Additional function names to extract messages from,
40
- * e.g: `['formatMessage', '$t']`
41
- * Default to `['formatMessage']`
42
- */
43
- additionalFunctionNames?: string[];
44
- /**
45
- * Callback function that gets called everytime we encountered something
46
- * that looks like a MessageDescriptor
47
- *
48
- * @type {Extractor}
49
- * @memberof Opts
50
- */
51
- onMsgExtracted?: Extractor;
52
- /**
53
- * Callback function that gets called when we successfully parsed meta
54
- * declared in pragma
55
- */
56
- onMetaExtracted?: MetaExtractor;
57
- /**
58
- * webpack-style name interpolation.
59
- * Can also be a string like '[sha512:contenthash:hex:6]'
60
- *
61
- * @type {(InterpolateNameFn | string)}
62
- * @memberof Opts
63
- */
64
- overrideIdFn?: InterpolateNameFn | string;
65
- /**
66
- * Whether to compile `defaultMessage` to AST.
67
- * This is no-op if `removeDefaultMessage` is `true`
68
- */
69
- ast?: boolean;
70
- /**
71
- * Whether to preserve whitespace and newlines.
72
- */
73
- preserveWhitespace?: boolean;
74
- /**
75
- * Whether to hoist selectors & flatten sentences
76
- */
77
- flatten?: boolean;
8
+ /**
9
+ * Parse specific additional custom pragma.
10
+ * This allows you to tag certain file with metadata such as `project`.
11
+ * For example with this file:
12
+ * ```tsx
13
+ * // @intl-meta project:my-custom-project
14
+ * import {FormattedMessage} from 'react-intl';
15
+ * <FormattedMessage defaultMessage="foo" id="bar" />;
16
+ * ```
17
+ * and with option `{pragma: "@intl-meta"}`,
18
+ * we'll parse out `// @intl-meta project:my-custom-project`
19
+ * into `{project: 'my-custom-project'}` in the result file.
20
+ */
21
+ pragma?: string;
22
+ /**
23
+ * Whether the metadata about the location of the message in the source file
24
+ * should be extracted. If `true`, then `file`, `start`, and `end`
25
+ * fields will exist for each extracted message descriptors.
26
+ * Defaults to `false`.
27
+ */
28
+ extractSourceLocation?: boolean;
29
+ /**
30
+ * Remove `defaultMessage` field in generated js after extraction.
31
+ */
32
+ removeDefaultMessage?: boolean;
33
+ /**
34
+ * Additional component names to extract messages from,
35
+ * e.g: `['FormattedFooBarMessage']`.
36
+ */
37
+ additionalComponentNames?: string[];
38
+ /**
39
+ * Additional function names to extract messages from,
40
+ * e.g: `['formatMessage', '$t']`
41
+ * Default to `['formatMessage']`
42
+ */
43
+ additionalFunctionNames?: string[];
44
+ /**
45
+ * Callback function that gets called everytime we encountered something
46
+ * that looks like a MessageDescriptor
47
+ *
48
+ * @type {Extractor}
49
+ * @memberof Opts
50
+ */
51
+ onMsgExtracted?: Extractor;
52
+ /**
53
+ * Callback function that gets called when we successfully parsed meta
54
+ * declared in pragma
55
+ */
56
+ onMetaExtracted?: MetaExtractor;
57
+ /**
58
+ * webpack-style name interpolation.
59
+ * Can also be a string like '[sha512:contenthash:hex:6]'
60
+ *
61
+ * @type {(InterpolateNameFn | string)}
62
+ * @memberof Opts
63
+ */
64
+ overrideIdFn?: InterpolateNameFn | string;
65
+ /**
66
+ * Whether to compile `defaultMessage` to AST.
67
+ * This is no-op if `removeDefaultMessage` is `true`
68
+ */
69
+ ast?: boolean;
70
+ /**
71
+ * Whether to preserve whitespace and newlines.
72
+ */
73
+ preserveWhitespace?: boolean;
74
+ /**
75
+ * Whether to hoist selectors & flatten sentences
76
+ */
77
+ flatten?: boolean;
78
78
  }
79
79
  export declare function transformWithTs(ts: TypeScript, opts: Opts): typescript.TransformerFactory<typescript.SourceFile>;
80
80
  export declare function transform(opts: Opts): typescript.TransformerFactory<typescript.SourceFile>;