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

implement $lib #511

Merged
merged 4 commits into from
Mar 14, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/great-kangaroos-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'create-svelte': patch
'@sveltejs/kit': patch
---

Add \$lib alias
4 changes: 2 additions & 2 deletions documentation/migrating/01-migrating.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ SvelteKit is largely backwards-compatible with Sapper. However, there are a numb
- Remove `rollup.config.js` or `webpack.config.js` and replace with [`vite.config.js`](https://github.com/sveltejs/kit/blob/master/packages/create-svelte/template/vite.config.js)
- Add a [`svelte.config.cjs` ](https://github.com/sveltejs/kit/blob/master/packages/create-svelte/template/svelte.config.cjs) with the adapter of your choice
- Update your `template.html` to [`app.html`](https://github.com/sveltejs/kit/blob/master/packages/create-svelte/template/src/app.html)
- It should have a `<div id="svelte">` (or `id` matching `target` in `svelte.config.cjs`)
- It should have a `<div id="svelte">` (or `id` matching `target` in `svelte.config.cjs`)
- The error and layout pages should prefixed by `$` instead of `_`
- Replace imports from `@sapper/app` with imports from `$app/navigation` and `$app/stores` and update APIs accordingly
- If you have `src/node_modules/components`, move it to `src/components` and update the import path to `$components`
- If you have source code in `src/node_modules`, move it to `src/lib` and update the import path to `$lib`
- Move any custom `client.js` code to `$layout.svelte` and put it in an `if (browser)` check (`import { browser } from "$app/env";`).
- `preload` has been renamed to `load`. It has a new method signature and return values
- `sapper:prefetch` and `sapper:noscroll` have been renamed to `sveltekit:prefetch` and `sveltekit:noscroll`
6 changes: 3 additions & 3 deletions examples/hn.svelte.dev/src/routes/$layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

<script>
import { page, navigating } from '$app/stores';
import Nav from '$components/Nav.svelte';
import PreloadingIndicator from '$components/PreloadingIndicator.svelte';
import ThemeToggler from '$components/ThemeToggler.svelte';
import Nav from '$lib/Nav.svelte';
import PreloadingIndicator from '$lib/PreloadingIndicator.svelte';
import ThemeToggler from '$lib/ThemeToggler.svelte';

$: section = $page.path.split('/')[1];
</script>
Expand Down
2 changes: 1 addition & 1 deletion examples/hn.svelte.dev/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"baseUrl": ".",
"paths": {
"$components/*": ["./src/components/*"]
"$lib/*": ["./src/lib/*"]
}
}
}
7 changes: 1 addition & 6 deletions examples/hn.svelte.dev/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
// Consult https://vitejs.dev/config/ to learn about these options
import { join, resolve } from 'path';
import { join } from 'path';
import { readFileSync } from 'fs';
import { cwd } from 'process';

const pkg = JSON.parse(readFileSync(join(cwd(), 'package.json')));

/** @type {import('vite').UserConfig} */
export default {
resolve: {
alias: {
$components: resolve('src/components')
}
},
ssr: {
noExternal: Object.keys(pkg.dependencies || {})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script>
import * as api from '$common/api.js';
import * as api from '$lib/api.js';

export let article;
export let user;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script>
import { session, page } from '$app/stores';
import * as api from '$common/api.js';
import * as api from '$lib/api.js';
import ArticlePreview from './ArticlePreview.svelte';
import ListPagination from './ListPagination.svelte';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { onMount } from 'svelte';
import MainView from './MainView/index.svelte';
import Tags from './Tags.svelte';
import * as api from '$common/api.js';
import * as api from '$lib/api.js';

export let p = 1;

Expand Down
2 changes: 1 addition & 1 deletion examples/realworld.svelte.dev/src/routes/$layout.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script>
import Nav from '$components/Nav.svelte';
import Nav from '$lib/Nav.svelte';

export let segment;
</script>
Expand Down
2 changes: 1 addition & 1 deletion examples/realworld.svelte.dev/src/routes/[p].svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script>
import { page } from '$app/stores';
import Home from '$components/Home.svelte';
import Home from '$lib/Home.svelte';
</script>

<Home p={+$page.params.p}/>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script context="module">
import * as api from '$common/api.js';
import * as api from '$lib/api.js';

export async function load({ page }) {
const { slug } = page.params;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script>
import { goto } from '$app/navigation';
import * as api from '$common/api.js';
import * as api from '$lib/api.js';

export let article;
export let user;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script>
import { createEventDispatcher } from 'svelte';
import * as api from '$common/api.js';
import * as api from '$lib/api.js';

export let comment;
export let slug;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script>
import ListErrors from '$components/ListErrors.svelte';
import ListErrors from '$lib/ListErrors.svelte';
import CommentInput from './_CommentInput.svelte';
import Comment from './_Comment.svelte';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script>
import { createEventDispatcher } from 'svelte';
import * as api from '$common/api.js';
import * as api from '$lib/api.js';

export let slug;
export let user;
Expand Down
4 changes: 2 additions & 2 deletions examples/realworld.svelte.dev/src/routes/auth/login.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as api from '$common/api.js';
import * as api from '$lib/api.js';

export async function post({ body }) {
const response = await api.post('users/login', {
Expand All @@ -8,4 +8,4 @@ export async function post({ body }) {
return {
body: response
};
}
}
6 changes: 3 additions & 3 deletions examples/realworld.svelte.dev/src/routes/auth/register.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as api from '$common/api.js';
import * as api from '$lib/api.js';

export function post(req, res) {
const user = req.body;

api.post('users', { user }).then(response => {
api.post('users', { user }).then((response) => {
if (response.user) {
req.session.user = response.user;
}
Expand All @@ -12,4 +12,4 @@ export function post(req, res) {

res.end(JSON.stringify(response));
});
}
}
6 changes: 3 additions & 3 deletions examples/realworld.svelte.dev/src/routes/auth/save.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as api from '$common/api.js';
import * as api from '$lib/api.js';

export function post(req, res) {
const user = req.body;

api.put('user', { user }, req.session.user && req.session.user.token).then(response => {
api.put('user', { user }, req.session.user && req.session.user.token).then((response) => {
if (response.user) {
req.session.user = response.user;
}
Expand All @@ -12,4 +12,4 @@ export function post(req, res) {

res.end(JSON.stringify(response));
});
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script context="module">
import * as api from '$common/api.js';
import * as api from '$lib/api.js';

export async function load({ page, session }) {
if (!session.user) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<script>
import { goto } from '$app/navigation';
import { session } from '$app/stores';
import ListErrors from '$components/ListErrors.svelte';
import * as api from '$common/api.js';
import ListErrors from '$lib/ListErrors.svelte';
import * as api from '$lib/api.js';

export let article;
export let slug;
Expand Down
2 changes: 1 addition & 1 deletion examples/realworld.svelte.dev/src/routes/index.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script>
import Home from '$components/Home.svelte';
import Home from '$lib/Home.svelte';
</script>

<Home p={1}/>
4 changes: 2 additions & 2 deletions examples/realworld.svelte.dev/src/routes/login/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<script>
import { session } from '$app/stores';
import { goto } from '$app/navigation';
import { post } from '$common/utils.js';
import ListErrors from '$components/ListErrors.svelte';
import { post } from '$lib/utils.js';
import ListErrors from '$lib/ListErrors.svelte';

let email = '';
let password = '';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script context="module">
import * as api from '$common/api.js';
import * as api from '$lib/api.js';

export async function load({ page, session: { user } }) {
const username = page.params.user.slice(1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script>
import { goto } from '$app/navigation';
import ArticleList from '$components/ArticleList/index.svelte';
import * as api from '$common/api.js';
import ArticleList from '$lib/ArticleList/index.svelte';
import * as api from '$lib/api.js';

export let profile;
export let favorites;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script context="module">
import * as api from '$common/api.js';
import * as api from '$lib/api.js';

export async function load({ page, session: { user } }) {
const username = page.params.user.slice(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<script>
import { session } from '$app/stores';
import { goto } from '$app/navigation';
import { post } from '$common/utils.js';
import ListErrors from '$components/ListErrors.svelte';
import { post } from '$lib/utils.js';
import ListErrors from '$lib/ListErrors.svelte';

let username = '';
let email = '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

<script>
import { goto } from '$app/navigation';
import ListErrors from '$components/ListErrors.svelte';
import ListErrors from '$lib/ListErrors.svelte';
import SettingsForm from './_SettingsForm.svelte';
import { post } from '$common/utils.js';
import { post } from '$lib/utils.js';

let inProgress;
let errors;
Expand Down
8 changes: 1 addition & 7 deletions examples/realworld.svelte.dev/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
// Consult https://vitejs.dev/config/ to learn about these options
import { join, resolve } from 'path';
import { join } from 'path';
import { readFileSync } from 'fs';
import { cwd } from 'process';

const pkg = JSON.parse(readFileSync(join(cwd(), 'package.json')));

/** @type {import('vite').UserConfig} */
export default {
resolve: {
alias: {
$common: resolve('src/common'),
$components: resolve('src/components')
}
},
ssr: {
noExternal: Object.keys(pkg.dependencies || {})
}
Expand Down
2 changes: 1 addition & 1 deletion examples/sandbox/src/routes/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</script>

<script>
import Counter from '$components/Counter.svelte';
import Counter from '$lib/Counter.svelte';

export let answer;

Expand Down
7 changes: 1 addition & 6 deletions examples/sandbox/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Consult https://vitejs.dev/config/ to learn about these options
import { join, resolve } from 'path';
import { join } from 'path';
import { readFileSync } from 'fs';
import { cwd } from 'process';

Expand All @@ -10,11 +10,6 @@ export default {
build: {
minify: false
},
resolve: {
alias: {
$components: resolve('src/components')
}
},
ssr: {
noExternal: Object.keys(pkg.dependencies || {})
}
Expand Down
2 changes: 1 addition & 1 deletion examples/svelte-kit-demo/src/routes/$layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
</script>

<script>
import Nav from '$components/Nav.svelte';
import Nav from '$lib/Nav.svelte';
export let segment;
</script>

Expand Down
7 changes: 1 addition & 6 deletions examples/svelte-kit-demo/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
// Consult https://vitejs.dev/config/ to learn about these options
import { join, resolve } from 'path';
import { join } from 'path';
import { readFileSync } from 'fs';
import { cwd } from 'process';

const pkg = JSON.parse(readFileSync(join(cwd(), 'package.json')));

/** @type {import('vite').UserConfig} */
export default {
resolve: {
alias: {
$components: resolve('src/components')
}
},
ssr: {
noExternal: Object.keys(pkg.dependencies || {})
}
Expand Down
2 changes: 1 addition & 1 deletion packages/create-svelte/template/jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"paths": {
"$app/*": [".svelte/dev/runtime/app/*", ".svelte/build/runtime/app/*"],
"$components/*": ["src/components/*"]
"$lib/*": ["src/lib/*"]
}
},
"include": ["src/**/*.js", "src/**/*.svelte"]
Expand Down
2 changes: 1 addition & 1 deletion packages/create-svelte/template/src/routes/index.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script>
import Counter from '$components/Counter.svelte';
import Counter from '$lib/Counter.svelte';
</script>

<main>
Expand Down
7 changes: 1 addition & 6 deletions packages/create-svelte/template/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
// Consult https://vitejs.dev/config/ to learn about these options
import { join, resolve } from 'path';
import { join } from 'path';
import { readFileSync } from 'fs';
import { cwd } from 'process';

const pkg = JSON.parse(readFileSync(join(cwd(), 'package.json')));

/** @type {import('vite').UserConfig} */
export default {
resolve: {
alias: {
$components: resolve('src/components')
}
},
ssr: {
noExternal: Object.keys(pkg.dependencies || {})
}
Expand Down
Loading