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

Overhaul adapter API #2931

Merged
merged 108 commits into from
Dec 29, 2021
Merged
Changes from 1 commit
Commits
Show all changes
108 commits
Select commit Hold shift + click to select a range
2ba38e8
start fiddling about with code-split server code
Rich-Harris Nov 25, 2021
0f62263
generate build data
Rich-Harris Nov 26, 2021
78652d9
Merge branch 'master' into function-per-page
Rich-Harris Nov 26, 2021
7301352
working, minus adapters
Rich-Harris Nov 26, 2021
2dda44a
prerendering
Rich-Harris Nov 26, 2021
be773cb
WIP
Rich-Harris Nov 27, 2021
e04303e
tidy some stuff up
Rich-Harris Nov 28, 2021
3fcbdad
fix prefixes
Rich-Harris Nov 28, 2021
9b82b0b
adapter-node working
Rich-Harris Nov 28, 2021
89420d4
tidy up
Rich-Harris Nov 28, 2021
0d13e4c
tidy
Rich-Harris Nov 28, 2021
0ac7c8d
make copy opts optional
Rich-Harris Nov 28, 2021
44f53c7
tidy
Rich-Harris Nov 28, 2021
17e24fc
netlify working albeit with single function
Rich-Harris Nov 29, 2021
f0ee8fa
expose route data to adapters
Rich-Harris Nov 29, 2021
ea3eee1
some progress
Rich-Harris Nov 29, 2021
a0a66d1
lint
Rich-Harris Nov 29, 2021
c36c55f
remove sandbox example from PR
Rich-Harris Nov 29, 2021
785153b
implement createEntries API
Rich-Harris Nov 30, 2021
b3b1c3c
fix some tests
Rich-Harris Nov 30, 2021
5466250
AMP fix
Rich-Harris Nov 30, 2021
e51deba
tests all passing
Rich-Harris Nov 30, 2021
8828f11
update gitignore
Rich-Harris Nov 30, 2021
1407e57
disable adapter-node tests for now
Rich-Harris Nov 30, 2021
11e3b91
typechecking
Rich-Harris Dec 1, 2021
4328dae
always create prerendered output directory
Rich-Harris Dec 1, 2021
158964b
manifest -> generateManifest, add utils-level method
Rich-Harris Dec 1, 2021
a1a2e70
spread -> rest
Rich-Harris Dec 1, 2021
3a63cbb
finesse return value of copy helper
Rich-Harris Dec 1, 2021
786f5b0
no need to pass config through to adapter
Rich-Harris Dec 1, 2021
1f6f45a
Update packages/kit/src/core/build/index.js
Rich-Harris Dec 1, 2021
1fc9ab4
Merge branch 'function-per-page' of github.com:sveltejs/kit into func…
Rich-Harris Dec 1, 2021
4b18f16
fix
Rich-Harris Dec 1, 2021
53e05b4
refactor out method extraction
Rich-Harris Dec 1, 2021
078be6c
add comment to resolve separate routes/components entry point detection
Rich-Harris Dec 1, 2021
5895f75
remove out of date comment
Rich-Harris Dec 1, 2021
4a158f0
nicer rollup output types
Rich-Harris Dec 1, 2021
1158649
actually that caused chaos, doing this instead
Rich-Harris Dec 1, 2021
80256a1
fix tests
Rich-Harris Dec 1, 2021
39c83af
update adapter-cloudflare
Rich-Harris Dec 1, 2021
0b6e0c8
merge master -> funtion-per-page
Rich-Harris Dec 1, 2021
0b04e81
refactor build functions into client/server/service worker modules
Rich-Harris Dec 1, 2021
4f2ae17
make build_server more readable
Rich-Harris Dec 1, 2021
b58eb43
oops
Rich-Harris Dec 1, 2021
32006b4
gah
Rich-Harris Dec 1, 2021
5d12522
windows fix
Rich-Harris Dec 1, 2021
28a5054
split manifest into public and private parts, so we can use manifest.…
Rich-Harris Dec 1, 2021
317111e
lint
Rich-Harris Dec 1, 2021
9c45146
prevent duplication of asset list in functions
Rich-Harris Dec 1, 2021
dc6314f
exclude prerendered pages from functions
Rich-Harris Dec 1, 2021
9f3ee18
lint
Rich-Harris Dec 1, 2021
c0e6abf
get vercel adapter working (with v1 filesystem API)
Rich-Harris Dec 2, 2021
2f68be1
target node14
Rich-Harris Dec 3, 2021
d2dc49d
rename utils -> builder
Rich-Harris Dec 3, 2021
bd03083
add methods for getting directories instead of hardcoding .svelte-kit…
Rich-Harris Dec 3, 2021
22ffd4d
fix lockfile
Rich-Harris Dec 3, 2021
d758024
update cloudflare adapters
Rich-Harris Dec 3, 2021
485bb27
update readmes
Rich-Harris Dec 3, 2021
4f12eb4
tidy up
Rich-Harris Dec 3, 2021
31dd184
tweak docs
Rich-Harris Dec 3, 2021
d4a9c12
remove sandbox from workspace
Rich-Harris Dec 3, 2021
b2e2d4f
try pinning version
Dec 3, 2021
373a165
Update packages/kit/src/core/build/build_server.js
Rich-Harris Dec 3, 2021
150ee18
remove redundant strict fs stuff
Dec 3, 2021
6836f42
Merge branch 'function-per-page' of github.com:sveltejs/kit into func…
Dec 3, 2021
628f625
remove sandbox from gitignore
Dec 3, 2021
91de58a
merge master -> function-per-page
Rich-Harris Dec 26, 2021
17d3fea
merge master -> function-per-page
Rich-Harris Dec 26, 2021
ebf1f1f
lint
Rich-Harris Dec 26, 2021
f7a8115
fix cloudflare pages handling of prerendered pages
Rich-Harris Dec 27, 2021
bdc64ff
support esm and cjs manifests
Rich-Harris Dec 27, 2021
608fd3d
force createEntries to follow prerender
Rich-Harris Dec 27, 2021
fc17a67
fix types
Rich-Harris Dec 27, 2021
275628e
fix adapter-netlify
Rich-Harris Dec 27, 2021
a522611
separate App from InternalApp
Rich-Harris Dec 27, 2021
d257702
Update packages/kit/types/internal.d.ts
Rich-Harris Dec 27, 2021
64be780
expose SSRManifest
Rich-Harris Dec 27, 2021
c7958ba
fall back to HTTP fetch if endpoint is missing
Rich-Harris Dec 27, 2021
c49e4cd
app.render should not be passed a host string
Rich-Harris Dec 27, 2021
5233b25
default hostHeader to host in config
Rich-Harris Dec 27, 2021
dd40079
fix types
Rich-Harris Dec 27, 2021
21dfb9b
replace page.host with page.origin
Rich-Harris Dec 27, 2021
e7aaf32
fix host stuff
Rich-Harris Dec 27, 2021
57a8464
make SSR route splitting optional
Rich-Harris Dec 27, 2021
0d20729
simplify createEntries
Rich-Harris Dec 27, 2021
351674c
these are no longer generic
Rich-Harris Dec 27, 2021
c32df77
implement .json heuristic
Rich-Harris Dec 28, 2021
d057b0d
lint
Rich-Harris Dec 28, 2021
b459265
changesets
Rich-Harris Dec 28, 2021
7ebc950
changeset
Rich-Harris Dec 28, 2021
6f4c36e
merge master -> function-per-page
Rich-Harris Dec 28, 2021
a9093c8
Update packages/adapter-netlify/index.js
Rich-Harris Dec 28, 2021
d481e09
Update packages/adapter-node/README.md
Rich-Harris Dec 28, 2021
de897a2
this.server -> this.vite
Rich-Harris Dec 28, 2021
b6613c9
Merge branch 'function-per-page' of github.com:sveltejs/kit into func…
Rich-Harris Dec 28, 2021
ace5f35
give a bare-bones description of createEntries
Rich-Harris Dec 28, 2021
246b88c
lint
Rich-Harris Dec 28, 2021
5d810ec
merge master -> function-per-page
Rich-Harris Dec 28, 2021
c51641b
add missing origin
Rich-Harris Dec 28, 2021
c096dfa
remove TODO
Rich-Harris Dec 28, 2021
ed75d52
replace ts-ignore with ts-expect-error
Rich-Harris Dec 28, 2021
f1c84a6
Update packages/kit/src/core/adapt/builder.js
Rich-Harris Dec 28, 2021
cd60fae
document AdapterEntry
Rich-Harris Dec 28, 2021
77486c0
Merge branch 'function-per-page' of github.com:sveltejs/kit into func…
Rich-Harris Dec 28, 2021
052dbf7
Update packages/kit/types/config.d.ts
Rich-Harris Dec 28, 2021
53d0007
merge master -> function-per-page
Rich-Harris Dec 29, 2021
d4d071e
oops
Rich-Harris Dec 29, 2021
0de1aa3
manifest -> vite_manifest
Rich-Harris Dec 29, 2021
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
Prev Previous commit
Next Next commit
rename utils -> builder
  • Loading branch information
Rich-Harris committed Dec 3, 2021
commit d2dc49dc77f43200ac68b0873b0ed7f7be5a26ef
10 changes: 4 additions & 6 deletions packages/adapter-auto/index.js
Original file line number Diff line number Diff line change
@@ -5,12 +5,10 @@ export default function () {
return {
name: '@sveltejs/adapter-auto',

async adapt(options) {
async adapt(builder) {
for (const candidate of adapters) {
if (candidate.test()) {
options.utils.log.info(
`Detected environment: ${candidate.name}. Using ${candidate.module}`
);
builder.log.info(`Detected environment: ${candidate.name}. Using ${candidate.module}`);

let module;

@@ -30,11 +28,11 @@ export default function () {
}

const adapter = module.default();
return adapter.adapt(options);
return adapter.adapt(builder);
}
}

options.utils.log.warn(
builder.log.warn(
'Could not detect a supported production environment. See https://kit.svelte.dev/docs#adapters to learn how to configure your app to run on the platform of your choosing'
);
}
34 changes: 17 additions & 17 deletions packages/adapter-cloudflare-workers/index.js
Original file line number Diff line number Diff line change
@@ -13,26 +13,26 @@ export default function (options) {
return {
name: '@sveltejs/adapter-cloudflare-workers',

async adapt(utils) {
const { site } = validate_config(utils);
async adapt(builder) {
const { site } = validate_config(builder);

const bucket = site.bucket;
const entrypoint = site['entry-point'] || 'workers-site';

const files = fileURLToPath(new URL('./files', import.meta.url));

utils.rimraf(bucket);
utils.rimraf(entrypoint);
builder.rimraf(bucket);
builder.rimraf(entrypoint);

utils.log.info('Installing worker dependencies...');
utils.copy(`${files}/_package.json`, '.svelte-kit/cloudflare-workers/package.json');
builder.log.info('Installing worker dependencies...');
builder.copy(`${files}/_package.json`, '.svelte-kit/cloudflare-workers/package.json');

// TODO would be cool if we could make this step unnecessary somehow
const stdout = execSync('npm install', { cwd: '.svelte-kit/cloudflare-workers' });
utils.log.info(stdout.toString());
builder.log.info(stdout.toString());

utils.log.minor('Generating worker...');
utils.copy(`${files}/entry.js`, '.svelte-kit/cloudflare-workers/entry.js');
builder.log.minor('Generating worker...');
builder.copy(`${files}/entry.js`, '.svelte-kit/cloudflare-workers/entry.js');

/** @type {BuildOptions} */
const default_options = {
@@ -50,19 +50,19 @@ export default function (options) {

fs.writeFileSync(`${entrypoint}/package.json`, JSON.stringify({ main: 'index.js' }));

utils.log.info('Prerendering static pages...');
await utils.prerender({
builder.log.info('Prerendering static pages...');
await builder.prerender({
dest: bucket
});

utils.log.minor('Copying assets...');
utils.copy_static_files(bucket);
utils.copy_client_files(bucket);
builder.log.minor('Copying assets...');
builder.copy_static_files(bucket);
builder.copy_client_files(bucket);
}
};
}

function validate_config(utils) {
function validate_config(builder) {
if (fs.existsSync('wrangler.toml')) {
let wrangler_config;

@@ -82,11 +82,11 @@ function validate_config(utils) {
return wrangler_config;
}

utils.log.error(
builder.log.error(
'Consult https://developers.cloudflare.com/workers/platform/sites/configuration on how to setup your site'
);

utils.log(
builder.log(
`
Sample wrangler.toml:

16 changes: 8 additions & 8 deletions packages/adapter-cloudflare/index.js
Original file line number Diff line number Diff line change
@@ -7,29 +7,29 @@ import * as esbuild from 'esbuild';
export default function (options = {}) {
return {
name: '@sveltejs/adapter-cloudflare',
async adapt(utils) {
async adapt(builder) {
const files = fileURLToPath(new URL('./files', import.meta.url));
const dest = join(process.cwd(), '.svelte-kit', 'cloudflare');
utils.rimraf(dest);
builder.rimraf(dest);

utils.writeStatic(dest);
utils.writeClient(dest);
builder.writeStatic(dest);
builder.writeClient(dest);

const { paths } = await utils.prerender({ dest });
const { paths } = await builder.prerender({ dest });

const tmp = join(process.cwd(), '.svelte-kit', 'cloudflare-tmp');
utils.mkdirp(tmp);
builder.mkdirp(tmp);

writeFileSync(
join(tmp, 'manifest.js'),
`export const manifest = ${utils.generateManifest({
`export const manifest = ${builder.generateManifest({
relativePath: '../output/server'
})};\n\nexport const prerendered = new Set(${JSON.stringify(paths)});\n`
);

const worker = join(dest, '_worker.js');

utils.copy(join(files, 'worker.js'), worker);
builder.copy(join(files, 'worker.js'), worker);

await esbuild.build({
target: 'es2020',
50 changes: 25 additions & 25 deletions packages/adapter-netlify/index.js
Original file line number Diff line number Diff line change
@@ -19,35 +19,35 @@ export default function () {
return {
name: '@sveltejs/adapter-netlify',

async adapt(utils) {
async adapt(builder) {
const netlify_config = get_netlify_config();

// "build" is the default publish directory when Netlify detects SvelteKit
const publish = get_publish_directory(netlify_config, utils) || 'build';
const publish = get_publish_directory(netlify_config, builder) || 'build';

// empty out existing build directories
utils.rimraf(publish);
utils.rimraf('.netlify/functions-internal');
utils.rimraf('.netlify/server');
utils.rimraf('.netlify/package.json');
utils.rimraf('.netlify/handler.js');
builder.rimraf(publish);
builder.rimraf('.netlify/functions-internal');
builder.rimraf('.netlify/server');
builder.rimraf('.netlify/package.json');
builder.rimraf('.netlify/handler.js');

utils.mkdirp('.netlify/functions-internal');
builder.mkdirp('.netlify/functions-internal');

utils.log.minor(`Publishing to "${publish}"`);
builder.log.minor(`Publishing to "${publish}"`);

utils.log.minor('Generating serverless function...');
utils.writeServer('.netlify/server');
builder.log.minor('Generating serverless function...');
builder.writeServer('.netlify/server');

utils.log.minor('Prerendering static pages...');
await utils.prerender({
builder.log.minor('Prerendering static pages...');
await builder.prerender({
dest: publish
});

// for esbuild, use ESM; for zip-it-and-ship-it, use CJS
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
const esm = netlify_config?.functions?.node_bundler === 'esbuild';

const entries = utils.createEntries((route) => {
const entries = builder.createEntries((route) => {
const parts = [];

for (const segment of route.segments) {
@@ -83,7 +83,7 @@ export default function () {
});

if (esm) {
utils.copy(join(files, 'esm/handler.js'), '.netlify/handler.js');
builder.copy(join(files, 'esm/handler.js'), '.netlify/handler.js');
} else {
// TODO might be useful if you could specify CJS/ESM as an option to writeServer
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
glob('**/*.js', { cwd: '.netlify/server' }).forEach((file) => {
@@ -93,17 +93,17 @@ export default function () {
writeFileSync(filepath, output);
});

utils.copy(join(files, 'cjs/handler.js'), '.netlify/handler.js');
builder.copy(join(files, 'cjs/handler.js'), '.netlify/handler.js');
writeFileSync(join('.netlify', 'package.json'), JSON.stringify({ type: 'commonjs' }));
}

utils.log.minor('Copying assets...');
utils.writeStatic(publish);
utils.writeClient(publish);
builder.log.minor('Copying assets...');
builder.writeStatic(publish);
builder.writeClient(publish);

utils.log.minor('Writing redirects...');
builder.log.minor('Writing redirects...');
const redirect_file = join(publish, '_redirects');
utils.copy('_redirects', redirect_file);
builder.copy('_redirects', redirect_file);
const redirects = entries
.map(({ id, data }) => `${id} /.netlify/functions/${data.name} 200`)
.join('\n');
@@ -127,12 +127,12 @@ function get_netlify_config() {

/**
* @param {NetlifyConfig} netlify_config
* @param {import('@sveltejs/kit').AdapterUtils} utils
* @param {import('@sveltejs/kit').Builder} builder
**/
function get_publish_directory(netlify_config, utils) {
function get_publish_directory(netlify_config, builder) {
if (netlify_config) {
if (!netlify_config.build || !netlify_config.build.publish) {
utils.log.warn('No publish directory specified in netlify.toml, using default');
builder.log.warn('No publish directory specified in netlify.toml, using default');
return;
}

@@ -149,7 +149,7 @@ function get_publish_directory(netlify_config, utils) {
return netlify_config.build.publish;
}

utils.log.warn(
builder.log.warn(
'No netlify.toml found. Using default publish directory. Consult https://github.com/sveltejs/kit/tree/master/packages/adapter-netlify#configuration for more details '
);
}
22 changes: 11 additions & 11 deletions packages/adapter-node/index.js
Original file line number Diff line number Diff line change
@@ -22,22 +22,22 @@ export default function ({
return {
name: '@sveltejs/adapter-node',

async adapt(utils) {
utils.rimraf(out);
async adapt(builder) {
builder.rimraf(out);

utils.log.minor('Copying assets');
utils.writeClient(`${out}/client`);
utils.writeServer(`${out}/server`);
utils.writeStatic(`${out}/static`);
builder.log.minor('Copying assets');
builder.writeClient(`${out}/client`);
builder.writeServer(`${out}/server`);
builder.writeStatic(`${out}/static`);

writeFileSync(
`${out}/manifest.js`,
`export const manifest = ${utils.generateManifest({
`export const manifest = ${builder.generateManifest({
relativePath: './server'
})};\n`
);

utils.copy(files, out, {
builder.copy(files, out, {
replace: {
APP: './server/app.js', // TODO hard-coded path is brittle
MANIFEST: './manifest.js',
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
@@ -47,13 +47,13 @@ export default function ({
}
});

utils.log.minor('Prerendering static pages');
await utils.prerender({
builder.log.minor('Prerendering static pages');
await builder.prerender({
dest: `${out}/prerendered`
});

if (precompress) {
utils.log.minor('Compressing assets');
builder.log.minor('Compressing assets');
await compress(`${out}/client`);
await compress(`${out}/static`);
await compress(`${out}/prerendered`);
12 changes: 6 additions & 6 deletions packages/adapter-static/index.js
Original file line number Diff line number Diff line change
@@ -3,14 +3,14 @@ export default function ({ pages = 'build', assets = pages, fallback } = {}) {
return {
name: '@sveltejs/adapter-static',

async adapt(utils) {
utils.rimraf(assets);
utils.rimraf(pages);
async adapt(builder) {
builder.rimraf(assets);
builder.rimraf(pages);

utils.writeStatic(assets);
utils.writeClient(assets);
builder.writeStatic(assets);
builder.writeClient(assets);

await utils.prerender({
await builder.prerender({
fallback,
all: !fallback,
dest: pages
26 changes: 14 additions & 12 deletions packages/adapter-vercel/index.js
Original file line number Diff line number Diff line change
@@ -12,9 +12,9 @@ export default function () {
return {
name: '@sveltejs/adapter-vercel',

async adapt(utils) {
async adapt(builder) {
const dir = '.output';
utils.rimraf(dir);
builder.rimraf(dir);

const files = fileURLToPath(new URL('./files', import.meta.url));

@@ -23,18 +23,18 @@ export default function () {
lambda: join(dir, 'server/pages')
};

utils.log.minor('Prerendering static pages...');
await utils.prerender({
builder.log.minor('Prerendering static pages...');
await builder.prerender({
dest: dirs.static
});

// TODO ideally we'd have something like utils.tmpdir('vercel')
// TODO ideally we'd have something like builder.tmpdir('vercel')
// rather than hardcoding '.svelte-kit/vercel/entry.js', and the
// relative import from that file to output/server/app.js
// would be controlled. at the moment we're exposing
// implementation details that could change
utils.log.minor('Generating serverless function...');
utils.copy(files, '.svelte-kit/vercel', {
builder.log.minor('Generating serverless function...');
builder.copy(files, '.svelte-kit/vercel', {
replace: {
APP: '../output/server/app.js',
MANIFEST: './manifest.js'
@@ -43,7 +43,9 @@ export default function () {

writeFileSync(
'.svelte-kit/vercel/manifest.js',
`export const manifest = ${utils.generateManifest({ relativePath: '../output/server' })};\n`
`export const manifest = ${builder.generateManifest({
relativePath: '../output/server'
})};\n`
);

await esbuild.build({
@@ -56,11 +58,11 @@ export default function () {

writeFileSync(join(dirs.lambda, 'package.json'), JSON.stringify({ type: 'commonjs' }));

utils.log.minor('Copying assets...');
utils.writeClient(dirs.static);
utils.writeStatic(dirs.static);
builder.log.minor('Copying assets...');
builder.writeClient(dirs.static);
builder.writeStatic(dirs.static);

utils.log.minor('Writing manifests...');
builder.log.minor('Writing manifests...');
writeFileSync(
join(dir, 'routes-manifest.json'),
JSON.stringify({
Original file line number Diff line number Diff line change
@@ -10,9 +10,9 @@ import { generate_manifest } from '../generate_manifest/index.js';
* build_data: import('types/internal').BuildData;
* log: import('types/internal').Logger;
* }} opts
* @returns {import('types/config').AdapterUtils}
* @returns {import('types/config').Builder}
*/
export function get_utils({ cwd, config, build_data, log }) {
export function create_builder({ cwd, config, build_data, log }) {
/** @type {Set<string>} */
const prerendered_paths = new Set();
let generated_manifest = false;
Loading