Skip to content

implement config.kit.alias #4964

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

Merged
merged 7 commits into from
May 23, 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
5 changes: 5 additions & 0 deletions .changeset/real-mice-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Add `config.kit.alias`
22 changes: 22 additions & 0 deletions documentation/docs/14-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const config = {

kit: {
adapter: undefined,
alias: {},
appDir: '_app',
browser: {
hydrate: true,
Expand Down Expand Up @@ -91,6 +92,27 @@ export default config;

Required when running `svelte-kit build` and determines how the output is converted for different platforms. See [Adapters](/docs/adapters).

### alias

An object containing zero or more aliases used to replace values in `import` statements. These aliases are automatically passed to Vite and TypeScript.

For example, you can add aliases to a `components` and `utils` folder:

```js
/// file: svelte.config.js
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
alias: {
$components: 'src/components',
$utils: 'src/utils'
}
}
};
```

> The built-in `$lib` alias is controlled by `config.kit.files.lib` as it is used for packaging.

### appDir

The directory relative to `paths.assets` where the built JS and CSS (and imported assets) are served from. (The filenames therein contain content-based hashes, meaning they can be cached indefinitely). Must not start or end with `/`.
Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/core/config/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const get_defaults = (prefix = '') => ({
extensions: ['.svelte'],
kit: {
adapter: null,
alias: {},
amp: undefined,
appDir: '_app',
browser: {
Expand Down
12 changes: 12 additions & 0 deletions packages/kit/src/core/config/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ const options = object(
return input;
}),

alias: validate({}, (input, keypath) => {
if (typeof input !== 'object') {
throw new Error(`${keypath} should be an object`);
}

for (const key in input) {
assert_string(input[key], `${keypath}.${key}`);
}

return input;
}),

// TODO: remove this for the 1.0 release
amp: error(
(keypath) =>
Expand Down
20 changes: 14 additions & 6 deletions packages/kit/src/core/sync/write_tsconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,27 @@ export function write_tsconfig(config, cwd = process.cwd()) {
include.push(config_relative(`${dir}/**/*.svelte`));
});

/** @type {Record<string, string[]>} */
const paths = {};
const alias = {
$lib: project_relative(config.kit.files.lib),
...config.kit.alias
};
for (const [key, value] of Object.entries(alias)) {
if (fs.existsSync(project_relative(value))) {
paths[key] = [project_relative(value)];
paths[key + '/*'] = [project_relative(value) + '/*'];
}
}

write_if_changed(
out,
JSON.stringify(
{
compilerOptions: {
// generated options
baseUrl: config_relative('.'),
paths: fs.existsSync(config.kit.files.lib)
? {
$lib: [project_relative(config.kit.files.lib)],
'$lib/*': [project_relative(config.kit.files.lib + '/*')]
}
: {},
paths,
rootDirs: [config_relative('.'), './types'],

// essential options
Expand Down
8 changes: 8 additions & 0 deletions packages/kit/src/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,19 @@ export function get_mime_lookup(manifest_data) {

/** @param {import('types').ValidatedConfig} config */
export function get_aliases(config) {
/** @type {Record<string, string>} */
const alias = {
__GENERATED__: path.posix.join(config.kit.outDir, 'generated'),
$app: `${get_runtime_path(config)}/app`,

// For now, we handle `$lib` specially here rather than make it a default value for
// `config.kit.alias` since it has special meaning for packaging, etc.
$lib: config.kit.files.lib
};

for (const [key, value] of Object.entries(config.kit.alias)) {
alias[key] = path.resolve(value);
}

return alias;
}
1 change: 1 addition & 0 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export interface Config {
extensions?: string[];
kit?: {
adapter?: Adapter;
alias?: Record<string, string>;
appDir?: string;
browser?: {
hydrate?: boolean;
Expand Down