browser-runtime-svelte 0.0.1 → 0.0.2

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/index.d.ts CHANGED
@@ -1,2 +1 @@
1
- import type {RuntimePlugin} from 'browser-module-runtime';
2
- export declare function svelte(options?:Record<string,unknown>):RuntimePlugin;
1
+ export * from './src/index.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "browser-runtime-svelte",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -22,7 +22,7 @@
22
22
  "node": ">=20"
23
23
  },
24
24
  "peerDependencies": {
25
- "browser-module-runtime": "0.0.1",
25
+ "browser-module-runtime": "0.0.2",
26
26
  "svelte": "^5.0.0"
27
27
  },
28
28
  "description": "Svelte compiler and renderer adapter for browser-module-runtime.",
package/src/index.d.ts CHANGED
@@ -1 +1,9 @@
1
- export * from '../index.js';
1
+ import type {RuntimePlugin} from 'browser-module-runtime';
2
+ export interface SvelteAdapterOptions {
3
+ compiler?: {compile(source:string,options?:Record<string,unknown>):unknown;compileModule?(source:string,options?:Record<string,unknown>):unknown};
4
+ renderer?: {mount(component:unknown,options:{target:Element;props:Record<string,unknown>}):unknown;unmount(instance:unknown):unknown};
5
+ version?:string;provider?:string;compileOptions?:Record<string,unknown>;
6
+ }
7
+ export declare function svelte(options?:SvelteAdapterOptions):RuntimePlugin;
8
+ /** Convert HTML-safe <$script> tags in inline Svelte source; preserves positions. */
9
+ export declare function normalizeInlineSvelte(source:string):string;
package/src/index.js CHANGED
@@ -1,3 +1,6 @@
1
+ import {normalizeInlineSvelte} from './inline-source.js';
2
+ export {normalizeInlineSvelte} from './inline-source.js';
3
+
1
4
  /** Uses the official Svelte 5 compiler API and mount/unmount runtime. */
2
5
  export function svelte({compiler,renderer,version='5',provider='esm.sh',compileOptions={}}={}) {
3
6
  let c=compiler;
@@ -7,6 +10,7 @@ export function svelte({compiler,renderer,version='5',provider='esm.sh',compileO
7
10
  ? {svelte:{provider,version:String(version)}} : {};
8
11
  return {
9
12
  name:'svelte',version:String(version),packageEntries,
13
+ html:{normalizeInlineSource:normalizeInlineSvelte},
10
14
  configure({runtime}){ownerRuntime=runtime;},
11
15
  compilers:[{
12
16
  types:['svelte','svelte-module'],extensions:['.svelte'],
@@ -0,0 +1,76 @@
1
+ /** Normalize HTML-safe Svelte <$script> delimiters without altering normal Svelte source.
2
+ * Replacement is same-width (the '$' becomes a space), preserving source positions.
3
+ * Only call for inline HTML declarations, never for external files or runtime.define().
4
+ */
5
+ function skipString(source, position) {
6
+ const quote = source[position];
7
+ let i = position + 1;
8
+ for (; i < source.length; i++) {
9
+ if (source[i] === '\\') { i++; continue; }
10
+ if (source[i] === quote) return i + 1;
11
+ }
12
+ return i;
13
+ }
14
+ function skipJsToken(source, i) {
15
+ if (source[i] === '"' || source[i] === "'" || source[i] === '`') return skipString(source, i);
16
+ if (source.startsWith('//', i)) { const end = source.indexOf('\n', i + 2); return end < 0 ? source.length : end; }
17
+ if (source.startsWith('/*', i)) { const end = source.indexOf('*/', i + 2); return end < 0 ? source.length : end + 2; }
18
+ return i;
19
+ }
20
+ function tagEnd(source, start) {
21
+ let i = start;
22
+ while (i < source.length) {
23
+ const skipped = skipJsToken(source, i);
24
+ if (skipped !== i) { i = skipped; continue; }
25
+ if (source[i] === '>') return i + 1;
26
+ i++;
27
+ }
28
+ throw new SyntaxError('Unclosed <$script> opening tag in inline Svelte source');
29
+ }
30
+ export function normalizeInlineSvelte(source) {
31
+ if (typeof source !== 'string') throw new TypeError('Inline Svelte source must be a string');
32
+ let result = '';
33
+ let cursor = 0;
34
+ let i = 0;
35
+ while (i < source.length) {
36
+ if (source.startsWith('<!--', i)) { const end = source.indexOf('-->', i + 4); i = end < 0 ? source.length : end + 3; continue; }
37
+ // An HTML tag's attribute string must not be mistaken for a script delimiter.
38
+ if (source.startsWith('<style', i) && /[\s>]/.test(source[i + 6] || '>')) {
39
+ const end = source.indexOf('</style>', tagEnd(source, i));
40
+ i = end < 0 ? source.length : end + '</style>'.length; continue;
41
+ }
42
+ if (source.startsWith('<$script', i) && /[\s>]/.test(source[i + 8] || '>')) {
43
+ const openingEnd = tagEnd(source, i + 8);
44
+ let closing = openingEnd;
45
+ while (closing < source.length) {
46
+ const skipped = skipJsToken(source, closing);
47
+ if (skipped !== closing) { closing = skipped; continue; }
48
+ if (source.startsWith('</$script', closing) && /[\s>]/.test(source[closing + 9] || '>')) break;
49
+ closing++;
50
+ }
51
+ if (closing >= source.length) throw new SyntaxError('Missing </$script> closing tag in inline Svelte source');
52
+ const closingEnd = tagEnd(source, closing + 9);
53
+ result += source.slice(cursor, i) + '<script ' + source.slice(i + 8, closing) + '</script ' + source.slice(closing + 9, closingEnd);
54
+ i = closingEnd;
55
+ cursor = i;
56
+ continue;
57
+ }
58
+ if (source[i] === '<') {
59
+ const maybeTag = /^<\/?[A-Za-z][\w:-]*/.exec(source.slice(i));
60
+ if (maybeTag) { i = tagEnd(source, i + maybeTag[0].length); continue; }
61
+ }
62
+ if (source[i] === '{') {
63
+ let depth = 1; let j = i + 1;
64
+ while (j < source.length && depth > 0) {
65
+ const skipped = skipJsToken(source, j);
66
+ if (skipped !== j) { j = skipped; continue; }
67
+ if (source[j] === '{') depth++;
68
+ if (source[j] === '}') depth--;
69
+ j++;
70
+ }
71
+ i = j; continue;
72
+ }
73
+ i++;
74
+ }
75
+ return result + source.slice(cursor);
76
+ }