svelte-adapter-uws 0.2.2 → 0.2.4

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.
Files changed (2) hide show
  1. package/index.js +43 -18
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -87,24 +87,49 @@ export default function (opts = {}) {
87
87
  }
88
88
 
89
89
  if (handlerFile) {
90
- if (handlerFile.endsWith('.ts')) {
91
- // TypeScript needs transpilation - Rollup can't handle .ts natively.
92
- // esbuild is always available (transitive dep via vite -> @sveltejs/kit).
93
- const { transform } = await import('esbuild');
94
- const tsSource = readFileSync(handlerFile, 'utf8');
95
- const { code } = await transform(tsSource, {
96
- loader: 'ts',
97
- format: 'esm',
98
- sourcefile: handlerFile
99
- });
100
- writeFileSync(`${tmp}/ws-handler.js`, code);
101
- } else {
102
- const handlerPath = path.resolve(handlerFile).replace(/\\/g, '/');
103
- writeFileSync(
104
- `${tmp}/ws-handler.js`,
105
- `export * from '${handlerPath}';\n`
106
- );
107
- }
90
+ // Bundle through esbuild to resolve SvelteKit aliases ($lib, $env, $app)
91
+ // and handle TypeScript. Without this, these imports survive into
92
+ // the Rollup step which doesn't know about SvelteKit virtual modules.
93
+ const esbuild = await import('esbuild');
94
+ const { loadEnv } = await import('vite');
95
+ const libDir = path.resolve(builder.config.kit.files?.lib || 'src/lib');
96
+ const publicPrefix = builder.config.kit.env?.publicPrefix ?? 'PUBLIC_';
97
+ const allEnv = loadEnv('production', process.cwd(), '');
98
+ const version = builder.config.kit.version?.name ?? '';
99
+
100
+ await esbuild.build({
101
+ entryPoints: [path.resolve(handlerFile)],
102
+ bundle: true,
103
+ format: 'esm',
104
+ platform: 'node',
105
+ outfile: `${tmp}/ws-handler.js`,
106
+ alias: { '$lib': libDir },
107
+ packages: 'external',
108
+ plugins: [{
109
+ name: 'sveltekit-virtual-modules',
110
+ setup(build) {
111
+ build.onResolve({ filter: /^\$(env|app)\// }, (args) => ({
112
+ path: args.path,
113
+ namespace: 'sveltekit'
114
+ }));
115
+ build.onLoad({ filter: /.*/, namespace: 'sveltekit' }, (args) => {
116
+ if (args.path === '$app/environment') {
117
+ return { contents: `export const dev = false;\nexport const building = false;\nexport const version = ${JSON.stringify(version)};` };
118
+ }
119
+ const isPublic = args.path.includes('/public');
120
+ const isStatic = args.path.includes('/static');
121
+ const entries = Object.entries(allEnv).filter(([k]) =>
122
+ isPublic ? k.startsWith(publicPrefix) : !k.startsWith(publicPrefix)
123
+ );
124
+ if (isStatic) {
125
+ return { contents: entries.map(([k, v]) => `export const ${k} = ${JSON.stringify(v)};`).join('\n') || 'export {};' };
126
+ }
127
+ // dynamic: read from process.env at runtime
128
+ return { contents: entries.map(([k]) => `export const ${k} = process.env[${JSON.stringify(k)}];`).join('\n') || 'export {};' };
129
+ });
130
+ }
131
+ }]
132
+ });
108
133
  builder.log.minor(`WebSocket handler: ${handlerFile}`);
109
134
  } else {
110
135
  // No handler found - use built-in default (subscribe/unsubscribe only)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-adapter-uws",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "SvelteKit adapter for uWebSockets.js - high-performance C++ HTTP server with built-in WebSocket support",
5
5
  "author": "Kevin Radziszewski",
6
6
  "license": "MIT",