element-vir 3.1.0 → 4.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/README.md +76 -66
- package/dist/functional-element/define-functional-element.js +0 -1
- package/dist/functional-element/functional-element.d.ts +0 -2
- package/index.html +14 -0
- package/jest/jest.config.ts +32 -0
- package/jest/jest.setup.ts +12 -0
- package/jest/read-tsconfig.ts +27 -0
- package/package.json +16 -11
- package/public/index.css +7 -0
- package/vite/always-reload-plugin.ts +90 -0
- package/vite/vite.config.ts +10 -0
package/README.md
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
# element-vir
|
|
2
2
|
|
|
3
|
-
Heroic
|
|
3
|
+
Heroic. Reactive. Functional. Type safe. Web components without compromise.
|
|
4
4
|
|
|
5
5
|
No need for an extra build step,<br>
|
|
6
|
-
no need for
|
|
6
|
+
no need for side effect imports, <br>
|
|
7
|
+
no need for unique file extensions,<br>
|
|
7
8
|
no need for extra static analysis tools,<br>
|
|
8
|
-
no need for a dedicated
|
|
9
|
+
no need for a dedicated, unique syntax.<br>
|
|
9
10
|
_**It's just TypeScript.**_
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
Uses the power of _native_ JavaScript custom web elements, _native_ JavaScript template literals, _native_ JavaScript functions<sup>\*</sup>, _native_ HTML, and [lit-element](http://lit.dev).
|
|
12
13
|
|
|
13
|
-
|
|
14
|
+
In reality this is basically a [lit-element](http://lit.dev) wrapper that adds type-safe element tag usage and I/O with functional-programming style component definition.
|
|
14
15
|
|
|
15
|
-
[Works in every major browser except Internet Explorer.](https://caniuse.com/mdn-api_window_customelements)
|
|
16
|
+
[Works in every major web browser except Internet Explorer.](https://caniuse.com/mdn-api_window_customelements)
|
|
16
17
|
|
|
17
18
|
<sub>\*okay I hope it's obvious that functions are native</sub>
|
|
18
19
|
|
|
@@ -24,15 +25,17 @@ This is basically a [lit-element](http://lit.dev) wrapper that adds type-safe I/
|
|
|
24
25
|
npm i element-vir
|
|
25
26
|
```
|
|
26
27
|
|
|
28
|
+
Make sure to install this as a normal dependency (not just a dev dependency) because it needs to exist at run time.
|
|
29
|
+
|
|
27
30
|
# Usage
|
|
28
31
|
|
|
29
32
|
Most usage of this package is done through the [`defineFunctionalElement` function](https://github.com/electrovir/element-vir/blob/main/src/functional-element/define-functional-element.ts#L25-L30). See the [`FunctionalElementInit` type](https://github.com/electrovir/element-vir/blob/main/src/functional-element/functional-element-init.ts#L7-L20) for that function's inputs. These inputs are also described below with examples.
|
|
30
33
|
|
|
31
|
-
All of [`lit`](https://lit.dev)'s syntax and functionality is also available for use.
|
|
34
|
+
All of [`lit`](https://lit.dev)'s syntax and functionality is also available for use if you wish.
|
|
32
35
|
|
|
33
36
|
## Simple element definition
|
|
34
37
|
|
|
35
|
-
Use `defineFunctionalElement` to define your element.
|
|
38
|
+
Use `defineFunctionalElement` to define your element. Tt must be given an object with at least `tagName` and `renderCallback` properties (the types enforce this). Here is a bare-minimum example custom element:
|
|
36
39
|
|
|
37
40
|
<!-- example-link: src/readme-examples/my-simple.element.ts -->
|
|
38
41
|
|
|
@@ -47,7 +50,7 @@ export const MySimpleElement = defineFunctionalElement({
|
|
|
47
50
|
});
|
|
48
51
|
```
|
|
49
52
|
|
|
50
|
-
Make sure to export your element definition
|
|
53
|
+
Make sure to export your element definition if you need to use it in other files.
|
|
51
54
|
|
|
52
55
|
## Using in other elements
|
|
53
56
|
|
|
@@ -70,17 +73,16 @@ export const MyAppElement = defineFunctionalElement({
|
|
|
70
73
|
|
|
71
74
|
This requirement ensures that the element is properly imported and registered with the browser. (Compare to pure [lit](http://lit.dev) where you must remember to import each element file as a side effect, or without actually referencing any of its exports in your code.)
|
|
72
75
|
|
|
73
|
-
If you wish to bypass this interpolation
|
|
76
|
+
If you wish to bypass this interpolation, make sure to [import the `html` tagged template directly from `lit`](https://lit.dev/docs/components/overview/), `import {html} from 'lit';`, instead of version contained in `element-vir`.
|
|
74
77
|
|
|
75
78
|
## Adding styles
|
|
76
79
|
|
|
77
|
-
Styles are added through `styles` when defining a functional element (similar to [how they are defined in `lit`](https://lit.dev/docs/components/styles/)):
|
|
80
|
+
Styles are added through the `styles` property when defining a functional element (similar to [how they are defined in `lit`](https://lit.dev/docs/components/styles/)):
|
|
78
81
|
|
|
79
82
|
<!-- example-link: src/readme-examples/my-simple-app-with-styles.element.ts -->
|
|
80
83
|
|
|
81
84
|
```TypeScript
|
|
82
|
-
import {css} from '
|
|
83
|
-
import {defineFunctionalElement, html} from 'element-vir';
|
|
85
|
+
import {css, defineFunctionalElement, html} from 'element-vir';
|
|
84
86
|
|
|
85
87
|
export const MySimpleWithStylesElement = defineFunctionalElement({
|
|
86
88
|
tagName: 'my-simple-with-styles-element',
|
|
@@ -102,9 +104,32 @@ export const MySimpleWithStylesElement = defineFunctionalElement({
|
|
|
102
104
|
});
|
|
103
105
|
```
|
|
104
106
|
|
|
107
|
+
### Element definition as style selector
|
|
108
|
+
|
|
109
|
+
Functional element definitions can be used in the `css` tagged template just like in the `html` tagged template. This will be replaced by the element's tag name:
|
|
110
|
+
|
|
111
|
+
<!-- example-link: src/readme-examples/my-simple-app-with-styles-and-interpolated-selector.element.ts -->
|
|
112
|
+
|
|
113
|
+
```TypeScript
|
|
114
|
+
import {css, defineFunctionalElement, html} from 'element-vir';
|
|
115
|
+
import {MySimpleElement} from './my-simple.element';
|
|
116
|
+
|
|
117
|
+
export const MySimpleWithStylesAndInterpolatedSelectorElement = defineFunctionalElement({
|
|
118
|
+
tagName: 'my-simple-with-styles-and-interpolated-selector-element',
|
|
119
|
+
styles: css`
|
|
120
|
+
${MySimpleElement} {
|
|
121
|
+
background-color: blue;
|
|
122
|
+
}
|
|
123
|
+
`,
|
|
124
|
+
renderCallback: () => html`
|
|
125
|
+
<${MySimpleElement}></${MySimpleElement}>
|
|
126
|
+
`,
|
|
127
|
+
});
|
|
128
|
+
```
|
|
129
|
+
|
|
105
130
|
## Defining and using properties (inputs)
|
|
106
131
|
|
|
107
|
-
Define properties with `props` when
|
|
132
|
+
Define element properties with `props` when making a functional element. Each property must be given a default value. If you wish to leave the property's default value as `undefined`, give it a type as well (shown below with `as string | undefined`) so you can assign a defined value of that type to it later.
|
|
108
133
|
|
|
109
134
|
To use a custom element's properties, grab `props` from `renderCallback`'s parameters and interpolate it into your HTML template:
|
|
110
135
|
|
|
@@ -148,43 +173,37 @@ export const MyAppWithPropsElement = defineFunctionalElement({
|
|
|
148
173
|
});
|
|
149
174
|
```
|
|
150
175
|
|
|
151
|
-
##
|
|
176
|
+
## Element events (outputs)
|
|
152
177
|
|
|
153
|
-
Define events with `events` when
|
|
178
|
+
Define events with `events` when making a functional element. Each event must be initialized with `defineElementEvent` and a type parameter. `defineElementEvent` accepts no inputs as it doesn't make sense for events to have default values.
|
|
154
179
|
|
|
155
|
-
To dispatch an event, grab `
|
|
180
|
+
To dispatch an event, grab `dispatch` from `renderCallback`'s parameters.
|
|
156
181
|
|
|
157
182
|
<!-- example-link: src/readme-examples/my-simple-with-events.element.ts -->
|
|
158
183
|
|
|
159
184
|
```TypeScript
|
|
160
|
-
import {
|
|
185
|
+
import {defineElementEvent, defineFunctionalElement, html, listen} from 'element-vir';
|
|
161
186
|
|
|
162
187
|
export const MySimpleWithEventsElement = defineFunctionalElement({
|
|
163
188
|
tagName: 'my-simple-element-with-events',
|
|
164
189
|
events: {
|
|
165
|
-
logoutClick:
|
|
166
|
-
randomNumber:
|
|
190
|
+
logoutClick: defineElementEvent<void>(),
|
|
191
|
+
randomNumber: defineElementEvent<number>(),
|
|
167
192
|
},
|
|
168
|
-
renderCallback: ({
|
|
169
|
-
|
|
170
|
-
<button
|
|
171
|
-
@click=${() => dispatchElementEvent(new ElementEvent(events.logoutClick, undefined))}
|
|
172
|
-
>
|
|
193
|
+
renderCallback: ({dispatch, events}) => html`
|
|
194
|
+
<button ${listen('click', () => dispatch(new events.logoutClick(undefined)))}>
|
|
173
195
|
log out
|
|
174
196
|
</button>
|
|
175
|
-
<button
|
|
176
|
-
@click=${() =>
|
|
177
|
-
dispatchElementEvent(new ElementEvent(events.randomNumber, Math.random()))}
|
|
178
|
-
>
|
|
197
|
+
<button ${listen('click', () => dispatch(new events.randomNumber(Math.random())))}>
|
|
179
198
|
generate random number
|
|
180
199
|
</button>
|
|
181
200
|
`,
|
|
182
201
|
});
|
|
183
202
|
```
|
|
184
203
|
|
|
185
|
-
## Listening to
|
|
204
|
+
## Listening to typed events (outputs)
|
|
186
205
|
|
|
187
|
-
Use the `listen` directive to listen to
|
|
206
|
+
Use the `listen` directive to listen to typed events emitted by your custom functional elements:
|
|
188
207
|
|
|
189
208
|
<!-- example-link: src/readme-examples/my-app-with-events.element.ts -->
|
|
190
209
|
|
|
@@ -213,21 +232,25 @@ export const MyAppWithEventsElement = defineFunctionalElement({
|
|
|
213
232
|
});
|
|
214
233
|
```
|
|
215
234
|
|
|
216
|
-
|
|
235
|
+
`listen` can also be used to listen to native DOM events (like `click`) and the proper event type will be provided for the listener callback.
|
|
236
|
+
|
|
237
|
+
## Typed events without an element
|
|
217
238
|
|
|
218
|
-
Create a custom event type with `
|
|
239
|
+
Create a custom event type with `defineTypedEvent`. Make sure to include the type generic (like this: `defineTypedEvent<number>`) and call it twice, the second time with the event type string, (like this: `defineTypedEvent<number>()('my-event-type-name')`) to ensure type safety when using your event. Note that event type names should probably be unique, or they may clash with each other.
|
|
219
240
|
|
|
220
|
-
Creating a
|
|
241
|
+
### Creating a typed event
|
|
221
242
|
|
|
222
243
|
<!-- example-link: src/readme-examples/custom-event-no-element.ts -->
|
|
223
244
|
|
|
224
245
|
```TypeScript
|
|
225
|
-
import {
|
|
246
|
+
import {defineTypedEvent} from 'element-vir';
|
|
226
247
|
|
|
227
|
-
export const MyCustomEvent =
|
|
248
|
+
export const MyCustomEvent = defineTypedEvent<number>()('myCustomEventName');
|
|
228
249
|
```
|
|
229
250
|
|
|
230
|
-
Using a
|
|
251
|
+
### Using a typed event
|
|
252
|
+
|
|
253
|
+
Both dispatching a custom event and listening to a custom event:
|
|
231
254
|
|
|
232
255
|
<!-- example-link: src/readme-examples/custom-event-usage.element.ts -->
|
|
233
256
|
|
|
@@ -237,16 +260,16 @@ import {MyCustomEvent} from './custom-event-no-element';
|
|
|
237
260
|
|
|
238
261
|
export const MyElementWithCustomEvents = defineFunctionalElement({
|
|
239
262
|
tagName: 'my-app-with-custom-events',
|
|
240
|
-
renderCallback: ({
|
|
263
|
+
renderCallback: ({genericDispatch}) => html`
|
|
241
264
|
<div
|
|
242
265
|
${listen(MyCustomEvent, (event) => {
|
|
243
266
|
console.log(`Got a number! ${event.detail}`);
|
|
244
267
|
})}
|
|
245
268
|
>
|
|
246
269
|
<div
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
}}
|
|
270
|
+
${listen('click', () => {
|
|
271
|
+
genericDispatch(new MyCustomEvent(Math.random()));
|
|
272
|
+
})}
|
|
250
273
|
></div>
|
|
251
274
|
</div>
|
|
252
275
|
`,
|
|
@@ -255,13 +278,13 @@ export const MyElementWithCustomEvents = defineFunctionalElement({
|
|
|
255
278
|
|
|
256
279
|
## Directives
|
|
257
280
|
|
|
258
|
-
|
|
281
|
+
The following custom [`lit` directives](https://lit.dev/docs/templates/custom-directives/) are contained within this package.
|
|
259
282
|
|
|
260
283
|
### onDomCreated
|
|
261
284
|
|
|
262
285
|
This directive should be used instead of trying to use `querySelector` directly on the custom element.
|
|
263
286
|
|
|
264
|
-
This triggers only once when the element it's
|
|
287
|
+
This triggers only once when the element it's attached has actually been created in the DOM. If it's attached element changes, the callback will be triggered again.
|
|
265
288
|
|
|
266
289
|
<!-- example-link: src/readme-examples/my-simple-with-on-dom-created.element.ts -->
|
|
267
290
|
|
|
@@ -285,7 +308,7 @@ export const MySimpleWithOnDomCreatedElement = defineFunctionalElement({
|
|
|
285
308
|
|
|
286
309
|
### onResize
|
|
287
310
|
|
|
288
|
-
This directive fulfills a common use case of triggering callbacks when something resizes. Instead of just tracking the _globally_ resizing window though, this allows you to track resizes of an individual element. The callback here is
|
|
311
|
+
This directive fulfills a common use case of triggering callbacks when something resizes. Instead of just tracking the _globally_ resizing window though, this allows you to track resizes of an individual element. The callback here is passed an object with a portion of the [`ResizeObserverEntry`](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserverEntry) properties (since not all properties are supported well in browsers).
|
|
289
312
|
|
|
290
313
|
<!-- example-link: src/readme-examples/my-simple-with-on-resize.element.ts -->
|
|
291
314
|
|
|
@@ -316,33 +339,14 @@ Assign a value to one of a custom element's properties. This is explained in the
|
|
|
316
339
|
|
|
317
340
|
Listen to a specific event emitted from a custom element. This is explained in the **Listening to custom events (outputs)** section earlier in this README.
|
|
318
341
|
|
|
319
|
-
### namedListen
|
|
320
|
-
|
|
321
|
-
Listen to an event by name directly. This can be used to listen to native events as well as custom events. However, `listen` (explained above) is better for custom events as it'll give you better type information. When using `namedListen` for native DOM events (like `'click'`) the callback will be typed correctly. When listening to custom events, however, it can't guess the callback type from anywhere so the event parameter type will be unknown.
|
|
322
|
-
|
|
323
|
-
<!-- example-link: src/readme-examples/my-simple-with-named-listen.element.ts -->
|
|
324
|
-
|
|
325
|
-
```TypeScript
|
|
326
|
-
import {defineFunctionalElement, html, namedListen} from 'element-vir';
|
|
327
|
-
|
|
328
|
-
export const MySimpleWithNamedListenElement = defineFunctionalElement({
|
|
329
|
-
tagName: 'my-simple-element-with-named-listen',
|
|
330
|
-
renderCallback: () => html`
|
|
331
|
-
<!-- normal DOM events can be listened to -->
|
|
332
|
-
<button ${namedListen('click', (event) => console.log(event.buttons))}>click me</button>
|
|
333
|
-
`,
|
|
334
|
-
});
|
|
335
|
-
```
|
|
336
|
-
|
|
337
342
|
### assignWithCleanup
|
|
338
343
|
|
|
339
|
-
This directive is the same as the `assign` directive but it accepts an additional `cleanupCallback` input. Use this directive to assign values which need some kind of cleanup
|
|
344
|
+
This directive is the same as the `assign` directive but it accepts an additional `cleanupCallback` input. Use this directive to assign values which need some kind of cleanup when they're overwritten. For example, a 3D rendering engine which uses the canvas that should free up memory when it's swapped out.
|
|
340
345
|
|
|
341
346
|
<!-- example-link: src/readme-examples/my-app-with-cleanup.element.ts -->
|
|
342
347
|
|
|
343
348
|
```TypeScript
|
|
344
|
-
import {assign, defineFunctionalElement, html} from 'element-vir';
|
|
345
|
-
import {assignWithCleanup} from '../functional-element/directives/assign-with-clean-up.directive';
|
|
349
|
+
import {assign, assignWithCleanup, defineFunctionalElement, html} from 'element-vir';
|
|
346
350
|
import {MySimpleWithPropsElement} from './my-simple-with-props.element';
|
|
347
351
|
|
|
348
352
|
export const MyAppWithPropsElement = defineFunctionalElement({
|
|
@@ -373,7 +377,13 @@ To require all child elements to be functional elements defined by this package,
|
|
|
373
377
|
<!-- example-link: src/readme-examples/require-functional-element.ts -->
|
|
374
378
|
|
|
375
379
|
```TypeScript
|
|
376
|
-
import {requireAllCustomElementsToBeFunctionalElement} from '
|
|
380
|
+
import {requireAllCustomElementsToBeFunctionalElement} from 'element-vir';
|
|
377
381
|
|
|
378
382
|
requireAllCustomElementsToBeFunctionalElement();
|
|
379
383
|
```
|
|
384
|
+
|
|
385
|
+
# Dev
|
|
386
|
+
|
|
387
|
+
## markdown out of date
|
|
388
|
+
|
|
389
|
+
If you see this: `Code in Markdown file(s) is out of date. Run without --check to update. code-in-markdown failed.`, run `npm run update-docs` to fix it.
|
|
@@ -27,7 +27,6 @@ export function defineFunctionalElement(functionalElementInit) {
|
|
|
27
27
|
},
|
|
28
28
|
_a.tagName = functionalElementInit.tagName,
|
|
29
29
|
_a.styles = functionalElementInit.styles || css ``,
|
|
30
|
-
_a.propNames = Object.keys(functionalElementInit.props || {}),
|
|
31
30
|
_a.events = eventsMap,
|
|
32
31
|
_a.renderCallback = functionalElementInit.renderCallback,
|
|
33
32
|
_a.props = createPropertyDescriptorMap(functionalElementInit.props),
|
|
@@ -20,7 +20,6 @@ export declare type FunctionalElementInit<PropertyInitGeneric extends PropertyIn
|
|
|
20
20
|
export declare abstract class FunctionalElementBaseClass<PropertyInitGeneric extends PropertyInitMapBase> extends LitElement {
|
|
21
21
|
static readonly tagName: string;
|
|
22
22
|
static readonly styles: CSSResult;
|
|
23
|
-
static readonly propNames: string[];
|
|
24
23
|
abstract render(): TemplateResult | Promise<TemplateResult>;
|
|
25
24
|
abstract readonly instanceProps: PropertyInitGeneric;
|
|
26
25
|
}
|
|
@@ -37,5 +36,4 @@ export declare type ExtraStaticFunctionalElementProperties<PropertyInitGeneric e
|
|
|
37
36
|
*/
|
|
38
37
|
tagName: string;
|
|
39
38
|
styles: CSSResult;
|
|
40
|
-
propNames: string[];
|
|
41
39
|
}>;
|
package/index.html
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<title>Element Vir Test</title>
|
|
5
|
+
<script type="module" src="/src/test/elements/app.element.ts"></script>
|
|
6
|
+
<link rel="stylesheet" type="text/css" href="/index.css" />
|
|
7
|
+
<script>
|
|
8
|
+
console.log('yo');
|
|
9
|
+
</script>
|
|
10
|
+
</head>
|
|
11
|
+
<body>
|
|
12
|
+
<element-vir-test-app></element-vir-test-app>
|
|
13
|
+
</body>
|
|
14
|
+
</html>
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import {dirname, join} from 'path';
|
|
2
|
+
import {pathsToModuleNameMapper} from 'ts-jest';
|
|
3
|
+
import {InitialOptionsTsJest} from 'ts-jest/dist/types';
|
|
4
|
+
import {findTsConfigFile, getTsconfigPathAliases} from './read-tsconfig';
|
|
5
|
+
|
|
6
|
+
const repoRootDir = dirname(__dirname);
|
|
7
|
+
|
|
8
|
+
const config: InitialOptionsTsJest = {
|
|
9
|
+
preset: 'ts-jest',
|
|
10
|
+
testEnvironment: 'jsdom',
|
|
11
|
+
verbose: false,
|
|
12
|
+
// type tests are caught by the typescript compiler
|
|
13
|
+
testPathIgnorePatterns: ['.type.test.ts'],
|
|
14
|
+
rootDir: repoRootDir,
|
|
15
|
+
silent: false,
|
|
16
|
+
moduleNameMapper: pathsToModuleNameMapper(getTsconfigPathAliases(), {
|
|
17
|
+
prefix: '<rootDir>/',
|
|
18
|
+
}) as Record<string, string | string[]>,
|
|
19
|
+
roots: [join(repoRootDir, 'src')],
|
|
20
|
+
setupFilesAfterEnv: [join(__dirname, 'jest.setup.ts')],
|
|
21
|
+
globals: {
|
|
22
|
+
'ts-jest': {
|
|
23
|
+
tsconfig: findTsConfigFile(),
|
|
24
|
+
diagnostics: {
|
|
25
|
+
warnOnly: true,
|
|
26
|
+
ignoreCodes: ['TS151001'],
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export default config;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import {CustomConsole, LogMessage, LogType} from '@jest/console';
|
|
2
|
+
|
|
3
|
+
function simpleFormatter(type: LogType, message: LogMessage): string {
|
|
4
|
+
const consoleIndent = ' ';
|
|
5
|
+
|
|
6
|
+
return message
|
|
7
|
+
.split(/\n/)
|
|
8
|
+
.map((line) => consoleIndent + line)
|
|
9
|
+
.join('\n');
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
global.console = new CustomConsole(process.stdout, process.stderr, simpleFormatter);
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import {dirname} from 'path';
|
|
2
|
+
import {findConfigFile, parseJsonConfigFileContent, readConfigFile, sys as tsSys} from 'typescript';
|
|
3
|
+
|
|
4
|
+
export function findTsConfigFile(): string {
|
|
5
|
+
const dirToCheck = __dirname;
|
|
6
|
+
const configFileName = findConfigFile(dirToCheck, tsSys.fileExists, 'tsconfig.json');
|
|
7
|
+
if (!configFileName) {
|
|
8
|
+
throw new Error(
|
|
9
|
+
`Could not find tsconfig.json file from starting search at "${dirToCheck}"`,
|
|
10
|
+
);
|
|
11
|
+
}
|
|
12
|
+
return configFileName;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function getTsconfigPathAliases() {
|
|
16
|
+
const configFileName = findTsConfigFile();
|
|
17
|
+
|
|
18
|
+
if (!configFileName) {
|
|
19
|
+
throw new Error(`Failed to find tsconfig.`);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const configFile = readConfigFile(configFileName, tsSys.readFile);
|
|
23
|
+
const tsConfig = parseJsonConfigFileContent(configFile.config, tsSys, dirname(configFileName));
|
|
24
|
+
const tsConfigPaths = tsConfig.options.paths || {};
|
|
25
|
+
|
|
26
|
+
return tsConfigPaths;
|
|
27
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "element-vir",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"custom",
|
|
6
6
|
"web",
|
|
@@ -25,23 +25,28 @@
|
|
|
25
25
|
"main": "dist/index.js",
|
|
26
26
|
"types": "dist/index.d.ts",
|
|
27
27
|
"scripts": {
|
|
28
|
-
"compile": "
|
|
28
|
+
"compile": "tsc",
|
|
29
29
|
"format": "virmator format write",
|
|
30
|
+
"jest": "jest --config ./jest/jest.config.ts",
|
|
30
31
|
"prepublishOnly": "npm run test:full",
|
|
31
32
|
"spellcheck": "virmator spellcheck",
|
|
32
|
-
"start": "npm install &&
|
|
33
|
-
"test": "npm run
|
|
33
|
+
"start": "npm install && vite --force --config ./vite/vite.config.ts",
|
|
34
|
+
"test": "npm run type-check && npm run jest",
|
|
34
35
|
"test:full": "npm test && npm run spellcheck && virmator format check && npm run update-docs -- --check",
|
|
35
|
-
"
|
|
36
|
+
"type-check": "tsc --noEmit",
|
|
37
|
+
"update-docs": "virmator code-in-markdown README.md --index src/index.ts"
|
|
36
38
|
},
|
|
37
39
|
"dependencies": {
|
|
38
|
-
"augment-vir": "^1.4.
|
|
39
|
-
"lit": "^2.
|
|
40
|
+
"augment-vir": "^1.4.3",
|
|
41
|
+
"lit": "^2.1.1"
|
|
40
42
|
},
|
|
41
43
|
"devDependencies": {
|
|
42
|
-
"@
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
44
|
+
"@types/jest": "^27.4.0",
|
|
45
|
+
"jest": "^27.4.7",
|
|
46
|
+
"ts-jest": "^27.1.3",
|
|
47
|
+
"ts-node": "^10.4.0",
|
|
48
|
+
"typescript": "^4.5.4",
|
|
49
|
+
"virmator": "^1.3.7",
|
|
50
|
+
"vite": "^2.7.12"
|
|
46
51
|
}
|
|
47
52
|
}
|
package/public/index.css
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import {existsSync, lstatSync, readlinkSync} from 'fs';
|
|
3
|
+
import {relative} from 'path';
|
|
4
|
+
import {PluginOption} from 'vite';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Include actual paths and symlinked target paths if they exist.
|
|
8
|
+
*
|
|
9
|
+
* This is needed because when removing files from the watcher, sym links have be removed with the
|
|
10
|
+
* path to the symlink itself AND the path to the symlink target or the path will still be watched.
|
|
11
|
+
*/
|
|
12
|
+
function mapToActualPaths(paths: Readonly<string[]>): Readonly<string[]> {
|
|
13
|
+
return paths.reduce((accum, path) => {
|
|
14
|
+
if (existsSync(path)) {
|
|
15
|
+
if (lstatSync(path).isSymbolicLink()) {
|
|
16
|
+
console.info('reading symlink from', path);
|
|
17
|
+
// sym links AND the original path both need to be included
|
|
18
|
+
return accum.concat(readlinkSync(path), path);
|
|
19
|
+
} else {
|
|
20
|
+
return accum.concat(path);
|
|
21
|
+
}
|
|
22
|
+
} else {
|
|
23
|
+
return accum;
|
|
24
|
+
}
|
|
25
|
+
}, [] as string[]);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* There are similar plugins out there that try to do this but they aren't aggressive enough. This
|
|
30
|
+
* plugin literally always reloads on save, no questions asked.
|
|
31
|
+
*/
|
|
32
|
+
export function alwaysReloadPlugin(
|
|
33
|
+
config: Partial<{
|
|
34
|
+
exclusions: string[];
|
|
35
|
+
/** Inclusions apply after exclusions so they will override exclusions. */
|
|
36
|
+
inclusions: string[];
|
|
37
|
+
root: string;
|
|
38
|
+
}> = {},
|
|
39
|
+
): PluginOption {
|
|
40
|
+
return {
|
|
41
|
+
name: 'alwaysReloadPlugin',
|
|
42
|
+
apply: 'serve',
|
|
43
|
+
config: () => ({server: {watch: {disableGlobbing: false}}}),
|
|
44
|
+
configureServer({watcher, ws, config: {logger}}) {
|
|
45
|
+
const {root = process.cwd(), inclusions = [], exclusions = []} = config;
|
|
46
|
+
let callingAlready = false;
|
|
47
|
+
let loggedAlready = false;
|
|
48
|
+
|
|
49
|
+
const reloadCallback = (path: string) => {
|
|
50
|
+
if (!loggedAlready) {
|
|
51
|
+
loggedAlready = true;
|
|
52
|
+
// log watched stuff so that we can make sure it's not watching too much
|
|
53
|
+
// console.info({watched: watcher.getWatched()});
|
|
54
|
+
}
|
|
55
|
+
// prevent duplicate calls cause the watcher is very eager to call callbacks multiple times in a row
|
|
56
|
+
if (!callingAlready) {
|
|
57
|
+
callingAlready = true;
|
|
58
|
+
ws.send({
|
|
59
|
+
type: 'full-reload',
|
|
60
|
+
path: '*',
|
|
61
|
+
});
|
|
62
|
+
logger.info(
|
|
63
|
+
`${chalk.green('page reload')} ${chalk.dim(relative(root, path))}`,
|
|
64
|
+
{clear: true, timestamp: true},
|
|
65
|
+
);
|
|
66
|
+
/**
|
|
67
|
+
* Debounce reloads calls so that they don't get spammed. If you're saving
|
|
68
|
+
* faster than this, then what the heck are you doing anyway?
|
|
69
|
+
*/
|
|
70
|
+
setTimeout(() => {
|
|
71
|
+
callingAlready = false;
|
|
72
|
+
}, 100);
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
if (exclusions.length) {
|
|
77
|
+
watcher.unwatch(mapToActualPaths(exclusions));
|
|
78
|
+
// ignore macOS file system metadata stuff
|
|
79
|
+
watcher.unwatch('./**/.DS_Store');
|
|
80
|
+
}
|
|
81
|
+
if (inclusions.length) {
|
|
82
|
+
watcher.add(inclusions);
|
|
83
|
+
}
|
|
84
|
+
if (!watcher.listeners('change').includes(reloadCallback)) {
|
|
85
|
+
watcher.on('change', reloadCallback);
|
|
86
|
+
watcher.on('add', reloadCallback);
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
}
|