@formatjs/unplugin 1.0.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/LICENSE.md +9 -0
- package/README.md +106 -0
- package/esbuild.d.ts +5 -0
- package/esbuild.js +4 -0
- package/index.d.ts +6 -0
- package/index.js +14 -0
- package/package.json +65 -0
- package/rollup.d.ts +5 -0
- package/rollup.js +4 -0
- package/rspack.d.ts +5 -0
- package/rspack.js +4 -0
- package/transform.d.ts +14 -0
- package/transform.js +310 -0
- package/vite.d.ts +5 -0
- package/vite.js +4 -0
- package/webpack.d.ts +5 -0
- package/webpack.js +4 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 FormatJS
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# @formatjs/unplugin
|
|
2
|
+
|
|
3
|
+
Universal build plugin for FormatJS AST transformations. Supports Vite, Webpack, Rollup, esbuild, and Rspack with one codebase.
|
|
4
|
+
|
|
5
|
+
Replicates the functionality of `babel-plugin-formatjs` and `@formatjs/ts-transformer` using `oxc-parser` + `magic-string` for fast, compiler-agnostic builds.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @formatjs/unplugin --save-dev
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
### Vite
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
// vite.config.ts
|
|
19
|
+
import {defineConfig} from 'vite'
|
|
20
|
+
import formatjs from '@formatjs/unplugin/vite'
|
|
21
|
+
|
|
22
|
+
export default defineConfig({
|
|
23
|
+
plugins: [
|
|
24
|
+
formatjs({
|
|
25
|
+
idInterpolationPattern: '[sha512:contenthash:base64:6]',
|
|
26
|
+
ast: true,
|
|
27
|
+
}),
|
|
28
|
+
],
|
|
29
|
+
})
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Webpack
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
// webpack.config.js
|
|
36
|
+
const formatjs = require('@formatjs/unplugin/webpack').default
|
|
37
|
+
|
|
38
|
+
module.exports = {
|
|
39
|
+
plugins: [
|
|
40
|
+
formatjs({
|
|
41
|
+
idInterpolationPattern: '[sha512:contenthash:base64:6]',
|
|
42
|
+
ast: true,
|
|
43
|
+
}),
|
|
44
|
+
],
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Rollup
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
// rollup.config.js
|
|
52
|
+
import formatjs from '@formatjs/unplugin/rollup'
|
|
53
|
+
|
|
54
|
+
export default {
|
|
55
|
+
plugins: [
|
|
56
|
+
formatjs({
|
|
57
|
+
idInterpolationPattern: '[sha512:contenthash:base64:6]',
|
|
58
|
+
ast: true,
|
|
59
|
+
}),
|
|
60
|
+
],
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### esbuild
|
|
65
|
+
|
|
66
|
+
```js
|
|
67
|
+
import esbuild from 'esbuild'
|
|
68
|
+
import formatjs from '@formatjs/unplugin/esbuild'
|
|
69
|
+
|
|
70
|
+
esbuild.build({
|
|
71
|
+
plugins: [
|
|
72
|
+
formatjs({
|
|
73
|
+
idInterpolationPattern: '[sha512:contenthash:base64:6]',
|
|
74
|
+
ast: true,
|
|
75
|
+
}),
|
|
76
|
+
],
|
|
77
|
+
})
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Rspack
|
|
81
|
+
|
|
82
|
+
```js
|
|
83
|
+
// rspack.config.js
|
|
84
|
+
const formatjs = require('@formatjs/unplugin/rspack').default
|
|
85
|
+
|
|
86
|
+
module.exports = {
|
|
87
|
+
plugins: [
|
|
88
|
+
formatjs({
|
|
89
|
+
idInterpolationPattern: '[sha512:contenthash:base64:6]',
|
|
90
|
+
ast: true,
|
|
91
|
+
}),
|
|
92
|
+
],
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## Options
|
|
97
|
+
|
|
98
|
+
| Option | Type | Default | Description |
|
|
99
|
+
| -------------------------- | ---------- | ------------------------------- | --------------------------------------- |
|
|
100
|
+
| `idInterpolationPattern` | `string` | `[sha512:contenthash:base64:6]` | Pattern for generating message IDs |
|
|
101
|
+
| `overrideIdFn` | `function` | — | Custom function to generate message IDs |
|
|
102
|
+
| `removeDefaultMessage` | `boolean` | `false` | Remove `defaultMessage` from output |
|
|
103
|
+
| `additionalComponentNames` | `string[]` | `[]` | Extra JSX component names to process |
|
|
104
|
+
| `additionalFunctionNames` | `string[]` | `[]` | Extra function names to process |
|
|
105
|
+
| `ast` | `boolean` | `false` | Pre-parse `defaultMessage` into AST |
|
|
106
|
+
| `preserveWhitespace` | `boolean` | `false` | Keep original whitespace in messages |
|
package/esbuild.d.ts
ADDED
package/esbuild.js
ADDED
package/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type UnpluginFactory, type UnpluginInstance } from "unplugin";
|
|
2
|
+
import { type Options } from "./transform.js";
|
|
3
|
+
export type { Options } from "./transform.js";
|
|
4
|
+
export declare const unpluginFactory: UnpluginFactory<Options | undefined>;
|
|
5
|
+
export declare const unplugin: UnpluginInstance<Options | undefined>;
|
|
6
|
+
export default unplugin;
|
package/index.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { createUnplugin } from "unplugin";
|
|
2
|
+
import { transform } from "./transform.js";
|
|
3
|
+
export const unpluginFactory = (options = {}) => ({
|
|
4
|
+
name: "formatjs",
|
|
5
|
+
enforce: "pre",
|
|
6
|
+
transformInclude(id) {
|
|
7
|
+
return /\.[jt]sx?$/.test(id) && !id.includes("node_modules");
|
|
8
|
+
},
|
|
9
|
+
transform(code, id) {
|
|
10
|
+
return transform(code, id, options);
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
export const unplugin = /* @__PURE__ */ createUnplugin(unpluginFactory);
|
|
14
|
+
export default unplugin;
|
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@formatjs/unplugin",
|
|
3
|
+
"description": "Universal build plugin for FormatJS — supports Vite, Webpack, Rollup, esbuild, and Rspack",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Long Ho <holevietlong@gmail.com>",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"types": "index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": "./index.js",
|
|
11
|
+
"./vite": "./vite.js",
|
|
12
|
+
"./webpack": "./webpack.js",
|
|
13
|
+
"./rollup": "./rollup.js",
|
|
14
|
+
"./esbuild": "./esbuild.js",
|
|
15
|
+
"./rspack": "./rspack.js",
|
|
16
|
+
"./transform": "./transform.js"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@formatjs/icu-messageformat-parser": "workspace:*",
|
|
20
|
+
"@formatjs/ts-transformer": "workspace:*",
|
|
21
|
+
"magic-string": "^0.30.0",
|
|
22
|
+
"oxc-parser": "^0.114.0",
|
|
23
|
+
"unplugin": "^2.3.0"
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"@rspack/core": ">=1",
|
|
27
|
+
"esbuild": ">=0.17",
|
|
28
|
+
"rollup": ">=3",
|
|
29
|
+
"vite": ">=5",
|
|
30
|
+
"webpack": "^5.104.1"
|
|
31
|
+
},
|
|
32
|
+
"bugs": "https://github.com/formatjs/formatjs/issues",
|
|
33
|
+
"homepage": "https://github.com/formatjs/formatjs#readme",
|
|
34
|
+
"keywords": [
|
|
35
|
+
"esbuild-plugin",
|
|
36
|
+
"formatjs",
|
|
37
|
+
"i18n",
|
|
38
|
+
"internationalization",
|
|
39
|
+
"react-intl",
|
|
40
|
+
"rollup-plugin",
|
|
41
|
+
"rspack-plugin",
|
|
42
|
+
"unplugin",
|
|
43
|
+
"vite-plugin",
|
|
44
|
+
"webpack-plugin"
|
|
45
|
+
],
|
|
46
|
+
"main": "index.js",
|
|
47
|
+
"peerDependenciesMeta": {
|
|
48
|
+
"vite": {
|
|
49
|
+
"optional": true
|
|
50
|
+
},
|
|
51
|
+
"webpack": {
|
|
52
|
+
"optional": true
|
|
53
|
+
},
|
|
54
|
+
"rollup": {
|
|
55
|
+
"optional": true
|
|
56
|
+
},
|
|
57
|
+
"esbuild": {
|
|
58
|
+
"optional": true
|
|
59
|
+
},
|
|
60
|
+
"@rspack/core": {
|
|
61
|
+
"optional": true
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
"repository": "formatjs/formatjs.git"
|
|
65
|
+
}
|
package/rollup.d.ts
ADDED
package/rollup.js
ADDED
package/rspack.d.ts
ADDED
package/rspack.js
ADDED
package/transform.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import MagicString from "magic-string";
|
|
2
|
+
export interface Options {
|
|
3
|
+
overrideIdFn?: (id: string | undefined, defaultMessage: string | undefined, description: string | undefined, filePath: string) => string;
|
|
4
|
+
idInterpolationPattern?: string;
|
|
5
|
+
removeDefaultMessage?: boolean;
|
|
6
|
+
additionalComponentNames?: string[];
|
|
7
|
+
additionalFunctionNames?: string[];
|
|
8
|
+
ast?: boolean;
|
|
9
|
+
preserveWhitespace?: boolean;
|
|
10
|
+
}
|
|
11
|
+
export declare function transform(code: string, id: string, options: Options): {
|
|
12
|
+
code: string;
|
|
13
|
+
map: ReturnType<MagicString["generateMap"]>;
|
|
14
|
+
} | undefined;
|
package/transform.js
ADDED
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
import { parseSync, Visitor } from "oxc-parser";
|
|
2
|
+
import MagicString from "magic-string";
|
|
3
|
+
import { interpolateName } from "@formatjs/ts-transformer";
|
|
4
|
+
import { parse } from "@formatjs/icu-messageformat-parser";
|
|
5
|
+
const DEFAULT_ID_INTERPOLATION_PATTERN = "[sha512:contenthash:base64:6]";
|
|
6
|
+
export function transform(code, id, options) {
|
|
7
|
+
const { overrideIdFn, idInterpolationPattern = DEFAULT_ID_INTERPOLATION_PATTERN, removeDefaultMessage = false, additionalComponentNames = [], additionalFunctionNames = [], ast: preParseAst = false, preserveWhitespace = false } = options;
|
|
8
|
+
const componentNames = new Set(["FormattedMessage", ...additionalComponentNames]);
|
|
9
|
+
const functionNames = new Set([
|
|
10
|
+
"formatMessage",
|
|
11
|
+
"$t",
|
|
12
|
+
"$formatMessage",
|
|
13
|
+
"defineMessage",
|
|
14
|
+
"defineMessages",
|
|
15
|
+
...additionalFunctionNames
|
|
16
|
+
]);
|
|
17
|
+
// Compiled JSX runtime functions: _jsx(Component, props), React.createElement(Component, props)
|
|
18
|
+
const jsxRuntimeFunctions = new Set([
|
|
19
|
+
"jsx",
|
|
20
|
+
"_jsx",
|
|
21
|
+
"jsxs",
|
|
22
|
+
"_jsxs",
|
|
23
|
+
"jsxDEV",
|
|
24
|
+
"_jsxDEV",
|
|
25
|
+
"createElement"
|
|
26
|
+
]);
|
|
27
|
+
const result = parseSync(id, code, { sourceType: "module" });
|
|
28
|
+
const program = result.program;
|
|
29
|
+
let s;
|
|
30
|
+
function getStaticValue(node) {
|
|
31
|
+
if (!node) return undefined;
|
|
32
|
+
if (node.type === "StringLiteral" || node.type === "Literal") {
|
|
33
|
+
if (typeof node.value === "string") return node.value;
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
if (node.type === "TemplateLiteral") {
|
|
37
|
+
if (node.quasis.length === 1 && node.expressions.length === 0) {
|
|
38
|
+
return node.quasis[0].value.cooked ?? node.quasis[0].value.raw;
|
|
39
|
+
}
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
if (node.type === "BinaryExpression" && node.operator === "+") {
|
|
43
|
+
const left = getStaticValue(node.left);
|
|
44
|
+
const right = getStaticValue(node.right);
|
|
45
|
+
if (left !== undefined && right !== undefined) return left + right;
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
function extractDescriptor(objNode) {
|
|
51
|
+
const properties = objNode.properties;
|
|
52
|
+
const descriptor = {};
|
|
53
|
+
const locations = {};
|
|
54
|
+
// Use the position right after `{` as a stable insertion point
|
|
55
|
+
locations.insertionPoint = objNode.start + 1;
|
|
56
|
+
for (const prop of properties) {
|
|
57
|
+
if (prop.type !== "Property" && prop.type !== "ObjectProperty") continue;
|
|
58
|
+
const key = prop.key;
|
|
59
|
+
let name;
|
|
60
|
+
if (key.type === "Identifier") name = key.name;
|
|
61
|
+
else if (key.type === "StringLiteral" || key.type === "Literal") {
|
|
62
|
+
name = key.value;
|
|
63
|
+
}
|
|
64
|
+
if (!name) continue;
|
|
65
|
+
if (name !== "id" && name !== "defaultMessage" && name !== "description") continue;
|
|
66
|
+
const keyName = name;
|
|
67
|
+
const val = getStaticValue(prop.value);
|
|
68
|
+
if (val === undefined) continue;
|
|
69
|
+
descriptor[keyName] = val;
|
|
70
|
+
locations[keyName] = {
|
|
71
|
+
start: prop.value.start,
|
|
72
|
+
end: prop.value.end,
|
|
73
|
+
value: val
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
if (!descriptor.defaultMessage && !descriptor.id) return undefined;
|
|
77
|
+
return {
|
|
78
|
+
descriptor,
|
|
79
|
+
locations
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function extractJSXDescriptor(elementNode) {
|
|
83
|
+
const attributes = elementNode.attributes || [];
|
|
84
|
+
const descriptor = {};
|
|
85
|
+
const locations = {};
|
|
86
|
+
// Use the position right after the element name as the insertion point
|
|
87
|
+
// Find the first attribute start, or use the end of the element name
|
|
88
|
+
const firstAttr = attributes[0];
|
|
89
|
+
locations.insertionPoint = firstAttr ? firstAttr.start : elementNode.name.end;
|
|
90
|
+
for (const attr of attributes) {
|
|
91
|
+
if (attr.type !== "JSXAttribute") continue;
|
|
92
|
+
const attrName = attr.name;
|
|
93
|
+
if (!attrName || attrName.type !== "JSXIdentifier") continue;
|
|
94
|
+
const n = attrName.name;
|
|
95
|
+
if (n !== "id" && n !== "defaultMessage" && n !== "description") continue;
|
|
96
|
+
const keyName = n;
|
|
97
|
+
let val;
|
|
98
|
+
const valueNode = attr.value;
|
|
99
|
+
if (!valueNode) continue;
|
|
100
|
+
if (valueNode.type === "StringLiteral" || valueNode.type === "Literal") {
|
|
101
|
+
val = valueNode.value;
|
|
102
|
+
} else if (valueNode.type === "JSXExpressionContainer") {
|
|
103
|
+
val = getStaticValue(valueNode.expression);
|
|
104
|
+
}
|
|
105
|
+
if (val === undefined) continue;
|
|
106
|
+
descriptor[keyName] = val;
|
|
107
|
+
locations[keyName] = {
|
|
108
|
+
start: attr.value.start,
|
|
109
|
+
end: attr.value.end,
|
|
110
|
+
value: val
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
if (!descriptor.defaultMessage && !descriptor.id) return undefined;
|
|
114
|
+
return {
|
|
115
|
+
descriptor,
|
|
116
|
+
locations
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
function processDescriptor(descriptor, locations, isJSX) {
|
|
120
|
+
let { defaultMessage, description } = descriptor;
|
|
121
|
+
let existingId = descriptor.id;
|
|
122
|
+
// Normalize whitespace on defaultMessage
|
|
123
|
+
if (defaultMessage && !preserveWhitespace) {
|
|
124
|
+
defaultMessage = defaultMessage.trim().replace(/\s+/gm, " ");
|
|
125
|
+
}
|
|
126
|
+
// Generate ID
|
|
127
|
+
let newId = existingId;
|
|
128
|
+
if (overrideIdFn) {
|
|
129
|
+
newId = overrideIdFn(existingId, defaultMessage, description, id);
|
|
130
|
+
} else if (!existingId && idInterpolationPattern && defaultMessage) {
|
|
131
|
+
newId = interpolateName({ resourcePath: id }, idInterpolationPattern, { content: description ? `${defaultMessage}#${description}` : defaultMessage });
|
|
132
|
+
}
|
|
133
|
+
if (!newId && !defaultMessage) return;
|
|
134
|
+
if (!s) s = new MagicString(code);
|
|
135
|
+
// Insert/update id
|
|
136
|
+
if (newId) {
|
|
137
|
+
if (locations.id) {
|
|
138
|
+
// Replace existing id value
|
|
139
|
+
s.overwrite(locations.id.start, locations.id.end, JSON.stringify(newId));
|
|
140
|
+
} else if (locations.insertionPoint != null) {
|
|
141
|
+
// Insert id at a stable position (after `{` for objects, before first attr for JSX)
|
|
142
|
+
if (isJSX) {
|
|
143
|
+
s.appendLeft(locations.insertionPoint, `id=${JSON.stringify(newId)} `);
|
|
144
|
+
} else {
|
|
145
|
+
s.appendRight(locations.insertionPoint, `id: ${JSON.stringify(newId)}, `);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
// Remove description
|
|
150
|
+
if (locations.description) {
|
|
151
|
+
if (isJSX) {
|
|
152
|
+
// For JSX, we need to remove the whole attribute including the key
|
|
153
|
+
// Find the JSX attribute that contains this value
|
|
154
|
+
removeJSXAttribute(locations.description);
|
|
155
|
+
} else {
|
|
156
|
+
removeObjectProperty(locations.description);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
// Handle defaultMessage
|
|
160
|
+
if (locations.defaultMessage) {
|
|
161
|
+
if (removeDefaultMessage) {
|
|
162
|
+
if (isJSX) {
|
|
163
|
+
removeJSXAttribute(locations.defaultMessage);
|
|
164
|
+
} else {
|
|
165
|
+
removeObjectProperty(locations.defaultMessage);
|
|
166
|
+
}
|
|
167
|
+
} else if (defaultMessage) {
|
|
168
|
+
if (preParseAst) {
|
|
169
|
+
const parsed = parse(defaultMessage);
|
|
170
|
+
const jsonStr = JSON.stringify(parsed);
|
|
171
|
+
s.overwrite(locations.defaultMessage.start, locations.defaultMessage.end, isJSX ? `{${jsonStr}}` : jsonStr);
|
|
172
|
+
} else if (defaultMessage !== locations.defaultMessage.value) {
|
|
173
|
+
// Whitespace was normalized, update the value
|
|
174
|
+
s.overwrite(locations.defaultMessage.start, locations.defaultMessage.end, JSON.stringify(defaultMessage));
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
function removeObjectProperty(loc) {
|
|
180
|
+
if (!s) return;
|
|
181
|
+
// Find the property boundaries: walk back to find key start, walk forward past trailing comma
|
|
182
|
+
let propStart = loc.start;
|
|
183
|
+
// Walk backward past value, colon, key, and any whitespace
|
|
184
|
+
let i = loc.start - 1;
|
|
185
|
+
while (i >= 0 && (code[i] === " " || code[i] === " " || code[i] === "\n" || code[i] === "\r")) i--;
|
|
186
|
+
// Skip colon
|
|
187
|
+
if (i >= 0 && code[i] === ":") i--;
|
|
188
|
+
while (i >= 0 && (code[i] === " " || code[i] === " " || code[i] === "\n" || code[i] === "\r")) i--;
|
|
189
|
+
// Walk backward through key (identifier or string literal)
|
|
190
|
+
if (i >= 0 && (code[i] === "\"" || code[i] === "'")) {
|
|
191
|
+
const quote = code[i];
|
|
192
|
+
i--;
|
|
193
|
+
while (i >= 0 && code[i] !== quote) i--;
|
|
194
|
+
if (i >= 0) i--;
|
|
195
|
+
} else {
|
|
196
|
+
while (i >= 0 && /[a-zA-Z0-9_$]/.test(code[i])) i--;
|
|
197
|
+
}
|
|
198
|
+
propStart = i + 1;
|
|
199
|
+
let propEnd = loc.end;
|
|
200
|
+
// Walk forward past trailing comma and whitespace
|
|
201
|
+
let j = loc.end;
|
|
202
|
+
while (j < code.length && (code[j] === " " || code[j] === " ")) j++;
|
|
203
|
+
if (j < code.length && code[j] === ",") {
|
|
204
|
+
j++;
|
|
205
|
+
// Also skip whitespace after comma
|
|
206
|
+
while (j < code.length && (code[j] === " " || code[j] === " ")) j++;
|
|
207
|
+
// Skip one newline
|
|
208
|
+
if (j < code.length && code[j] === "\n") j++;
|
|
209
|
+
else if (j < code.length && code[j] === "\r") {
|
|
210
|
+
j++;
|
|
211
|
+
if (j < code.length && code[j] === "\n") j++;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
propEnd = j;
|
|
215
|
+
// Also remove leading whitespace/newline
|
|
216
|
+
let k = propStart - 1;
|
|
217
|
+
while (k >= 0 && (code[k] === " " || code[k] === " ")) k--;
|
|
218
|
+
if (k >= 0 && (code[k] === "\n" || code[k] === ",")) {
|
|
219
|
+
propStart = k + 1;
|
|
220
|
+
}
|
|
221
|
+
s.remove(propStart, propEnd);
|
|
222
|
+
}
|
|
223
|
+
function removeJSXAttribute(loc) {
|
|
224
|
+
if (!s) return;
|
|
225
|
+
// Walk backward from the value to find attribute name
|
|
226
|
+
let i = loc.start - 1;
|
|
227
|
+
// Skip the `=`
|
|
228
|
+
if (i >= 0 && code[i] === "=") i--;
|
|
229
|
+
// Walk backward through attribute name
|
|
230
|
+
while (i >= 0 && /[a-zA-Z0-9_$]/.test(code[i])) i--;
|
|
231
|
+
const attrStart = i + 1;
|
|
232
|
+
let attrEnd = loc.end;
|
|
233
|
+
// Skip trailing whitespace
|
|
234
|
+
let j = attrEnd;
|
|
235
|
+
while (j < code.length && (code[j] === " " || code[j] === " ")) j++;
|
|
236
|
+
attrEnd = j;
|
|
237
|
+
// Also remove leading whitespace
|
|
238
|
+
let k = attrStart - 1;
|
|
239
|
+
while (k >= 0 && (code[k] === " " || code[k] === " ")) k--;
|
|
240
|
+
const removeStart = k + 1;
|
|
241
|
+
s.remove(removeStart, attrEnd);
|
|
242
|
+
}
|
|
243
|
+
function getCalleeName(node) {
|
|
244
|
+
if (node.type === "Identifier") return node.name;
|
|
245
|
+
if (node.type === "MemberExpression" || node.type === "OptionalMemberExpression") {
|
|
246
|
+
if (node.property?.type === "Identifier") return node.property.name;
|
|
247
|
+
}
|
|
248
|
+
return undefined;
|
|
249
|
+
}
|
|
250
|
+
function handleCallExpression(node) {
|
|
251
|
+
const calleeName = getCalleeName(node.callee);
|
|
252
|
+
// Handle compiled JSX: _jsx(FormattedMessage, { id, description, defaultMessage })
|
|
253
|
+
if (calleeName && jsxRuntimeFunctions.has(calleeName)) {
|
|
254
|
+
const firstArg = node.arguments?.[0];
|
|
255
|
+
const componentName = firstArg?.type === "Identifier" ? firstArg.name : undefined;
|
|
256
|
+
if (componentName && componentNames.has(componentName)) {
|
|
257
|
+
const propsArg = node.arguments?.[1];
|
|
258
|
+
if (propsArg?.type === "ObjectExpression") {
|
|
259
|
+
const result = extractDescriptor(propsArg);
|
|
260
|
+
if (result) {
|
|
261
|
+
processDescriptor(result.descriptor, result.locations, false);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
if (calleeName && functionNames.has(calleeName)) {
|
|
267
|
+
if (calleeName === "defineMessages") {
|
|
268
|
+
// Process each value in the object
|
|
269
|
+
const arg = node.arguments?.[0];
|
|
270
|
+
if (arg?.type === "ObjectExpression") {
|
|
271
|
+
for (const prop of arg.properties) {
|
|
272
|
+
if (prop.type === "Property" && prop.value?.type === "ObjectExpression") {
|
|
273
|
+
const result = extractDescriptor(prop.value);
|
|
274
|
+
if (result) {
|
|
275
|
+
processDescriptor(result.descriptor, result.locations, false);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
} else {
|
|
281
|
+
const arg = node.arguments?.[0];
|
|
282
|
+
if (arg?.type === "ObjectExpression") {
|
|
283
|
+
const result = extractDescriptor(arg);
|
|
284
|
+
if (result) {
|
|
285
|
+
processDescriptor(result.descriptor, result.locations, false);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
function handleJSXOpeningElement(node) {
|
|
292
|
+
const name = node.name?.type === "JSXIdentifier" ? node.name.name : undefined;
|
|
293
|
+
if (name && componentNames.has(name)) {
|
|
294
|
+
const result = extractJSXDescriptor(node);
|
|
295
|
+
if (result) {
|
|
296
|
+
processDescriptor(result.descriptor, result.locations, true);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
const visitor = new Visitor({
|
|
301
|
+
CallExpression: handleCallExpression,
|
|
302
|
+
JSXOpeningElement: handleJSXOpeningElement
|
|
303
|
+
});
|
|
304
|
+
visitor.visit(program);
|
|
305
|
+
if (!s) return undefined;
|
|
306
|
+
return {
|
|
307
|
+
code: s.toString(),
|
|
308
|
+
map: s.generateMap({ hires: "boundary" })
|
|
309
|
+
};
|
|
310
|
+
}
|
package/vite.d.ts
ADDED
package/vite.js
ADDED
package/webpack.d.ts
ADDED
package/webpack.js
ADDED