browser-runtime-svelte 0.0.0-stage → 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/README.md +25 -2
- package/index.d.ts +1 -0
- package/package.json +39 -4
- package/src/index.d.ts +9 -0
- package/src/index.js +35 -0
- package/src/inline-source.js +76 -0
package/README.md
CHANGED
|
@@ -1,3 +1,26 @@
|
|
|
1
|
-
#
|
|
1
|
+
# browser-runtime-svelte
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Svelte 5 compiler and renderer plugin for [`browser-module-runtime`](https://www.npmjs.com/package/browser-module-runtime).
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install browser-module-runtime@0.0.1 browser-runtime-svelte@0.0.1 svelte@5
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import {createRuntime} from 'browser-module-runtime';
|
|
15
|
+
import {svelte} from 'browser-runtime-svelte';
|
|
16
|
+
|
|
17
|
+
const runtime = createRuntime({plugins:[svelte({version:'5.39.0'})]});
|
|
18
|
+
runtime.define('/Counter.svelte', `<script>let count = $state(0);</script>
|
|
19
|
+
<button onclick={() => count++}>Count: {count}</button>`, {type:'svelte'});
|
|
20
|
+
const handle = await runtime.mount({module:'/Counter.svelte',target:'#app'});
|
|
21
|
+
// Later: await handle.dispose(); await runtime.dispose();
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
The adapter uses `svelte/compiler` and `svelte` runtime mounting. Generated stylesheet assets are registered and cleaned up with the mount. An exact `version` contributes a matching `svelte` package root to the runtime linker so compiled `svelte/internal/*` imports resolve consistently. Real browser CDN operation depends on the selected provider serving that version.
|
|
25
|
+
|
|
26
|
+
For HTML declarations of complete `.svelte` files, prefer `type="svelte"` with `data-src`, since nested `<script>` text cannot safely be embedded in an outer HTML script element.
|
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './src/index.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "browser-runtime-svelte",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"exports": {
|
|
6
|
+
".": {
|
|
7
|
+
"types": "./index.d.ts",
|
|
8
|
+
"import": "./src/index.js"
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"src",
|
|
13
|
+
"index.d.ts",
|
|
14
|
+
"README.md"
|
|
15
|
+
],
|
|
16
|
+
"peerDependenciesMeta": {
|
|
17
|
+
"svelte": {
|
|
18
|
+
"optional": true
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=20"
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"browser-module-runtime": "0.0.2",
|
|
26
|
+
"svelte": "^5.0.0"
|
|
27
|
+
},
|
|
28
|
+
"description": "Svelte compiler and renderer adapter for browser-module-runtime.",
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"keywords": [
|
|
33
|
+
"browser",
|
|
34
|
+
"esm",
|
|
35
|
+
"runtime",
|
|
36
|
+
"modules",
|
|
37
|
+
"compiler",
|
|
38
|
+
"adapter",
|
|
39
|
+
"svelte"
|
|
40
|
+
]
|
|
41
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {normalizeInlineSvelte} from './inline-source.js';
|
|
2
|
+
export {normalizeInlineSvelte} from './inline-source.js';
|
|
3
|
+
|
|
4
|
+
/** Uses the official Svelte 5 compiler API and mount/unmount runtime. */
|
|
5
|
+
export function svelte({compiler,renderer,version='5',provider='esm.sh',compileOptions={}}={}) {
|
|
6
|
+
let c=compiler;
|
|
7
|
+
let runtime=renderer;
|
|
8
|
+
let ownerRuntime;
|
|
9
|
+
const packageEntries=/^\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?$/.test(String(version))
|
|
10
|
+
? {svelte:{provider,version:String(version)}} : {};
|
|
11
|
+
return {
|
|
12
|
+
name:'svelte',version:String(version),packageEntries,
|
|
13
|
+
html:{normalizeInlineSource:normalizeInlineSvelte},
|
|
14
|
+
configure({runtime}){ownerRuntime=runtime;},
|
|
15
|
+
compilers:[{
|
|
16
|
+
types:['svelte','svelte-module'],extensions:['.svelte'],
|
|
17
|
+
async compile(source,{id,type}) {
|
|
18
|
+
c ||= await (ownerRuntime ? ownerRuntime.import('svelte/compiler') : import('svelte/compiler'));
|
|
19
|
+
const options={filename:id,generate:'client',css:'external',...compileOptions};
|
|
20
|
+
const result=type==='svelte-module'&&typeof c.compileModule==='function'
|
|
21
|
+
? await c.compileModule(source,options)
|
|
22
|
+
: await c.compile(source,options);
|
|
23
|
+
return {code:result.js.code,map:result.js.map&&String(result.js.map),assets:result.css?.code?[{type:'css',id:`${id}:svelte-css`,content:result.css.code}]:[],diagnostics:result.warnings||[]};
|
|
24
|
+
}
|
|
25
|
+
}],
|
|
26
|
+
renderers:[{
|
|
27
|
+
name:'svelte',types:['svelte','svelte-module'],
|
|
28
|
+
async mount({component,target,props}){
|
|
29
|
+
runtime ||= await (ownerRuntime ? ownerRuntime.import('svelte') : import('svelte'));
|
|
30
|
+
const instance=runtime.mount(component,{target,props});
|
|
31
|
+
return {dispose:()=>runtime.unmount(instance)};
|
|
32
|
+
}
|
|
33
|
+
}]
|
|
34
|
+
};
|
|
35
|
+
}
|
|
@@ -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
|
+
}
|