Skip to content

Commit ec7a2a3

Browse files
implement config.kit.alias (#4964)
* implement basic alias config (closes #4734) * revise comments as benmccann says * document config.kit.alias * fix config default test * Update documentation/docs/14-configuration.md Co-authored-by: Rich Harris <hello@rich-harris.dev>
1 parent eb2ab91 commit ec7a2a3

File tree

7 files changed

+63
-6
lines changed

7 files changed

+63
-6
lines changed

.changeset/real-mice-argue.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
Add `config.kit.alias`

documentation/docs/14-configuration.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const config = {
1616

1717
kit: {
1818
adapter: undefined,
19+
alias: {},
1920
appDir: '_app',
2021
browser: {
2122
hydrate: true,
@@ -91,6 +92,27 @@ export default config;
9192

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

95+
### alias
96+
97+
An object containing zero or more aliases used to replace values in `import` statements. These aliases are automatically passed to Vite and TypeScript.
98+
99+
For example, you can add aliases to a `components` and `utils` folder:
100+
101+
```js
102+
/// file: svelte.config.js
103+
/** @type {import('@sveltejs/kit').Config} */
104+
const config = {
105+
kit: {
106+
alias: {
107+
$components: 'src/components',
108+
$utils: 'src/utils'
109+
}
110+
}
111+
};
112+
```
113+
114+
> The built-in `$lib` alias is controlled by `config.kit.files.lib` as it is used for packaging.
115+
94116
### appDir
95117

96118
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 `/`.

packages/kit/src/core/config/index.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const get_defaults = (prefix = '') => ({
1212
extensions: ['.svelte'],
1313
kit: {
1414
adapter: null,
15+
alias: {},
1516
amp: undefined,
1617
appDir: '_app',
1718
browser: {

packages/kit/src/core/config/options.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ const options = object(
3939
return input;
4040
}),
4141

42+
alias: validate({}, (input, keypath) => {
43+
if (typeof input !== 'object') {
44+
throw new Error(`${keypath} should be an object`);
45+
}
46+
47+
for (const key in input) {
48+
assert_string(input[key], `${keypath}.${key}`);
49+
}
50+
51+
return input;
52+
}),
53+
4254
// TODO: remove this for the 1.0 release
4355
amp: error(
4456
(keypath) =>

packages/kit/src/core/sync/write_tsconfig.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,27 @@ export function write_tsconfig(config, cwd = process.cwd()) {
3434
include.push(config_relative(`${dir}/**/*.svelte`));
3535
});
3636

37+
/** @type {Record<string, string[]>} */
38+
const paths = {};
39+
const alias = {
40+
$lib: project_relative(config.kit.files.lib),
41+
...config.kit.alias
42+
};
43+
for (const [key, value] of Object.entries(alias)) {
44+
if (fs.existsSync(project_relative(value))) {
45+
paths[key] = [project_relative(value)];
46+
paths[key + '/*'] = [project_relative(value) + '/*'];
47+
}
48+
}
49+
3750
write_if_changed(
3851
out,
3952
JSON.stringify(
4053
{
4154
compilerOptions: {
4255
// generated options
4356
baseUrl: config_relative('.'),
44-
paths: fs.existsSync(config.kit.files.lib)
45-
? {
46-
$lib: [project_relative(config.kit.files.lib)],
47-
'$lib/*': [project_relative(config.kit.files.lib + '/*')]
48-
}
49-
: {},
57+
paths,
5058
rootDirs: [config_relative('.'), './types'],
5159

5260
// essential options

packages/kit/src/core/utils.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,19 @@ export function get_mime_lookup(manifest_data) {
8080

8181
/** @param {import('types').ValidatedConfig} config */
8282
export function get_aliases(config) {
83+
/** @type {Record<string, string>} */
8384
const alias = {
8485
__GENERATED__: path.posix.join(config.kit.outDir, 'generated'),
8586
$app: `${get_runtime_path(config)}/app`,
87+
88+
// For now, we handle `$lib` specially here rather than make it a default value for
89+
// `config.kit.alias` since it has special meaning for packaging, etc.
8690
$lib: config.kit.files.lib
8791
};
8892

93+
for (const [key, value] of Object.entries(config.kit.alias)) {
94+
alias[key] = path.resolve(value);
95+
}
96+
8997
return alias;
9098
}

packages/kit/types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export interface Config {
9393
extensions?: string[];
9494
kit?: {
9595
adapter?: Adapter;
96+
alias?: Record<string, string>;
9697
appDir?: string;
9798
browser?: {
9899
hydrate?: boolean;

0 commit comments

Comments
 (0)