svelte-adapter-uws 0.2.3 → 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 +32 -4
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -87,11 +87,15 @@ export default function (opts = {}) {
87
87
  }
88
88
 
89
89
  if (handlerFile) {
90
- // Bundle through esbuild to resolve SvelteKit aliases ($lib etc.)
91
- // and handle TypeScript. Without this, $lib imports survive into
92
- // the Rollup step which doesn't know about SvelteKit aliases.
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
93
  const esbuild = await import('esbuild');
94
+ const { loadEnv } = await import('vite');
94
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 ?? '';
95
99
 
96
100
  await esbuild.build({
97
101
  entryPoints: [path.resolve(handlerFile)],
@@ -100,7 +104,31 @@ export default function (opts = {}) {
100
104
  platform: 'node',
101
105
  outfile: `${tmp}/ws-handler.js`,
102
106
  alias: { '$lib': libDir },
103
- packages: 'external'
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
+ }]
104
132
  });
105
133
  builder.log.minor(`WebSocket handler: ${handlerFile}`);
106
134
  } else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-adapter-uws",
3
- "version": "0.2.3",
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",