Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: sveltejs/kit
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: @sveltejs/kit@1.0.0-next.423
Choose a base ref
...
head repository: sveltejs/kit
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: @sveltejs/kit@1.0.0-next.424
Choose a head ref
  • 6 commits
  • 19 files changed
  • 4 contributors

Commits on Aug 19, 2022

  1. [fix] Silence unknown prop warnings coming from SvelteKit (#6071)

    This is a hack, but it's a working quick solution. Fixes #5980
    dummdidumm authored Aug 19, 2022
    Copy the full SHA
    ad673a7 View commit details
  2. [fix] typo in migration task (#6070)

    Closes #6059
    dummdidumm authored Aug 19, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    1efa3e3 View commit details
  3. [feat] Suggest props destructuring if possible (#6069)

    Should make migration easier in most cases
    dummdidumm authored Aug 19, 2022
    Copy the full SHA
    b1dafb8 View commit details
  4. [fix] Generate types when Svelte file missing (#6066)

    Fixes #6044
    Also fixes layout params. They wrongfully contained the slugs above them, too.
    dummdidumm authored Aug 19, 2022
    Copy the full SHA
    28f7de3 View commit details
  5. fix: prevent optimizeDeps for @sveltejs/kit (#6057)

    * fix: prevent optimizeDeps for @sveltejs/kit
    
    * fix: format
    dominikg authored Aug 19, 2022
    Copy the full SHA
    6c84f40 View commit details
  6. Version Packages (next) (#6073)

    Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
    github-actions[bot] and github-actions[bot] authored Aug 19, 2022
    Copy the full SHA
    6ccc735 View commit details
5 changes: 5 additions & 0 deletions .changeset/famous-boxes-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

prevent duplicate module ids by disabling optimizeDeps for @sveltejs/kit
5 changes: 5 additions & 0 deletions .changeset/ninety-snakes-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte-migrate': patch
---

Suggest props destructuring if possible
5 changes: 5 additions & 0 deletions .changeset/ninety-vans-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Generate types when Svelte file missing, fix layout params
5 changes: 5 additions & 0 deletions .changeset/old-berries-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte-migrate': patch
---

Fix typo in migration task
5 changes: 5 additions & 0 deletions .changeset/pre.json
Original file line number Diff line number Diff line change
@@ -240,6 +240,7 @@
"fair-pets-bake",
"famous-birds-boil",
"famous-birds-cheer",
"famous-boxes-tap",
"famous-cameras-cheer",
"famous-crabs-run",
"famous-donuts-whisper",
@@ -627,7 +628,9 @@
"ninety-games-flow",
"ninety-glasses-exercise",
"ninety-singers-invite",
"ninety-snakes-melt",
"ninety-suns-relax",
"ninety-vans-talk",
"odd-bottles-drop",
"odd-coats-shake",
"odd-coins-lie",
@@ -641,6 +644,7 @@
"odd-pumas-grow",
"odd-trees-visit",
"old-beers-push",
"old-berries-develop",
"old-cars-switch",
"old-comics-happen",
"old-ears-wait",
@@ -1056,6 +1060,7 @@
"tiny-humans-yawn",
"tiny-kids-sort",
"tiny-moles-joke",
"tiny-pears-check",
"tiny-shrimps-call",
"tiny-singers-allow",
"tiny-socks-sparkle",
5 changes: 5 additions & 0 deletions .changeset/tiny-pears-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Silence unknown prop warnings coming from SvelteKit
10 changes: 10 additions & 0 deletions packages/kit/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# @sveltejs/kit

## 1.0.0-next.424

### Patch Changes

- prevent duplicate module ids by disabling optimizeDeps for @sveltejs/kit ([#6057](https://github.com/sveltejs/kit/pull/6057))

* Generate types when Svelte file missing, fix layout params ([#6066](https://github.com/sveltejs/kit/pull/6066))

- Silence unknown prop warnings coming from SvelteKit ([#6071](https://github.com/sveltejs/kit/pull/6071))

## 1.0.0-next.423

### Patch Changes
2 changes: 1 addition & 1 deletion packages/kit/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sveltejs/kit",
"version": "1.0.0-next.423",
"version": "1.0.0-next.424",
"repository": {
"type": "git",
"url": "https://github.com/sveltejs/kit",
7 changes: 4 additions & 3 deletions packages/kit/src/core/sync/write_types.js
Original file line number Diff line number Diff line change
@@ -162,10 +162,11 @@ function get_groups(manifest_data, routes_dir) {
/** @type {Node} */
const node = { ...nodes[i] }; // shallow copy so we don't mutate the original when setting parent

const file_path = /** @type {string} */ (node.component ?? node.shared ?? node.server);
// skip default layout/error
if (!node?.component?.startsWith(routes_dir)) continue;
if (!file_path.startsWith(routes_dir)) continue;

const parts = /** @type {string} */ (node.component ?? node.shared ?? node.server).split('/');
const parts = file_path.split('/');

const file = /** @type {string} */ (parts.pop());
const dir = parts.join('/').slice(routes_dir.length + 1);
@@ -284,7 +285,7 @@ function write_types_for_dir(config, manifest_data, routes_dir, dir, groups, ts)
manifest_data.routes.forEach((route) => {
if (route.type === 'page' && route.id.startsWith(dir + '/')) {
// TODO this is O(n^2), see if we need to speed it up
for (const name of parse_route_id(route.id).names) {
for (const name of parse_route_id(route.id.slice(dir.length + 1)).names) {
layout_params.add(name);
}
}
46 changes: 40 additions & 6 deletions packages/kit/src/runtime/client/client.js
Original file line number Diff line number Diff line change
@@ -268,7 +268,22 @@ export function create_client({ target, base, trailing_slash }) {
navigation_result.props.page.url = url;
}

root.$set(navigation_result.props);
if (import.meta.env.DEV) {
// Nasty hack to silence harmless warnings the user can do nothing about
const warn = console.warn;
console.warn = (...args) => {
if (
args.length !== 1 ||
!/<(Layout|Page)> was created with unknown prop '(data|errors)'/.test(args[0])
) {
warn(...args);
}
};
root.$set(navigation_result.props);
tick().then(() => (console.warn = warn));
} else {
root.$set(navigation_result.props);
}
} else {
initialize(navigation_result);
}
@@ -347,11 +362,30 @@ export function create_client({ target, base, trailing_slash }) {

page = result.props.page;

root = new Root({
target,
props: { ...result.props, stores },
hydrate: true
});
if (import.meta.env.DEV) {
// Nasty hack to silence harmless warnings the user can do nothing about
const warn = console.warn;
console.warn = (...args) => {
if (
args.length !== 1 ||
!/<(Layout|Page)> was created with unknown prop '(data|errors)'/.test(args[0])
) {
warn(...args);
}
};
root = new Root({
target,
props: { ...result.props, stores },
hydrate: true
});
console.warn = warn;
} else {
root = new Root({
target,
props: { ...result.props, stores },
hydrate: true
});
}

if (router_enabled) {
const navigation = { from: null, to: new URL(location.href) };
3 changes: 3 additions & 0 deletions packages/kit/src/vite/build/utils.js
Original file line number Diff line number Diff line change
@@ -123,6 +123,9 @@ export function get_default_build_config({ config, input, ssr, outDir }) {
resolve: {
alias: get_aliases(config.kit)
},
optimizeDeps: {
exclude: ['@sveltejs/kit']
},
ssr: {
noExternal: ['@sveltejs/kit']
},
3 changes: 3 additions & 0 deletions packages/kit/src/vite/index.js
Original file line number Diff line number Diff line change
@@ -273,6 +273,9 @@ function kit() {
// under different IDs, which breaks a bunch of stuff
// https://github.com/vitejs/vite/pull/9296
external: ['@sveltejs/kit']
},
optimizeDeps: {
exclude: ['@sveltejs/kit']
}
};

8 changes: 8 additions & 0 deletions packages/migrate/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# svelte-migrate

## 1.0.0-next.8

### Patch Changes

- Suggest props destructuring if possible ([#6069](https://github.com/sveltejs/kit/pull/6069))

* Fix typo in migration task ([#6070](https://github.com/sveltejs/kit/pull/6070))

## 1.0.0-next.7

### Patch Changes
7 changes: 6 additions & 1 deletion packages/migrate/migrations/routes/index.js
Original file line number Diff line number Diff line change
@@ -148,7 +148,12 @@ export async function migrate() {

renamed += svelte_ext;

const { module, main, ext } = migrate_scripts(content, is_error_page, move_to_directory);
const { module, main, ext } = migrate_scripts(
content,
bare,
is_error_page,
move_to_directory
);

if (move_to_directory) {
const dir = path.dirname(renamed);
104 changes: 97 additions & 7 deletions packages/migrate/migrations/routes/migrate_scripts/index.js
Original file line number Diff line number Diff line change
@@ -9,16 +9,21 @@ import {
except_str
} from '../utils.js';
import * as TASKS from '../tasks.js';
import MagicString from 'magic-string';

/**
* @param {string} content
* @param {string} filename
* @param {boolean} is_error
* @param {boolean} moved
*/
export function migrate_scripts(content, is_error, moved) {
export function migrate_scripts(content, filename, is_error, moved) {
/** @type {string | null} */
let module = null;

const match = /__layout(?:-([^.@]+))?/.exec(filename);
const data_name = match?.[1] ? `LayoutData.${match[1]}` : match ? `LayoutData` : 'PageData';

let ext = '.js';

// instance script
@@ -31,6 +36,10 @@ export function migrate_scripts(content, is_error, moved) {
contents = adjust_imports(contents);
}

if (/lang(?:uage)?=(['"])(ts|typescript)\1/.test(attrs)) {
ext = '.ts';
}

if (/context=(['"])module\1/.test(attrs)) {
// special case — load is no longer supported in error
if (is_error) {
@@ -42,10 +51,6 @@ export function migrate_scripts(content, is_error, moved) {
return `<script${attrs}>${body}</script>${whitespace}`;
}

if (/lang(?:uage)?=(['"])(ts|typescript)\1/.test(attrs)) {
ext = '.ts';
}

module = dedent(contents.replace(/^\n/, ''));

const declared = find_declarations(contents);
@@ -88,8 +93,22 @@ export function migrate_scripts(content, is_error, moved) {
}

if (!is_error && /export/.test(contents)) {
contents = `\n${indent}${error('Add data prop', TASKS.PAGE_DATA_PROP)}\n${contents}`;
// Possible TODO: migrate props to data.prop, or suggest $: ({propX, propY, ...} = data);
let prefix = `\n${indent}${error('Add data prop', TASKS.PAGE_DATA_PROP)}`;
const props = get_props(contents);
if (props) {
prefix += `\n${comment(
`${indent}Suggestion (check code before using, and possibly convert to data.X access later):\n` +
`${indent}${
ext === '.ts'
? `import type { ${data_name} } from './$types';`
: `/** @type {import('./$types').${data_name}} */`
}\n` +
`${indent}export let data${ext === '.ts' ? `: ${data_name}` : ''};\n` +
`${indent}$: ({ ${props.join(', ')} } = data);`,
indent
)}`;
}
contents = `${prefix}\n${contents}`;
}

return `<script${attrs}>${contents}</script>${whitespace}`;
@@ -155,3 +174,74 @@ function find_declarations(content) {

return declared;
}

/**
* @param {string} content
*/
function get_props(content) {
try {
const ast = ts.createSourceFile(
'filename.ts',
content,
ts.ScriptTarget.Latest,
true,
ts.ScriptKind.TS
);

const code = new MagicString(content);
let give_up = false;
/** @type {string[] } */
let exports = [];

/** @param {ts.Node} node */
function walk(node) {
if (
ts.isExportDeclaration(node) ||
((ts.isFunctionDeclaration(node) || ts.isClassDeclaration(node)) && hasExportKeyword(node))
) {
give_up = true;
return;
}

if (ts.isVariableStatement(node)) {
if (hasExportKeyword(node)) {
const isLet = node.declarationList.flags === ts.NodeFlags.Let;
if (isLet) {
ts.forEachChild(node.declarationList, (node) => {
if (ts.isVariableDeclaration(node)) {
if (ts.isIdentifier(node.name)) {
const name = node.name.getText();
if (name === 'data') {
give_up = true;
return;
} else {
exports.push(name);
}
} else {
give_up = true;
return;
}
}
});
} else {
give_up = true;
return;
}
}
}
}

ts.forEachChild(ast, walk);
if (give_up) return;
return exports;
} catch (e) {
return;
}
}

/**
* @param {ts.Node} node
*/
export function hasExportKeyword(node) {
return node.modifiers?.find((x) => x.kind == ts.SyntaxKind.ExportKeyword);
}
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ for (const sample of read_samples(import.meta.url)) {
test(sample.description, () => {
const actual = migrate_scripts(
sample.before,
sample.filename ?? 'some-page.svelte',
sample.description.includes('error'),
sample.description.includes('moved')
);
Loading