@lithent/hmr-parser 0.1.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 +146 -0
- package/dist/helper/calculator.d.ts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/lithentHmrParser.mjs +21509 -0
- package/dist/lithentHmrParser.mjs.map +1 -0
- package/dist/lithentHmrParser.umd.js +216 -0
- package/dist/lithentHmrParser.umd.js.map +1 -0
- package/dist/markerParse.d.ts +2 -0
- package/dist/noMarkerParse.d.ts +2 -0
- package/dist/shared.d.ts +4 -0
- package/dist/strings.d.ts +1 -0
- package/dist/tests/mark.test.d.ts +1 -0
- package/dist/tests/nomark.test.d.ts +1 -0
- package/dist/transform/componentRegister.d.ts +3 -0
- package/dist/transform/renderGuard.d.ts +3 -0
- package/dist/types.d.ts +28 -0
- package/dist/utils/ast/componentCollector.d.ts +8 -0
- package/package.json +52 -0
- package/src/helper/calculator.ts +169 -0
- package/src/index.ts +33 -0
- package/src/markerParse.ts +61 -0
- package/src/noMarkerParse.ts +70 -0
- package/src/shared.ts +109 -0
- package/src/strings.ts +176 -0
- package/src/tests/mark.test.ts +162 -0
- package/src/tests/nomark.test.tsx +100 -0
- package/src/transform/componentRegister.ts +50 -0
- package/src/transform/renderGuard.ts +59 -0
- package/src/types.ts +33 -0
- package/src/utils/ast/componentCollector.ts +119 -0
package/README.md
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
# @lithent/hmr-parser
|
|
2
|
+
|
|
3
|
+
HMR (Hot Module Replacement) parser utilities for Lithent build tools.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
`@lithent/hmr-parser` provides AST-based code transformation to inject HMR boundaries into Lithent components. This package is designed to be used by build tool plugins (like `@lithent/lithent-vite`) to enable hot reloading during development.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- **Marker-based transformation**: Use `/* lithent:hmr-boundary */` comments to explicitly mark HMR boundaries
|
|
12
|
+
- **Automatic transformation**: Automatically detect and wrap mount components with HMR boundaries
|
|
13
|
+
- **AST-based parsing**: Uses Babel parser for accurate code transformation
|
|
14
|
+
- **Source map support**: Generates source maps for debugging
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @lithent/hmr-parser
|
|
20
|
+
# or
|
|
21
|
+
pnpm add @lithent/hmr-parser
|
|
22
|
+
# or
|
|
23
|
+
yarn add @lithent/hmr-parser
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
### In Build Tool Plugins
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { transformWithHmr, shouldSkipTransform } from '@lithent/hmr-parser';
|
|
32
|
+
|
|
33
|
+
// In your Vite/Webpack/etc plugin's transform hook
|
|
34
|
+
export function myBuildPlugin() {
|
|
35
|
+
return {
|
|
36
|
+
name: 'my-lithent-plugin',
|
|
37
|
+
transform(code, id) {
|
|
38
|
+
// Skip non-transformable files
|
|
39
|
+
if (shouldSkipTransform(code)) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const result = transformWithHmr({
|
|
44
|
+
code,
|
|
45
|
+
markerRegex: /\/\*\s*lithent:hmr-boundary\s*([^*]*)\*\//g,
|
|
46
|
+
boundaryImportSpecifier: 'lithent/devHelper',
|
|
47
|
+
tagFunctionImportSpecifier: 'lithent',
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
code: result.code,
|
|
52
|
+
map: result.map,
|
|
53
|
+
};
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Marker-based Transformation
|
|
60
|
+
|
|
61
|
+
Use comments to explicitly mark HMR boundaries:
|
|
62
|
+
|
|
63
|
+
```tsx
|
|
64
|
+
import { mount } from 'lithent';
|
|
65
|
+
|
|
66
|
+
/* lithent:hmr-boundary default */
|
|
67
|
+
|
|
68
|
+
const App = mount((renew, props) => {
|
|
69
|
+
return () => <div>Hello World</div>;
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
export default App;
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Automatic Transformation
|
|
76
|
+
|
|
77
|
+
Components using `mount` are automatically wrapped with HMR boundaries:
|
|
78
|
+
|
|
79
|
+
```tsx
|
|
80
|
+
import { mount } from 'lithent';
|
|
81
|
+
|
|
82
|
+
const App = mount((renew, props) => {
|
|
83
|
+
return () => <div>Hello World</div>;
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
// Automatically transformed to:
|
|
87
|
+
// const App = createHmrBoundary(
|
|
88
|
+
// mount((renew, props) => {
|
|
89
|
+
// return () => <div>Hello World</div>;
|
|
90
|
+
// }),
|
|
91
|
+
// import.meta.hot,
|
|
92
|
+
// 'App'
|
|
93
|
+
// );
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## API
|
|
97
|
+
|
|
98
|
+
### `transformWithHmr(options)`
|
|
99
|
+
|
|
100
|
+
Transform code with HMR boundaries.
|
|
101
|
+
|
|
102
|
+
**Options:**
|
|
103
|
+
- `code: string` - Source code to transform
|
|
104
|
+
- `markerRegex: RegExp` - Regular expression to match HMR boundary markers
|
|
105
|
+
- `boundaryImportSpecifier: string` - Import specifier for `createHmrBoundary` (default: `'lithent/devHelper'`)
|
|
106
|
+
- `tagFunctionImportSpecifier: string` - Import specifier for tag functions like `mount` (default: `'lithent'`)
|
|
107
|
+
|
|
108
|
+
**Returns:** `HmrTransformResult`
|
|
109
|
+
- `code: string` - Transformed code
|
|
110
|
+
- `map: object | null` - Source map
|
|
111
|
+
- `transformed: boolean` - Whether transformation occurred
|
|
112
|
+
|
|
113
|
+
### `shouldSkipTransform(code)`
|
|
114
|
+
|
|
115
|
+
Check if code should skip HMR transformation.
|
|
116
|
+
|
|
117
|
+
**Parameters:**
|
|
118
|
+
- `code: string` - Source code to check
|
|
119
|
+
|
|
120
|
+
**Returns:** `boolean` - `true` if transformation should be skipped
|
|
121
|
+
|
|
122
|
+
### `analyzeMarker(code, markerRegex)`
|
|
123
|
+
|
|
124
|
+
Analyze marker comments in code.
|
|
125
|
+
|
|
126
|
+
### `analyzeNoMarker(code)`
|
|
127
|
+
|
|
128
|
+
Analyze code without markers for automatic transformation.
|
|
129
|
+
|
|
130
|
+
## Browser Runtime
|
|
131
|
+
|
|
132
|
+
For the browser-side HMR runtime (`createHmrBoundary`, `registerUpdateHandler`), use `lithent/devHelper`:
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
import { createHmrBoundary } from 'lithent/devHelper';
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
**Note:** This package (`@lithent/hmr-parser`) is server-side only and includes Babel dependencies. It should only be used in build tool plugins, not bundled for browsers.
|
|
139
|
+
|
|
140
|
+
## License
|
|
141
|
+
|
|
142
|
+
MIT
|
|
143
|
+
|
|
144
|
+
## Repository
|
|
145
|
+
|
|
146
|
+
https://github.com/superlucky84/lithent
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { File } from '@babel/types';
|
|
2
|
+
export declare const collectExportNames: (ast: File) => string[];
|
|
3
|
+
export declare const detectLithentUsage: (ast: File) => boolean;
|
|
4
|
+
export declare const collectHeaderInsert: (ast: File, sourceCode: string, boundaryImportSpecifier: string, tagFunctionImportSpecifier: string) => {
|
|
5
|
+
importInsertionPos: number;
|
|
6
|
+
blockInsertionPos: number;
|
|
7
|
+
headerSnippet: string;
|
|
8
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { transformWithMarker } from './markerParse';
|
|
2
|
+
import { transformWithoutMarker } from './noMarkerParse';
|
|
3
|
+
import { analyzeMarker, analyzeNoMarker, shouldSkipTransform } from './shared';
|
|
4
|
+
import { MarkerTransformOptions, HmrTransformResult } from './types';
|
|
5
|
+
export declare const transformWithHmr: (options: MarkerTransformOptions) => HmrTransformResult;
|
|
6
|
+
export { analyzeMarker, analyzeNoMarker, shouldSkipTransform };
|
|
7
|
+
export { transformWithMarker, transformWithoutMarker };
|
|
8
|
+
export type { MarkerTransformOptions, BaseTransformOptions, HmrTransformResult, } from './types';
|