Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment API experiments #1

Open
wants to merge 63 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
9a8587f
Created vite-node-vm-environment playground
Aug 6, 2024
8edc12a
Created vite-workerd-environment playground
Aug 20, 2024
558f42b
Returned response from workerd environment
Aug 20, 2024
aa1679e
Added node module runner and entrypoint
Aug 22, 2024
dde5922
Initial POC for running inside module runner
Aug 23, 2024
87b7f9f
Revised manifest routes
Aug 27, 2024
e9e61e1
Revised manifest nodes
Aug 27, 2024
5889410
Used pathe for path resolution
Aug 28, 2024
0e61853
Fixed support for endpoints
Aug 28, 2024
6313516
Added links to playground example
Aug 28, 2024
04efeec
Removed commented code
Aug 28, 2024
47d0379
Used Cloudflare plugin to run in workerd environment
Aug 28, 2024
7dd8ced
Added Cloudflare platform context
Aug 28, 2024
483d137
Added cloudflare properties route to workerd playground
Aug 28, 2024
97489b2
Added KV example
Aug 28, 2024
e50f543
Added matchers example to workerd playground
Aug 28, 2024
c7484d0
Added layout and improved playground example
Aug 29, 2024
a356404
Commented out unused code and added invalidation for environment_cont…
Aug 29, 2024
63c51f3
Added cloudflare types
Aug 29, 2024
ceb2a09
Added additional types
Aug 29, 2024
8bca02f
Added kit.ssrEnvironment property to svelte config and created defaul…
Aug 29, 2024
665fb76
Used runtime_base to resolve import in virtual module
Aug 30, 2024
2ebda30
Simplified Cloudflare type imports
Aug 30, 2024
954ad1b
Merge upstream main
Aug 30, 2024
b7b36e1
Resolved typescript errors
Aug 30, 2024
d00737b
Asserted platform types in playground
Aug 30, 2024
c0a478c
Updated vite to alpha.23
Aug 30, 2024
2438651
Changed to environments option in svelte config
Aug 30, 2024
ece5364
Started adding original implementation back in
Aug 30, 2024
113418a
Fixed error and improved node playground
Aug 30, 2024
0c5c6c3
Updated config test
Aug 31, 2024
699aa8b
Export Server class to use in entrypoint
Aug 31, 2024
d087bbf
Tidied up virtual module code
Aug 31, 2024
afdbcdc
Imported default environment in vite-node-environment playground
Aug 31, 2024
b5a6c00
Reverted exporting Server
Aug 31, 2024
be9d6a0
Added comments
Sep 1, 2024
5f66708
Replaced cloudflare environment provider dependency
Sep 2, 2024
a1b19cb
Renamed vite-workerd-environment to vite-cloudflare-environment
Sep 2, 2024
71145cb
Changed playgrounds for TS to JS
Sep 2, 2024
10c4ddd
Added new default node environment and resolved test failures related…
jamesopstad Sep 20, 2024
660a724
merge
Rich-Harris Sep 21, 2024
4544b5e
remove kit-level environments option
Rich-Harris Sep 21, 2024
c44db0c
regenerate
Rich-Harris Sep 21, 2024
4db8a10
remove environments option
Rich-Harris Sep 21, 2024
cafdb6c
always render in DevEnvironment
Rich-Harris Sep 21, 2024
a7f671a
unused
Rich-Harris Sep 21, 2024
8355305
replace pathe
Rich-Harris Sep 21, 2024
d6f02a5
unused
Rich-Harris Sep 21, 2024
f012685
remove unused stuff
Rich-Harris Sep 21, 2024
814b240
note to self
Rich-Harris Sep 21, 2024
a0f32dd
update lockfile
Rich-Harris Sep 21, 2024
5d6cd36
fix
Rich-Harris Sep 22, 2024
7c5ba6e
remove EnvironmentContext, i don't think we'll need it
Rich-Harris Sep 22, 2024
9202703
reduce indirection
Rich-Harris Sep 22, 2024
16e4a78
select environment based on route (WIP)
Rich-Harris Sep 22, 2024
16711be
remove some more environment_context remnants
Rich-Harris Sep 22, 2024
825707e
colocate all the plugin stuff
Rich-Harris Sep 22, 2024
c777b0f
deprecate emulate
Rich-Harris Sep 22, 2024
b518ae6
per route environments
Rich-Harris Sep 22, 2024
40e7e03
on second thoughts, read the pragmas lazily
Rich-Harris Sep 22, 2024
64270ff
revert
Rich-Harris Sep 22, 2024
8c07a6f
note to self
Rich-Harris Sep 22, 2024
1c5212a
deprecate stuff
Rich-Harris Sep 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
"playwright": "^1.44.1",
"typescript-eslint": "^8.0.0"
},
"pnpm": {
"overrides": {
"vite": "6.0.0-beta.1"
}
},
"packageManager": "[email protected]",
"engines": {
"pnpm": "^9.0.0"
Expand Down
2 changes: 2 additions & 0 deletions packages/kit/src/exports/public.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export interface Adapter {
/**
* Creates an `Emulator`, which allows the adapter to influence the environment
* during dev, build and prerendering
* @deprecated
*/
emulate?(): MaybePromise<Emulator>;
}
Expand Down Expand Up @@ -268,6 +269,7 @@ export interface Cookies {

/**
* A collection of functions that influence the environment during dev, build and prerendering
* @deprecated
*/
export interface Emulator {
/**
Expand Down
34 changes: 34 additions & 0 deletions packages/kit/src/exports/vite/dev/cloudflare_entrypoint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// This should be exported from @sveltejs/kit so that the path isn't relative
import { Server } from '../../../runtime/server/index.js';

export default {
/**
* This fetch handler is the entrypoint for the environment.
* @param {Request & { cf: any }} request
* @param {any} env
* @param {any} context
*/
fetch: async (request, env, context) => {
const environment_context = await import('__sveltekit/environment_context');
const server = new Server(environment_context.manifest);

await server.init({
env: environment_context.env
});

return server.respond(request, {
getClientAddress: () => {
if (environment_context.remote_address) return environment_context.remote_address;
throw new Error('Could not determine clientAddress');
},
// We can provide the platform properties directly as the code is executed in a workerd environment.
// TODO we should probably deprecate both `platform` and `emulate`
platform: {
env,
cf: request.cf,
context,
caches
}
});
}
};
69 changes: 69 additions & 0 deletions packages/kit/src/exports/vite/dev/default_environment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import {
DevEnvironment,
BuildEnvironment,
createServerHotChannel,
createServerModuleRunner
} from 'vite';

export const AsyncFunction = /** @type {typeof Function} */ (async function () {}.constructor);

export const asyncFunctionDeclarationPaddingLineCount = /** #__PURE__ */ (() => {
const body = '/*code*/';
const source = new AsyncFunction('a', 'b', body).toString();
return source.slice(0, source.indexOf(body)).split('\n').length - 1;
})();

class NodeDevEnvironment extends DevEnvironment {
/** @type {{ entrypoint: string }} */
#options;

/** @type {import('vite/module-runner').ModuleRunner} */
#runner;

/**
* @param {string} name
* @param {import('vite').ResolvedConfig} config
* @param {{ entrypoint: string }} options
*/
constructor(name, config, options) {
super(name, config, {
hot: createServerHotChannel(),
runner: {
processSourceMap(map) {
// this assumes that "new AsyncFunction" is used to create the module
return Object.assign({}, map, {
mappings: ';'.repeat(asyncFunctionDeclarationPaddingLineCount) + map.mappings
});
}
}
});

this.#options = options;
this.#runner = createServerModuleRunner(this);
}

/** @param {Request} request */
async dispatchFetch(request) {
const entrypoint = await this.#runner.import(this.#options.entrypoint);
return entrypoint.default.fetch(request);
}
}

/**
* @param {{ entrypoint: string }} options
* @returns {import('vite').EnvironmentOptions}
*/
export function createNodeEnvironment(options) {
return {
dev: {
createEnvironment(name, config) {
return new NodeDevEnvironment(name, config, options);
}
},
build: {
createEnvironment(name, config) {
return new BuildEnvironment(name, config);
}
}
};
}
Loading
Loading