@macroforge/svelte-preprocessor 0.1.18

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.
@@ -0,0 +1,41 @@
1
+ import type { PreprocessorGroup } from "svelte/compiler";
2
+ /**
3
+ * Options for the macroforge Svelte preprocessor
4
+ */
5
+ export interface MacroforgePreprocessorOptions {
6
+ /**
7
+ * Whether to keep @derive decorators in the output.
8
+ * Useful for debugging or when you need to see the decorators in generated code.
9
+ * @default false
10
+ */
11
+ keepDecorators?: boolean;
12
+ /**
13
+ * Whether to process JavaScript files (not just TypeScript).
14
+ * By default, only `<script lang="ts">` blocks are processed.
15
+ * @default false
16
+ */
17
+ processJavaScript?: boolean;
18
+ }
19
+ /**
20
+ * Creates a Svelte preprocessor that expands macroforge macros in `<script>` blocks.
21
+ *
22
+ * This preprocessor should be placed BEFORE other preprocessors like `vitePreprocess()`
23
+ * to ensure macros are expanded before TypeScript compilation.
24
+ *
25
+ * @example
26
+ * ```js
27
+ * // svelte.config.js
28
+ * import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
29
+ * import { macroforgePreprocess } from '@macroforge/svelte-preprocessor';
30
+ *
31
+ * export default {
32
+ * preprocess: [
33
+ * macroforgePreprocess(), // Expand macros FIRST
34
+ * vitePreprocess() // Then handle TypeScript/CSS
35
+ * ]
36
+ * };
37
+ * ```
38
+ */
39
+ export declare function macroforgePreprocess(options?: MacroforgePreprocessorOptions): PreprocessorGroup;
40
+ export default macroforgePreprocess;
41
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAgB,MAAM,iBAAiB,CAAC;AA2CvE;;GAEG;AACH,MAAM,WAAW,6BAA6B;IAC5C;;;;OAIG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;IAEzB;;;;OAIG;IACH,iBAAiB,CAAC,EAAE,OAAO,CAAC;CAC7B;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,oBAAoB,CAClC,OAAO,GAAE,6BAAkC,GAC1C,iBAAiB,CAsEnB;AAED,eAAe,oBAAoB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,88 @@
1
+ // Dynamic import to handle native binding loading
2
+ let expandSync = null;
3
+ async function ensureExpandSync() {
4
+ if (expandSync === null) {
5
+ try {
6
+ const macroforge = await import("macroforge");
7
+ expandSync = macroforge.expandSync;
8
+ }
9
+ catch (error) {
10
+ console.warn("[@macroforge/svelte-preprocessor] Failed to load macroforge native bindings:", error);
11
+ expandSync = null;
12
+ }
13
+ }
14
+ return expandSync;
15
+ }
16
+ /**
17
+ * Creates a Svelte preprocessor that expands macroforge macros in `<script>` blocks.
18
+ *
19
+ * This preprocessor should be placed BEFORE other preprocessors like `vitePreprocess()`
20
+ * to ensure macros are expanded before TypeScript compilation.
21
+ *
22
+ * @example
23
+ * ```js
24
+ * // svelte.config.js
25
+ * import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
26
+ * import { macroforgePreprocess } from '@macroforge/svelte-preprocessor';
27
+ *
28
+ * export default {
29
+ * preprocess: [
30
+ * macroforgePreprocess(), // Expand macros FIRST
31
+ * vitePreprocess() // Then handle TypeScript/CSS
32
+ * ]
33
+ * };
34
+ * ```
35
+ */
36
+ export function macroforgePreprocess(options = {}) {
37
+ const { keepDecorators = false, processJavaScript = false } = options;
38
+ const scriptPreprocessor = async ({ content, filename, attributes, }) => {
39
+ // Check if we should process this script block
40
+ const lang = attributes.lang || attributes.type;
41
+ const isTypeScript = lang === "ts" || lang === "typescript";
42
+ const isJavaScript = !lang || lang === "js" || lang === "javascript" || lang === "module";
43
+ if (!isTypeScript && !(processJavaScript && isJavaScript)) {
44
+ return; // Return undefined = no changes
45
+ }
46
+ // Early bailout: no macros to process
47
+ if (!content.includes("@derive")) {
48
+ return;
49
+ }
50
+ // Ensure native bindings are loaded
51
+ const expand = await ensureExpandSync();
52
+ if (!expand) {
53
+ // Native bindings not available - skip silently
54
+ return;
55
+ }
56
+ try {
57
+ const result = expand(content, filename || "component.svelte", {
58
+ keepDecorators,
59
+ });
60
+ // Report any diagnostics
61
+ for (const diag of result.diagnostics) {
62
+ if (diag.level === "error") {
63
+ console.error(`[@macroforge/svelte-preprocessor] Error in ${filename}: ${diag.message}`);
64
+ }
65
+ else if (diag.level === "warning") {
66
+ console.warn(`[@macroforge/svelte-preprocessor] Warning in ${filename}: ${diag.message}`);
67
+ }
68
+ }
69
+ // Only return if code was actually changed
70
+ if (result.code && result.code !== content) {
71
+ return {
72
+ code: result.code,
73
+ // TODO: Add source map support when expandSync provides it
74
+ };
75
+ }
76
+ }
77
+ catch (error) {
78
+ // Log warning but don't fail - let Svelte continue
79
+ console.warn(`[@macroforge/svelte-preprocessor] Failed to expand macros in ${filename}:`, error instanceof Error ? error.message : String(error));
80
+ }
81
+ return; // No changes
82
+ };
83
+ return {
84
+ name: "macroforge",
85
+ script: scriptPreprocessor,
86
+ };
87
+ }
88
+ export default macroforgePreprocess;
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@macroforge/svelte-preprocessor",
3
+ "version": "0.1.18",
4
+ "type": "module",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/rymskip/macroforge-ts.git"
13
+ },
14
+ "scripts": {
15
+ "build": "tsc -p tsconfig.json",
16
+ "clean": "rm -rf dist",
17
+ "test": "npm run build && node --test tests/**/*.test.js"
18
+ },
19
+ "dependencies": {
20
+ "macroforge": "^0.1.18"
21
+ },
22
+ "peerDependencies": {
23
+ "svelte": "^5.0.0"
24
+ },
25
+ "devDependencies": {
26
+ "svelte": "^5.43.8",
27
+ "typescript": "^5.9.3"
28
+ }
29
+ }