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

DRY things out and tidy up #454

Merged
merged 10 commits into from
Mar 6, 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
9 changes: 3 additions & 6 deletions packages/kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,12 @@
"check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore",
"prepublishOnly": "npm run build",
"test": "npm run test:unit && npm run test:integration",
"test:unit": "uvu src \"(spec\\.mjs|test[\\\\/]index\\.mjs)\"",
"test:unit": "uvu src \"(spec\\.js|test[\\\\/]index\\.js)\"",
"test:integration": "uvu test test.js"
},
"exports": {
"./api": {
"import": "./dist/api.js"
},
"./renderer": {
"import": "./dist/renderer.js"
"./ssr": {
"import": "./dist/ssr.js"
}
},
"types": "types.d.ts"
Expand Down
13 changes: 4 additions & 9 deletions packages/kit/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ const external = [].concat(

export default [
{
// TODO could we just put `start.js` in `assets`, and everything
// else gets installed to `/web_modules`?
input: {
'internal/start': 'src/runtime/internal/start.js',
'internal/singletons': 'src/runtime/internal/singletons.js',
'internal/start': 'src/runtime/client/start.js',
'internal/singletons': 'src/runtime/client/singletons.js',
'app/navigation': 'src/runtime/app/navigation.js',
'app/stores': 'src/runtime/app/stores.js',
'app/paths': 'src/runtime/app/paths.js',
Expand All @@ -25,7 +23,6 @@ export default [
dir: 'assets/runtime',
format: 'esm',
chunkFileNames: 'chunks/[name].js',
sourcemap: true,
paths: {
ROOT: '../../generated/root.svelte',
MANIFEST: '../../generated/manifest.js'
Expand All @@ -42,14 +39,12 @@ export default [
{
input: {
cli: 'src/cli.js',
api: 'src/api/index.js',
renderer: 'src/renderer/index.js'
ssr: 'src/runtime/server/index.js'
},
output: {
dir: 'dist',
format: 'esm',
sourcemap: true,
chunkFileNames: '[name].js'
chunkFileNames: 'chunks/[name].js'
},
external: (id) => {
return external.includes(id);
Expand Down
4 changes: 0 additions & 4 deletions packages/kit/src/api/index.js

This file was deleted.

12 changes: 6 additions & 6 deletions packages/kit/src/cli.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { existsSync } from 'fs';
import sade from 'sade';
import colors from 'kleur';
import { load_config } from './api/load_config';
import { load_config } from './core/load_config/index.js';

async function get_config() {
// TODO this is temporary, for the benefit of early adopters
Expand Down Expand Up @@ -72,7 +72,7 @@ prog
process.env.NODE_ENV = 'development';
const config = await get_config();

const { dev } = await import('./api/dev');
const { dev } = await import('./core/dev/index.js');

try {
const watcher = await dev({ port, config });
Expand Down Expand Up @@ -100,14 +100,14 @@ prog
process.env.NODE_ENV = 'production';
const config = await get_config();

const { build } = await import('./api/build');
const { adapt } = await import('./api/adapt');

try {
const { build } = await import('./core/build/index.js');
await build(config);

console.log(`\nRun ${colors.bold().cyan('npm start')} to try your app locally.`);

if (config.kit.adapter[0]) {
const { adapt } = await import('./core/adapt/index.js');
await adapt(config, { verbose });
} else {
console.log(colors.bold().yellow('\nNo adapter specified'));
Expand All @@ -131,7 +131,7 @@ prog
process.env.NODE_ENV = 'production';
const config = await get_config();

const { start } = await import('./api/start');
const { start } = await import('./core/start/index.js');

try {
await start({ port, config });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fs from 'fs';
import path from 'path';
import { rimraf } from '@sveltejs/app-utils/files';
import create_manifest_data from '../../core/create_manifest_data.js';
import create_manifest_data from '../../core/create_manifest_data/index.js';
import { copy_assets } from '../utils.js';
import { create_app } from '../../core/create_app.js';
import { create_app } from '../../core/create_app/index.js';
import vite from 'vite';
import svelte from '@svitejs/vite-plugin-svelte';

Expand All @@ -14,13 +14,10 @@ const s = (value) => JSON.stringify(value);
* @param {import('../../types').ValidatedConfig} config
* @param {{
* cwd?: string;
* renderer?: string;
* runtime?: string;
* }} [opts]
*/
export async function build(
config,
{ cwd = process.cwd(), renderer = '@sveltejs/kit/renderer' } = {}
) {
export async function build(config, { cwd = process.cwd(), runtime = '@sveltejs/kit/ssr' } = {}) {
const build_dir = path.resolve(cwd, '.svelte/build');
const output_dir = path.resolve(cwd, '.svelte/output');

Expand Down Expand Up @@ -138,7 +135,7 @@ export async function build(
fs.writeFileSync(
app_file,
`
import * as renderer from '${renderer}';
import { ssr } from '${runtime}';
import root from './generated/root.svelte';
import { set_paths } from './runtime/internal/singletons.js';
import * as setup from ${s(app_relative(setup_file))};
Expand Down Expand Up @@ -236,7 +233,7 @@ export async function build(
only_prerender = false,
get_static_file
} = {}) {
return renderer.render(request, {
return ssr(request, {
paths,
local,
template,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import fs from 'fs';
import path from 'path';
import { mkdirp } from '@sveltejs/app-utils/files';
import { stringify, walk } from '../utils.js';

/** @type {Map<string, string>} */
const previous_contents = new Map();
Expand All @@ -20,7 +19,7 @@ export function write_if_changed(file, code) {

const s = JSON.stringify;

/** @typedef {import('../types').ManifestData} ManifestData */
/** @typedef {import('../../types').ManifestData} ManifestData */

/**
* @param {{
Expand Down Expand Up @@ -57,27 +56,21 @@ export function create_serviceworker_manifest({
client_files,
static_files
}) {
let files = ['service-worker-index.html'];

if (fs.existsSync(static_files)) {
files = files.concat(walk(static_files));
}

const code = trim(`
// This file is generated by @sveltejs/kit — do not edit it!
export const timestamp = ${Date.now()};

export const files = [\n\t${files.map((x) => stringify('/' + x)).join(',\n\t')}\n];
export { files as assets }; // legacy

export const shell = [\n\t${client_files.map((x) => stringify('/' + x)).join(',\n\t')}\n];

export const routes = [\n\t${manifest_data.pages
.map((r) => `{ pattern: ${r.pattern} }`)
.join(',\n\t')}\n];
`);

write_if_changed(`${output}/service-worker.js`, code);
// let files = ['service-worker-index.html'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This big block of commented-out code looks suspicious.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah it's not actually called anywhere at the moment, that'll be the focus of #10, which i was hoping to make a start on very soon

// if (fs.existsSync(static_files)) {
// files = files.concat(walk(static_files));
// }
// const code = trim(`
// // This file is generated by @sveltejs/kit — do not edit it!
// export const timestamp = ${Date.now()};
// export const files = [\n\t${files.map((x) => s('/' + x)).join(',\n\t')}\n];
// export { files as assets }; // legacy
// export const shell = [\n\t${client_files.map((x) => s('/' + x)).join(',\n\t')}\n];
// export const routes = [\n\t${manifest_data.pages
// .map((r) => `{ pattern: ${r.pattern} }`)
// .join(',\n\t')}\n];
// `);
// write_if_changed(`${output}/service-worker.js`, code);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import fs from 'fs';
import path from 'path';
import mime from 'mime';
import { posixify } from '../utils.js';

/** @typedef {{
* content: string;
Expand All @@ -23,11 +22,11 @@ import { posixify } from '../utils.js';

/**
* @param {{
* config: import('../types').ValidatedConfig;
* config: import('../../types').ValidatedConfig;
* output: string;
* cwd?: string;
* }} opts
* @returns {import('../types.js').ManifestData}
* @returns {import('../../types').ManifestData}
*/
export default function create_manifest_data({ config, output, cwd = process.cwd() }) {
/**
Expand All @@ -42,10 +41,10 @@ export default function create_manifest_data({ config, output, cwd = process.cwd
/** @type {string[]} */
const components = [];

/** @type {import('../types.js').PageData[]} */
/** @type {import('../../types').PageData[]} */
const pages = [];

/** @type {import('../types.js').EndpointData[]} */
/** @type {import('../../types').EndpointData[]} */
const endpoints = [];

/** @type {Map<string, string>} */
Expand Down Expand Up @@ -218,9 +217,12 @@ export default function create_manifest_data({ config, output, cwd = process.cwd
};
}

/**
* @param {string} path
*/
/** @param {string} str */
function posixify(str) {
return str.replace(/\\/g, '/');
}

/** @param {string} path */
function is_spread(path) {
const spread_pattern = /\[\.{3}/g;
return spread_pattern.test(path);
Expand Down Expand Up @@ -284,9 +286,7 @@ function comparator(a, b) {
}
}

/**
* @param {string} part
*/
/** @param {string} part */
function get_parts(part) {
return part
.split(/\[(.+?\(.+?\)|.+?)\]/)
Expand Down Expand Up @@ -336,7 +336,7 @@ function get_pattern(segments, add_trailing_slash) {
/**
* @param {string} dir
* @param {string} path
* @param {import('../types').Asset[]} files
* @param {import('../../types').Asset[]} files
*/
function list_files(dir, path, files = []) {
fs.readdirSync(dir).forEach((file) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import path from 'path';
import { fileURLToPath } from 'url';
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import create_manifest_data from '../create_manifest_data.js';
import create_manifest_data from './index.js';

const __filename = fileURLToPath(import.meta.url);
const cwd = path.join(__filename, '..');
const cwd = fileURLToPath(new URL('./test', import.meta.url));

const create = (dir, extensions = ['.svelte']) => {
return create_manifest_data({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { EventEmitter } from 'events';
import CheapWatch from 'cheap-watch';
import amp_validator from 'amphtml-validator';
import vite from 'vite';
import create_manifest_data from '../../core/create_manifest_data.js';
import { create_app } from '../../core/create_app.js';
import create_manifest_data from '../../core/create_manifest_data/index.js';
import { create_app } from '../../core/create_app/index.js';
import { rimraf } from '@sveltejs/app-utils/files';
import { render } from '../../renderer/index.js';
import { ssr } from '../../runtime/server/index.js';
import { get_body } from '@sveltejs/app-utils/http';
import { copy_assets } from '../utils.js';
import svelte from '@svitejs/vite-plugin-svelte';
Expand Down Expand Up @@ -128,7 +128,7 @@ class Watcher extends EventEmitter {

const body = await get_body(req);

const rendered = await render(
const rendered = await ssr(
{
headers: req.headers,
method: req.method,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const mutable = (dir) =>
* @param {{
* port: number;
* config: import('../../types').ValidatedConfig;
* cwd: string;
* cwd?: string;
* }} opts
* @returns {Promise<import('http').Server>}
*/
Expand Down
File renamed without changes.
20 changes: 15 additions & 5 deletions packages/kit/src/runtime/app/navigation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { router, renderer } from '../internal/singletons';
import { get_base_uri } from '../internal/utils';
import { router, renderer } from '../client/singletons.js';
import { get_base_uri } from '../client/utils.js';

function guard(name) {
return () => {
throw new Error(`Cannot call ${name}(...) on the server`);
};
}

export const goto = import.meta.env.SSR ? guard('goto') : goto_;
export const prefetch = import.meta.env.SSR ? guard('prefetch') : prefetch_;
export const prefetchRoutes = import.meta.env.SSR ? guard('prefetchRoutes') : prefetchRoutes_;

/**
* @param {string} href
Expand All @@ -8,17 +18,17 @@ import { get_base_uri } from '../internal/utils';
* resplaceState?: boolean;
* }} [opts]
*/
export async function goto(href, opts) {
async function goto_(href, opts) {
return router.goto(href, opts, []);
}

/** @param {string} href */
export function prefetch(href) {
function prefetch_(href) {
return renderer.prefetch(new URL(href, get_base_uri(document)));
}

/** @param {string[]} [pathnames] */
export async function prefetchRoutes(pathnames) {
async function prefetchRoutes_(pathnames) {
const path_routes = pathnames
? router.pages.filter((page) => pathnames.some((pathname) => page.pattern.test(pathname)))
: router.pages;
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/app/paths.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { base, assets } from '../internal/singletons';
export { base, assets } from '../paths.js';
Loading