vatts 2.0.2 → 2.0.3-canary.1

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/dist/vue/App.vue CHANGED
@@ -1,192 +1,192 @@
1
- <!--
2
- This file is part of the Vatts.js Project.
3
- Copyright (c) 2026 mfraz
4
-
5
- Licensed under the Apache License, Version 2.0 (the "License");
6
- you may not use this file except in compliance with the License.
7
- You may obtain a copy of the License at
8
-
9
- http://www.apache.org/licenses/LICENSE-2.0
10
-
11
- Unless required by applicable law or agreed to in writing, software
12
- distributed under the License is distributed on an "AS IS" BASIS,
13
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- See the License for the specific language governing permissions and
15
- limitations under the License.
16
- -->
17
- <template>
18
- <component :is="resolvedLayout" v-if="resolvedLayout">
19
- <component
20
- :is="resolvedContent"
21
- v-bind="contentProps"
22
- :key="`page-${hmrTimestamp}-${currentPathKey}`"
23
- />
24
- </component>
25
-
26
- <component
27
- v-else
28
- :is="resolvedContent"
29
- v-bind="contentProps"
30
- :key="`page-${hmrTimestamp}-${currentPathKey}`"
31
- />
32
-
33
- <DevIndicator
34
- v-if="isDev"
35
- :has-build-error="!!buildError"
36
- @click-build-error="isErrorOpen = true"
37
- />
38
-
39
- <ErrorModal
40
- :error="buildError"
41
- :is-open="isErrorOpen"
42
- @close="isErrorOpen = false"
43
- @copy="copyBuildError"
44
- />
45
- </template>
46
-
47
- <script setup>
48
- import { ref, computed, onMounted, onUnmounted, shallowRef, watch, nextTick } from 'vue';
49
- import { router } from '../client/clientRouter';
50
- import DevIndicator from './DevIndicator.vue';
51
- import ErrorModal from './ErrorModal.vue';
52
-
53
- // --- Props ---
54
- const props = defineProps({
55
- componentMap: Object,
56
- routes: Array,
57
- initialComponentPath: String,
58
- initialParams: null,
59
- layoutComponent: null
60
- });
61
-
62
- // --- Estado ---
63
- const hmrTimestamp = ref(Date.now());
64
- const currentPathKey = ref(window.location.pathname); // Mantendo a correção da rota anterior
65
-
66
- const buildError = ref(window.__VATTS_BUILD_ERROR__ || null);
67
- const isErrorOpen = ref(!!window.__VATTS_BUILD_ERROR__);
68
- const isDev = process.env.NODE_ENV !== 'production';
69
-
70
- // --- HMR & Error Handling ---
71
- const handleBuildError = (ev) => {
72
- const e = ev?.detail;
73
- buildError.value = e || null;
74
- isErrorOpen.value = true;
75
- };
76
-
77
- const handleBuildOk = () => {
78
- buildError.value = null;
79
- isErrorOpen.value = false;
80
- };
81
-
82
- const copyBuildError = async () => {
83
- try {
84
- if (!buildError.value) return;
85
- const payload = JSON.stringify(buildError.value, null, 2);
86
- await navigator.clipboard.writeText(payload);
87
- } catch {
88
- // ignore
89
- }
90
- };
91
-
92
- // --- Roteamento ---
93
- const findRouteForPath = (path) => {
94
- for (const route of props.routes) {
95
- const regexPattern = route.pattern
96
- .replace(/\[\[\.\.\.(\w+)\]\]/g, '(?<$1>.+)?')
97
- .replace(/\[\.\.\.(\w+)\]/g, '(?<$1>.+)')
98
- .replace(/\/\[\[(\w+)\]\]/g, '(?:/(?<$1>[^/]+))?')
99
- .replace(/\[\[(\w+)\]\]/g, '(?<$1>[^/]+)?')
100
- .replace(/\[(\w+)\]/g, '(?<$1>[^/]+)');
101
- const regex = new RegExp(`^${regexPattern}/?$`);
102
- const match = path.match(regex);
103
- if (match) {
104
- return {
105
- componentPath: route.componentPath,
106
- params: match.groups || {},
107
- metadata: route.metadata
108
- };
109
- }
110
- }
111
- return null;
112
- };
113
-
114
- // Estado da Rota
115
- const CurrentPageComponent = shallowRef(null);
116
- const params = ref({});
117
-
118
- const updateRoute = () => {
119
- const currentPath = window.location.pathname.replace("index.html", '');
120
-
121
- // Atualiza a chave para garantir re-render na mesma rota com params diferentes
122
- currentPathKey.value = currentPath;
123
-
124
- const match = findRouteForPath(currentPath);
125
- if (match) {
126
- CurrentPageComponent.value = props.componentMap[match.componentPath];
127
- console.log(props.componentMap[match.componentPath] || 'null')
128
- params.value = match.params;
129
-
130
- if (match.metadata?.title != null) {
131
- document.title = match.metadata.title;
132
- }
133
- } else {
134
- CurrentPageComponent.value = null;
135
- params.value = {};
136
- }
137
- };
138
-
139
- // --- Computed ---
140
- const resolvedContent = computed(() => {
141
- if (!CurrentPageComponent.value || props.initialComponentPath === '__404__') {
142
- const NotFoundComponent = window.__VATTS_NOT_FOUND__;
143
- if (NotFoundComponent) return NotFoundComponent;
144
-
145
- const DefaultNotFound = window.__VATTS_DEFAULT_NOT_FOUND__;
146
- return DefaultNotFound || 'div';
147
- }
148
- return CurrentPageComponent.value;
149
- });
150
-
151
- const contentProps = computed(() => {
152
- if (!CurrentPageComponent.value) return {};
153
- return { params: params.value };
154
- });
155
-
156
- const resolvedLayout = computed(() => {
157
- return props.layoutComponent || null;
158
- });
159
-
160
- // --- Lifecycle ---
161
- onMounted(() => {
162
- updateRoute();
163
-
164
- window.addEventListener('vatts:build-error', handleBuildError);
165
- window.addEventListener('vatts:build-ok', handleBuildOk);
166
- window.addEventListener('popstate', updateRoute);
167
-
168
- const unsubscribeRouter = router.subscribe(updateRoute);
169
-
170
- window.__HWEB_HMR__ = true;
171
- const handleHMRUpdate = (event) => {
172
- const { file, timestamp } = event.detail;
173
- try {
174
- hmrTimestamp.value = timestamp;
175
- window.__HMR_SUCCESS__ = true;
176
- setTimeout(() => { window.__HMR_SUCCESS__ = false; }, 3000);
177
- updateRoute();
178
- } catch (error) {
179
- console.error('❌ HMR Error:', error);
180
- }
181
- };
182
- window.addEventListener('hmr:component-update', handleHMRUpdate);
183
-
184
- onUnmounted(() => {
185
- window.removeEventListener('vatts:build-error', handleBuildError);
186
- window.removeEventListener('vatts:build-ok', handleBuildOk);
187
- window.removeEventListener('popstate', updateRoute);
188
- window.removeEventListener('hmr:component-update', handleHMRUpdate);
189
- unsubscribeRouter();
190
- });
191
- });
1
+ <!--
2
+ This file is part of the Vatts.js Project.
3
+ Copyright (c) 2026 mfraz
4
+
5
+ Licensed under the Apache License, Version 2.0 (the "License");
6
+ you may not use this file except in compliance with the License.
7
+ You may obtain a copy of the License at
8
+
9
+ http://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ Unless required by applicable law or agreed to in writing, software
12
+ distributed under the License is distributed on an "AS IS" BASIS,
13
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ See the License for the specific language governing permissions and
15
+ limitations under the License.
16
+ -->
17
+ <template>
18
+ <component :is="resolvedLayout" v-if="resolvedLayout">
19
+ <component
20
+ :is="resolvedContent"
21
+ v-bind="contentProps"
22
+ :key="`page-${hmrTimestamp}-${currentPathKey}`"
23
+ />
24
+ </component>
25
+
26
+ <component
27
+ v-else
28
+ :is="resolvedContent"
29
+ v-bind="contentProps"
30
+ :key="`page-${hmrTimestamp}-${currentPathKey}`"
31
+ />
32
+
33
+ <DevIndicator
34
+ v-if="isDev"
35
+ :has-build-error="!!buildError"
36
+ @click-build-error="isErrorOpen = true"
37
+ />
38
+
39
+ <ErrorModal
40
+ :error="buildError"
41
+ :is-open="isErrorOpen"
42
+ @close="isErrorOpen = false"
43
+ @copy="copyBuildError"
44
+ />
45
+ </template>
46
+
47
+ <script setup>
48
+ import { ref, computed, onMounted, onUnmounted, shallowRef, watch, nextTick } from 'vue';
49
+ import { router } from '../client/clientRouter';
50
+ import DevIndicator from './DevIndicator.vue';
51
+ import ErrorModal from './ErrorModal.vue';
52
+
53
+ // --- Props ---
54
+ const props = defineProps({
55
+ componentMap: Object,
56
+ routes: Array,
57
+ initialComponentPath: String,
58
+ initialParams: null,
59
+ layoutComponent: null
60
+ });
61
+
62
+ // --- Estado ---
63
+ const hmrTimestamp = ref(Date.now());
64
+ const currentPathKey = ref(window.location.pathname); // Mantendo a correção da rota anterior
65
+
66
+ const buildError = ref(window.__VATTS_BUILD_ERROR__ || null);
67
+ const isErrorOpen = ref(!!window.__VATTS_BUILD_ERROR__);
68
+ const isDev = process.env.NODE_ENV !== 'production';
69
+
70
+ // --- HMR & Error Handling ---
71
+ const handleBuildError = (ev) => {
72
+ const e = ev?.detail;
73
+ buildError.value = e || null;
74
+ isErrorOpen.value = true;
75
+ };
76
+
77
+ const handleBuildOk = () => {
78
+ buildError.value = null;
79
+ isErrorOpen.value = false;
80
+ };
81
+
82
+ const copyBuildError = async () => {
83
+ try {
84
+ if (!buildError.value) return;
85
+ const payload = JSON.stringify(buildError.value, null, 2);
86
+ await navigator.clipboard.writeText(payload);
87
+ } catch {
88
+ // ignore
89
+ }
90
+ };
91
+
92
+ // --- Roteamento ---
93
+ const findRouteForPath = (path) => {
94
+ for (const route of props.routes) {
95
+ const regexPattern = route.pattern
96
+ .replace(/\[\[\.\.\.(\w+)\]\]/g, '(?<$1>.+)?')
97
+ .replace(/\[\.\.\.(\w+)\]/g, '(?<$1>.+)')
98
+ .replace(/\/\[\[(\w+)\]\]/g, '(?:/(?<$1>[^/]+))?')
99
+ .replace(/\[\[(\w+)\]\]/g, '(?<$1>[^/]+)?')
100
+ .replace(/\[(\w+)\]/g, '(?<$1>[^/]+)');
101
+ const regex = new RegExp(`^${regexPattern}/?$`);
102
+ const match = path.match(regex);
103
+ if (match) {
104
+ return {
105
+ componentPath: route.componentPath,
106
+ params: match.groups || {},
107
+ metadata: route.metadata
108
+ };
109
+ }
110
+ }
111
+ return null;
112
+ };
113
+
114
+ // Estado da Rota
115
+ const CurrentPageComponent = shallowRef(null);
116
+ const params = ref({});
117
+
118
+ const updateRoute = () => {
119
+ const currentPath = window.location.pathname.replace("index.html", '');
120
+
121
+ // Atualiza a chave para garantir re-render na mesma rota com params diferentes
122
+ currentPathKey.value = currentPath;
123
+
124
+ const match = findRouteForPath(currentPath);
125
+ if (match) {
126
+ CurrentPageComponent.value = props.componentMap[match.componentPath];
127
+ console.log(props.componentMap[match.componentPath] || 'null')
128
+ params.value = match.params;
129
+
130
+ if (match.metadata?.title != null) {
131
+ document.title = match.metadata.title;
132
+ }
133
+ } else {
134
+ CurrentPageComponent.value = null;
135
+ params.value = {};
136
+ }
137
+ };
138
+
139
+ // --- Computed ---
140
+ const resolvedContent = computed(() => {
141
+ if (!CurrentPageComponent.value || props.initialComponentPath === '__404__') {
142
+ const NotFoundComponent = window.__VATTS_NOT_FOUND__;
143
+ if (NotFoundComponent) return NotFoundComponent;
144
+
145
+ const DefaultNotFound = window.__VATTS_DEFAULT_NOT_FOUND__;
146
+ return DefaultNotFound || 'div';
147
+ }
148
+ return CurrentPageComponent.value;
149
+ });
150
+
151
+ const contentProps = computed(() => {
152
+ if (!CurrentPageComponent.value) return {};
153
+ return { params: params.value };
154
+ });
155
+
156
+ const resolvedLayout = computed(() => {
157
+ return props.layoutComponent || null;
158
+ });
159
+
160
+ // --- Lifecycle ---
161
+ onMounted(() => {
162
+ updateRoute();
163
+
164
+ window.addEventListener('vatts:build-error', handleBuildError);
165
+ window.addEventListener('vatts:build-ok', handleBuildOk);
166
+ window.addEventListener('popstate', updateRoute);
167
+
168
+ const unsubscribeRouter = router.subscribe(updateRoute);
169
+
170
+ window.__HWEB_HMR__ = true;
171
+ const handleHMRUpdate = (event) => {
172
+ const { file, timestamp } = event.detail;
173
+ try {
174
+ hmrTimestamp.value = timestamp;
175
+ window.__HMR_SUCCESS__ = true;
176
+ setTimeout(() => { window.__HMR_SUCCESS__ = false; }, 3000);
177
+ updateRoute();
178
+ } catch (error) {
179
+ console.error('❌ HMR Error:', error);
180
+ }
181
+ };
182
+ window.addEventListener('hmr:component-update', handleHMRUpdate);
183
+
184
+ onUnmounted(() => {
185
+ window.removeEventListener('vatts:build-error', handleBuildError);
186
+ window.removeEventListener('vatts:build-ok', handleBuildOk);
187
+ window.removeEventListener('popstate', updateRoute);
188
+ window.removeEventListener('hmr:component-update', handleHMRUpdate);
189
+ unsubscribeRouter();
190
+ });
191
+ });
192
192
  </script>