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

feat: reference kit types in ambient file #5745

Merged
merged 3 commits into from
Jul 29, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/tricky-moose-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'create-svelte': patch
'@sveltejs/kit': patch
---

[feat] include reference to `@sveltejs/kit` types in ambient file
2 changes: 0 additions & 2 deletions packages/create-svelte/templates/default/src/app.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/// <reference types="@sveltejs/kit" />

// See https://kit.svelte.dev/docs/types#app
// for information about these interfaces
// and what to do when importing types
Expand Down
2 changes: 0 additions & 2 deletions packages/create-svelte/templates/skeleton/src/app.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/// <reference types="@sveltejs/kit" />

// See https://kit.svelte.dev/docs/types#app
// for information about these interfaces
// and what to do when importing types
Expand Down
4 changes: 2 additions & 2 deletions packages/kit/src/core/sync/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { write_matchers } from './write_matchers.js';
import { write_root } from './write_root.js';
import { write_tsconfig } from './write_tsconfig.js';
import { write_types } from './write_types.js';
import { write_env } from './write_env.js';
import { write_ambient } from './write_ambient.js';

/**
* Initialize SvelteKit's generated files.
Expand All @@ -17,7 +17,7 @@ export function init(config, mode) {
copy_assets(path.join(config.kit.outDir, 'runtime'));

write_tsconfig(config.kit);
write_env(config.kit, mode);
write_ambient(config.kit, mode);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,37 @@ import { get_env } from '../../vite/utils.js';
import { write_if_changed, reserved, valid_identifier } from './utils.js';

const autogen_comment = '// this file is generated — do not edit it\n';
const types_reference = '/// <reference types="@sveltejs/kit" />\n\n';

/**
* Writes the existing environment variables in process.env to
* Writes ambient declarations including types reference to @sveltejs/kit,
* and the existing environment variables in process.env to
* $env/static/private and $env/static/public
* @param {import('types').ValidatedKitConfig} config
* @param {string} mode The Vite mode
*/
export function write_env(config, mode) {
export function write_ambient(config, mode) {
const env = get_env(mode, config.env.publicPrefix);

// TODO when testing src, `$app` points at `src/runtime/app`... will
// probably need to fiddle with aliases
write_if_changed(
path.join(config.outDir, 'runtime/env/static/public.js'),
create_module('$env/static/public', env.public)
create_env_module('$env/static/public', env.public)
);

write_if_changed(
path.join(config.outDir, 'runtime/env/static/private.js'),
create_module('$env/static/private', env.private)
create_env_module('$env/static/private', env.private)
);

write_if_changed(
path.join(config.outDir, 'ambient.d.ts'),
autogen_comment +
create_types('$env/static/public', env.public) +
types_reference +
create_env_types('$env/static/public', env.public) +
'\n\n' +
create_types('$env/static/private', env.private)
create_env_types('$env/static/private', env.private)
);
}

Expand All @@ -40,7 +43,7 @@ export function write_env(config, mode) {
* @param {Record<string, string>} env
* @returns {string}
*/
function create_module(id, env) {
function create_env_module(id, env) {
/** @type {string[]} */
const declarations = [];

Expand Down Expand Up @@ -74,7 +77,7 @@ function create_module(id, env) {
* @param {Record<string, string>} env
* @returns {string}
*/
function create_types(id, env) {
function create_env_types(id, env) {
const declarations = Object.keys(env)
.filter((k) => valid_identifier.test(k))
.map((k) => `\texport const ${k}: string;`)
Expand Down