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 adapt #430

Merged
merged 9 commits into from
Mar 2, 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
5 changes: 5 additions & 0 deletions .changeset/good-beans-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Run adapt via svelte-kit build
12 changes: 6 additions & 6 deletions packages/kit/src/api/adapt/Builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ import { copy } from '@sveltejs/app-utils/files';
import { prerender } from './prerender.js';

export default class Builder {
#generated_files;
#cwd;
#config;

constructor({ generated_files, config, log }) {
this.#generated_files = generated_files;
constructor({ cwd, config, log }) {
this.#cwd = cwd;
this.#config = config;

this.log = log;
}

copy_client_files(dest) {
copy(`${this.#generated_files}/client`, dest, (file) => file[0] !== '.');
copy(`${this.#cwd}/client`, dest, (file) => file[0] !== '.');
}

copy_server_files(dest) {
copy(`${this.#generated_files}/server`, dest, (file) => file[0] !== '.');
copy(`${this.#cwd}/server`, dest, (file) => file[0] !== '.');
}

copy_static_files(dest) {
Expand All @@ -29,7 +29,7 @@ export default class Builder {
await prerender({
out: dest,
force,
dir: this.#generated_files,
dir: this.#cwd,
config: this.#config,
log: this.log
});
Expand Down
8 changes: 2 additions & 6 deletions packages/kit/src/api/adapt/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { logger } from '../utils';
import Builder from './Builder';
import { createRequire } from 'module';

export async function adapt(config, { verbose }) {
export async function adapt(config, { cwd, verbose }) {
if (!config.adapter) {
throw new Error('No adapter specified');
}
Expand All @@ -15,11 +15,7 @@ export async function adapt(config, { verbose }) {

console.log(colors.bold().cyan(`\n> Using ${adapter}`));

const builder = new Builder({
generated_files: '.svelte/build/optimized',
config,
log
});
const builder = new Builder({ cwd, config, log });

const require = createRequire(import.meta.url);
const resolved = require.resolve(adapter, pathToFileURL(process.cwd()));
Expand Down
16 changes: 11 additions & 5 deletions packages/kit/src/api/adapt/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const REDIRECT = 3;

export async function prerender({ dir, out, log, config, force }) {
const seen = new Set();
const seen_files = new Set();

const server_root = resolve_path(dir);
const app = await import(pathToFileURL(`${server_root}/server/app.js`));
Expand Down Expand Up @@ -165,19 +166,24 @@ export async function prerender({ dir, out, log, config, force }) {

for (const href of hrefs) {
const resolved = resolve(path, href);

if (resolved[0] !== '/') continue;
if (seen_files.has(resolved)) continue;

const parsed = parse(resolved);

const file = parsed.pathname.replace(config.paths.assets, '');
const file = parsed.pathname.replace(config.paths.assets, '').slice(1);

const file_exists =
(file.startsWith(`/${config.appDir}/`) && fs.existsSync(`${dir}/client/${file}`)) ||
(file.startsWith(`${config.appDir}/`) && fs.existsSync(`${dir}/client/${file}`)) ||
fs.existsSync(`${out}/${file}`) ||
fs.existsSync(`${config.files.static}/${file}`) ||
fs.existsSync(`${config.files.static}/${file}/index.html`);
fs.existsSync(`${config.files.assets}/${file}`) ||
fs.existsSync(`${config.files.assets}/${file}/index.html`);

if (file_exists) continue;
if (file_exists) {
seen_files.add(resolved);
continue;
}

if (parsed.query) {
// TODO warn that query strings have no effect on statically-exported pages
Expand Down

This file was deleted.

40 changes: 7 additions & 33 deletions packages/kit/src/api/adapt/test/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ suite('builder ', () => {
});

suite('copy files', () => {
const generated_files = join(__dirname, 'fixtures/basic/.svelte/build/optimized');
const cwd = join(__dirname, 'fixtures/basic/_svelte/output');
const config = {
files: {
assets: join(__dirname, 'fixtures/basic/static')
Expand All @@ -25,21 +25,8 @@ suite('copy files', () => {
};

const builder = new Builder({
generated_files,
cwd,
config,
manifest: {
error: {
name: '$default_error',
url: '/_app/assets/components/error.svelte.js'
},
layout: {
name: '$default_layout',
url: '/_app/assets/components/layout.svelte.js'
},
components: [],
pages: [],
endpoints: []
},
log: Object.assign((_msg) => {}, {
info: (_msg) => {},
warn: (_msg) => {},
Expand All @@ -58,21 +45,21 @@ suite('copy files', () => {
rimraf.sync(dest);
builder.copy_client_files(dest);

assert.equal(glob('**', { cwd: `${generated_files}/client` }), glob('**', { cwd: dest }));
assert.equal(glob('**', { cwd: `${cwd}/client` }), glob('**', { cwd: dest }));

rimraf.sync(dest);
builder.copy_server_files(dest);

assert.equal(glob('**', { cwd: `${generated_files}/server` }), glob('**', { cwd: dest }));
assert.equal(glob('**', { cwd: `${cwd}/server` }), glob('**', { cwd: dest }));
});

suite('prerender', async () => {
const generated_files = join(__dirname, 'fixtures/prerender/.svelte/build/optimized');
const cwd = join(__dirname, 'fixtures/prerender/_svelte/output');
const prerendered_files = join(__dirname, 'fixtures/prerender/build');
const config = {
files: {
assets: join(__dirname, 'fixtures/prerender/static'),
routes: join(__dirname, 'fixtures/prerender/.svelte/build/optimized/server/routes')
routes: join(__dirname, 'fixtures/prerender/src/routes')
},
appDir: '_app',
prerender: {
Expand All @@ -82,21 +69,8 @@ suite('prerender', async () => {
};

const builder = new Builder({
generated_files,
cwd,
config,
manifest: {
error: {
name: '$default_error',
url: '/_app/assets/components/error.svelte.js'
},
layout: {
name: '$default_layout',
url: '/_app/assets/components/layout.svelte.js'
},
components: [],
pages: [],
endpoints: []
},
log: Object.assign((_msg) => {}, {
info: (_msg) => {},
warn: (_msg) => {},
Expand Down
7 changes: 3 additions & 4 deletions packages/kit/src/api/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import svelte from '@sveltejs/vite-plugin-svelte';
const s = JSON.stringify;

const build_dir = '.svelte/build';
const output_dir = '.svelte/output';

export async function build(config) {
export async function build(config, { cwd }) {
const manifest = create_manifest_data({
config,
output: build_dir
Expand All @@ -35,7 +34,7 @@ export async function build(config) {
].join('\n'));

const client_entry_file = `${build_dir}/runtime/internal/start.js`;
const client_out_dir = `${output_dir}/client/${config.appDir}`;
const client_out_dir = `${cwd}/client/${config.appDir}`;
const client_manifest_file = `${client_out_dir}/manifest.json`;

const base =
Expand Down Expand Up @@ -251,7 +250,7 @@ export async function build(config) {
name: 'app',
formats: ['es']
},
outDir: `${output_dir}/server`
outDir: `${cwd}/server`
},
resolve: {
alias: {
Expand Down
32 changes: 20 additions & 12 deletions packages/kit/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,30 @@ prog
prog
.command('build')
.describe('Create a production build of your app')
.action(async () => {
.option('--verbose', 'Log more stuff', false)
.action(async ({ verbose }) => {
process.env.NODE_ENV = 'production';
const config = await get_config();

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

try {
await build(config);
const cwd = '.svelte/output';

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

if (config.adapter[0]) {
await adapt(config, { cwd, verbose });
} else {
console.log(colors.bold().yellow('\nNo adapter specified'));

// prettier-ignore
console.log(
`See ${colors.bold().cyan('https://kit.svelte.dev/docs#adapters')} to learn how to configure your app to run on the platform of your choosing`
);
}
} catch (error) {
handle_error(error);
}
Expand All @@ -128,21 +144,13 @@ prog
}
});

// For the benefit of early-adopters. Can later be removed
prog
.command('adapt')
.describe('Customise your production build for different platforms')
.option('--verbose', 'Log more stuff', false)
.action(async ({ verbose }) => {
process.env.NODE_ENV = 'production';
const config = await get_config();

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

try {
await adapt(config, { verbose });
} catch (error) {
handle_error(error);
}
console.log('"svelte-kit build" will now run the adapter');
});

prog.parse(process.argv, { unknown: (arg) => `Unknown option: ${arg}` });
3 changes: 1 addition & 2 deletions test/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ export async function runner(prepare_tests, options = {}) {
const port = await ports.find(3000);
const config = await config_promise;

// TODO implement `svelte start` so we don't need to use an adapter
await build(config);
await build(config, { cwd: '.svelte/output' });

context.server = await start({ port, config });
Object.assign(context, await setup({ port }));
Expand Down