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

Fix $app/paths import not working at dev mode inside context="module" #2145

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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 packages/kit/src/core/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,12 @@ async function create_handler(vite, config, dir, cwd, manifest) {

const root = (await vite.ssrLoadModule(`/${dir}/generated/root.svelte`)).default;

const paths = await vite.ssrLoadModule(`/${SVELTE_KIT}/dev/runtime/paths.js`);

if (paths != null && config.kit.paths != null) {
paths.set_paths(config.kit.paths);
}

let body;

try {
Expand Down
16 changes: 11 additions & 5 deletions packages/kit/src/core/preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ export async function preview({
});

app.init({
paths: {
base: '',
assets: '/.'
},
paths: config.kit.paths,
prerendering: false,
read: (file) => fs.readFileSync(join(config.kit.files.assets, file))
});
Expand All @@ -63,7 +60,14 @@ export async function preview({
const vite_config = (config.kit.vite && config.kit.vite()) || {};

const server = await get_server(use_https, vite_config, (req, res) => {
const parsed = parse(req.url || '');
const initial_url = req.url;
const { assets } = config.kit.paths;

// Emulate app.use(`${assets}/`, sirv(...))
req.url =
assets.length > 1 && assets !== '/.' && req.url?.startsWith(`${assets}/`)
benmccann marked this conversation as resolved.
Show resolved Hide resolved
? req.url.slice(assets.length)
: req.url;

assets_handler(req, res, () => {
static_handler(req, res, async () => {
Expand All @@ -78,6 +82,8 @@ export async function preview({
return res.end(err.reason || 'Invalid request body');
}

const parsed = parse(initial_url || '');

const rendered = await app.render({
host: /** @type {string} */ (config.kit.host ||
req.headers[config.kit.hostHeader || 'host']),
Expand Down
16 changes: 16 additions & 0 deletions packages/kit/test/apps/options-paths-base/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "test-options",
"private": true,
"version": "0.0.1",
"scripts": {
"dev": "../../../svelte-kit.js dev",
"build": "../../../svelte-kit.js build",
"preview": "../../../svelte-kit.js preview"
},
"devDependencies": {
"@sveltejs/adapter-node": "workspace:*",
"@sveltejs/kit": "workspace:*",
"svelte": "^3.40.0"
},
"type": "module"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as assert from 'uvu/assert';

/** @type {import('test').TestMaker} */
export default function (test) {
test('sets_paths', '/base/', async ({ page }) => {
assert.equal(await page.textContent('[data-source="base"]'), '/path-base');
assert.equal(await page.textContent('[data-source="assets"]'), '/path-base');
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<script context="module">
import { base, assets } from '$app/paths';

/** @type {import('@sveltejs/kit').Load} */
export async function load({ page, fetch }) {
return {
props: {
data: {
base,
assets
}
}
};
}
</script>

<script>
/** @type {any} */
export let data;
</script>

<p data-source="base">{data.base}</p>
<p data-source="assets">{data.assets}</p>
12 changes: 12 additions & 0 deletions packages/kit/test/apps/options-paths-base/source/template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
%svelte.head%
</head>
<body>
<h1>I am in the template</h1>
<div id="content-goes-here">%svelte.body%</div>
</body>
</html>
29 changes: 29 additions & 0 deletions packages/kit/test/apps/options-paths-base/svelte.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/** @type {import('@sveltejs/kit').Config} */
const config = {
extensions: ['.jesuslivesineveryone', '.whokilledthemuffinman', '.svelte.md', '.svelte'],
kit: {
files: {
assets: 'public',
lib: 'source/components',
routes: 'source/pages',
template: 'source/template.html'
},
// appDir: '_wheee',
floc: true,
target: '#content-goes-here',
host: 'example.com',
trailingSlash: 'always',
vite: {
build: {
minify: false
},
clearScreen: false
},
paths: {
base: '/path-base',
benmccann marked this conversation as resolved.
Show resolved Hide resolved
assets: ''
}
}
};

export default config;