@formatjs/ts-transformer 3.9.10 → 3.9.11
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 +4 -0
- package/index.d.ts.map +1 -0
- package/index.js +6 -0
- package/package.json +2 -2
- package/src/console_utils.d.ts +4 -0
- package/src/console_utils.d.ts.map +1 -0
- package/src/console_utils.js +31 -0
- package/src/interpolate-name.d.ts +15 -0
- package/src/interpolate-name.d.ts.map +1 -0
- package/src/interpolate-name.js +90 -0
- package/src/transform.d.ts +78 -0
- package/src/transform.d.ts.map +1 -0
- package/src/transform.js +479 -0
- package/src/types.d.ts +12 -0
- package/src/types.d.ts.map +1 -0
- package/src/types.js +2 -0
- package/ts-jest-integration.d.ts +6 -0
- package/ts-jest-integration.d.ts.map +1 -0
- package/ts-jest-integration.js +10 -0
- package/BUILD +0 -82
- package/CHANGELOG.md +0 -713
- package/examples/compile.ts +0 -50
- package/index.ts +0 -3
- package/integration-tests/BUILD +0 -37
- package/integration-tests/integration/comp.tsx +0 -40
- package/integration-tests/integration/jest.config.js +0 -25
- package/integration-tests/integration/ts-jest.test.tsx +0 -32
- package/integration-tests/package.json +0 -5
- package/integration-tests/vue/fixtures/index.vue +0 -30
- package/integration-tests/vue/fixtures/main.ts +0 -4
- package/integration-tests/vue/integration.test.ts +0 -75
- package/src/console_utils.ts +0 -32
- package/src/interpolate-name.ts +0 -147
- package/src/transform.ts +0 -764
- package/src/types.ts +0 -12
- package/tests/__snapshots__/index.test.ts.snap +0 -908
- package/tests/fixtures/FormattedMessage.tsx +0 -35
- package/tests/fixtures/additionalComponentNames.tsx +0 -16
- package/tests/fixtures/additionalFunctionNames.tsx +0 -20
- package/tests/fixtures/ast.tsx +0 -83
- package/tests/fixtures/defineMessages.tsx +0 -67
- package/tests/fixtures/defineMessagesPreserveWhitespace.tsx +0 -87
- package/tests/fixtures/descriptionsAsObjects.tsx +0 -17
- package/tests/fixtures/extractFromFormatMessage.tsx +0 -45
- package/tests/fixtures/extractFromFormatMessageStateless.tsx +0 -46
- package/tests/fixtures/extractSourceLocation.tsx +0 -8
- package/tests/fixtures/formatMessageCall.tsx +0 -44
- package/tests/fixtures/inline.tsx +0 -26
- package/tests/fixtures/nested.tsx +0 -10
- package/tests/fixtures/noImport.tsx +0 -52
- package/tests/fixtures/overrideIdFn.tsx +0 -70
- package/tests/fixtures/removeDefaultMessage.tsx +0 -22
- package/tests/fixtures/removeDescription.tsx +0 -22
- package/tests/fixtures/resourcePath.tsx +0 -23
- package/tests/fixtures/stringConcat.tsx +0 -26
- package/tests/fixtures/templateLiteral.tsx +0 -21
- package/tests/index.test.ts +0 -127
- package/tests/interpolate-name.test.ts +0 -14
- package/ts-jest-integration.ts +0 -9
- package/tsconfig.json +0 -5
package/index.d.ts
ADDED
package/index.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAA;AAC/B,cAAc,aAAa,CAAA;AAC3B,cAAc,wBAAwB,CAAA"}
|
package/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./src/transform"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./src/types"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./src/interpolate-name"), exports);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@formatjs/ts-transformer",
|
|
3
|
-
"version": "3.9.
|
|
3
|
+
"version": "3.9.11",
|
|
4
4
|
"description": "TS Compiler transformer for formatjs",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"react-intl"
|
|
18
18
|
],
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@formatjs/icu-messageformat-parser": "2.1.
|
|
20
|
+
"@formatjs/icu-messageformat-parser": "2.1.6",
|
|
21
21
|
"@types/json-stable-stringify": "^1.0.32",
|
|
22
22
|
"@types/node": "14 || 16 || 17",
|
|
23
23
|
"chalk": "^4.0.0",
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"console_utils.d.ts","sourceRoot":"","sources":["console_utils.ts"],"names":[],"mappings":"AAeA,wBAAsB,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,iBAM1D;AAED,wBAAgB,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,IAAI,CAG1D;AAED,wBAAgB,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,IAAI,CAG3D"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.error = exports.warn = exports.debug = void 0;
|
|
4
|
+
const chalk_1 = require("chalk");
|
|
5
|
+
const util_1 = require("util");
|
|
6
|
+
const LEVEL_COLORS = {
|
|
7
|
+
debug: chalk_1.green,
|
|
8
|
+
warn: chalk_1.yellow,
|
|
9
|
+
error: chalk_1.red,
|
|
10
|
+
};
|
|
11
|
+
function label(level, message) {
|
|
12
|
+
return `[@formatjs/ts-transformer] [${LEVEL_COLORS[level](level.toUpperCase())}] ${message}`;
|
|
13
|
+
}
|
|
14
|
+
async function debug(message, ...args) {
|
|
15
|
+
if (process.env.LOG_LEVEL !== 'debug') {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
console.error((0, util_1.format)(label('debug', message), ...args));
|
|
19
|
+
console.error('\n');
|
|
20
|
+
}
|
|
21
|
+
exports.debug = debug;
|
|
22
|
+
function warn(message, ...args) {
|
|
23
|
+
console.error((0, util_1.format)(label('warn', message), ...args));
|
|
24
|
+
console.error('\n');
|
|
25
|
+
}
|
|
26
|
+
exports.warn = warn;
|
|
27
|
+
function error(message, ...args) {
|
|
28
|
+
console.error((0, util_1.format)(label('error', message), ...args));
|
|
29
|
+
console.error('\n');
|
|
30
|
+
}
|
|
31
|
+
exports.error = error;
|
|
@@ -0,0 +1,15 @@
|
|
|
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
|
+
};
|
|
7
|
+
}
|
|
8
|
+
export interface Options {
|
|
9
|
+
context?: string;
|
|
10
|
+
content?: string;
|
|
11
|
+
regExp?: RegExp;
|
|
12
|
+
}
|
|
13
|
+
export declare type NameFn = (resourcePath?: string, resourceQuery?: string) => string;
|
|
14
|
+
export declare function interpolateName(loaderContext: LoaderContext, name: string | NameFn, options: Options): string;
|
|
15
|
+
//# sourceMappingURL=interpolate-name.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interpolate-name.d.ts","sourceRoot":"","sources":["interpolate-name.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,aAAa;IAC5B,aAAa,CAAC,EAAE,MAAM,CAAA;IACtB,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,OAAO,CAAC,EAAE;QACR,qBAAqB,CACnB,IAAI,EAAE,aAAa,EACnB,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,MAAM,GAAG,MAAM,EACrB,OAAO,EAAE,OAAO,GACf,MAAM,CAAA;KACV,CAAA;CACF;AAED,MAAM,WAAW,OAAO;IACtB,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB;AAED,oBAAY,MAAM,GAAG,CAAC,YAAY,CAAC,EAAE,MAAM,EAAE,aAAa,CAAC,EAAE,MAAM,KAAK,MAAM,CAAA;AAa9E,wBAAgB,eAAe,CAC7B,aAAa,EAAE,aAAa,EAC5B,IAAI,EAAE,MAAM,GAAG,MAAM,EACrB,OAAO,EAAE,OAAO,UA6GjB"}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.interpolateName = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const path = tslib_1.__importStar(require("path"));
|
|
6
|
+
const crypto_1 = require("crypto");
|
|
7
|
+
function getHashDigest(content, hashType = 'md5', digestType = 'hex', length = 9999) {
|
|
8
|
+
const hasher = (0, crypto_1.createHash)(hashType);
|
|
9
|
+
hasher.update(content);
|
|
10
|
+
return hasher.digest(digestType).slice(0, length);
|
|
11
|
+
}
|
|
12
|
+
function interpolateName(loaderContext, name, options) {
|
|
13
|
+
let filename;
|
|
14
|
+
const hasQuery = loaderContext.resourceQuery && loaderContext.resourceQuery.length > 1;
|
|
15
|
+
if (typeof name === 'function') {
|
|
16
|
+
filename = name(loaderContext.resourcePath, hasQuery ? loaderContext.resourceQuery : undefined);
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
filename = name || '[hash].[ext]';
|
|
20
|
+
}
|
|
21
|
+
const context = options.context;
|
|
22
|
+
const content = options.content;
|
|
23
|
+
const regExp = options.regExp;
|
|
24
|
+
let ext = 'bin';
|
|
25
|
+
let basename = 'file';
|
|
26
|
+
let directory = '';
|
|
27
|
+
let folder = '';
|
|
28
|
+
let query = '';
|
|
29
|
+
if (loaderContext.resourcePath) {
|
|
30
|
+
const parsed = path.parse(loaderContext.resourcePath);
|
|
31
|
+
let resourcePath = loaderContext.resourcePath;
|
|
32
|
+
if (parsed.ext) {
|
|
33
|
+
ext = parsed.ext.slice(1);
|
|
34
|
+
}
|
|
35
|
+
if (parsed.dir) {
|
|
36
|
+
basename = parsed.name;
|
|
37
|
+
resourcePath = parsed.dir + path.sep;
|
|
38
|
+
}
|
|
39
|
+
if (typeof context !== 'undefined') {
|
|
40
|
+
directory = path
|
|
41
|
+
.relative(context, resourcePath + '_')
|
|
42
|
+
.replace(/\\/g, '/')
|
|
43
|
+
.replace(/\.\.(\/)?/g, '_$1');
|
|
44
|
+
directory = directory.slice(0, -1);
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
directory = resourcePath.replace(/\\/g, '/').replace(/\.\.(\/)?/g, '_$1');
|
|
48
|
+
}
|
|
49
|
+
if (directory.length === 1) {
|
|
50
|
+
directory = '';
|
|
51
|
+
}
|
|
52
|
+
else if (directory.length > 1) {
|
|
53
|
+
folder = path.basename(directory);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (loaderContext.resourceQuery && loaderContext.resourceQuery.length > 1) {
|
|
57
|
+
query = loaderContext.resourceQuery;
|
|
58
|
+
const hashIdx = query.indexOf('#');
|
|
59
|
+
if (hashIdx >= 0) {
|
|
60
|
+
query = query.slice(0, hashIdx);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
let url = filename;
|
|
64
|
+
if (content) {
|
|
65
|
+
// Match hash template
|
|
66
|
+
url = url
|
|
67
|
+
// `hash` and `contenthash` are same in `loader-utils` context
|
|
68
|
+
// let's keep `hash` for backward compatibility
|
|
69
|
+
.replace(/\[(?:([^:\]]+):)?(?:hash|contenthash)(?::([a-z]+\d*))?(?::(\d+))?\]/gi, (_, hashType, digestType, maxLength) => getHashDigest(content, hashType, digestType, parseInt(maxLength, 10)));
|
|
70
|
+
}
|
|
71
|
+
url = url
|
|
72
|
+
.replace(/\[ext\]/gi, () => ext)
|
|
73
|
+
.replace(/\[name\]/gi, () => basename)
|
|
74
|
+
.replace(/\[path\]/gi, () => directory)
|
|
75
|
+
.replace(/\[folder\]/gi, () => folder)
|
|
76
|
+
.replace(/\[query\]/gi, () => query);
|
|
77
|
+
if (regExp && loaderContext.resourcePath) {
|
|
78
|
+
const match = loaderContext.resourcePath.match(new RegExp(regExp));
|
|
79
|
+
match &&
|
|
80
|
+
match.forEach((matched, i) => {
|
|
81
|
+
url = url.replace(new RegExp('\\[' + i + '\\]', 'ig'), matched);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
if (typeof loaderContext.options === 'object' &&
|
|
85
|
+
typeof loaderContext.options.customInterpolateName === 'function') {
|
|
86
|
+
url = loaderContext.options.customInterpolateName.call(loaderContext, url, name, options);
|
|
87
|
+
}
|
|
88
|
+
return url;
|
|
89
|
+
}
|
|
90
|
+
exports.interpolateName = interpolateName;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import * as typescript from 'typescript';
|
|
2
|
+
import { MessageDescriptor } from './types';
|
|
3
|
+
export declare type Extractor = (filePath: string, msgs: MessageDescriptor[]) => void;
|
|
4
|
+
export declare type MetaExtractor = (filePath: string, meta: Record<string, string>) => void;
|
|
5
|
+
export declare type InterpolateNameFn = (id?: MessageDescriptor['id'], defaultMessage?: MessageDescriptor['defaultMessage'], description?: MessageDescriptor['description'], filePath?: string) => string;
|
|
6
|
+
declare type TypeScript = typeof typescript;
|
|
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
|
+
export declare function transformWithTs(ts: TypeScript, opts: Opts): typescript.TransformerFactory<typescript.SourceFile>;
|
|
76
|
+
export declare function transform(opts: Opts): typescript.TransformerFactory<typescript.SourceFile>;
|
|
77
|
+
export {};
|
|
78
|
+
//# sourceMappingURL=transform.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transform.d.ts","sourceRoot":"","sources":["transform.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,UAAU,MAAM,YAAY,CAAA;AACxC,OAAO,EAAC,iBAAiB,EAAC,MAAM,SAAS,CAAA;AAKzC,oBAAY,SAAS,GAAG,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,iBAAiB,EAAE,KAAK,IAAI,CAAA;AAC7E,oBAAY,aAAa,GAAG,CAC1B,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,KACzB,IAAI,CAAA;AAET,oBAAY,iBAAiB,GAAG,CAC9B,EAAE,CAAC,EAAE,iBAAiB,CAAC,IAAI,CAAC,EAC5B,cAAc,CAAC,EAAE,iBAAiB,CAAC,gBAAgB,CAAC,EACpD,WAAW,CAAC,EAAE,iBAAiB,CAAC,aAAa,CAAC,EAC9C,QAAQ,CAAC,EAAE,MAAM,KACd,MAAM,CAAA;AAQX,aAAK,UAAU,GAAG,OAAO,UAAU,CAAA;AA2FnC,MAAM,WAAW,IAAI;IACnB;;;;;;;;;;;;OAYG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;IACf;;;;;OAKG;IACH,qBAAqB,CAAC,EAAE,OAAO,CAAA;IAC/B;;OAEG;IACH,oBAAoB,CAAC,EAAE,OAAO,CAAA;IAC9B;;;OAGG;IACH,wBAAwB,CAAC,EAAE,MAAM,EAAE,CAAA;IACnC;;;;OAIG;IACH,uBAAuB,CAAC,EAAE,MAAM,EAAE,CAAA;IAClC;;;;;;OAMG;IACH,cAAc,CAAC,EAAE,SAAS,CAAA;IAC1B;;;OAGG;IACH,eAAe,CAAC,EAAE,aAAa,CAAA;IAC/B;;;;;;OAMG;IACH,YAAY,CAAC,EAAE,iBAAiB,GAAG,MAAM,CAAA;IACzC;;;OAGG;IACH,GAAG,CAAC,EAAE,OAAO,CAAA;IACb;;OAEG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAA;CAC7B;AAmiBD,wBAAgB,eAAe,CAAC,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,IAAI,wDA6BzD;AAED,wBAAgB,SAAS,CAAC,IAAI,EAAE,IAAI,wDAEnC"}
|