@lexical/compiler 0.0.0-bootstrap.0 → 0.49.1-nightly.20260901.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/README.md +228 -2
- package/dist/LexicalCompiler.dev.js +1725 -0
- package/dist/LexicalCompiler.dev.mjs +1704 -0
- package/dist/LexicalCompiler.js +11 -0
- package/dist/LexicalCompiler.js.flow +32 -0
- package/dist/LexicalCompiler.mjs +17 -0
- package/dist/LexicalCompiler.node.mjs +15 -0
- package/dist/LexicalCompiler.prod.js +9 -0
- package/dist/LexicalCompiler.prod.mjs +9 -0
- package/dist/LexicalCompilerPureAnnotations.dev.js +1725 -0
- package/dist/LexicalCompilerPureAnnotations.dev.mjs +1704 -0
- package/dist/LexicalCompilerPureAnnotations.js +11 -0
- package/dist/LexicalCompilerPureAnnotations.js.flow +78 -0
- package/dist/LexicalCompilerPureAnnotations.mjs +17 -0
- package/dist/LexicalCompilerPureAnnotations.node.mjs +15 -0
- package/dist/LexicalCompilerPureAnnotations.prod.js +9 -0
- package/dist/LexicalCompilerPureAnnotations.prod.mjs +9 -0
- package/dist/PureAnnotations.d.ts +172 -0
- package/dist/index.d.ts +8 -0
- package/dist/passes/pureAnnotations.d.mts +246 -0
- package/dist/typescript-too-old.d.ts +18 -0
- package/package.json +111 -6
- package/src/PureAnnotations.ts +206 -0
- package/src/index.ts +12 -0
- package/src/passes/pureAnnotations.mjs +1618 -0
package/package.json
CHANGED
|
@@ -1,11 +1,116 @@
|
|
|
1
1
|
{
|
|
2
|
-
"description": "Placeholder published to claim the npm name during trusted publishing setup. Do not install.",
|
|
3
|
-
"homepage": "https://lexical.dev",
|
|
4
|
-
"license": "MIT",
|
|
5
2
|
"name": "@lexical/compiler",
|
|
3
|
+
"description": "Build-time compiler for Lexical: tree-shaking annotations and inlining for the side-effect-free factories",
|
|
4
|
+
"keywords": [
|
|
5
|
+
"lexical",
|
|
6
|
+
"editor",
|
|
7
|
+
"compiler",
|
|
8
|
+
"vite-plugin",
|
|
9
|
+
"rollup-plugin",
|
|
10
|
+
"tree-shaking"
|
|
11
|
+
],
|
|
12
|
+
"version": "0.49.1-nightly.20260901.0",
|
|
13
|
+
"license": "MIT",
|
|
6
14
|
"repository": {
|
|
7
15
|
"type": "git",
|
|
8
|
-
"url": "git+https://github.com/facebook/lexical.git"
|
|
16
|
+
"url": "git+https://github.com/facebook/lexical.git",
|
|
17
|
+
"directory": "packages/lexical-compiler"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/facebook/lexical/issues"
|
|
21
|
+
},
|
|
22
|
+
"homepage": "https://lexical.dev/docs/packages/lexical-compiler",
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@babel/parser": "^8.0.0",
|
|
25
|
+
"magic-string": "^0.30.21"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"rollup": "^4.61.0"
|
|
29
|
+
},
|
|
30
|
+
"sideEffects": false,
|
|
31
|
+
"exports": {
|
|
32
|
+
"./PureAnnotations": {
|
|
33
|
+
"source": "./src/PureAnnotations.ts",
|
|
34
|
+
"import": {
|
|
35
|
+
"types@<5.2": "./dist/typescript-too-old.d.ts",
|
|
36
|
+
"types": "./dist/PureAnnotations.d.ts",
|
|
37
|
+
"development": "./dist/LexicalCompilerPureAnnotations.dev.mjs",
|
|
38
|
+
"production": "./dist/LexicalCompilerPureAnnotations.prod.mjs",
|
|
39
|
+
"node": "./dist/LexicalCompilerPureAnnotations.node.mjs",
|
|
40
|
+
"default": "./dist/LexicalCompilerPureAnnotations.mjs"
|
|
41
|
+
},
|
|
42
|
+
"require": {
|
|
43
|
+
"types@<5.2": "./dist/typescript-too-old.d.ts",
|
|
44
|
+
"types": "./dist/PureAnnotations.d.ts",
|
|
45
|
+
"development": "./dist/LexicalCompilerPureAnnotations.dev.js",
|
|
46
|
+
"production": "./dist/LexicalCompilerPureAnnotations.prod.js",
|
|
47
|
+
"default": "./dist/LexicalCompilerPureAnnotations.js"
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"./PureAnnotations.js": {
|
|
51
|
+
"source": "./src/PureAnnotations.ts",
|
|
52
|
+
"import": {
|
|
53
|
+
"types@<5.2": "./dist/typescript-too-old.d.ts",
|
|
54
|
+
"types": "./dist/PureAnnotations.d.ts",
|
|
55
|
+
"development": "./dist/LexicalCompilerPureAnnotations.dev.mjs",
|
|
56
|
+
"production": "./dist/LexicalCompilerPureAnnotations.prod.mjs",
|
|
57
|
+
"node": "./dist/LexicalCompilerPureAnnotations.node.mjs",
|
|
58
|
+
"default": "./dist/LexicalCompilerPureAnnotations.mjs"
|
|
59
|
+
},
|
|
60
|
+
"require": {
|
|
61
|
+
"types@<5.2": "./dist/typescript-too-old.d.ts",
|
|
62
|
+
"types": "./dist/PureAnnotations.d.ts",
|
|
63
|
+
"development": "./dist/LexicalCompilerPureAnnotations.dev.js",
|
|
64
|
+
"production": "./dist/LexicalCompilerPureAnnotations.prod.js",
|
|
65
|
+
"default": "./dist/LexicalCompilerPureAnnotations.js"
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
".": {
|
|
69
|
+
"source": "./src/index.ts",
|
|
70
|
+
"import": {
|
|
71
|
+
"types@<5.2": "./dist/typescript-too-old.d.ts",
|
|
72
|
+
"types": "./dist/index.d.ts",
|
|
73
|
+
"development": "./dist/LexicalCompiler.dev.mjs",
|
|
74
|
+
"production": "./dist/LexicalCompiler.prod.mjs",
|
|
75
|
+
"node": "./dist/LexicalCompiler.node.mjs",
|
|
76
|
+
"default": "./dist/LexicalCompiler.mjs"
|
|
77
|
+
},
|
|
78
|
+
"require": {
|
|
79
|
+
"types@<5.2": "./dist/typescript-too-old.d.ts",
|
|
80
|
+
"types": "./dist/index.d.ts",
|
|
81
|
+
"development": "./dist/LexicalCompiler.dev.js",
|
|
82
|
+
"production": "./dist/LexicalCompiler.prod.js",
|
|
83
|
+
"default": "./dist/LexicalCompiler.js"
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
"types": "./dist/typescript-too-old.d.ts",
|
|
88
|
+
"typesVersions": {
|
|
89
|
+
"*": {
|
|
90
|
+
"*": [
|
|
91
|
+
"./dist/typescript-too-old.d.ts"
|
|
92
|
+
]
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
"peerDependencies": {
|
|
96
|
+
"typescript": ">=5.2"
|
|
97
|
+
},
|
|
98
|
+
"peerDependenciesMeta": {
|
|
99
|
+
"typescript": {
|
|
100
|
+
"optional": true
|
|
101
|
+
}
|
|
9
102
|
},
|
|
10
|
-
"
|
|
11
|
-
|
|
103
|
+
"files": [
|
|
104
|
+
"dist",
|
|
105
|
+
"src",
|
|
106
|
+
"!src/__tests__",
|
|
107
|
+
"!src/__bench__",
|
|
108
|
+
"!src/__mocks__",
|
|
109
|
+
"!src/**/*.test.ts",
|
|
110
|
+
"!src/**/*.test.tsx",
|
|
111
|
+
"!src/**/*.bench.ts",
|
|
112
|
+
"!src/**/*.bench.tsx",
|
|
113
|
+
"README.md",
|
|
114
|
+
"LICENSE"
|
|
115
|
+
]
|
|
116
|
+
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import * as impl from './passes/pureAnnotations.mjs';
|
|
10
|
+
|
|
11
|
+
// The comments here spell the annotations without their leading sigil
|
|
12
|
+
// (`__PURE__`) because bundlers scan comments for annotations, and one that
|
|
13
|
+
// does not precede a call expression is reported as misplaced (rollup's
|
|
14
|
+
// INVALID_ANNOTATION).
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The subset of a source map that bundlers consume, matching what
|
|
18
|
+
* `magic-string` produces. Declared structurally so this package needs no
|
|
19
|
+
* dependency on any particular bundler's types.
|
|
20
|
+
*/
|
|
21
|
+
export interface PureAnnotationsSourceMap {
|
|
22
|
+
file?: string;
|
|
23
|
+
mappings: string;
|
|
24
|
+
names: string[];
|
|
25
|
+
sources: string[];
|
|
26
|
+
sourcesContent?: (string | null)[];
|
|
27
|
+
version: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface TransformPureAnnotationsOptions {
|
|
31
|
+
/**
|
|
32
|
+
* Path of the module being transformed, used to choose the TypeScript and
|
|
33
|
+
* JSX parser plugins (and named as the source map's source).
|
|
34
|
+
*/
|
|
35
|
+
filename?: string;
|
|
36
|
+
/**
|
|
37
|
+
* Names of the factories whose module-scope calls are annotated. Defaults
|
|
38
|
+
* to {@link PURE_FACTORY_FUNCTIONS}.
|
|
39
|
+
*/
|
|
40
|
+
functions?: Iterable<string>;
|
|
41
|
+
/**
|
|
42
|
+
* Replace calls to the factories whose result is a trivial expression over
|
|
43
|
+
* their arguments (`safeCast`, `defineExtension`, `configExtension`,
|
|
44
|
+
* `declarePeerDependency`, `defineImportRule`) with that expression rather
|
|
45
|
+
* than annotating them — a literal needs no annotation to be dropped, and
|
|
46
|
+
* nothing is left to pin the definition it appears in.
|
|
47
|
+
*
|
|
48
|
+
* Off by default, because it assumes the Lexical being built matches this
|
|
49
|
+
* package's version. Lexical's own build turns it on for the bundles it
|
|
50
|
+
* publishes.
|
|
51
|
+
*/
|
|
52
|
+
inline?: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Imported objects whose method calls are side-effect free, so that
|
|
55
|
+
* `sel.tag('p').attr('data-x', true)` is annotated the way a call to a
|
|
56
|
+
* factory function is. Defaults to {@link PURE_NAMESPACES}.
|
|
57
|
+
*/
|
|
58
|
+
namespaces?: Iterable<string>;
|
|
59
|
+
/** Extra `@babel/parser` plugins to parse the module with. */
|
|
60
|
+
parserPlugins?: readonly unknown[];
|
|
61
|
+
/**
|
|
62
|
+
* Set to false to skip reading relatively imported modules to look for a
|
|
63
|
+
* `__NO_SIDE_EFFECTS__` declaration. Defaults to true.
|
|
64
|
+
*/
|
|
65
|
+
relativeImports?: boolean;
|
|
66
|
+
/** Set to false to skip source map generation. */
|
|
67
|
+
sourceMap?: boolean;
|
|
68
|
+
/**
|
|
69
|
+
* Module specifiers whose exports are trusted to be the factories, without
|
|
70
|
+
* reading the module they come from. Defaults to `lexical` and
|
|
71
|
+
* `@lexical/*`; a call to a same-named function from anywhere else is only
|
|
72
|
+
* annotated when the declaration it resolves to is itself marked
|
|
73
|
+
* `__NO_SIDE_EFFECTS__`.
|
|
74
|
+
*/
|
|
75
|
+
sources?: RegExp | readonly RegExp[];
|
|
76
|
+
/**
|
|
77
|
+
* Throw when a call evaluated inside one of the definitions is not known
|
|
78
|
+
* to be side-effect free. Such a call pins the definition into every
|
|
79
|
+
* bundle that imports the module, however well annotated the definition
|
|
80
|
+
* itself is, so this turns a silent loss of tree-shaking into a build
|
|
81
|
+
* error naming the call. Off by default.
|
|
82
|
+
*/
|
|
83
|
+
strict?: boolean;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface PureAnnotationsResult {
|
|
87
|
+
code: string;
|
|
88
|
+
/** The number of annotations that were inserted. */
|
|
89
|
+
count: number;
|
|
90
|
+
/**
|
|
91
|
+
* The number of calls that were replaced by the literal they would have
|
|
92
|
+
* returned. Always 0 unless `inline` is set.
|
|
93
|
+
*/
|
|
94
|
+
inlined: number;
|
|
95
|
+
map?: PureAnnotationsSourceMap;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export interface PureAnnotationsOptions extends TransformPureAnnotationsOptions {
|
|
99
|
+
/**
|
|
100
|
+
* Vite plugin ordering. Defaults to `'post'` so the transform runs after
|
|
101
|
+
* TypeScript and JSX have been compiled away.
|
|
102
|
+
*/
|
|
103
|
+
enforce?: 'post' | 'pre';
|
|
104
|
+
/** Module ids to skip. Defaults to none. */
|
|
105
|
+
exclude?: RegExp | readonly RegExp[];
|
|
106
|
+
/**
|
|
107
|
+
* Module ids to transform. Defaults to every `.js`/`.jsx`/`.ts`/`.tsx`
|
|
108
|
+
* (and `.mjs`/`.cjs`/`.mts`/`.cts`) module, including those in
|
|
109
|
+
* `node_modules` so that a Lexical package consumed through its `source`
|
|
110
|
+
* export condition is annotated.
|
|
111
|
+
*/
|
|
112
|
+
include?: RegExp | readonly RegExp[];
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* The structural shape of the returned plugin. It is assignable to Vite's
|
|
117
|
+
* `Plugin` and to Rollup's `Plugin`, so it can be dropped into the `plugins`
|
|
118
|
+
* array of either.
|
|
119
|
+
*/
|
|
120
|
+
export interface PureAnnotationsPlugin {
|
|
121
|
+
enforce?: 'post' | 'pre';
|
|
122
|
+
name: string;
|
|
123
|
+
transform(
|
|
124
|
+
code: string,
|
|
125
|
+
id: string,
|
|
126
|
+
): {code: string; map: PureAnnotationsSourceMap | null} | null;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* The Lexical factories that are annotated with `__NO_SIDE_EFFECTS__` at
|
|
131
|
+
* their definition and whose module-scope call sites therefore need a
|
|
132
|
+
* `__PURE__` annotation for bundlers to tree-shake unused definitions.
|
|
133
|
+
*/
|
|
134
|
+
export const PURE_FACTORY_FUNCTIONS: readonly string[] =
|
|
135
|
+
impl.PURE_FACTORY_FUNCTIONS;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Objects imported from a Lexical package whose method calls are
|
|
139
|
+
* side-effect free, so that a module-scope `sel.tag('p')` is annotated the
|
|
140
|
+
* way a factory call is. Only the outermost call of a chain is annotated:
|
|
141
|
+
* rollup, terser and esbuild all drop the whole chain from that one.
|
|
142
|
+
*/
|
|
143
|
+
export const PURE_NAMESPACES: readonly string[] = impl.PURE_NAMESPACES;
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* The factories that `inline` replaces with a literal, and the form each one
|
|
147
|
+
* takes: `identity` returns its single argument and `args` returns all of its
|
|
148
|
+
* arguments as an array. Each is marked `@lexical-inline <form>` where it is
|
|
149
|
+
* defined.
|
|
150
|
+
*/
|
|
151
|
+
export const INLINE_FACTORY_FORMS: ReadonlyMap<string, string> =
|
|
152
|
+
impl.INLINE_FACTORY_FORMS;
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Insert a `__PURE__` annotation before every module-scope call to one of the
|
|
156
|
+
* side-effect-free Lexical factories that does not already have one. Returns
|
|
157
|
+
* `null` when the module needs no annotations.
|
|
158
|
+
*/
|
|
159
|
+
export function transformPureAnnotations(
|
|
160
|
+
code: string,
|
|
161
|
+
options?: TransformPureAnnotationsOptions,
|
|
162
|
+
): PureAnnotationsResult | null {
|
|
163
|
+
return impl.transformPureAnnotations(code, options);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* The local names in `code` whose module-scope calls the transform would
|
|
168
|
+
* annotate: `functions` are called directly, `namespaces` are objects whose
|
|
169
|
+
* method calls are annotated. A name qualifies by being imported from a
|
|
170
|
+
* Lexical package, declared in the module as side-effect free, or imported
|
|
171
|
+
* from a relative module that declares it that way. For tooling that needs
|
|
172
|
+
* the same decision without rewriting the module — a lint rule that removes
|
|
173
|
+
* a hand-written annotation only where the build puts one back. Throws on
|
|
174
|
+
* source it cannot parse.
|
|
175
|
+
*/
|
|
176
|
+
export function pureCallNames(
|
|
177
|
+
code: string,
|
|
178
|
+
options?: TransformPureAnnotationsOptions,
|
|
179
|
+
): {
|
|
180
|
+
functions: ReadonlySet<string>;
|
|
181
|
+
namespaces: ReadonlySet<string>;
|
|
182
|
+
} {
|
|
183
|
+
return impl.pureCallNames(code, options);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* A Vite/Rollup plugin that inserts the `__PURE__` annotations that let
|
|
188
|
+
* bundlers tree-shake unused Lexical extension, command, and rule
|
|
189
|
+
* definitions.
|
|
190
|
+
*
|
|
191
|
+
* The published `dist` bundles already carry the annotations, so this is for
|
|
192
|
+
* builds that compile Lexical from its TypeScript source (the `source`
|
|
193
|
+
* export condition, a git checkout, or a vendored copy) — and for annotating
|
|
194
|
+
* the extensions and commands your own application defines.
|
|
195
|
+
*
|
|
196
|
+
* ```js
|
|
197
|
+
* import {pureAnnotations} from '@lexical/compiler';
|
|
198
|
+
*
|
|
199
|
+
* export default defineConfig({plugins: [pureAnnotations()]});
|
|
200
|
+
* ```
|
|
201
|
+
*/
|
|
202
|
+
export function pureAnnotations(
|
|
203
|
+
options?: PureAnnotationsOptions,
|
|
204
|
+
): PureAnnotationsPlugin {
|
|
205
|
+
return impl.pureAnnotations(options);
|
|
206
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
// The compiler's passes, all of them. A build that wants one of them can
|
|
10
|
+
// import it on its own — `@lexical/compiler/PureAnnotations` — and skip
|
|
11
|
+
// loading the rest.
|
|
12
|
+
export * from './PureAnnotations';
|