Skip to content

Commit

Permalink
simplify types (#4010)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris authored Feb 19, 2022
1 parent 1666b58 commit 4476ccb
Show file tree
Hide file tree
Showing 50 changed files with 787 additions and 784 deletions.
12 changes: 6 additions & 6 deletions packages/kit/src/core/adapt/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ import { generate_manifest } from '../generate_manifest/index.js';
/**
* @param {{
* cwd: string;
* config: import('types/config').ValidatedConfig;
* build_data: import('types/internal').BuildData;
* log: import('types/internal').Logger;
* config: import('types').ValidatedConfig;
* build_data: import('types').BuildData;
* log: import('types').Logger;
* }} opts
* @returns {import('types/config').Builder}
* @returns {import('types').Builder}
*/
export function create_builder({ cwd, config, build_data, log }) {
/** @type {Set<string>} */
let prerendered_paths;

let generated_manifest = false;

/** @param {import('types/internal').RouteData} route */
/** @param {import('types').RouteData} route */
function not_prerendered(route) {
if (!prerendered_paths) return true;

Expand All @@ -43,7 +43,7 @@ export function create_builder({ cwd, config, build_data, log }) {

const { routes } = build_data.manifest_data;

/** @type {import('types/config').RouteDefinition[]} */
/** @type {import('types').RouteDefinition[]} */
const facades = routes.map((route) => ({
type: route.type,
segments: route.segments,
Expand Down
6 changes: 3 additions & 3 deletions packages/kit/src/core/adapt/builder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const __dirname = join(__filename, '..');
test('copy files', () => {
const cwd = join(__dirname, 'fixtures/basic');

/** @type {import('types/config').Config} */
/** @type {import('types').Config} */
const mocked = {
extensions: ['.svelte'],
kit: {
Expand All @@ -26,7 +26,7 @@ test('copy files', () => {

const builder = create_builder({
cwd,
config: /** @type {import('types/config').ValidatedConfig} */ (mocked),
config: /** @type {import('types').ValidatedConfig} */ (mocked),
// @ts-expect-error
build_data: {},
// @ts-expect-error
Expand All @@ -40,7 +40,7 @@ test('copy files', () => {

assert.equal(
glob('**', {
cwd: /** @type {import('types/config').ValidatedConfig} */ (mocked).kit.files.assets
cwd: /** @type {import('types').ValidatedConfig} */ (mocked).kit.files.assets
}),
glob('**', { cwd: dest })
);
Expand Down
4 changes: 2 additions & 2 deletions packages/kit/src/core/adapt/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { logger } from '../utils.js';
import { create_builder } from './builder.js';

/**
* @param {import('types/config').ValidatedConfig} config
* @param {import('types/internal').BuildData} build_data
* @param {import('types').ValidatedConfig} config
* @param {import('types').BuildData} build_data
* @param {{ cwd?: string, verbose: boolean }} opts
*/
export async function adapt(config, build_data, { cwd = process.cwd(), verbose }) {
Expand Down
16 changes: 8 additions & 8 deletions packages/kit/src/core/adapt/prerender/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { crawl } from './crawl.js';
import { escape_html_attr } from '../../../utils/escape.js';

/**
* @typedef {import('types/config').PrerenderErrorHandler} PrerenderErrorHandler
* @typedef {import('types/config').PrerenderOnErrorValue} OnError
* @typedef {import('types/internal').Logger} Logger
* @typedef {import('types').PrerenderErrorHandler} PrerenderErrorHandler
* @typedef {import('types').PrerenderOnErrorValue} OnError
* @typedef {import('types').Logger} Logger
*/

/** @type {(details: Parameters<PrerenderErrorHandler>[0] ) => string} */
Expand Down Expand Up @@ -44,14 +44,14 @@ const REDIRECT = 3;
* cwd: string;
* out: string;
* log: Logger;
* config: import('types/config').ValidatedConfig;
* build_data: import('types/internal').BuildData;
* config: import('types').ValidatedConfig;
* build_data: import('types').BuildData;
* fallback?: string;
* all: boolean; // disregard `export const prerender = true`
* }} opts
*/
export async function prerender({ cwd, out, log, config, build_data, fallback, all }) {
/** @type {import('types/config').Prerendered} */
/** @type {import('types').Prerendered} */
const prerendered = {
pages: new Map(),
assets: new Map(),
Expand All @@ -67,7 +67,7 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a

const server_root = resolve_path(cwd, `${SVELTE_KIT}/output`);

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

Expand Down Expand Up @@ -142,7 +142,7 @@ export async function prerender({ cwd, out, log, config, build_data, fallback, a
return;
}

/** @type {Map<string, import('types/internal').PrerenderDependency>} */
/** @type {Map<string, import('types').PrerenderDependency>} */
const dependencies = new Map();

const response = await app.render(new Request(`http://sveltekit-prerender${encoded}`), {
Expand Down
4 changes: 2 additions & 2 deletions packages/kit/src/core/build/build_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import { posixify } from '../../utils/filesystem.js';
* @param {{
* cwd: string;
* assets_base: string;
* config: import('types/config').ValidatedConfig
* manifest_data: import('types/internal').ManifestData
* config: import('types').ValidatedConfig
* manifest_data: import('types').ManifestData
* output_dir: string;
* client_entry_file: string;
* service_worker_entry_file: string | null;
Expand Down
12 changes: 6 additions & 6 deletions packages/kit/src/core/build/build_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { s } from '../../utils/misc.js';
/**
* @param {{
* hooks: string;
* config: import('types/config').ValidatedConfig;
* config: import('types').ValidatedConfig;
* has_service_worker: boolean;
* template: string;
* }} opts
Expand Down Expand Up @@ -109,8 +109,8 @@ export class App {
* @param {{
* cwd: string;
* assets_base: string;
* config: import('types/config').ValidatedConfig
* manifest_data: import('types/internal').ManifestData
* config: import('types').ValidatedConfig
* manifest_data: import('types').ManifestData
* build_dir: string;
* output_dir: string;
* service_worker_entry_file: string | null;
Expand Down Expand Up @@ -307,7 +307,7 @@ const method_names = {
*
* @param {string} cwd
* @param {import('rollup').OutputChunk[]} output
* @param {import('types/internal').ManifestData} manifest_data
* @param {import('types').ManifestData} manifest_data
*/
function get_methods(cwd, output, manifest_data) {
/** @type {Record<string, string[]>} */
Expand All @@ -318,14 +318,14 @@ function get_methods(cwd, output, manifest_data) {
lookup[id] = chunk.exports;
});

/** @type {Record<string, import('types/internal').HttpMethod[]>} */
/** @type {Record<string, import('types').HttpMethod[]>} */
const methods = {};
manifest_data.routes.forEach((route) => {
const file = route.type === 'endpoint' ? route.file : route.shadow;

if (file && lookup[file]) {
methods[file] = lookup[file]
.map((x) => /** @type {import('types/internal').HttpMethod} */ (method_names[x]))
.map((x) => /** @type {import('types').HttpMethod} */ (method_names[x]))
.filter(Boolean);
}
});
Expand Down
4 changes: 2 additions & 2 deletions packages/kit/src/core/build/build_service_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { SVELTE_KIT } from '../constants.js';
* @param {{
* cwd: string;
* assets_base: string;
* config: import('types/config').ValidatedConfig
* manifest_data: import('types/internal').ManifestData
* config: import('types').ValidatedConfig
* manifest_data: import('types').ManifestData
* output_dir: string;
* service_worker_entry_file: string | null;
* }} options
Expand Down
4 changes: 2 additions & 2 deletions packages/kit/src/core/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { build_client } from './build_client.js';
import { build_server } from './build_server.js';

/**
* @param {import('types/config').ValidatedConfig} config
* @returns {Promise<import('types/internal').BuildData>}
* @param {import('types').ValidatedConfig} config
* @returns {Promise<import('types').BuildData>}
*/
export async function build(config) {
const cwd = process.cwd(); // TODO is this necessary?
Expand Down
6 changes: 3 additions & 3 deletions packages/kit/src/core/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import options from './options.js';

/**
* @param {string} cwd
* @param {import('types/config').ValidatedConfig} config
* @param {import('types').ValidatedConfig} config
*/
export function load_template(cwd, config) {
const { template } = config.kit.files;
Expand Down Expand Up @@ -51,8 +51,8 @@ export async function load_config({ cwd = process.cwd() } = {}) {
}

/**
* @param {import('types/config').Config} config
* @returns {import('types/config').ValidatedConfig}
* @param {import('types').Config} config
* @returns {import('types').ValidatedConfig}
*/
export function validate_config(config) {
if (typeof config !== 'object') {
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/create_app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function write_if_changed(file, code) {
}
}

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

/**
* @param {{
Expand Down
10 changes: 5 additions & 5 deletions packages/kit/src/core/create_manifest_data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ const specials = new Set(['__layout', '__layout.reset', '__error']);

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

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

const default_layout = posixify(path.relative(cwd, `${fallback}/layout.svelte`));
Expand Down Expand Up @@ -435,10 +435,10 @@ function get_pattern(segments, add_trailing_slash) {

/**
* @param {{
* config: import('types/config').ValidatedConfig;
* config: import('types').ValidatedConfig;
* dir: string;
* path: string;
* files?: import('types/internal').Asset[]
* files?: import('types').Asset[]
* }} args
*/
function list_files({ config, dir, path, files = [] }) {
Expand Down
4 changes: 2 additions & 2 deletions packages/kit/src/core/create_manifest_data/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const cwd = fileURLToPath(new URL('./test', import.meta.url));

/**
* @param {string} dir
* @param {import('types/config').Config} config
* @param {import('types').Config} config
* @returns
*/
const create = (dir, config = {}) => {
Expand All @@ -19,7 +19,7 @@ const create = (dir, config = {}) => {
initial.kit.files.routes = path.resolve(cwd, dir);

return create_manifest_data({
config: /** @type {import('types/config').ValidatedConfig} */ (initial),
config: /** @type {import('types').ValidatedConfig} */ (initial),
fallback: cwd,
cwd
});
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/core/dev/amp_hook.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @type {import('amphtml-validator').Validator} */
const amp = await (await import('amphtml-validator')).getInstance();

/** @type {import('types/hooks').Handle} */
/** @type {import('types').Handle} */
export async function handle({ event, resolve }) {
const response = await resolve(event);
if (response.headers.get('content-type') !== 'text/html') {
Expand Down
4 changes: 2 additions & 2 deletions packages/kit/src/core/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import { create_plugin } from './plugin.js';
* port: number,
* host?: string,
* https: boolean,
* config: import('types/config').ValidatedConfig
* config: import('types').ValidatedConfig
* }} Options
* @typedef {import('types/internal').SSRComponent} SSRComponent
* @typedef {import('types').SSRComponent} SSRComponent
*/

/** @param {Options} opts */
Expand Down
14 changes: 7 additions & 7 deletions packages/kit/src/core/dev/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import { load_template } from '../config/index.js';
import { sequence } from '../../hooks.js';

/**
* @param {import('types/config').ValidatedConfig} config
* @param {import('types').ValidatedConfig} config
* @param {string} cwd
* @returns {Promise<import('vite').Plugin>}
*/
export async function create_plugin(config, cwd) {
/** @type {import('types/hooks').Handle} */
/** @type {import('types').Handle} */
let amp;

if (config.kit.amp) {
Expand All @@ -29,7 +29,7 @@ export async function create_plugin(config, cwd) {

process.env.VITE_SVELTEKIT_APP_VERSION_POLL_INTERVAL = '0';

/** @type {import('types/internal').Respond} */
/** @type {import('types').Respond} */
const respond = (await import(`${runtime}/server/index.js`)).respond;

return {
Expand All @@ -38,7 +38,7 @@ export async function create_plugin(config, cwd) {
configureServer(vite) {
__fetch_polyfill();

/** @type {import('types/app').SSRManifest} */
/** @type {import('types').SSRManifest} */
let manifest;

function update_manifest() {
Expand All @@ -60,7 +60,7 @@ export async function create_plugin(config, cwd) {
return async () => {
const url = id.startsWith('..') ? `/@fs${path.posix.resolve(id)}` : `/${id}`;

const module = /** @type {import('types/internal').SSRComponent} */ (
const module = /** @type {import('types').SSRComponent} */ (
await vite.ssrLoadModule(url)
);
const node = await vite.moduleGraph.getModuleByUrl(url);
Expand Down Expand Up @@ -173,14 +173,14 @@ export async function create_plugin(config, cwd) {

if (!decoded.startsWith(config.kit.paths.base)) return not_found(res);

/** @type {Partial<import('types/internal').Hooks>} */
/** @type {Partial<import('types').Hooks>} */
const user_hooks = resolve_entry(config.kit.files.hooks)
? await vite.ssrLoadModule(`/${config.kit.files.hooks}`)
: {};

const handle = user_hooks.handle || (({ event, resolve }) => resolve(event));

/** @type {import('types/internal').Hooks} */
/** @type {import('types').Hooks} */
const hooks = {
// @ts-expect-error this picks up types that belong to the tests
getSession: user_hooks.getSession || (() => ({})),
Expand Down
4 changes: 2 additions & 2 deletions packages/kit/src/core/generate_manifest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { s } from '../../utils/misc.js';
import { get_mime_lookup } from '../utils.js';

/**
* @param {import('../../../types/internal').BuildData} build_data;
* @param {import('types').BuildData} build_data;
* @param {string} relative_path;
* @param {import('../../../types/internal').RouteData[]} routes;
* @param {import('types').RouteData[]} routes;
* @param {'esm' | 'cjs'} format
*/
export function generate_manifest(
Expand Down
4 changes: 2 additions & 2 deletions packages/kit/src/core/preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const mutable = (dir) =>
* @param {{
* port: number;
* host?: string;
* config: import('types/config').ValidatedConfig;
* config: import('types').ValidatedConfig;
* https?: boolean;
* cwd?: string;
* }} opts
Expand All @@ -36,7 +36,7 @@ export async function preview({
const app_file = resolve(cwd, `${SVELTE_KIT}/output/server/app.js`);
const manifest_file = resolve(cwd, `${SVELTE_KIT}/output/server/manifest.js`);

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

const { manifest } = await import(pathToFileURL(manifest_file).href);
Expand Down
Loading

0 comments on commit 4476ccb

Please sign in to comment.