Skip to content

Commit

Permalink
lay groundwork for #10
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Mar 8, 2021
1 parent 27ba872 commit 257a8c0
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 23 deletions.
145 changes: 123 additions & 22 deletions packages/kit/src/core/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import svelte from '@svitejs/vite-plugin-svelte';
/** @param {any} value */
const s = (value) => JSON.stringify(value);

/** @typedef {Record<string, {
* file: string;
* css: string[];
* imports: string[];
* }>} ClientManifest */

/**
* @param {import('../../types').ValidatedConfig} config
* @param {{
Expand All @@ -19,16 +25,55 @@ const s = (value) => JSON.stringify(value);
*/
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');

const manifest = create_manifest_data({
rimraf(build_dir);

const options = {
cwd,
config,
output: build_dir,
cwd
});
build_dir,
base:
config.kit.paths.assets === '/.'
? `/${config.kit.appDir}/`
: `${config.kit.paths.assets}/${config.kit.appDir}/`,
manifest: create_manifest_data({
config,
output: build_dir,
cwd
}),
output_dir: path.resolve(cwd, '.svelte/output'),
client_entry_file: '.svelte/build/runtime/internal/start.js'
};

rimraf(build_dir);
const client_manifest = await build_client(options);
await build_server(options, client_manifest, runtime);

const service_worker_entry = resolve_entry(config.kit.files.serviceWorker);
if (service_worker_entry) {
await build_service_worker(options, client_manifest);
}
}

/**
* @param {{
* cwd: string;
* base: string;
* config: import('../../types').ValidatedConfig
* manifest: import('../../types').ManifestData
* build_dir: string;
* output_dir: string;
* client_entry_file: string;
* }} options
*/
async function build_client({
cwd,
base,
config,
manifest,
build_dir,
output_dir,
client_entry_file
}) {
create_app({
manifest_data: manifest,
output: build_dir,
Expand All @@ -39,15 +84,9 @@ export async function build(config, { cwd = process.cwd(), runtime = '@sveltejs/

process.env.VITE_AMP = config.kit.amp ? 'true' : '';

const client_entry_file = '.svelte/build/runtime/internal/start.js';
const client_out_dir = `${output_dir}/client/${config.kit.appDir}`;
const client_manifest_file = `${client_out_dir}/manifest.json`;

const base =
config.kit.paths.assets === '/.'
? `/${config.kit.appDir}/`
: `${config.kit.paths.assets}/${config.kit.appDir}/`;

// client build
await vite.build({
root: cwd,
Expand Down Expand Up @@ -80,25 +119,43 @@ export async function build(config, { cwd = process.cwd(), runtime = '@sveltejs/
]
});

/** @type {Record<string, {
* file: string;
* css: string[];
* imports: string[];
* }>} */
/** @type {ClientManifest} */
const client_manifest = JSON.parse(fs.readFileSync(client_manifest_file, 'utf-8'));
fs.unlinkSync(client_manifest_file);

let setup_file = 'src/setup/index.js'; // TODO this is wrong... should see if we can resolve files.setup using Vite's resolution logic
if (!fs.existsSync(path.resolve(cwd, setup_file))) {
setup_file = '.svelte/build/setup.js';
fs.writeFileSync(path.resolve(cwd, setup_file), '');
return client_manifest;
}

/**
* @param {{
* cwd: string;
* base: string;
* config: import('../../types').ValidatedConfig
* manifest: import('../../types').ManifestData
* build_dir: string;
* output_dir: string;
* client_entry_file: string;
* }} options
* @param {ClientManifest} client_manifest
* @param {string} runtime
*/
async function build_server(
{ cwd, base, config, manifest, build_dir, output_dir, client_entry_file },
client_manifest,
runtime
) {
let setup_file = resolve_entry(config.kit.files.setup);
console.log({ setup_file });
if (!fs.existsSync(setup_file)) {
setup_file = path.resolve(cwd, '.svelte/build/setup.js');
fs.writeFileSync(setup_file, '');
}

const app_file = `${build_dir}/app.js`;

/** @type {(file: string) => string} */
const app_relative = (file) => {
const relative_file = path.relative('.svelte/build', file);
const relative_file = path.relative(build_dir, path.resolve(cwd, file));
return relative_file[0] === '.' ? relative_file : `./${relative_file}`;
};

Expand Down Expand Up @@ -294,6 +351,50 @@ export async function build(config, { cwd = process.cwd(), runtime = '@sveltejs/
});
}

/**
* @param {{
* cwd: string;
* base: string;
* config: import('../../types').ValidatedConfig
* manifest: import('../../types').ManifestData
* build_dir: string;
* output_dir: string;
* client_entry_file: string;
* }} options
* @param {ClientManifest} client_manifest
*/
async function build_service_worker(options, client_manifest) {
// TODO
}

/**
* @param {string} entry
* @returns {string}
*/
function resolve_entry(entry) {
if (fs.existsSync(entry)) {
const stats = fs.statSync(entry);
if (stats.isDirectory()) {
return resolve_entry(path.join(entry, 'index'));
}

return entry;
} else {
const dir = path.dirname(entry);

if (fs.existsSync(dir)) {
const base = path.basename(entry);
const files = fs.readdirSync(dir);

const found = files.find((file) => file.replace(/\.[^.]+$/, '') === base);

if (found) return path.join(dir, found);
}
}

return null;
}

/** @param {string[]} array */
function get_params(array) {
// given an array of params like `['x', 'y', 'z']` for
Expand Down
3 changes: 2 additions & 1 deletion packages/kit/src/core/load_config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export async function load_config({ cwd = process.cwd() } = {}) {

validated.kit.files.assets = path.resolve(cwd, validated.kit.files.assets);
validated.kit.files.routes = path.resolve(cwd, validated.kit.files.routes);
validated.kit.files.serviceWorker = path.resolve(cwd, validated.kit.files.serviceWorker);
validated.kit.files.setup = path.resolve(cwd, validated.kit.files.setup);
validated.kit.files.template = path.resolve(cwd, validated.kit.files.template);

Expand All @@ -96,7 +97,7 @@ export async function load_config({ cwd = process.cwd() } = {}) {
}

/**
* @param {import('../../types').Config} config
* @param {import('../../../types').Config} config
* @returns {import('../../types.js').ValidatedConfig}
*/
export function validate_config(config) {
Expand Down
2 changes: 2 additions & 0 deletions packages/kit/src/core/load_config/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ test('fills in defaults', () => {
files: {
assets: 'static',
routes: 'src/routes',
serviceWorker: 'src/service-worker',
setup: 'src/setup',
template: 'src/app.html'
},
Expand Down Expand Up @@ -86,6 +87,7 @@ test('fills in partial blanks', () => {
files: {
assets: 'public',
routes: 'src/routes',
serviceWorker: 'src/service-worker',
setup: 'src/setup',
template: 'src/app.html'
},
Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/core/load_config/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const options = {
children: {
assets: expect_string('static'),
routes: expect_string('src/routes'),
serviceWorker: expect_string('src/service-worker'),
setup: expect_string('src/setup'),
template: expect_string('src/app.html')
}
Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/core/load_config/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ suite('load default config', async () => {
files: {
assets: join(cwd, 'static'),
routes: join(cwd, 'src/routes'),
serviceWorker: join(cwd, 'src/service-worker'),
setup: join(cwd, 'src/setup'),
template: join(cwd, 'src/app.html')
},
Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type ValidatedConfig = {
files: {
assets: string;
routes: string;
serviceWorker: string;
setup: string;
template: string;
};
Expand Down
1 change: 1 addition & 0 deletions packages/kit/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type Config = {
files?: {
assets?: string;
routes?: string;
serviceWorker?: string;
setup?: string;
template?: string;
};
Expand Down

0 comments on commit 257a8c0

Please sign in to comment.