@graphox/swc-plugin 0.2.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/README.md ADDED
@@ -0,0 +1,160 @@
1
+ # @graphox/swc-plugin
2
+
3
+ ## Overview
4
+
5
+ Pre-built SWC plugin for Graphox codesplitting. This package bundles the WASM binary for easy use with rsbuild, Turbopack, or native SWC.
6
+
7
+ ## Prerequisites
8
+
9
+ Building the WASM plugin from source requires:
10
+
11
+ - **Rust toolchain** (1.70+): `rustup install stable`
12
+ - **WASM target**: `rustup target add wasm32-wasip1`
13
+ - **wasm-pack**: `cargo install wasm-pack`
14
+ - **Node.js** 18+
15
+ - **pnpm**: `corepack enable && corepack install -g pnpm@latest`
16
+
17
+ For users of the pre-built package, only Node.js 18+ is required.
18
+
19
+ ## Installation
20
+
21
+ ```bash
22
+ pnpm add @graphox/swc-plugin
23
+ ```
24
+
25
+ ## Requirements
26
+
27
+ - Node.js 18+
28
+ - rsbuild, Turbopack, or native SWC
29
+
30
+ ## Usage
31
+
32
+ ### rsbuild Configuration
33
+
34
+ ```typescript
35
+ // rsbuild.config.ts
36
+ import { defineConfig } from '@rsbuild/core';
37
+ import { createSWCPlugin } from '@graphox/swc-plugin';
38
+ import path from 'path';
39
+
40
+ export default defineConfig({
41
+ source: {
42
+ alias: {
43
+ '__generated__': path.resolve(__dirname, './__generated__'),
44
+ },
45
+ },
46
+ tools: {
47
+ swc: {
48
+ jsc: {
49
+ parser: {
50
+ syntax: 'typescript',
51
+ tsx: true,
52
+ },
53
+ experimental: {
54
+ plugins: [
55
+ createSWCPlugin({
56
+ manifestPath: './__generated__/manifest.json',
57
+ outputDir: './__generated__'
58
+ })
59
+ ],
60
+ },
61
+ },
62
+ },
63
+ },
64
+ });
65
+ ```
66
+
67
+ ### Turbopack/Next.js Configuration
68
+
69
+ ```javascript
70
+ // next.config.js
71
+ import { createSWCPlugin } from '@graphox/swc-plugin';
72
+
73
+ /** @type {import('next').NextConfig} */
74
+ const nextConfig = {
75
+ experimental: {
76
+ turbo: {
77
+ rules: {
78
+ '*.{ts,tsx}': [
79
+ {
80
+ loader: 'next-swc-loader',
81
+ options: {
82
+ jsc: {
83
+ parser: {
84
+ syntax: 'typescript',
85
+ tsx: true,
86
+ },
87
+ experimental: {
88
+ plugins: [
89
+ createSWCPlugin({
90
+ manifestPath: './__generated__/manifest.json',
91
+ outputDir: './__generated__'
92
+ })
93
+ ],
94
+ },
95
+ },
96
+ },
97
+ },
98
+ ],
99
+ },
100
+ },
101
+ },
102
+ };
103
+
104
+ module.exports = nextConfig;
105
+ ```
106
+
107
+ ## Configuration Options
108
+
109
+ | Option | Type | Required | Description |
110
+ |--------|------|----------|-------------|
111
+ | `manifestPath` | `string` | Yes* | Path to `manifest.json` generated by codegen |
112
+ | `manifestData` | `object[]` | Yes* | Inline manifest data (alternative to `manifestPath`) |
113
+ | `outputDir` | `string` | Yes | Directory containing generated files |
114
+ | `graphqlImportPaths` | `string[]` | No | Explicit import paths to treat as GraphQL entrypoints |
115
+ | `emitExtensions` | `string` | No | File extension for generated imports: `"none"` (default), `"ts"`, `"js"`, `"dts"` |
116
+
117
+ *Either `manifestPath` or `manifestData` is required.
118
+
119
+ ### emitExtensions
120
+
121
+ Controls the file extension appended to generated import paths. Should match the `emit_extensions` setting in your `graphox.yaml`:
122
+
123
+ | Value | Result |
124
+ |-------|--------|
125
+ | `"none"` (default) | `import { X } from "./file.codegen"` |
126
+ | `"ts"` | `import { X } from "./file.codegen.ts"` |
127
+ | `"js"` | `import { X } from "./file.codegen.js"` |
128
+ | `"dts"` | `import { X } from "./file.codegen.d.ts"` |
129
+
130
+ ## Fragment Documents
131
+
132
+ When `generate_ast_for_fragments: true` is enabled in your config, fragment documents are also included in the manifest and will be properly rewritten by the plugin.
133
+
134
+ ## Building from Source
135
+
136
+ If you need to rebuild the WASM plugin:
137
+
138
+ ```bash
139
+ # Install dependencies
140
+ pnpm install
141
+
142
+ # Build TypeScript only (requires pre-built WASM)
143
+ pnpm run build
144
+
145
+ # Build WASM only (requires Rust toolchain)
146
+ pnpm run build:wasm
147
+
148
+ # Build everything (TypeScript + WASM)
149
+ pnpm run build:all
150
+
151
+ # Run tests
152
+ pnpm test
153
+ ```
154
+
155
+ ## See Also
156
+
157
+ - [Babel Plugin](../babel/README.md)
158
+ - [graphox CLI](../../README.md)
159
+ - [Configuration Guide](../../docs/configurations.md)
160
+ - [Plugin Development Guide](../../../docs/plugin-development.md)
@@ -0,0 +1,87 @@
1
+ /**
2
+ * SWC GraphQL Plugin - Node.js WASM Wrapper
3
+ *
4
+ * This package provides a convenient Node.js interface to the SWC GraphQL plugin.
5
+ * The actual transformation logic is implemented in Rust and compiled to WASM.
6
+ *
7
+ * @example
8
+ * // rsbuild.config.ts
9
+ * import { defineConfig } from '@rsbuild/core';
10
+ * import { createSWCPlugin } from '@graphox/swc-plugin';
11
+ *
12
+ * export default defineConfig({
13
+ * tools: {
14
+ * swc: {
15
+ * jsc: {
16
+ * experimental: {
17
+ * plugins: [
18
+ * createSWCPlugin({
19
+ * manifestPath: './__generated__/manifest.json',
20
+ * outputDir: './__generated__'
21
+ * })
22
+ * ]
23
+ * }
24
+ * }
25
+ * }
26
+ * }
27
+ * });
28
+ */
29
+ /**
30
+ * Configuration options for the SWC GraphQL plugin.
31
+ */
32
+ export interface PluginConfig {
33
+ /**
34
+ * Path to the manifest.json file generated by graphox codegen.
35
+ * The manifest maps GraphQL source code to generated document files.
36
+ */
37
+ manifestPath?: string;
38
+ /**
39
+ * Inline manifest data. Can be used instead of manifestPath.
40
+ * Takes precedence over manifestPath if both are provided.
41
+ */
42
+ manifestData?: Array<{
43
+ source: string;
44
+ path: string;
45
+ name: string;
46
+ }>;
47
+ /**
48
+ * Directory where generated files are located.
49
+ * Used for resolving relative paths and detecting imports.
50
+ */
51
+ outputDir: string;
52
+ /**
53
+ * Additional import paths to treat as GraphQL entrypoints.
54
+ * Useful for path aliases like '@/graphql'.
55
+ */
56
+ graphqlImportPaths?: string[];
57
+ /**
58
+ * File extension to append to generated import paths.
59
+ * Options: "Ts", "Dts", "Js" or None (default)
60
+ */
61
+ emitExtensions?: 'Ts' | 'Dts' | 'Js' | 'None';
62
+ }
63
+ /**
64
+ * Load the manifest from disk or return inline data.
65
+ *
66
+ * @param config - Plugin configuration
67
+ * @returns Parsed manifest entries
68
+ */
69
+ export declare function loadManifest(config: PluginConfig): Array<{
70
+ source: string;
71
+ path: string;
72
+ name: string;
73
+ }>;
74
+ /**
75
+ * Create a SWC plugin configuration.
76
+ *
77
+ * Returns a tuple [wasmPath, options] that can be passed directly to SWC's
78
+ * experimental plugins configuration.
79
+ */
80
+ export declare function createSWCPlugin(config: PluginConfig): [string, PluginConfig];
81
+ /**
82
+ * Validate that the WASM plugin is available.
83
+ *
84
+ * @returns true if WASM is built and available
85
+ */
86
+ export declare function isWasmAvailable(): boolean;
87
+ export default createSWCPlugin;
package/dist/index.js ADDED
@@ -0,0 +1,99 @@
1
+ /**
2
+ * SWC GraphQL Plugin - Node.js WASM Wrapper
3
+ *
4
+ * This package provides a convenient Node.js interface to the SWC GraphQL plugin.
5
+ * The actual transformation logic is implemented in Rust and compiled to WASM.
6
+ *
7
+ * @example
8
+ * // rsbuild.config.ts
9
+ * import { defineConfig } from '@rsbuild/core';
10
+ * import { createSWCPlugin } from '@graphox/swc-plugin';
11
+ *
12
+ * export default defineConfig({
13
+ * tools: {
14
+ * swc: {
15
+ * jsc: {
16
+ * experimental: {
17
+ * plugins: [
18
+ * createSWCPlugin({
19
+ * manifestPath: './__generated__/manifest.json',
20
+ * outputDir: './__generated__'
21
+ * })
22
+ * ]
23
+ * }
24
+ * }
25
+ * }
26
+ * }
27
+ * });
28
+ */
29
+ import * as path from 'path';
30
+ import * as fs from 'fs';
31
+ /**
32
+ * Get the path to the WASM plugin.
33
+ *
34
+ * The WASM file is built from the Rust source in ../rust/
35
+ * and bundled with this package.
36
+ */
37
+ function getWasmPath() {
38
+ // The WASM file should be in the wasm/ directory after building
39
+ const wasmPath = path.join(__dirname, '..', 'wasm', 'graphox_swc_plugin.wasm');
40
+ if (!fs.existsSync(wasmPath)) {
41
+ throw new Error(`WASM plugin not found at ${wasmPath}. ` +
42
+ 'Run "pnpm run build:wasm" to build the plugin.');
43
+ }
44
+ return wasmPath;
45
+ }
46
+ /**
47
+ * Load the manifest from disk or return inline data.
48
+ *
49
+ * @param config - Plugin configuration
50
+ * @returns Parsed manifest entries
51
+ */
52
+ export function loadManifest(config) {
53
+ if (config.manifestData) {
54
+ return config.manifestData;
55
+ }
56
+ if (config.manifestPath && fs.existsSync(config.manifestPath)) {
57
+ const content = fs.readFileSync(config.manifestPath, 'utf-8');
58
+ const data = JSON.parse(content);
59
+ return Array.isArray(data) ? data : (data.entries || []);
60
+ }
61
+ return [];
62
+ }
63
+ /**
64
+ * Create a SWC plugin configuration.
65
+ *
66
+ * Returns a tuple [wasmPath, options] that can be passed directly to SWC's
67
+ * experimental plugins configuration.
68
+ */
69
+ export function createSWCPlugin(config) {
70
+ // Validate required fields first
71
+ if (!config.outputDir) {
72
+ throw new Error('outputDir is required in PluginConfig');
73
+ }
74
+ const wasmPath = getWasmPath();
75
+ // Inline manifest data to ensure it's available to the WASM plugin
76
+ // since WASM plugins may not have filesystem access.
77
+ const inlinedConfig = {
78
+ ...config,
79
+ manifestData: loadManifest(config)
80
+ };
81
+ // Return tuple for SWC
82
+ return [wasmPath, inlinedConfig];
83
+ }
84
+ /**
85
+ * Validate that the WASM plugin is available.
86
+ *
87
+ * @returns true if WASM is built and available
88
+ */
89
+ export function isWasmAvailable() {
90
+ try {
91
+ getWasmPath();
92
+ return true;
93
+ }
94
+ catch {
95
+ return false;
96
+ }
97
+ }
98
+ // Default export
99
+ export default createSWCPlugin;
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@graphox/swc-plugin",
3
+ "version": "0.2.0",
4
+ "description": "SWC plugin for GraphQL Rust codesplitting",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist/",
9
+ "wasm/",
10
+ "README.md"
11
+ ],
12
+ "keywords": [
13
+ "graphql",
14
+ "swc",
15
+ "plugin",
16
+ "codegen",
17
+ "codesplitting"
18
+ ],
19
+ "engines": {
20
+ "node": ">=18.0.0"
21
+ },
22
+ "devDependencies": {
23
+ "@types/node": "^20.0.0",
24
+ "typescript": "^5.0.0",
25
+ "vitest": "^1.0.0"
26
+ },
27
+ "publishConfig": {
28
+ "access": "public"
29
+ },
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "https://github.com/soundtrackyourbrand/graphox",
33
+ "directory": "plugins/swc/node"
34
+ },
35
+ "license": "UNLICENSED",
36
+ "scripts": {
37
+ "build": "tsc",
38
+ "build:wasm": "wasm-pack build ../rust --target nodejs --out-dir $PWD/wasm --no-pack",
39
+ "build:all": "pnpm run build:wasm && pnpm run build",
40
+ "test": "vitest run"
41
+ }
42
+ }