vite-hmr-tchmi 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/.github/workflows/publish.yml +28 -0
- package/LICENSE +21 -0
- package/Readme.md +71 -0
- package/package.json +33 -0
- package/src/index.ts +46 -0
- package/tsconfig.json +28 -0
- package/vite.config.ts +25 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
name: Publish to npm
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [published]
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
build-and-publish:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
steps:
|
|
11
|
+
- uses: actions/checkout@v4
|
|
12
|
+
|
|
13
|
+
- name: Setup Node.js
|
|
14
|
+
uses: actions/setup-node@v4
|
|
15
|
+
with:
|
|
16
|
+
node-version: '20'
|
|
17
|
+
registry-url: 'https://registry.npmjs.org'
|
|
18
|
+
|
|
19
|
+
- name: Install dependencies
|
|
20
|
+
run: npm install
|
|
21
|
+
|
|
22
|
+
- name: Build project
|
|
23
|
+
run: npm run build
|
|
24
|
+
|
|
25
|
+
- name: Publish to npm
|
|
26
|
+
run: npm publish
|
|
27
|
+
env:
|
|
28
|
+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Robin Pichel
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/Readme.md
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# tchmi-vite-hmr
|
|
2
|
+
|
|
3
|
+
A utility library to enable Hot Module Replacement (HMR) for Beckhoff TwinCAT HMI (TcHmi) Framework Controls when using Vite.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## Disclaimer
|
|
8
|
+
|
|
9
|
+
**This is not an official Beckhoff product.** This project is a community-maintained tool. "TwinCAT" and "Beckhoff" are registered trademarks of Beckhoff Automation GmbH & Co. KG.
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Description
|
|
14
|
+
|
|
15
|
+
This package provides the necessary logic to hot-reload TcHmi Controls without refreshing the entire browser page. It works by:
|
|
16
|
+
|
|
17
|
+
* **Prototype Patching:** Dynamically updating the control's logic while preserving the internal state where possible.
|
|
18
|
+
* **Lifecycle Management:** Automatically triggering `__detach()` and `__attach()` to re-render the control in the DOM.
|
|
19
|
+
* **Seamless Integration:** Designed to work specifically with Vite's `import.meta.hot` API.
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install tchmi-vite-hmr --save-dev
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
In your TcHmi Control script file, import the handler and call it within the Vite HMR lifecycle:
|
|
34
|
+
```ts
|
|
35
|
+
import { handleTcHmiHmr } from 'tchmi-vite-hmr';
|
|
36
|
+
|
|
37
|
+
// ... Your Control Class Definition ...
|
|
38
|
+
export class MyCustomControl extends TcHmi.Controls.System.TcHmiControl {
|
|
39
|
+
// ...
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Enable HMR for this module
|
|
43
|
+
if (import.meta.hot) import.meta.hot.accept((newModule) => handleTcHmiHmr(newModule));
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
Hier ist der restliche Teil der README ab How it works im Markdown-Format zum Kopieren:
|
|
49
|
+
|
|
50
|
+
Markdown
|
|
51
|
+
|
|
52
|
+
## How it works
|
|
53
|
+
|
|
54
|
+
When a file change is detected, Vite provides the updated module. The `handleTcHmiHmr` function:
|
|
55
|
+
1. **Module Scanning:** Scans the new module for classes extending `TcHmiControl`.
|
|
56
|
+
2. **Instance Mapping:** Locates all active instances of these controls in the current `TcHmi.Controls.getMap()`.
|
|
57
|
+
3. **Detaching:** Calls `__detach()` on each instance to cleanly stop its current lifecycle.
|
|
58
|
+
4. **Prototype Patching:** Swaps the internal prototype of the existing instance with the new logic from the updated module.
|
|
59
|
+
5. **Re-Attaching:** Calls `__attach()` to re-render the control and restart its lifecycle with the new code.
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
## API Reference
|
|
64
|
+
|
|
65
|
+
### `handleTcHmiHmr(newModule: any): void`
|
|
66
|
+
|
|
67
|
+
The main entry point for patching controls.
|
|
68
|
+
|
|
69
|
+
| Parameter | Type | Description |
|
|
70
|
+
| :--- | :--- | :--- |
|
|
71
|
+
| `newModule` | `any` | The module object provided by `import.meta.hot.accept`. |
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "vite-hmr-tchmi",
|
|
3
|
+
"private": false,
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"dev": "tsc --watch",
|
|
8
|
+
"build": "tsc && vite build"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/robinpichel/vite-hmr-tchmi.git"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"beckhoff",
|
|
16
|
+
"twincat",
|
|
17
|
+
"tchmi",
|
|
18
|
+
"vite",
|
|
19
|
+
"hmr"
|
|
20
|
+
],
|
|
21
|
+
"author": "Robin Pichel",
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"vite": "^6.0.0 || ^7.0.0"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"typescript": "~5.9.3",
|
|
28
|
+
"vite": "npm:rolldown-vite@7.2.5"
|
|
29
|
+
},
|
|
30
|
+
"overrides": {
|
|
31
|
+
"vite": "npm:rolldown-vite@7.2.5"
|
|
32
|
+
}
|
|
33
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
function isClassExtending(targetClass: any, baseClass: any) {
|
|
2
|
+
if (!targetClass || !baseClass) return false;
|
|
3
|
+
|
|
4
|
+
const baseName = typeof baseClass === 'string' ? baseClass : baseClass.name;
|
|
5
|
+
|
|
6
|
+
let current = targetClass;
|
|
7
|
+
while (current !== null && current !== undefined) {
|
|
8
|
+
if (current === baseClass) return true;
|
|
9
|
+
if (current.constructor.name === baseName) return true;
|
|
10
|
+
|
|
11
|
+
current = Object.getPrototypeOf(current);
|
|
12
|
+
}
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function handleTcHmiHmr(newModule: any) {
|
|
17
|
+
|
|
18
|
+
if (!newModule) return;
|
|
19
|
+
|
|
20
|
+
for (const [_, newModuleElement] of Object.entries(newModule)) {
|
|
21
|
+
if (!(isClassExtending(newModuleElement, "TcHmi.Controls.System.TcHmiControl"))) continue;
|
|
22
|
+
const newControl = newModuleElement as any;
|
|
23
|
+
|
|
24
|
+
const instances = Array.from((window as any).TcHmi.Controls.getMap())
|
|
25
|
+
.filter(([_, inst]: any) => isClassExtending(inst, newControl)) as any[];
|
|
26
|
+
|
|
27
|
+
console.groupCollapsed(`🔥 HMR: Updated '${newControl.name}' with ${instances.length} instances.`);
|
|
28
|
+
|
|
29
|
+
for (const [id, instance] of instances) {
|
|
30
|
+
instance.__detach();
|
|
31
|
+
Object.setPrototypeOf(instance, newControl.prototype);
|
|
32
|
+
instance.__attach();
|
|
33
|
+
|
|
34
|
+
const logHelper = {
|
|
35
|
+
type: instance.getType(),
|
|
36
|
+
dom: instance.getElement()[0],
|
|
37
|
+
instance: instance,
|
|
38
|
+
[Symbol.toStringTag]: `✅ Patched & Reattached: '${id}'`
|
|
39
|
+
}
|
|
40
|
+
console.dir(logHelper);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
console.groupEnd();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"useDefineForClassFields": true,
|
|
5
|
+
"module": "ESNext",
|
|
6
|
+
"lib": ["ES2022", "DOM", "DOM.Iterable"],
|
|
7
|
+
"types": ["vite/client"],
|
|
8
|
+
"skipLibCheck": true,
|
|
9
|
+
|
|
10
|
+
"moduleResolution": "bundler",
|
|
11
|
+
"allowImportingTsExtensions": true,
|
|
12
|
+
"verbatimModuleSyntax": true,
|
|
13
|
+
"moduleDetection": "force",
|
|
14
|
+
|
|
15
|
+
"noEmit": false,
|
|
16
|
+
"outDir": "dist",
|
|
17
|
+
"declaration": true,
|
|
18
|
+
"emitDeclarationOnly": true,
|
|
19
|
+
|
|
20
|
+
"strict": true,
|
|
21
|
+
"noUnusedLocals": true,
|
|
22
|
+
"noUnusedParameters": true,
|
|
23
|
+
"erasableSyntaxOnly": true,
|
|
24
|
+
"noFallthroughCasesInSwitch": true,
|
|
25
|
+
"noUncheckedSideEffectImports": true
|
|
26
|
+
},
|
|
27
|
+
"include": ["src"]
|
|
28
|
+
}
|
package/vite.config.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { defineConfig } from 'vite';
|
|
2
|
+
import { resolve } from 'path';
|
|
3
|
+
|
|
4
|
+
export default defineConfig({
|
|
5
|
+
build: {
|
|
6
|
+
ssr: true,
|
|
7
|
+
lib: {
|
|
8
|
+
entry: resolve(__dirname, 'src/index.ts'),
|
|
9
|
+
name: 'ViteHmrTcHmi',
|
|
10
|
+
fileName: 'index',
|
|
11
|
+
formats: ['es']
|
|
12
|
+
},
|
|
13
|
+
rollupOptions: {
|
|
14
|
+
external: [
|
|
15
|
+
'vite',
|
|
16
|
+
],
|
|
17
|
+
output: {
|
|
18
|
+
globals: {
|
|
19
|
+
vite: 'Vite'
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
minify: true
|
|
24
|
+
}
|
|
25
|
+
});
|