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

add out option to adapter-node, and add types for adapters #531

Merged
merged 4 commits into from Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/green-spoons-count.md
@@ -0,0 +1,6 @@
---
'@sveltejs/adapter-node': patch
'@sveltejs/kit': patch
---

Add options to adapter-node, and add adapter types
19 changes: 13 additions & 6 deletions packages/adapter-node/index.js
Expand Up @@ -2,26 +2,33 @@ import { copyFileSync } from 'fs';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';

export default function () {
return {
/**
* @param {{
* out?: string;
* }} options
*/
export default function ({ out = 'build' } = {}) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we have an interface for this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nah, it's adapter-specific

/** @type {import('@sveltejs/kit').Adapter} */
const adapter = {
async adapt(builder) {
const dir = dirname(fileURLToPath(import.meta.url));
const out = 'build'; // TODO implement adapter options

builder.log.minor('Writing client application...');
builder.log.minor('Copying assets');
const static_directory = join(out, 'assets');
builder.copy_client_files(static_directory);
builder.copy_static_files(static_directory);

builder.log.minor('Building server');
builder.log.minor('Copying server');
builder.copy_server_files(out);

copyFileSync(`${dir}/files/server.js`, `${out}/index.js`);

builder.log.minor('Prerendering static pages...');
builder.log.minor('Prerendering static pages');
await builder.prerender({
dest: `${out}/prerendered`
});
}
};

return adapter;
}
4 changes: 3 additions & 1 deletion packages/adapter-node/package.json
Expand Up @@ -17,9 +17,11 @@
"devDependencies": {
"@rollup/plugin-json": "^4.1.0",
"@sveltejs/app-utils": "workspace:*",
"@sveltejs/kit": "workspace:*",
"compression": "^1.7.4",
"polka": "^0.5.2",
"rollup": "^2.41.1",
"sirv": "^1.0.11"
"sirv": "^1.0.11",
"typescript": "^4.2.3"
}
}
13 changes: 13 additions & 0 deletions packages/adapter-node/tsconfig.json
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"noEmit": true,
"noImplicitAny": true,
"target": "es2020",
"module": "es2020",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true
},
"include": ["./index.js", "src"]
}
5 changes: 3 additions & 2 deletions packages/kit/package.json
Expand Up @@ -40,8 +40,9 @@
"files": [
"assets",
"dist",
"client",
"types.d.ts"
"types.d.ts",
"types.internal.d.ts",
"svelte-kit.js"
],
"scripts": {
"dev": "rm -rf assets/runtime && rollup -cw",
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/adapt/Builder.js
Expand Up @@ -8,7 +8,7 @@ export default class Builder {
/** @param {{
* cwd: string;
* config: any; // TODO
* log: import('../../types').Logger
* log: import('../../../types.internal').Logger
* }} opts */
constructor({ cwd, config, log }) {
this.#cwd = cwd;
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/adapt/index.js
Expand Up @@ -5,7 +5,7 @@ import Builder from './Builder.js';
import { createRequire } from 'module';

/**
* @param {import('../../types').ValidatedConfig} config
* @param {import('../../../types.internal').ValidatedConfig} config
* @param {{ cwd?: string, verbose: boolean }} opts
*/
export async function adapt(config, { cwd = process.cwd(), verbose }) {
Expand Down
6 changes: 3 additions & 3 deletions packages/kit/src/core/adapt/prerender.js
Expand Up @@ -50,8 +50,8 @@ const REDIRECT = 3;
/** @param {{
* cwd: string;
* out: string;
* log: import('../../types').Logger;
* config: import('../../types').ValidatedConfig;
* log: import('../../../types.internal').Logger;
* config: import('../../../types.internal').ValidatedConfig;
* force: boolean; // disregard `export const prerender = true`
* }} opts */
export async function prerender({ cwd, out, log, config, force }) {
Expand All @@ -62,7 +62,7 @@ export async function prerender({ cwd, out, log, config, force }) {

const server_root = resolve_path(dir);

/** @type {import('../../types').App} */
/** @type {import('../../../types.internal').App} */
const app = await import(pathToFileURL(`${server_root}/server/app.js`).href);

app.init({
Expand Down
14 changes: 7 additions & 7 deletions packages/kit/src/core/build/index.js
Expand Up @@ -17,7 +17,7 @@ const s = (value) => JSON.stringify(value);
* }>} ClientManifest */

/**
* @param {import('../../types').ValidatedConfig} config
* @param {import('../../../types.internal').ValidatedConfig} config
* @param {{
* cwd?: string;
* runtime?: string;
Expand Down Expand Up @@ -64,8 +64,8 @@ export async function build(config, { cwd = process.cwd(), runtime = '@sveltejs/
* @param {{
* cwd: string;
* base: string;
* config: import('../../types').ValidatedConfig
* manifest: import('../../types').ManifestData
* config: import('../../../types.internal').ValidatedConfig
* manifest: import('../../../types.internal').ManifestData
* build_dir: string;
* output_dir: string;
* client_entry_file: string;
Expand Down Expand Up @@ -159,8 +159,8 @@ async function build_client({
* @param {{
* cwd: string;
* base: string;
* config: import('../../types').ValidatedConfig
* manifest: import('../../types').ManifestData
* config: import('../../../types.internal').ValidatedConfig
* manifest: import('../../../types.internal').ManifestData
* build_dir: string;
* output_dir: string;
* client_entry_file: string;
Expand Down Expand Up @@ -405,8 +405,8 @@ async function build_server(
* @param {{
* cwd: string;
* base: string;
* config: import('../../types').ValidatedConfig
* manifest: import('../../types').ManifestData
* config: import('../../../types.internal').ValidatedConfig
* manifest: import('../../../types.internal').ManifestData
* build_dir: string;
* output_dir: string;
* client_entry_file: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/create_app/index.js
Expand Up @@ -19,7 +19,7 @@ export function write_if_changed(file, code) {

const s = JSON.stringify;

/** @typedef {import('../../types').ManifestData} ManifestData */
/** @typedef {import('../../../types.internal').ManifestData} ManifestData */

/**
* @param {{
Expand Down
10 changes: 5 additions & 5 deletions packages/kit/src/core/create_manifest_data/index.js
Expand Up @@ -22,11 +22,11 @@ import mime from 'mime';

/**
* @param {{
* config: import('../../types').ValidatedConfig;
* config: import('../../../types.internal').ValidatedConfig;
* output: string;
* cwd?: string;
* }} opts
* @returns {import('../../types').ManifestData}
* @returns {import('../../../types.internal').ManifestData}
*/
export default function create_manifest_data({ config, output, cwd = process.cwd() }) {
/**
Expand All @@ -41,10 +41,10 @@ export default function create_manifest_data({ config, output, cwd = process.cwd
/** @type {string[]} */
const components = [];

/** @type {import('../../types').PageData[]} */
/** @type {import('../../../types.internal').PageData[]} */
const pages = [];

/** @type {import('../../types').EndpointData[]} */
/** @type {import('../../../types.internal').EndpointData[]} */
const endpoints = [];

/** @type {Map<string, string>} */
Expand Down Expand Up @@ -336,7 +336,7 @@ function get_pattern(segments, add_trailing_slash) {
/**
* @param {string} dir
* @param {string} path
* @param {import('../../types').Asset[]} files
* @param {import('../../../types.internal').Asset[]} files
*/
function list_files(dir, path, files = []) {
fs.readdirSync(dir).forEach((file) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/dev/index.js
Expand Up @@ -14,7 +14,7 @@ import { get_body } from '@sveltejs/app-utils/http';
import { copy_assets } from '../utils.js';
import svelte from '@svitejs/vite-plugin-svelte';

/** @typedef {{ cwd?: string, port: number, config: import('../../types').ValidatedConfig }} Options */
/** @typedef {{ cwd?: string, port: number, config: import('../../../types.internal').ValidatedConfig }} Options */

/** @param {Options} opts */
export function dev(opts) {
Expand Down
6 changes: 3 additions & 3 deletions packages/kit/src/core/load_config/index.js
Expand Up @@ -98,11 +98,11 @@ export async function load_config({ cwd = process.cwd() } = {}) {
}

/**
* @param {import('../../../types').Config} config
* @returns {import('../../types.js').ValidatedConfig}
* @param {import('../../../../types.internal').Config} config
* @returns {import('../../../types.internal.js').ValidatedConfig}
*/
export function validate_config(config) {
/** @type {import('../../types.js').ValidatedConfig} */
/** @type {import('../../../types.internal.js').ValidatedConfig} */
const validated = validate(options, config, 'config');

// resolve paths
Expand Down
4 changes: 2 additions & 2 deletions packages/kit/src/core/start/index.js
Expand Up @@ -15,15 +15,15 @@ const mutable = (dir) =>
/**
* @param {{
* port: number;
* config: import('../../types').ValidatedConfig;
* config: import('../../../types.internal').ValidatedConfig;
* cwd?: string;
* }} opts
* @returns {Promise<import('http').Server>}
*/
export async function start({ port, config, cwd = process.cwd() }) {
const app_file = resolve(cwd, '.svelte/output/server/app.js');

/** @type {import('../../types').App} */
/** @type {import('../../../types.internal').App} */
const app = await import(pathToFileURL(app_file).href);

/** @type {import('sirv').RequestHandler} */
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/utils.js
Expand Up @@ -28,7 +28,7 @@ function noop() {}

/** @param {{ verbose: boolean }} opts */
export function logger({ verbose }) {
/** @type {import('../types').Logger} */
/** @type {import('../../types.internal').Logger} */
const log = (msg) => console.log(msg.replace(/^/gm, ' '));

log.success = (msg) => log(colors.green(`✔ ${msg}`));
Expand Down
10 changes: 5 additions & 5 deletions packages/kit/src/runtime/client/renderer.js
Expand Up @@ -34,8 +34,8 @@ function page_store(value) {

export class Renderer {
/** @param {{
* Root: import('../../types').CSRComponent;
* layout: import('../../types').CSRComponent;
* Root: import('../../../types.internal').CSRComponent;
* layout: import('../../../types.internal').CSRComponent;
* target: Node;
* error: Error;
* status: number;
Expand Down Expand Up @@ -207,7 +207,7 @@ export class Renderer {
/** @type {Error} */
error: null,

/** @type {import('../../types').CSRComponent[]} */
/** @type {import('../../../types.internal').CSRComponent[]} */
components: []
};

Expand All @@ -231,7 +231,7 @@ export class Renderer {

// TODO come up with a better name
/** @typedef {{
* component: import('../../types').CSRComponent;
* component: import('../../../types.internal').CSRComponent;
* uses: {
* params: Set<string>;
* query: boolean;
Expand Down Expand Up @@ -298,7 +298,7 @@ export class Renderer {
/** @type {Branch} */
let node;

/** @type {import('../../types').LoadResult} */
/** @type {import('../../../types.internal').LoadResult} */
let loaded;

if (cached && (!changed.context || !cached.node.uses.context)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/client/router.js
Expand Up @@ -16,7 +16,7 @@ export class Router {
/** @param {{
* base: string;
* host: string;
* pages: import('../../types').Page[];
* pages: import('../../../types.internal').Page[];
* ignore: RegExp[];
* }} opts */
constructor({ base, host, pages, ignore }) {
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/client/types.d.ts
@@ -1,6 +1,6 @@
export type NavigationTarget = {
href: string;
route: import('../../types').Page;
route: import('../../../types.internal').Page;
match: RegExpExecArray;
page: {
host: string;
Expand Down
4 changes: 2 additions & 2 deletions packages/kit/src/runtime/load.js
@@ -1,6 +1,6 @@
/**
* @param {import('../types').LoadResult} loaded
* @returns {import('../types').LoadResult}
* @param {import('../../types.internal').LoadResult} loaded
* @returns {import('../../types.internal').LoadResult}
*/
export function normalize(loaded) {
// TODO should this behaviour be dev-only?
Expand Down
6 changes: 3 additions & 3 deletions packages/kit/src/runtime/server/endpoint.js
@@ -1,8 +1,8 @@
/**
* @param {import('../../types').Request} request
* @param {import('../../../types.internal').Request} request
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect that Request and Response should not be internal because almost every adapter will have to deal with them

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably. we can move things out of internal as and when necessary

* @param {*} context // TODO
* @param {import('../../types').RenderOptions} options
* @returns {Promise<import('../../types').Response>}
* @param {import('../../../types.internal').RenderOptions} options
* @returns {Promise<import('../../../types.internal').Response>}
*/
export default function render_route(request, context, options) {
const route = options.manifest.endpoints.find((route) => route.pattern.test(request.path));
Expand Down
4 changes: 2 additions & 2 deletions packages/kit/src/runtime/server/index.js
Expand Up @@ -8,8 +8,8 @@ function md5(body) {
}

/**
* @param {import('../../types').Request} request
* @param {import('../../types').RenderOptions} options
* @param {import('../../../types.internal').Request} request
* @param {import('../../../types.internal').RenderOptions} options
*/
export async function ssr(request, options) {
if (request.path.endsWith('/') && request.path !== '/') {
Expand Down