@oddsquad/vite-plugin-lit 0.2.3 → 0.3.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/CHANGELOG.md +64 -0
- package/LICENSE +28 -0
- package/README.md +153 -0
- package/bin.mjs +154 -0
- package/client.d.ts +61 -0
- package/dist/client/assets/browser-agent-rpc-BXhoSh1z-BvWe4sTJ.js +69 -0
- package/dist/client/assets/browser-agent-rpc-BXhoSh1z-BvWe4sTJ.js.map +1 -0
- package/dist/client/assets/index-CFrVgNfS.js +9740 -0
- package/dist/client/assets/index-CFrVgNfS.js.map +1 -0
- package/{panel → dist/client}/index.html +1 -0
- package/index.d.ts.map +1 -1
- package/index.js +2 -2
- package/index.js.map +1 -1
- package/lib/color-scheme.d.ts.map +1 -1
- package/lib/color-scheme.js +17 -21
- package/lib/color-scheme.js.map +1 -1
- package/lib/devframe/definition.d.ts +45 -0
- package/lib/devframe/definition.d.ts.map +1 -0
- package/lib/devframe/definition.js +547 -0
- package/lib/devframe/definition.js.map +1 -0
- package/lib/devframe/icon.d.ts +8 -0
- package/lib/devframe/icon.d.ts.map +1 -0
- package/lib/devframe/icon.js +21 -0
- package/lib/devframe/icon.js.map +1 -0
- package/lib/devframe/paths.d.ts +12 -0
- package/lib/devframe/paths.d.ts.map +1 -0
- package/lib/devframe/paths.js +38 -0
- package/lib/devframe/paths.js.map +1 -0
- package/lib/devframe/protocol.d.ts +218 -0
- package/lib/devframe/protocol.d.ts.map +1 -0
- package/lib/devframe/protocol.js +76 -0
- package/lib/devframe/protocol.js.map +1 -0
- package/lib/devframe/source.d.ts +49 -0
- package/lib/devframe/source.d.ts.map +1 -0
- package/lib/devframe/source.js +23 -0
- package/lib/devframe/source.js.map +1 -0
- package/lib/devframe/vite.d.ts +116 -0
- package/lib/devframe/vite.d.ts.map +1 -0
- package/lib/devframe/vite.js +291 -0
- package/lib/devframe/vite.js.map +1 -0
- package/lib/http.d.ts +29 -0
- package/lib/http.d.ts.map +1 -0
- package/lib/http.js +35 -0
- package/lib/http.js.map +1 -0
- package/lib/icons.d.ts +2 -0
- package/lib/icons.d.ts.map +1 -1
- package/lib/icons.js +10 -4
- package/lib/icons.js.map +1 -1
- package/lib/plugin.d.ts +15 -4
- package/lib/plugin.d.ts.map +1 -1
- package/lib/plugin.js +640 -574
- package/lib/plugin.js.map +1 -1
- package/lib/runtime/css.d.ts +2 -2
- package/lib/runtime/css.d.ts.map +1 -1
- package/lib/runtime/css.js +34 -34
- package/lib/runtime/css.js.map +1 -1
- package/lib/runtime/edge-panel.d.ts.map +1 -1
- package/lib/runtime/edge-panel.js +82 -91
- package/lib/runtime/edge-panel.js.map +1 -1
- package/lib/runtime/fonts.d.ts.map +1 -1
- package/lib/runtime/fonts.js +1 -1
- package/lib/runtime/fonts.js.map +1 -1
- package/lib/runtime/indicator.d.ts.map +1 -1
- package/lib/runtime/indicator.js +71 -74
- package/lib/runtime/indicator.js.map +1 -1
- package/lib/runtime/inspector/collect.d.ts.map +1 -1
- package/lib/runtime/inspector/collect.js +83 -90
- package/lib/runtime/inspector/collect.js.map +1 -1
- package/lib/runtime/inspector/highlight.d.ts +16 -0
- package/lib/runtime/inspector/highlight.d.ts.map +1 -0
- package/lib/runtime/inspector/highlight.js +66 -0
- package/lib/runtime/inspector/highlight.js.map +1 -0
- package/lib/runtime/inspector/install.d.ts.map +1 -1
- package/lib/runtime/inspector/install.js +180 -205
- package/lib/runtime/inspector/install.js.map +1 -1
- package/lib/runtime/inspector/serialize.d.ts.map +1 -1
- package/lib/runtime/inspector/serialize.js +86 -107
- package/lib/runtime/inspector/serialize.js.map +1 -1
- package/lib/runtime/intern.d.ts.map +1 -1
- package/lib/runtime/intern.js +31 -29
- package/lib/runtime/intern.js.map +1 -1
- package/lib/runtime/overrides.d.ts.map +1 -1
- package/lib/runtime/overrides.js +14 -13
- package/lib/runtime/overrides.js.map +1 -1
- package/lib/runtime/patch.d.ts +8 -0
- package/lib/runtime/patch.d.ts.map +1 -1
- package/lib/runtime/patch.js +321 -282
- package/lib/runtime/patch.js.map +1 -1
- package/lib/runtime/source-meta.d.ts.map +1 -1
- package/lib/runtime/source-meta.js +1 -1
- package/lib/runtime/source-meta.js.map +1 -1
- package/lib/runtime/source-overlay/editors.d.ts.map +1 -1
- package/lib/runtime/source-overlay/editors.js +27 -27
- package/lib/runtime/source-overlay/editors.js.map +1 -1
- package/lib/runtime/source-overlay/mask-path.d.ts.map +1 -1
- package/lib/runtime/source-overlay/mask-path.js +15 -15
- package/lib/runtime/source-overlay/mask-path.js.map +1 -1
- package/lib/runtime/source-overlay/overlay-element.d.ts.map +1 -1
- package/lib/runtime/source-overlay/overlay-element.js +347 -356
- package/lib/runtime/source-overlay/overlay-element.js.map +1 -1
- package/lib/runtime/source-overlay/source-host.d.ts.map +1 -1
- package/lib/runtime/source-overlay/source-host.js +64 -71
- package/lib/runtime/source-overlay/source-host.js.map +1 -1
- package/lib/runtime/source-overlay/template.d.ts.map +1 -1
- package/lib/runtime/source-overlay/template.js +3 -3
- package/lib/runtime/source-overlay/template.js.map +1 -1
- package/lib/runtime/source-overlay.d.ts.map +1 -1
- package/lib/runtime/source-overlay.js +5 -2
- package/lib/runtime/source-overlay.js.map +1 -1
- package/lib/runtime/timeline/clock.d.ts.map +1 -1
- package/lib/runtime/timeline/clock.js +2 -2
- package/lib/runtime/timeline/clock.js.map +1 -1
- package/lib/runtime/timeline/flash.d.ts +39 -0
- package/lib/runtime/timeline/flash.d.ts.map +1 -0
- package/lib/runtime/timeline/flash.js +169 -0
- package/lib/runtime/timeline/flash.js.map +1 -0
- package/lib/runtime/timeline/identity.d.ts.map +1 -1
- package/lib/runtime/timeline/identity.js +27 -27
- package/lib/runtime/timeline/identity.js.map +1 -1
- package/lib/runtime/timeline/input.d.ts.map +1 -1
- package/lib/runtime/timeline/input.js +54 -67
- package/lib/runtime/timeline/input.js.map +1 -1
- package/lib/runtime/timeline/install.d.ts.map +1 -1
- package/lib/runtime/timeline/install.js +38 -29
- package/lib/runtime/timeline/install.js.map +1 -1
- package/lib/runtime/timeline/lifecycle.d.ts +8 -0
- package/lib/runtime/timeline/lifecycle.d.ts.map +1 -1
- package/lib/runtime/timeline/lifecycle.js +149 -133
- package/lib/runtime/timeline/lifecycle.js.map +1 -1
- package/lib/runtime/timeline/public-api.d.ts.map +1 -1
- package/lib/runtime/timeline/public-api.js +9 -9
- package/lib/runtime/timeline/public-api.js.map +1 -1
- package/lib/runtime/timeline/render.d.ts.map +1 -1
- package/lib/runtime/timeline/render.js +70 -75
- package/lib/runtime/timeline/render.js.map +1 -1
- package/lib/runtime/timeline/transport.d.ts.map +1 -1
- package/lib/runtime/timeline/transport.js +39 -43
- package/lib/runtime/timeline/transport.js.map +1 -1
- package/lib/segmented-tabs.d.ts +3 -1
- package/lib/segmented-tabs.d.ts.map +1 -1
- package/lib/segmented-tabs.js +136 -107
- package/lib/segmented-tabs.js.map +1 -1
- package/lib/snapshot.d.ts +28 -0
- package/lib/snapshot.d.ts.map +1 -0
- package/lib/snapshot.js +57 -0
- package/lib/snapshot.js.map +1 -0
- package/lib/source-meta.d.ts.map +1 -1
- package/lib/source-meta.js +176 -162
- package/lib/source-meta.js.map +1 -1
- package/lib/timeline/derive.d.ts +152 -0
- package/lib/timeline/derive.d.ts.map +1 -0
- package/lib/timeline/derive.js +242 -0
- package/lib/timeline/derive.js.map +1 -0
- package/lib/timeline-plugin.d.ts.map +1 -1
- package/lib/timeline-plugin.js +304 -321
- package/lib/timeline-plugin.js.map +1 -1
- package/lib/tokens.d.ts.map +1 -1
- package/lib/tokens.js +17 -18
- package/lib/tokens.js.map +1 -1
- package/lib/transform.d.ts.map +1 -1
- package/lib/transform.js +62 -57
- package/lib/transform.js.map +1 -1
- package/lib/types.d.ts.map +1 -1
- package/lib/types.js +1 -1
- package/lib/types.js.map +1 -1
- package/lib/wrap-table.d.ts.map +1 -1
- package/lib/wrap-table.js +19 -17
- package/lib/wrap-table.js.map +1 -1
- package/package.json +77 -13
- package/types/hmr-incompatibility.d.ts +59 -0
- package/types/hmr-incompatibility.d.ts.map +1 -0
- package/types/hmr-incompatibility.js +30 -0
- package/types/hmr-incompatibility.js.map +1 -0
- package/types/in-page.d.ts +49 -0
- package/types/in-page.d.ts.map +1 -0
- package/types/in-page.js +33 -0
- package/types/in-page.js.map +1 -0
- package/types/inspector.d.ts.map +1 -1
- package/types/inspector.js.map +1 -1
- package/types/snapshot.d.ts +44 -0
- package/types/snapshot.d.ts.map +1 -0
- package/types/snapshot.js +7 -0
- package/types/snapshot.js.map +1 -0
- package/types/timeline.d.ts +11 -0
- package/types/timeline.d.ts.map +1 -1
- package/types/timeline.js.map +1 -1
- package/panel/components-view.d.ts +0 -60
- package/panel/components-view.d.ts.map +0 -1
- package/panel/components-view.js +0 -587
- package/panel/components-view.js.map +0 -1
- package/panel/devtools-settings.d.ts +0 -41
- package/panel/devtools-settings.d.ts.map +0 -1
- package/panel/devtools-settings.js +0 -463
- package/panel/devtools-settings.js.map +0 -1
- package/panel/lit-devtools-panel.d.ts +0 -37
- package/panel/lit-devtools-panel.d.ts.map +0 -1
- package/panel/lit-devtools-panel.js +0 -185
- package/panel/lit-devtools-panel.js.map +0 -1
- package/panel/timeline-event-list.d.ts +0 -43
- package/panel/timeline-event-list.d.ts.map +0 -1
- package/panel/timeline-event-list.js +0 -416
- package/panel/timeline-event-list.js.map +0 -1
- package/panel/timeline-layers.d.ts +0 -25
- package/panel/timeline-layers.d.ts.map +0 -1
- package/panel/timeline-layers.js +0 -96
- package/panel/timeline-layers.js.map +0 -1
- package/panel/timeline-view.d.ts +0 -38
- package/panel/timeline-view.d.ts.map +0 -1
- package/panel/timeline-view.js +0 -234
- package/panel/timeline-view.js.map +0 -1
package/lib/plugin.js
CHANGED
|
@@ -3,35 +3,32 @@
|
|
|
3
3
|
* Copyright 2026 Google LLC
|
|
4
4
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
5
5
|
*/
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
6
|
+
import {existsSync} from 'node:fs';
|
|
7
|
+
import {resolve as resolvePath, sep} from 'node:path';
|
|
8
|
+
import {fileURLToPath} from 'node:url';
|
|
9
|
+
import {loadEnv} from 'vite';
|
|
10
10
|
import MagicString from 'magic-string';
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
'inline',
|
|
19
|
-
'inline-raw',
|
|
20
|
-
];
|
|
11
|
+
import {isTrustedRequest} from './http.js';
|
|
12
|
+
import {injectSourceMeta} from './source-meta.js';
|
|
13
|
+
import {INSTALL_ID, VIRTUAL_PREFIX, transformLitModule} from './transform.js';
|
|
14
|
+
import {WRAP_TABLE} from './wrap-table.js';
|
|
15
|
+
import {createLitDevframePlugin} from './devframe/vite.js';
|
|
16
|
+
import {PACKAGE_VERSION} from './devframe/paths.js';
|
|
17
|
+
const CSS_SHEET_BUILD_MODES = ['auto', 'url', 'inline', 'inline-raw'];
|
|
21
18
|
/** Env var prefix consumed at config time. */
|
|
22
19
|
const ENV_PREFIX = 'LIT_PLUGIN';
|
|
23
20
|
/** Parse a boolean-ish env string; `undefined` when unset/unrecognized. */
|
|
24
|
-
const envBool = (v) =>
|
|
21
|
+
const envBool = (v) =>
|
|
22
|
+
v === 'true' || v === '1'
|
|
25
23
|
? true
|
|
26
24
|
: v === 'false' || v === '0'
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
? false
|
|
26
|
+
: undefined;
|
|
29
27
|
/** Parse a numeric env string; `undefined` when unset/non-numeric. */
|
|
30
28
|
const envNum = (v) => {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
return Number.isNaN(n) ? undefined : n;
|
|
29
|
+
if (v === undefined || v === '') return undefined;
|
|
30
|
+
const n = Number(v);
|
|
31
|
+
return Number.isNaN(n) ? undefined : n;
|
|
35
32
|
};
|
|
36
33
|
/**
|
|
37
34
|
* Parse a string against a closed set of values; `undefined` (with a warning)
|
|
@@ -39,60 +36,73 @@ const envNum = (v) => {
|
|
|
39
36
|
* instead of silently disabling a feature.
|
|
40
37
|
*/
|
|
41
38
|
const envEnum = (v, allowed, source) => {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
39
|
+
if (v === undefined || v === '') return undefined;
|
|
40
|
+
if (allowed.includes(v)) return v;
|
|
41
|
+
console.warn(
|
|
42
|
+
`[lit-plugin] ignoring ${source}=${JSON.stringify(v)}; expected one of ` +
|
|
43
|
+
allowed.map((a) => JSON.stringify(a)).join(', ')
|
|
44
|
+
);
|
|
45
|
+
return undefined;
|
|
49
46
|
};
|
|
50
47
|
/**
|
|
51
48
|
* Merge explicit options over env vars over defaults (in that precedence) into
|
|
52
49
|
* the flat shape the plugin hooks consume.
|
|
53
50
|
*/
|
|
54
51
|
export const resolveOptions = (options, env) => {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
52
|
+
const hmr = options.hmr;
|
|
53
|
+
const hmrObj = typeof hmr === 'object' ? hmr : undefined;
|
|
54
|
+
const hmrEnabled =
|
|
55
|
+
(typeof hmr === 'boolean' ? hmr : hmrObj?.enabled) ??
|
|
56
|
+
envBool(env[`${ENV_PREFIX}_HMR`]) ??
|
|
57
|
+
true;
|
|
58
|
+
const reconnect =
|
|
59
|
+
hmrObj?.reconnect ?? envBool(env[`${ENV_PREFIX}_HMR_RECONNECT`]) ?? false;
|
|
60
|
+
const onIncompatible =
|
|
61
|
+
hmrObj?.onIncompatible ??
|
|
62
|
+
env[`${ENV_PREFIX}_HMR_ON_INCOMPATIBLE`] ??
|
|
63
|
+
'reload';
|
|
64
|
+
const ind = hmrObj?.indicator;
|
|
65
|
+
const indObj = typeof ind === 'object' ? ind : undefined;
|
|
66
|
+
const indEnabled =
|
|
67
|
+
(typeof ind === 'boolean' ? ind : indObj?.enabled) ??
|
|
68
|
+
envBool(env[`${ENV_PREFIX}_HMR_INDICATOR`]) ??
|
|
69
|
+
true;
|
|
70
|
+
const indCount =
|
|
71
|
+
indObj?.count ?? envBool(env[`${ENV_PREFIX}_HMR_INDICATOR_COUNT`]) ?? false;
|
|
72
|
+
// The indicator is meaningless without HMR, so it follows the master toggle.
|
|
73
|
+
const indicator = hmrEnabled && indEnabled ? {count: indCount} : false;
|
|
74
|
+
const so = options.sourceOverlay;
|
|
75
|
+
const soExplicit =
|
|
76
|
+
typeof so === 'boolean' ? so : so === undefined ? undefined : true;
|
|
77
|
+
const soEnabled =
|
|
78
|
+
soExplicit ?? envBool(env[`${ENV_PREFIX}_SOURCE_OVERLAY`]) ?? false;
|
|
79
|
+
let sourceOverlay = false;
|
|
80
|
+
if (soEnabled) {
|
|
81
|
+
const base = typeof so === 'object' ? {...so} : {};
|
|
82
|
+
base.key ??= env[`${ENV_PREFIX}_SOURCE_OVERLAY_KEY`] || undefined;
|
|
83
|
+
base.editor ??= env[`${ENV_PREFIX}_SOURCE_OVERLAY_EDITOR`] || undefined;
|
|
84
|
+
base.throttleMs ??= envNum(env[`${ENV_PREFIX}_SOURCE_OVERLAY_THROTTLE_MS`]);
|
|
85
|
+
sourceOverlay = base;
|
|
86
|
+
}
|
|
87
|
+
const timeline =
|
|
88
|
+
options.timeline ?? envBool(env[`${ENV_PREFIX}_TIMELINE`]) ?? false;
|
|
89
|
+
const cssSheetBuild =
|
|
90
|
+
envEnum(options.cssSheetBuild, CSS_SHEET_BUILD_MODES, 'cssSheetBuild') ??
|
|
91
|
+
envEnum(
|
|
92
|
+
env[`${ENV_PREFIX}_CSS_SHEET_BUILD`],
|
|
93
|
+
CSS_SHEET_BUILD_MODES,
|
|
94
|
+
`${ENV_PREFIX}_CSS_SHEET_BUILD`
|
|
95
|
+
) ??
|
|
96
|
+
'auto';
|
|
97
|
+
return {
|
|
98
|
+
hmrEnabled,
|
|
99
|
+
reconnect,
|
|
100
|
+
onIncompatible,
|
|
101
|
+
indicator,
|
|
102
|
+
sourceOverlay,
|
|
103
|
+
timeline,
|
|
104
|
+
cssSheetBuild,
|
|
105
|
+
};
|
|
96
106
|
};
|
|
97
107
|
/**
|
|
98
108
|
* Flattens resolved options into the JSON-serializable shape the panel's
|
|
@@ -101,24 +111,25 @@ export const resolveOptions = (options, env) => {
|
|
|
101
111
|
* are applied here so the panel shows the effective values the runtime uses.
|
|
102
112
|
*/
|
|
103
113
|
const toFeatureSettings = (r) => {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
114
|
+
const so = r.sourceOverlay;
|
|
115
|
+
const editor = so === false ? undefined : so.editor;
|
|
116
|
+
return {
|
|
117
|
+
hmr: {
|
|
118
|
+
enabled: r.hmrEnabled,
|
|
119
|
+
reconnect: r.reconnect,
|
|
120
|
+
onIncompatible: r.onIncompatible,
|
|
121
|
+
indicatorEnabled: r.indicator !== false,
|
|
122
|
+
indicatorCount: r.indicator !== false && r.indicator.count,
|
|
123
|
+
},
|
|
124
|
+
sourceOverlay: {
|
|
125
|
+
enabled: so !== false,
|
|
126
|
+
key: (so === false ? undefined : so.key) ?? 's',
|
|
127
|
+
editor:
|
|
128
|
+
typeof editor === 'string' ? editor : editor ? 'custom' : 'vscode',
|
|
129
|
+
throttleMs: (so === false ? undefined : so.throttleMs) ?? 50,
|
|
130
|
+
},
|
|
131
|
+
timeline: r.timeline,
|
|
132
|
+
};
|
|
122
133
|
};
|
|
123
134
|
/**
|
|
124
135
|
* Resolves a runtime module to an absolute fs path (served via `/@fs/`), so
|
|
@@ -126,100 +137,96 @@ const toFeatureSettings = (r) => {
|
|
|
126
137
|
* the `.ts` source when running un-built (e.g. under vitest).
|
|
127
138
|
*/
|
|
128
139
|
const resolveRuntimeModule = (name) => {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
}
|
|
140
|
+
for (const ext of ['js', 'ts']) {
|
|
141
|
+
const url = new URL(`./runtime/${name}.${ext}`, import.meta.url);
|
|
142
|
+
if (existsSync(url)) {
|
|
143
|
+
return fileURLToPath(url).replace(/\\/g, '/');
|
|
134
144
|
}
|
|
135
|
-
|
|
145
|
+
}
|
|
146
|
+
throw new Error(`[lit-plugin] runtime module not found: ${name}`);
|
|
136
147
|
};
|
|
137
148
|
const JS_FILE_RE = /\.[cm]?[jt]sx?$/;
|
|
138
149
|
const OPEN_IN_EDITOR_PATH = '/__lit-open-in-editor';
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
const
|
|
143
|
-
|
|
144
|
-
if (
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
return new URL(origin).host === host;
|
|
150
|
+
export const createOpenInEditorMiddleware = (allowedRoots) => {
|
|
151
|
+
// First entry is the primary root: relative `file` params resolve against
|
|
152
|
+
// it. Every entry grants open access to files beneath it.
|
|
153
|
+
const roots = allowedRoots.map((r) => resolvePath(r));
|
|
154
|
+
return (req, res, next) => {
|
|
155
|
+
if (req.url === undefined) {
|
|
156
|
+
next();
|
|
157
|
+
return;
|
|
148
158
|
}
|
|
149
|
-
|
|
150
|
-
|
|
159
|
+
if (req.method !== undefined && req.method !== 'GET') {
|
|
160
|
+
res.statusCode = 405;
|
|
161
|
+
res.end('method not allowed');
|
|
162
|
+
return;
|
|
151
163
|
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
const
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
});
|
|
219
|
-
finish(200, 'ok');
|
|
220
|
-
})
|
|
221
|
-
.catch(next);
|
|
222
|
-
};
|
|
164
|
+
if (!isTrustedRequest(req.headers ?? {})) {
|
|
165
|
+
res.statusCode = 403;
|
|
166
|
+
res.end('forbidden');
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
const query = req.url.includes('?')
|
|
170
|
+
? req.url.slice(req.url.indexOf('?') + 1)
|
|
171
|
+
: '';
|
|
172
|
+
const params = new URLSearchParams(query);
|
|
173
|
+
const file = params.get('file');
|
|
174
|
+
if (file === null) {
|
|
175
|
+
res.statusCode = 400;
|
|
176
|
+
res.end('missing file parameter');
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
// Confine the open to the allowed roots: resolve the requested path and
|
|
180
|
+
// reject anything that escapes all of them (path traversal, absolute
|
|
181
|
+
// paths to arbitrary files). `launch-editor` spawns the user's editor on
|
|
182
|
+
// this path, so an unvalidated `file` is a local-file-open /
|
|
183
|
+
// arg-injection vector.
|
|
184
|
+
const resolved = resolvePath(roots[0] ?? process.cwd(), file);
|
|
185
|
+
const allowed = roots.some(
|
|
186
|
+
(root) => resolved === root || resolved.startsWith(root + sep)
|
|
187
|
+
);
|
|
188
|
+
if (!allowed) {
|
|
189
|
+
res.statusCode = 403;
|
|
190
|
+
res.end('file outside allowed roots');
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
if (!existsSync(resolved)) {
|
|
194
|
+
res.statusCode = 404;
|
|
195
|
+
res.end('file not found');
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
// Coerce to integers so a crafted `line`/`column` can't smuggle extra
|
|
199
|
+
// shell-visible content through the `file:line:column` ref.
|
|
200
|
+
const line = String(
|
|
201
|
+
Math.max(1, Number.parseInt(params.get('line') ?? '1', 10) || 1)
|
|
202
|
+
);
|
|
203
|
+
const column = String(
|
|
204
|
+
Math.max(1, Number.parseInt(params.get('column') ?? '1', 10) || 1)
|
|
205
|
+
);
|
|
206
|
+
const fileRef = `${resolved}:${line}:${column}`;
|
|
207
|
+
import('launch-editor')
|
|
208
|
+
.then((mod) => {
|
|
209
|
+
const launch = mod.default ?? mod;
|
|
210
|
+
// `launch-editor` invokes the callback only on failure — synchronously
|
|
211
|
+
// when it can't guess an editor, later if the spawn errors. Reply `ok`
|
|
212
|
+
// right after launching; the guard keeps a sync failure's 500 first
|
|
213
|
+
// and drops a late async failure (the response is long gone).
|
|
214
|
+
let finished = false;
|
|
215
|
+
const finish = (statusCode, msg) => {
|
|
216
|
+
if (finished) return;
|
|
217
|
+
finished = true;
|
|
218
|
+
res.statusCode = statusCode;
|
|
219
|
+
res.end(msg);
|
|
220
|
+
};
|
|
221
|
+
launch(fileRef, (_fileName, errorMessage) => {
|
|
222
|
+
if (errorMessage !== null) {
|
|
223
|
+
finish(500, errorMessage);
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
finish(200, 'ok');
|
|
227
|
+
})
|
|
228
|
+
.catch(next);
|
|
229
|
+
};
|
|
223
230
|
};
|
|
224
231
|
/**
|
|
225
232
|
* `import href from './x.css?hmr-url'` — `?url` semantics with working HMR.
|
|
@@ -265,83 +272,139 @@ const CSS_VIRTUAL_SUFFIX = '.js';
|
|
|
265
272
|
* default.
|
|
266
273
|
*/
|
|
267
274
|
export const litCssQueries = (getCssSheetBuild = () => 'auto') => {
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
275
|
+
let isBuild = false;
|
|
276
|
+
let isLib = false;
|
|
277
|
+
return {
|
|
278
|
+
name: 'lit-css-query',
|
|
279
|
+
// Vite's core resolver claims `./x.css?hmr-url` for the CSS pipeline
|
|
280
|
+
// before normal plugins get a look, so resolve ahead of it.
|
|
281
|
+
enforce: 'pre',
|
|
282
|
+
configResolved(config) {
|
|
283
|
+
isBuild = config.command === 'build';
|
|
284
|
+
isLib = config.build.lib !== false && config.build.lib !== undefined;
|
|
285
|
+
},
|
|
286
|
+
async resolveId(id, importer) {
|
|
287
|
+
const prefix = CSS_URL_QUERY_RE.test(id)
|
|
288
|
+
? CSS_URL_VIRTUAL_PREFIX
|
|
289
|
+
: CSS_SHEET_QUERY_RE.test(id)
|
|
290
|
+
? CSS_SHEET_VIRTUAL_PREFIX
|
|
291
|
+
: null;
|
|
292
|
+
if (prefix === null) {
|
|
293
|
+
return null;
|
|
294
|
+
}
|
|
295
|
+
const file = id.slice(0, id.indexOf('?'));
|
|
296
|
+
const resolved = await this.resolve(file, importer);
|
|
297
|
+
if (resolved === null) {
|
|
298
|
+
return null;
|
|
299
|
+
}
|
|
300
|
+
return prefix + resolved.id + CSS_VIRTUAL_SUFFIX;
|
|
301
|
+
},
|
|
302
|
+
load(id) {
|
|
303
|
+
const helperPath = resolveRuntimeModule('css');
|
|
304
|
+
if (id.startsWith(CSS_URL_VIRTUAL_PREFIX)) {
|
|
305
|
+
const file = id.slice(
|
|
306
|
+
CSS_URL_VIRTUAL_PREFIX.length,
|
|
307
|
+
-CSS_VIRTUAL_SUFFIX.length
|
|
308
|
+
);
|
|
309
|
+
return {
|
|
310
|
+
code:
|
|
311
|
+
`import url from ${JSON.stringify(`${file}?url`)};\n` +
|
|
312
|
+
`import {devCacheBust} from ${JSON.stringify(helperPath)};\n` +
|
|
313
|
+
`export default devCacheBust(url);\n`,
|
|
314
|
+
moduleType: 'js',
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
if (id.startsWith(CSS_SHEET_VIRTUAL_PREFIX)) {
|
|
318
|
+
const file = id.slice(
|
|
319
|
+
CSS_SHEET_VIRTUAL_PREFIX.length,
|
|
320
|
+
-CSS_VIRTUAL_SUFFIX.length
|
|
321
|
+
);
|
|
322
|
+
const mode = getCssSheetBuild();
|
|
323
|
+
const inlineQuery =
|
|
324
|
+
!isBuild || mode === 'url' || (mode === 'auto' && !isLib)
|
|
325
|
+
? null
|
|
326
|
+
: mode === 'inline-raw'
|
|
327
|
+
? '?raw'
|
|
328
|
+
: '?inline';
|
|
329
|
+
if (inlineQuery !== null) {
|
|
330
|
+
// Build only, so no `import.meta.hot` block. The sheet is still
|
|
331
|
+
// constructed once per virtual module, so adopters share it exactly
|
|
332
|
+
// as they do in the fetch-backed form.
|
|
333
|
+
return {
|
|
334
|
+
code:
|
|
335
|
+
`import css from ${JSON.stringify(`${file}${inlineQuery}`)};\n` +
|
|
336
|
+
`const sheet = new CSSStyleSheet();\n` +
|
|
337
|
+
`sheet.replaceSync(css);\n` +
|
|
338
|
+
`export default sheet;\n`,
|
|
339
|
+
moduleType: 'js',
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
// The accept specifier must be byte-identical to the import above —
|
|
343
|
+
// Vite resolves accepted HMR deps by static analysis. Generating both
|
|
344
|
+
// here is exactly what frees the caller from writing it. The swap is
|
|
345
|
+
// self-accepted at this boundary, so it never propagates to adopters.
|
|
346
|
+
const urlSpecifier = JSON.stringify(`${file}?url`);
|
|
347
|
+
return {
|
|
348
|
+
code:
|
|
349
|
+
`import url from ${urlSpecifier};\n` +
|
|
350
|
+
`import {urlSheet} from ${JSON.stringify(helperPath)};\n` +
|
|
351
|
+
`const {sheet, onHotUpdate} = urlSheet(url);\n` +
|
|
352
|
+
`export default sheet;\n` +
|
|
353
|
+
`if (import.meta.hot) {\n` +
|
|
354
|
+
` import.meta.hot.accept(${urlSpecifier}, onHotUpdate);\n` +
|
|
355
|
+
`}\n`,
|
|
356
|
+
moduleType: 'js',
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
return null;
|
|
360
|
+
},
|
|
361
|
+
};
|
|
362
|
+
};
|
|
363
|
+
const TIMELINE_VIRTUAL_ID = 'virtual:lit-plugin/timeline';
|
|
364
|
+
const TIMELINE_RESOLVED_ID = '\0virtual:lit-plugin/timeline';
|
|
365
|
+
const TIMELINE_STUB =
|
|
366
|
+
'export const addTimelineEvent = () => {};\n' +
|
|
367
|
+
'export const addTimelineLayer = () => {};\n';
|
|
368
|
+
/**
|
|
369
|
+
* The public timeline API virtual module, served in dev and build alike:
|
|
370
|
+
* app code importing it must keep building under `vite build`, where the HMR
|
|
371
|
+
* plugin doesn't apply. While these hooks lived on that serve-only plugin,
|
|
372
|
+
* nothing claimed the specifier during a build and the bundler failed to
|
|
373
|
+
* resolve it.
|
|
374
|
+
*
|
|
375
|
+
* `getTimeline` is a getter, not a value: `litPlugin()` re-resolves its
|
|
376
|
+
* options against the loaded env in a `config` hook, which runs after the
|
|
377
|
+
* plugin array is built.
|
|
378
|
+
*/
|
|
379
|
+
export const litTimelineVirtual = (getTimeline = () => false) => {
|
|
380
|
+
let isBuild = false;
|
|
381
|
+
return {
|
|
382
|
+
name: 'lit-timeline-virtual',
|
|
383
|
+
configResolved(config) {
|
|
384
|
+
isBuild = config.command === 'build';
|
|
385
|
+
},
|
|
386
|
+
resolveId(id) {
|
|
387
|
+
return id === TIMELINE_VIRTUAL_ID ? TIMELINE_RESOLVED_ID : null;
|
|
388
|
+
},
|
|
389
|
+
load(id) {
|
|
390
|
+
if (id !== TIMELINE_RESOLVED_ID) {
|
|
391
|
+
return null;
|
|
392
|
+
}
|
|
393
|
+
// Under build the stub is unconditional, even with `timeline: true`.
|
|
394
|
+
// The runtime delivers only over `import.meta.hot`, so in a production
|
|
395
|
+
// bundle it is a queue that never drains; the stub keeps the transport
|
|
396
|
+
// out of the bundle and makes the documented "no-op in production"
|
|
397
|
+
// behavior true.
|
|
398
|
+
if (isBuild || !getTimeline()) {
|
|
399
|
+
return {code: TIMELINE_STUB, moduleType: 'js'};
|
|
400
|
+
}
|
|
401
|
+
const apiPath = resolveRuntimeModule('timeline/public-api');
|
|
402
|
+
return {
|
|
403
|
+
code: `export * from ${JSON.stringify(apiPath)};\n`,
|
|
404
|
+
moduleType: 'js',
|
|
405
|
+
};
|
|
406
|
+
},
|
|
407
|
+
};
|
|
345
408
|
};
|
|
346
409
|
/**
|
|
347
410
|
* A `css` tagged template literal with no interpolations and no escape
|
|
@@ -358,333 +421,336 @@ const CSS_LITERAL_RE = /(?<![\w$.])css`((?:[^`\\$]|\$(?!\{))*)`/g;
|
|
|
358
421
|
* treatment (targets, drafts, …) as `.css` files. Applies in dev and build.
|
|
359
422
|
*/
|
|
360
423
|
const litCssLiterals = () => {
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
}
|
|
424
|
+
let lightningcss = null;
|
|
425
|
+
let options;
|
|
426
|
+
let minify = false;
|
|
427
|
+
return {
|
|
428
|
+
name: 'lit-css-literals',
|
|
429
|
+
async configResolved(config) {
|
|
430
|
+
if (config.css.transformer !== 'lightningcss') {
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
// The transformer setting guarantees the dependency: Vite itself
|
|
434
|
+
// can't process .css files without it.
|
|
435
|
+
lightningcss = await import('lightningcss');
|
|
436
|
+
// cssModules makes no sense for a literal; everything else carries
|
|
437
|
+
// over.
|
|
438
|
+
const {cssModules: _cssModules, ...rest} = config.css.lightningcss ?? {};
|
|
439
|
+
options = rest;
|
|
440
|
+
minify = config.command === 'build';
|
|
441
|
+
},
|
|
442
|
+
transform(code, id) {
|
|
443
|
+
if (lightningcss === null) {
|
|
444
|
+
return null;
|
|
445
|
+
}
|
|
446
|
+
if (id.startsWith('\0') || id.includes('/node_modules/')) {
|
|
447
|
+
return null;
|
|
448
|
+
}
|
|
449
|
+
const [file] = id.split('?', 2);
|
|
450
|
+
if (!JS_FILE_RE.test(file) || !code.includes('css`')) {
|
|
451
|
+
return null;
|
|
452
|
+
}
|
|
453
|
+
const ms = new MagicString(code);
|
|
454
|
+
let changed = false;
|
|
455
|
+
for (const m of code.matchAll(CSS_LITERAL_RE)) {
|
|
456
|
+
const literal = m[1];
|
|
457
|
+
if (literal.trim() === '') {
|
|
458
|
+
continue;
|
|
459
|
+
}
|
|
460
|
+
let out;
|
|
461
|
+
try {
|
|
462
|
+
const result = lightningcss.transform({
|
|
463
|
+
...options,
|
|
464
|
+
filename: file,
|
|
465
|
+
code: Buffer.from(literal),
|
|
466
|
+
minify,
|
|
467
|
+
});
|
|
468
|
+
out = Buffer.from(result.code).toString();
|
|
469
|
+
} catch (e) {
|
|
470
|
+
this.warn(
|
|
471
|
+
`[lit-plugin] skipping css literal Lightning CSS couldn't parse: ${e.message}`
|
|
472
|
+
);
|
|
473
|
+
continue;
|
|
474
|
+
}
|
|
475
|
+
// Re-escape for the template literal the output goes back into.
|
|
476
|
+
out = out.replace(/[\\`$]/g, '\\$&');
|
|
477
|
+
if (out !== literal) {
|
|
478
|
+
const start = m.index + 'css`'.length;
|
|
479
|
+
ms.overwrite(start, start + literal.length, out);
|
|
480
|
+
changed = true;
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
if (!changed) {
|
|
484
|
+
return null;
|
|
485
|
+
}
|
|
486
|
+
return {code: ms.toString(), map: ms.generateMap({hires: true})};
|
|
487
|
+
},
|
|
488
|
+
};
|
|
425
489
|
};
|
|
426
490
|
/**
|
|
427
491
|
* Vite plugin set providing HMR, CSS helpers, and Lightning CSS processing
|
|
428
492
|
* for Lit projects.
|
|
429
493
|
*/
|
|
430
494
|
export const litPlugin = (options = {}) => {
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
495
|
+
// Resolved from explicit options first, env vars second, defaults last. The
|
|
496
|
+
// `lit-plugin-options` `config` hook below re-resolves with the loaded env
|
|
497
|
+
// once Vite hands us the mode; this initial pass covers code paths that run
|
|
498
|
+
// before (or without) it.
|
|
499
|
+
let resolved = resolveOptions(options, {});
|
|
500
|
+
let root = '';
|
|
501
|
+
// Env resolution lives in its own always-applied plugin: `?css-sheet` is a
|
|
502
|
+
// build-time feature too, and the HMR plugin (`apply: 'serve'`) never gets a
|
|
503
|
+
// `config` hook under `vite build`. `enforce: 'pre'` and first position put
|
|
504
|
+
// it ahead of every other hook that reads `resolved`.
|
|
505
|
+
const optionsPlugin = {
|
|
506
|
+
name: 'lit-plugin-options',
|
|
507
|
+
enforce: 'pre',
|
|
508
|
+
config(viteConfig, {mode}) {
|
|
509
|
+
// `loadEnv` reads `.env*` files from the env dir (root by default) and
|
|
510
|
+
// merges in matching `process.env` keys, filtered to the `LIT_PLUGIN`
|
|
511
|
+
// prefix. Explicit options still win (handled in resolveOptions).
|
|
512
|
+
const envDir = viteConfig.envDir
|
|
513
|
+
? resolvePath(viteConfig.envDir)
|
|
514
|
+
: viteConfig.root
|
|
515
|
+
? resolvePath(viteConfig.root)
|
|
516
|
+
: process.cwd();
|
|
517
|
+
resolved = resolveOptions(options, loadEnv(mode, envDir, ENV_PREFIX));
|
|
518
|
+
},
|
|
519
|
+
};
|
|
520
|
+
const sourceOverlayPlugin = {
|
|
521
|
+
name: 'lit-source-overlay',
|
|
522
|
+
apply: 'serve',
|
|
523
|
+
// Run before Vite's esbuild TS transform so source-meta line numbers are
|
|
524
|
+
// measured against the author's raw source (and the `@customElement … class`
|
|
525
|
+
// forms are still intact), not against transpiled output.
|
|
526
|
+
enforce: 'pre',
|
|
527
|
+
configResolved(config) {
|
|
528
|
+
root = config.root;
|
|
529
|
+
},
|
|
530
|
+
resolveId(id) {
|
|
531
|
+
if (id === '@oddsquad/vite-plugin-lit/source-overlay.js') {
|
|
532
|
+
return resolveRuntimeModule('source-overlay');
|
|
533
|
+
}
|
|
534
|
+
return null;
|
|
535
|
+
},
|
|
536
|
+
configureServer(server) {
|
|
537
|
+
if (!resolved.sourceOverlay) return;
|
|
538
|
+
// Allow opens from everything vite itself is willing to serve
|
|
539
|
+
// (`server.fs.allow` defaults to the workspace root), not just
|
|
540
|
+
// `config.root` — in monorepos, component sources regularly live in
|
|
541
|
+
// sibling packages outside the served app's root.
|
|
542
|
+
const fsAllow = server.config.server.fs?.allow ?? [];
|
|
543
|
+
server.middlewares.use(
|
|
544
|
+
OPEN_IN_EDITOR_PATH,
|
|
545
|
+
createOpenInEditorMiddleware([root, ...fsAllow])
|
|
546
|
+
);
|
|
547
|
+
},
|
|
548
|
+
transform(code, id, transformOptions) {
|
|
549
|
+
if (!resolved.sourceOverlay) return null;
|
|
550
|
+
if (transformOptions?.ssr) return null;
|
|
551
|
+
if (
|
|
552
|
+
id.startsWith('\0') ||
|
|
553
|
+
id.includes('__x00__') ||
|
|
554
|
+
id.includes('lit-plugin:') ||
|
|
555
|
+
id.includes('/node_modules/')
|
|
556
|
+
) {
|
|
557
|
+
return null;
|
|
558
|
+
}
|
|
559
|
+
const [file] = id.split('?', 2);
|
|
560
|
+
if (!JS_FILE_RE.test(file) && !id.includes('?html-proxy')) {
|
|
561
|
+
return null;
|
|
562
|
+
}
|
|
563
|
+
if (!code.includes('customElement') && !code.includes('customElements')) {
|
|
564
|
+
return null;
|
|
565
|
+
}
|
|
566
|
+
const ms = new MagicString(code);
|
|
567
|
+
const relativeFile = file.startsWith(root + '/')
|
|
568
|
+
? file.slice(root.length + 1)
|
|
569
|
+
: file;
|
|
570
|
+
if (!injectSourceMeta(code, relativeFile, ms)) {
|
|
571
|
+
return null;
|
|
572
|
+
}
|
|
573
|
+
return {code: ms.toString(), map: ms.generateMap({hires: true})};
|
|
574
|
+
},
|
|
575
|
+
transformIndexHtml() {
|
|
576
|
+
if (!resolved.sourceOverlay) return;
|
|
577
|
+
const overlayUrl = `/@fs/${resolveRuntimeModule('source-overlay')}`;
|
|
578
|
+
const {
|
|
579
|
+
exclude: _exclude,
|
|
580
|
+
onSelect: _onSelect,
|
|
581
|
+
...overlayInit
|
|
582
|
+
} = resolved.sourceOverlay;
|
|
583
|
+
const initConfig = {
|
|
584
|
+
...overlayInit,
|
|
585
|
+
openInEditorPath: OPEN_IN_EDITOR_PATH,
|
|
586
|
+
};
|
|
587
|
+
return [
|
|
588
|
+
{
|
|
589
|
+
tag: 'script',
|
|
590
|
+
attrs: {type: 'module'},
|
|
591
|
+
children: `import {initSourceOverlay} from ${JSON.stringify(overlayUrl)};\ninitSourceOverlay(${JSON.stringify(initConfig)});\n`,
|
|
592
|
+
injectTo: 'body',
|
|
465
593
|
},
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
const [file] = id.split('?', 2);
|
|
628
|
-
// Allow inline scripts extracted from HTML (`?html-proxy`).
|
|
629
|
-
if (!JS_FILE_RE.test(file) && !id.includes('?html-proxy')) {
|
|
630
|
-
return null;
|
|
631
|
-
}
|
|
632
|
-
return transformLitModule(code);
|
|
633
|
-
},
|
|
634
|
-
transformIndexHtml() {
|
|
635
|
-
const tags = [];
|
|
636
|
-
const indicator = resolved.indicator;
|
|
637
|
-
if (indicator) {
|
|
638
|
-
const indicatorUrl = `/@fs/` + resolveRuntimeModule('indicator');
|
|
639
|
-
tags.push({
|
|
640
|
-
tag: 'script',
|
|
641
|
-
attrs: { type: 'module', src: indicatorUrl },
|
|
642
|
-
injectTo: 'body',
|
|
643
|
-
}, {
|
|
644
|
-
tag: 'lit-devtools-hmr-indicator',
|
|
645
|
-
attrs: indicator.count ? { count: '' } : undefined,
|
|
646
|
-
children: '',
|
|
647
|
-
injectTo: 'body',
|
|
648
|
-
});
|
|
649
|
-
}
|
|
650
|
-
if (resolved.timeline) {
|
|
651
|
-
const installUrl = `/@fs/` + resolveRuntimeModule('timeline/install');
|
|
652
|
-
tags.push({
|
|
653
|
-
tag: 'script',
|
|
654
|
-
attrs: { type: 'module', src: installUrl },
|
|
655
|
-
injectTo: 'body',
|
|
656
|
-
});
|
|
657
|
-
// Components inspector runtime — answers the panel's tree/details
|
|
658
|
-
// requests. Paired with the timeline panel, which hosts its tab.
|
|
659
|
-
const inspectorUrl = `/@fs/` + resolveRuntimeModule('inspector/install');
|
|
660
|
-
tags.push({
|
|
661
|
-
tag: 'script',
|
|
662
|
-
attrs: { type: 'module', src: inspectorUrl },
|
|
663
|
-
injectTo: 'body',
|
|
664
|
-
});
|
|
665
|
-
}
|
|
666
|
-
return tags.length > 0 ? tags : undefined;
|
|
667
|
-
},
|
|
668
|
-
};
|
|
669
|
-
// The source-overlay plugin is always present; its hooks no-op when the
|
|
670
|
-
// feature is disabled (which env may decide), so inclusion can't be gated
|
|
671
|
-
// on the synchronously-known options here.
|
|
672
|
-
const plugins = [
|
|
673
|
-
optionsPlugin,
|
|
674
|
-
// Getter, not a snapshot: `resolved` is re-resolved against the loaded env
|
|
675
|
-
// in `optionsPlugin`'s `config` hook, which runs after this array is built.
|
|
676
|
-
litCssQueries(() => resolved.cssSheetBuild),
|
|
677
|
-
litCssLiterals(),
|
|
678
|
-
sourceOverlayPlugin,
|
|
679
|
-
hmr,
|
|
680
|
-
];
|
|
681
|
-
if (resolved.timeline) {
|
|
682
|
-
// Pass a getter, not a snapshot: `resolved` is re-resolved against the
|
|
683
|
-
// loaded env in the `config` hook, which runs after this plugin array is
|
|
684
|
-
// built. The settings endpoint reads it per-request, by which point env is
|
|
685
|
-
// applied.
|
|
686
|
-
plugins.push(litTimelinePlugin(() => toFeatureSettings(resolved)));
|
|
687
|
-
}
|
|
688
|
-
return plugins;
|
|
594
|
+
];
|
|
595
|
+
},
|
|
596
|
+
};
|
|
597
|
+
const hmr = {
|
|
598
|
+
name: 'lit-plugin',
|
|
599
|
+
apply: 'serve',
|
|
600
|
+
config: () => {
|
|
601
|
+
// Options are already resolved against the loaded env by
|
|
602
|
+
// `lit-plugin-options`, which runs first.
|
|
603
|
+
if (!resolved.hmrEnabled) {
|
|
604
|
+
return;
|
|
605
|
+
}
|
|
606
|
+
// The injected runtime imports are invisible to the dep scanner. The
|
|
607
|
+
// lit family stays prebundle-eligible on purpose: the wrapper modules'
|
|
608
|
+
// bare imports then resolve to the same URL every other importer gets —
|
|
609
|
+
// single lit instance, single template cache.
|
|
610
|
+
return {optimizeDeps: {exclude: ['@oddsquad/vite-plugin-lit']}};
|
|
611
|
+
},
|
|
612
|
+
resolveId(id) {
|
|
613
|
+
// Resolve the browser CSS helpers and indicator runtime to the copy
|
|
614
|
+
// shipped next to this plugin, so they work even when the package
|
|
615
|
+
// isn't reachable through node resolution from the served root (and
|
|
616
|
+
// stay out of prebundling).
|
|
617
|
+
//
|
|
618
|
+
// These bare specifiers must match the package name in package.json —
|
|
619
|
+
// consumers import them by name (see README "Stylesheets"), and Vite
|
|
620
|
+
// only consults this hook for the exact string. A rename that misses
|
|
621
|
+
// them turns the fallback into silently dead code.
|
|
622
|
+
if (id === '@oddsquad/vite-plugin-lit/css.js') {
|
|
623
|
+
return resolveRuntimeModule('css');
|
|
624
|
+
}
|
|
625
|
+
if (id === '@oddsquad/vite-plugin-lit/indicator.js') {
|
|
626
|
+
return resolveRuntimeModule('indicator');
|
|
627
|
+
}
|
|
628
|
+
if (id === '@oddsquad/vite-plugin-lit/source-overlay.js') {
|
|
629
|
+
return resolveRuntimeModule('source-overlay');
|
|
630
|
+
}
|
|
631
|
+
if (resolved.hmrEnabled && id.startsWith(VIRTUAL_PREFIX)) {
|
|
632
|
+
return id;
|
|
633
|
+
}
|
|
634
|
+
return null;
|
|
635
|
+
},
|
|
636
|
+
load(id) {
|
|
637
|
+
if (!resolved.hmrEnabled || !id.startsWith(VIRTUAL_PREFIX)) {
|
|
638
|
+
return null;
|
|
639
|
+
}
|
|
640
|
+
if (id === INSTALL_ID) {
|
|
641
|
+
const patchPath = resolveRuntimeModule('patch');
|
|
642
|
+
const runtimeOptions = {
|
|
643
|
+
reconnect: resolved.reconnect,
|
|
644
|
+
onIncompatible: resolved.onIncompatible,
|
|
645
|
+
};
|
|
646
|
+
return {
|
|
647
|
+
code:
|
|
648
|
+
`import {install} from ${JSON.stringify(patchPath)};\n` +
|
|
649
|
+
`install(${JSON.stringify(runtimeOptions)});\n`,
|
|
650
|
+
moduleType: 'js',
|
|
651
|
+
};
|
|
652
|
+
}
|
|
653
|
+
const spec = id.slice(VIRTUAL_PREFIX.length);
|
|
654
|
+
const wrappedTags = WRAP_TABLE.get(spec);
|
|
655
|
+
if (wrappedTags === undefined) {
|
|
656
|
+
return null;
|
|
657
|
+
}
|
|
658
|
+
const internPath = resolveRuntimeModule('intern');
|
|
659
|
+
// Browser-native ESM: explicit local exports shadow `export *` names
|
|
660
|
+
// (spec-guaranteed), so everything except the wrapped tags passes
|
|
661
|
+
// through unchanged.
|
|
662
|
+
const lines = [
|
|
663
|
+
`import * as __lit from ${JSON.stringify(spec)};`,
|
|
664
|
+
`import {wrapTag} from ${JSON.stringify(internPath)};`,
|
|
665
|
+
`export * from ${JSON.stringify(spec)};`,
|
|
666
|
+
];
|
|
667
|
+
for (const {exportName, ns} of wrappedTags) {
|
|
668
|
+
lines.push(
|
|
669
|
+
`export const ${exportName} = wrapTag(__lit.${exportName}, ${JSON.stringify(ns)});`
|
|
670
|
+
);
|
|
671
|
+
}
|
|
672
|
+
return {code: lines.join('\n') + '\n', moduleType: 'js'};
|
|
673
|
+
},
|
|
674
|
+
async transform(code, id, transformOptions) {
|
|
675
|
+
if (!resolved.hmrEnabled) {
|
|
676
|
+
return null;
|
|
677
|
+
}
|
|
678
|
+
if (transformOptions?.ssr) {
|
|
679
|
+
return null;
|
|
680
|
+
}
|
|
681
|
+
if (id.startsWith('\0') || id.includes('/node_modules/')) {
|
|
682
|
+
return null;
|
|
683
|
+
}
|
|
684
|
+
const [file] = id.split('?', 2);
|
|
685
|
+
// Allow inline scripts extracted from HTML (`?html-proxy`).
|
|
686
|
+
if (!JS_FILE_RE.test(file) && !id.includes('?html-proxy')) {
|
|
687
|
+
return null;
|
|
688
|
+
}
|
|
689
|
+
return transformLitModule(code);
|
|
690
|
+
},
|
|
691
|
+
transformIndexHtml() {
|
|
692
|
+
const tags = [];
|
|
693
|
+
const indicator = resolved.indicator;
|
|
694
|
+
if (indicator) {
|
|
695
|
+
const indicatorUrl = `/@fs/` + resolveRuntimeModule('indicator');
|
|
696
|
+
tags.push(
|
|
697
|
+
{
|
|
698
|
+
tag: 'script',
|
|
699
|
+
attrs: {type: 'module', src: indicatorUrl},
|
|
700
|
+
injectTo: 'body',
|
|
701
|
+
},
|
|
702
|
+
{
|
|
703
|
+
tag: 'lit-devtools-hmr-indicator',
|
|
704
|
+
attrs: indicator.count ? {count: ''} : undefined,
|
|
705
|
+
children: '',
|
|
706
|
+
injectTo: 'body',
|
|
707
|
+
}
|
|
708
|
+
);
|
|
709
|
+
}
|
|
710
|
+
if (resolved.timeline) {
|
|
711
|
+
const installUrl = `/@fs/` + resolveRuntimeModule('timeline/install');
|
|
712
|
+
tags.push({
|
|
713
|
+
tag: 'script',
|
|
714
|
+
attrs: {type: 'module', src: installUrl},
|
|
715
|
+
injectTo: 'body',
|
|
716
|
+
});
|
|
717
|
+
// Components inspector runtime — answers the panel's tree/details
|
|
718
|
+
// requests. Paired with the timeline panel, which hosts its tab.
|
|
719
|
+
const inspectorUrl =
|
|
720
|
+
`/@fs/` + resolveRuntimeModule('inspector/install');
|
|
721
|
+
tags.push({
|
|
722
|
+
tag: 'script',
|
|
723
|
+
attrs: {type: 'module', src: inspectorUrl},
|
|
724
|
+
injectTo: 'body',
|
|
725
|
+
});
|
|
726
|
+
}
|
|
727
|
+
return tags.length > 0 ? tags : undefined;
|
|
728
|
+
},
|
|
729
|
+
};
|
|
730
|
+
// The source-overlay plugin is always present; its hooks no-op when the
|
|
731
|
+
// feature is disabled (which env may decide), so inclusion can't be gated
|
|
732
|
+
// on the synchronously-known options here.
|
|
733
|
+
const plugins = [
|
|
734
|
+
optionsPlugin,
|
|
735
|
+
// Getter, not a snapshot: `resolved` is re-resolved against the loaded env
|
|
736
|
+
// in `optionsPlugin`'s `config` hook, which runs after this array is built.
|
|
737
|
+
litCssQueries(() => resolved.cssSheetBuild),
|
|
738
|
+
litTimelineVirtual(() => resolved.timeline),
|
|
739
|
+
litCssLiterals(),
|
|
740
|
+
sourceOverlayPlugin,
|
|
741
|
+
hmr,
|
|
742
|
+
];
|
|
743
|
+
if (resolved.timeline) {
|
|
744
|
+
// `features` is a getter, not a snapshot: `resolved` is re-resolved
|
|
745
|
+
// against the loaded env in `optionsPlugin`'s `config` hook, which runs
|
|
746
|
+
// after this array is built.
|
|
747
|
+
plugins.push(
|
|
748
|
+
createLitDevframePlugin({
|
|
749
|
+
version: PACKAGE_VERSION,
|
|
750
|
+
features: () => toFeatureSettings(resolved),
|
|
751
|
+
})
|
|
752
|
+
);
|
|
753
|
+
}
|
|
754
|
+
return plugins;
|
|
689
755
|
};
|
|
690
|
-
//# sourceMappingURL=plugin.js.map
|
|
756
|
+
//# sourceMappingURL=plugin.js.map
|