@loupfeed/svelte-preprocess 0.1.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/LICENSE.md ADDED
@@ -0,0 +1,114 @@
1
+ # Functional Source License, Version 1.1, Apache 2.0 Future License
2
+
3
+ ## Abbreviation
4
+
5
+ FSL-1.1-ALv2
6
+
7
+ ## Notice
8
+
9
+ Copyright 2026 loupfeed
10
+
11
+ ## Terms and Conditions
12
+
13
+ ### Licensor ("We")
14
+
15
+ The party offering the Software under these Terms and Conditions.
16
+
17
+ ### The Software
18
+
19
+ The "Software" is each version of the software that we make available under
20
+ these Terms and Conditions, as indicated by our inclusion of these Terms and
21
+ Conditions with the Software.
22
+
23
+ ### License Grant
24
+
25
+ Subject to your compliance with this License Grant and the Patents,
26
+ Redistribution and Trademark clauses below, we hereby grant you the right to use,
27
+ copy, modify, create derivative works, publicly perform, publicly display and
28
+ redistribute the Software for any Permitted Purpose identified below.
29
+
30
+ ### Permitted Purpose
31
+
32
+ A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
33
+ means making the Software available to others in a commercial product or service
34
+ that:
35
+
36
+ 1. substitutes for the Software;
37
+
38
+ 2. substitutes for any other product or service we offer using the Software that
39
+ exists as of the date we make the Software available; or
40
+
41
+ 3. offers the same or substantially similar functionality as the Software.
42
+
43
+ Permitted Purposes specifically include using the Software:
44
+
45
+ 1. for your internal use and access;
46
+
47
+ 2. for non-commercial education;
48
+
49
+ 3. for non-commercial research; and
50
+
51
+ 4. in connection with professional services that you provide to a licensee using
52
+ the Software in accordance with these Terms and Conditions.
53
+
54
+ ### Patents
55
+
56
+ To the extent your use for a Permitted Purpose would necessarily infringe our
57
+ patents, the license grant above includes a license under our patents. If you
58
+ make a claim against any party that the Software infringes or contributes to the
59
+ infringement of any patent, then your patent license to the Software ends
60
+ immediately.
61
+
62
+ ### Redistribution
63
+
64
+ The Terms and Conditions apply to all copies, modifications and derivatives of
65
+ the Software.
66
+
67
+ If you redistribute any copies, modifications or derivatives of the Software, you
68
+ must include a copy of or a link to these Terms and Conditions and not remove any
69
+ copyright notices provided in or with the Software.
70
+
71
+ ### Disclaimer
72
+
73
+ THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, INCLUDING
74
+ BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
75
+ PURPOSE, NON-INFRINGEMENT AND TITLE.
76
+
77
+ IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
78
+ SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, EVEN
79
+ IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
80
+
81
+ ### Trademarks
82
+
83
+ Except for displaying the License Details and identifying us as the origin of the
84
+ Software, you have no right under these Terms and Conditions to use our
85
+ trademarks, trade names, service marks or product names.
86
+
87
+ ## Grant of Future License
88
+
89
+ We hereby irrevocably grant you an additional license to use the Software under
90
+ the Apache License, Version 2.0 that is effective on the second anniversary of
91
+ the date we make the Software available. On or after that date, you may use the
92
+ Software under the Apache License, Version 2.0, in which case the following will
93
+ apply:
94
+
95
+ We hereby grant you a license to the Software under the Apache License, Version
96
+ 2.0, a copy of which is available at https://www.apache.org/licenses/LICENSE-2.0.
97
+
98
+ ---
99
+
100
+ ## In plain language (non-binding summary)
101
+
102
+ This is a **Fair Source** license. You can:
103
+
104
+ - **Self-host and run loupfeed** for your own purposes — including inside a
105
+ company, on your own servers or cloud account, for your own products.
106
+ - Read, modify, fork, and redistribute the source.
107
+
108
+ The one thing you may **not** do is offer loupfeed (or something substantially
109
+ similar built from it) **as a competing commercial product or hosted service**.
110
+
111
+ Two years after each release is published, that version automatically becomes
112
+ available to you under the **Apache License 2.0** (fully open source).
113
+
114
+ This summary is for convenience only; the Terms and Conditions above govern.
package/dist/index.cjs ADDED
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ getManifest: () => getManifest,
34
+ loupfeedSveltePreprocess: () => loupfeedSveltePreprocess,
35
+ resetManifest: () => resetManifest
36
+ });
37
+ module.exports = __toCommonJS(index_exports);
38
+ var import_node_path = __toESM(require("path"), 1);
39
+ var import_compiler = require("svelte/compiler");
40
+ var import_build = require("@loupfeed/build");
41
+ var manifest = {};
42
+ function getManifest() {
43
+ return { ...manifest };
44
+ }
45
+ function resetManifest() {
46
+ for (const k of Object.keys(manifest)) delete manifest[k];
47
+ }
48
+ function lineColOf(content, offset) {
49
+ const pre = content.slice(0, offset);
50
+ return { line: pre.split("\n").length, col: offset - (pre.lastIndexOf("\n") + 1) };
51
+ }
52
+ function loupfeedSveltePreprocess(options = {}) {
53
+ const projectRoot = options.projectRoot ?? process.cwd();
54
+ const include = options.include ?? /\.svelte$/;
55
+ const attribute = options.attribute ?? import_build.FB_ATTR;
56
+ return {
57
+ name: "loupfeed",
58
+ markup({ content, filename }) {
59
+ if (filename && !include.test(filename)) return { code: content };
60
+ let ast;
61
+ try {
62
+ ast = (0, import_compiler.parse)(content, { filename, modern: true });
63
+ } catch {
64
+ return { code: content };
65
+ }
66
+ const rel = filename ? import_node_path.default.relative(projectRoot, filename).split(import_node_path.default.sep).join("/") : "unknown";
67
+ const inserts = [];
68
+ const visit = (node) => {
69
+ if (!node || typeof node !== "object") return;
70
+ const n = node;
71
+ if (n.type === "RegularElement" && typeof n.start === "number" && typeof n.name === "string") {
72
+ const tagged = Array.isArray(n.attributes) ? n.attributes.some((a) => a?.type === "Attribute" && a?.name === attribute) : false;
73
+ if (!tagged) {
74
+ const { line, col } = lineColOf(content, n.start);
75
+ const id = (0, import_build.fbId)(rel, line, col);
76
+ manifest[id] = `${rel}:${line}`;
77
+ inserts.push({ offset: n.start + 1 + n.name.length, text: ` ${attribute}="${id}"` });
78
+ }
79
+ }
80
+ const kids = n.fragment?.nodes ?? n.nodes ?? n.children;
81
+ if (Array.isArray(kids)) kids.forEach(visit);
82
+ };
83
+ visit(ast.fragment ?? ast);
84
+ if (inserts.length === 0) return { code: content };
85
+ inserts.sort((a, b) => b.offset - a.offset);
86
+ let code = content;
87
+ for (const ins of inserts) code = code.slice(0, ins.offset) + ins.text + code.slice(ins.offset);
88
+ return { code };
89
+ }
90
+ };
91
+ }
92
+ // Annotate the CommonJS export names for ESM import in node:
93
+ 0 && (module.exports = {
94
+ getManifest,
95
+ loupfeedSveltePreprocess,
96
+ resetManifest
97
+ });
98
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * @loupfeed/svelte-preprocess — a Svelte `markup` preprocessor that injects an\n * opaque `data-fb` attribute onto every element at compile time (the Svelte\n * analogue of the JSX tagger). Wire into svelte.config.js:\n *\n * preprocess: [loupfeedSveltePreprocess()]\n *\n * It parses the template with `svelte/compiler`, finds element nodes, and splices\n * in `data-fb=\"fb<hash>\"`. The source path is recorded only in the manifest\n * (server-side); the compiled component carries only the opaque id.\n */\n\nimport path from 'node:path';\nimport { parse } from 'svelte/compiler';\nimport { FB_ATTR, fbId } from '@loupfeed/build';\n\nexport interface LoupfeedSveltePreprocessOptions {\n projectRoot?: string;\n include?: RegExp;\n attribute?: string;\n}\n\nexport interface MarkupInput {\n content: string;\n filename?: string;\n}\n\nconst manifest: Record<string, string> = {};\n\nexport function getManifest(): Record<string, string> {\n return { ...manifest };\n}\nexport function resetManifest(): void {\n for (const k of Object.keys(manifest)) delete manifest[k];\n}\n\nfunction lineColOf(content: string, offset: number): { line: number; col: number } {\n const pre = content.slice(0, offset);\n return { line: pre.split('\\n').length, col: offset - (pre.lastIndexOf('\\n') + 1) };\n}\n\nexport function loupfeedSveltePreprocess(options: LoupfeedSveltePreprocessOptions = {}) {\n const projectRoot = options.projectRoot ?? process.cwd();\n const include = options.include ?? /\\.svelte$/;\n const attribute = options.attribute ?? FB_ATTR;\n\n return {\n name: 'loupfeed',\n markup({ content, filename }: MarkupInput): { code: string } {\n if (filename && !include.test(filename)) return { code: content };\n\n let ast: { fragment?: { nodes?: unknown[] } };\n try {\n ast = parse(content, { filename, modern: true } as never) as never;\n } catch {\n return { code: content }; // let the Svelte compiler surface the real error\n }\n\n const rel = filename ? path.relative(projectRoot, filename).split(path.sep).join('/') : 'unknown';\n const inserts: Array<{ offset: number; text: string }> = [];\n\n const visit = (node: unknown): void => {\n if (!node || typeof node !== 'object') return;\n const n = node as {\n type?: string;\n name?: string;\n start?: number;\n attributes?: Array<{ type?: string; name?: string }>;\n fragment?: { nodes?: unknown[] };\n nodes?: unknown[];\n children?: unknown[];\n };\n if (n.type === 'RegularElement' && typeof n.start === 'number' && typeof n.name === 'string') {\n const tagged = Array.isArray(n.attributes)\n ? n.attributes.some((a) => a?.type === 'Attribute' && a?.name === attribute)\n : false;\n if (!tagged) {\n const { line, col } = lineColOf(content, n.start);\n const id = fbId(rel, line, col);\n manifest[id] = `${rel}:${line}`;\n inserts.push({ offset: n.start + 1 + n.name.length, text: ` ${attribute}=\"${id}\"` });\n }\n }\n const kids = n.fragment?.nodes ?? n.nodes ?? n.children;\n if (Array.isArray(kids)) kids.forEach(visit);\n };\n\n visit(ast.fragment ?? ast);\n\n if (inserts.length === 0) return { code: content };\n // Apply splices from the end so earlier offsets stay valid.\n inserts.sort((a, b) => b.offset - a.offset);\n let code = content;\n for (const ins of inserts) code = code.slice(0, ins.offset) + ins.text + code.slice(ins.offset);\n return { code };\n },\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAYA,uBAAiB;AACjB,sBAAsB;AACtB,mBAA8B;AAa9B,IAAM,WAAmC,CAAC;AAEnC,SAAS,cAAsC;AACpD,SAAO,EAAE,GAAG,SAAS;AACvB;AACO,SAAS,gBAAsB;AACpC,aAAW,KAAK,OAAO,KAAK,QAAQ,EAAG,QAAO,SAAS,CAAC;AAC1D;AAEA,SAAS,UAAU,SAAiB,QAA+C;AACjF,QAAM,MAAM,QAAQ,MAAM,GAAG,MAAM;AACnC,SAAO,EAAE,MAAM,IAAI,MAAM,IAAI,EAAE,QAAQ,KAAK,UAAU,IAAI,YAAY,IAAI,IAAI,GAAG;AACnF;AAEO,SAAS,yBAAyB,UAA2C,CAAC,GAAG;AACtF,QAAM,cAAc,QAAQ,eAAe,QAAQ,IAAI;AACvD,QAAM,UAAU,QAAQ,WAAW;AACnC,QAAM,YAAY,QAAQ,aAAa;AAEvC,SAAO;AAAA,IACL,MAAM;AAAA,IACN,OAAO,EAAE,SAAS,SAAS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AAM,EAAE,SAAS,EAAE,MAAM;AAC1C,UAAI,OAAO;AACX,iBAAW,OAAO,QAAS,QAAO,KAAK,MAAM,GAAG,IAAI,MAAM,IAAI,IAAI,OAAO,KAAK,MAAM,IAAI,MAAM;AAC9F,aAAO,EAAE,KAAK;AAAA,IAChB;AAAA,EACF;AACF;","names":["path"]}
@@ -0,0 +1,30 @@
1
+ /**
2
+ * @loupfeed/svelte-preprocess — a Svelte `markup` preprocessor that injects an
3
+ * opaque `data-fb` attribute onto every element at compile time (the Svelte
4
+ * analogue of the JSX tagger). Wire into svelte.config.js:
5
+ *
6
+ * preprocess: [loupfeedSveltePreprocess()]
7
+ *
8
+ * It parses the template with `svelte/compiler`, finds element nodes, and splices
9
+ * in `data-fb="fb<hash>"`. The source path is recorded only in the manifest
10
+ * (server-side); the compiled component carries only the opaque id.
11
+ */
12
+ interface LoupfeedSveltePreprocessOptions {
13
+ projectRoot?: string;
14
+ include?: RegExp;
15
+ attribute?: string;
16
+ }
17
+ interface MarkupInput {
18
+ content: string;
19
+ filename?: string;
20
+ }
21
+ declare function getManifest(): Record<string, string>;
22
+ declare function resetManifest(): void;
23
+ declare function loupfeedSveltePreprocess(options?: LoupfeedSveltePreprocessOptions): {
24
+ name: string;
25
+ markup({ content, filename }: MarkupInput): {
26
+ code: string;
27
+ };
28
+ };
29
+
30
+ export { type LoupfeedSveltePreprocessOptions, type MarkupInput, getManifest, loupfeedSveltePreprocess, resetManifest };
@@ -0,0 +1,30 @@
1
+ /**
2
+ * @loupfeed/svelte-preprocess — a Svelte `markup` preprocessor that injects an
3
+ * opaque `data-fb` attribute onto every element at compile time (the Svelte
4
+ * analogue of the JSX tagger). Wire into svelte.config.js:
5
+ *
6
+ * preprocess: [loupfeedSveltePreprocess()]
7
+ *
8
+ * It parses the template with `svelte/compiler`, finds element nodes, and splices
9
+ * in `data-fb="fb<hash>"`. The source path is recorded only in the manifest
10
+ * (server-side); the compiled component carries only the opaque id.
11
+ */
12
+ interface LoupfeedSveltePreprocessOptions {
13
+ projectRoot?: string;
14
+ include?: RegExp;
15
+ attribute?: string;
16
+ }
17
+ interface MarkupInput {
18
+ content: string;
19
+ filename?: string;
20
+ }
21
+ declare function getManifest(): Record<string, string>;
22
+ declare function resetManifest(): void;
23
+ declare function loupfeedSveltePreprocess(options?: LoupfeedSveltePreprocessOptions): {
24
+ name: string;
25
+ markup({ content, filename }: MarkupInput): {
26
+ code: string;
27
+ };
28
+ };
29
+
30
+ export { type LoupfeedSveltePreprocessOptions, type MarkupInput, getManifest, loupfeedSveltePreprocess, resetManifest };
package/dist/index.js ADDED
@@ -0,0 +1,61 @@
1
+ // src/index.ts
2
+ import path from "path";
3
+ import { parse } from "svelte/compiler";
4
+ import { FB_ATTR, fbId } from "@loupfeed/build";
5
+ var manifest = {};
6
+ function getManifest() {
7
+ return { ...manifest };
8
+ }
9
+ function resetManifest() {
10
+ for (const k of Object.keys(manifest)) delete manifest[k];
11
+ }
12
+ function lineColOf(content, offset) {
13
+ const pre = content.slice(0, offset);
14
+ return { line: pre.split("\n").length, col: offset - (pre.lastIndexOf("\n") + 1) };
15
+ }
16
+ function loupfeedSveltePreprocess(options = {}) {
17
+ const projectRoot = options.projectRoot ?? process.cwd();
18
+ const include = options.include ?? /\.svelte$/;
19
+ const attribute = options.attribute ?? FB_ATTR;
20
+ return {
21
+ name: "loupfeed",
22
+ markup({ content, filename }) {
23
+ if (filename && !include.test(filename)) return { code: content };
24
+ let ast;
25
+ try {
26
+ ast = parse(content, { filename, modern: true });
27
+ } catch {
28
+ return { code: content };
29
+ }
30
+ const rel = filename ? path.relative(projectRoot, filename).split(path.sep).join("/") : "unknown";
31
+ const inserts = [];
32
+ const visit = (node) => {
33
+ if (!node || typeof node !== "object") return;
34
+ const n = node;
35
+ if (n.type === "RegularElement" && typeof n.start === "number" && typeof n.name === "string") {
36
+ const tagged = Array.isArray(n.attributes) ? n.attributes.some((a) => a?.type === "Attribute" && a?.name === attribute) : false;
37
+ if (!tagged) {
38
+ const { line, col } = lineColOf(content, n.start);
39
+ const id = fbId(rel, line, col);
40
+ manifest[id] = `${rel}:${line}`;
41
+ inserts.push({ offset: n.start + 1 + n.name.length, text: ` ${attribute}="${id}"` });
42
+ }
43
+ }
44
+ const kids = n.fragment?.nodes ?? n.nodes ?? n.children;
45
+ if (Array.isArray(kids)) kids.forEach(visit);
46
+ };
47
+ visit(ast.fragment ?? ast);
48
+ if (inserts.length === 0) return { code: content };
49
+ inserts.sort((a, b) => b.offset - a.offset);
50
+ let code = content;
51
+ for (const ins of inserts) code = code.slice(0, ins.offset) + ins.text + code.slice(ins.offset);
52
+ return { code };
53
+ }
54
+ };
55
+ }
56
+ export {
57
+ getManifest,
58
+ loupfeedSveltePreprocess,
59
+ resetManifest
60
+ };
61
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * @loupfeed/svelte-preprocess — a Svelte `markup` preprocessor that injects an\n * opaque `data-fb` attribute onto every element at compile time (the Svelte\n * analogue of the JSX tagger). Wire into svelte.config.js:\n *\n * preprocess: [loupfeedSveltePreprocess()]\n *\n * It parses the template with `svelte/compiler`, finds element nodes, and splices\n * in `data-fb=\"fb<hash>\"`. The source path is recorded only in the manifest\n * (server-side); the compiled component carries only the opaque id.\n */\n\nimport path from 'node:path';\nimport { parse } from 'svelte/compiler';\nimport { FB_ATTR, fbId } from '@loupfeed/build';\n\nexport interface LoupfeedSveltePreprocessOptions {\n projectRoot?: string;\n include?: RegExp;\n attribute?: string;\n}\n\nexport interface MarkupInput {\n content: string;\n filename?: string;\n}\n\nconst manifest: Record<string, string> = {};\n\nexport function getManifest(): Record<string, string> {\n return { ...manifest };\n}\nexport function resetManifest(): void {\n for (const k of Object.keys(manifest)) delete manifest[k];\n}\n\nfunction lineColOf(content: string, offset: number): { line: number; col: number } {\n const pre = content.slice(0, offset);\n return { line: pre.split('\\n').length, col: offset - (pre.lastIndexOf('\\n') + 1) };\n}\n\nexport function loupfeedSveltePreprocess(options: LoupfeedSveltePreprocessOptions = {}) {\n const projectRoot = options.projectRoot ?? process.cwd();\n const include = options.include ?? /\\.svelte$/;\n const attribute = options.attribute ?? FB_ATTR;\n\n return {\n name: 'loupfeed',\n markup({ content, filename }: MarkupInput): { code: string } {\n if (filename && !include.test(filename)) return { code: content };\n\n let ast: { fragment?: { nodes?: unknown[] } };\n try {\n ast = parse(content, { filename, modern: true } as never) as never;\n } catch {\n return { code: content }; // let the Svelte compiler surface the real error\n }\n\n const rel = filename ? path.relative(projectRoot, filename).split(path.sep).join('/') : 'unknown';\n const inserts: Array<{ offset: number; text: string }> = [];\n\n const visit = (node: unknown): void => {\n if (!node || typeof node !== 'object') return;\n const n = node as {\n type?: string;\n name?: string;\n start?: number;\n attributes?: Array<{ type?: string; name?: string }>;\n fragment?: { nodes?: unknown[] };\n nodes?: unknown[];\n children?: unknown[];\n };\n if (n.type === 'RegularElement' && typeof n.start === 'number' && typeof n.name === 'string') {\n const tagged = Array.isArray(n.attributes)\n ? n.attributes.some((a) => a?.type === 'Attribute' && a?.name === attribute)\n : false;\n if (!tagged) {\n const { line, col } = lineColOf(content, n.start);\n const id = fbId(rel, line, col);\n manifest[id] = `${rel}:${line}`;\n inserts.push({ offset: n.start + 1 + n.name.length, text: ` ${attribute}=\"${id}\"` });\n }\n }\n const kids = n.fragment?.nodes ?? n.nodes ?? n.children;\n if (Array.isArray(kids)) kids.forEach(visit);\n };\n\n visit(ast.fragment ?? ast);\n\n if (inserts.length === 0) return { code: content };\n // Apply splices from the end so earlier offsets stay valid.\n inserts.sort((a, b) => b.offset - a.offset);\n let code = content;\n for (const ins of inserts) code = code.slice(0, ins.offset) + ins.text + code.slice(ins.offset);\n return { code };\n },\n };\n}\n"],"mappings":";AAYA,OAAO,UAAU;AACjB,SAAS,aAAa;AACtB,SAAS,SAAS,YAAY;AAa9B,IAAM,WAAmC,CAAC;AAEnC,SAAS,cAAsC;AACpD,SAAO,EAAE,GAAG,SAAS;A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AK,KAAK,KAAK,MAAM,GAAG;AAC9B,qBAAS,EAAE,IAAI,GAAG,GAAG,IAAI,IAAI;AAC7B,oBAAQ,KAAK,EAAE,QAAQ,EAAE,QAAQ,IAAI,EAAE,KAAK,QAAQ,MAAM,IAAI,SAAS,KAAK,EAAE,IAAI,CAAC;AAAA,UACrF;AAAA,QACF;AACA,cAAM,OAAO,EAAE,UAAU,SAAS,EAAE,SAAS,EAAE;AAC/C,YAAI,MAAM,QAAQ,IAAI,EAAG,MAAK,QAAQ,KAAK;AAAA,MAC7C;AAEA,YAAM,IAAI,YAAY,GAAG;AAEzB,UAAI,QAAQ,WAAW,EAAG,QAAO,EAAE,MAAM,QAAQ;AAEjD,cAAQ,KAAK,CAAC,GAAG,MAAM,EAAE,SAAS,EAAE,MAAM;AAC1C,UAAI,OAAO;AACX,iBAAW,OAAO,QAAS,QAAO,KAAK,MAAM,GAAG,IAAI,MAAM,IAAI,IAAI,OAAO,KAAK,MAAM,IAAI,MAAM;AAC9F,aAAO,EAAE,KAAK;AAAA,IAChB;AAAA,EACF;AACF;","names":[]}
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@loupfeed/svelte-preprocess",
3
+ "version": "0.1.0",
4
+ "description": "loupfeed Svelte preprocessor — markup tagger that injects opaque data-fb attributes on element nodes at compile time.",
5
+ "license": "FSL-1.1-ALv2",
6
+ "homepage": "https://loupfeed.com",
7
+ "type": "module",
8
+ "sideEffects": false,
9
+ "main": "./dist/index.cjs",
10
+ "module": "./dist/index.js",
11
+ "types": "./dist/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "import": "./dist/index.js",
16
+ "require": "./dist/index.cjs"
17
+ },
18
+ "./package.json": "./package.json"
19
+ },
20
+ "files": [
21
+ "dist",
22
+ "README.md"
23
+ ],
24
+ "publishConfig": {
25
+ "access": "public"
26
+ },
27
+ "dependencies": {
28
+ "@loupfeed/build": "0.1.0"
29
+ },
30
+ "peerDependencies": {
31
+ "svelte": "^4.0.0 || ^5.0.0"
32
+ },
33
+ "devDependencies": {
34
+ "@types/node": "^22.10.2",
35
+ "svelte": "^5.15.0",
36
+ "tsup": "^8.3.5",
37
+ "typescript": "~5.7.2",
38
+ "vitest": "^2.1.8"
39
+ },
40
+ "scripts": {
41
+ "build": "tsup",
42
+ "dev": "tsup --watch",
43
+ "typecheck": "tsc --noEmit",
44
+ "test": "vitest run",
45
+ "clean": "rm -rf dist .turbo"
46
+ }
47
+ }