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

Upgrade to Vite 3 #5005

Merged
merged 51 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
fa4ab63
Upgrade to Vite 3
benmccann Jul 5, 2022
be61e1c
Merge branch 'master' into vite-3
Rich-Harris Jul 5, 2022
5fe2ae7
this... seems to work?
Rich-Harris Jul 5, 2022
52afcb2
try this
Rich-Harris Jul 5, 2022
f98e13f
merge
Rich-Harris Jul 5, 2022
8852ce4
remove some more unneeded fixStacktrace options
Rich-Harris Jul 5, 2022
9e68461
Merge branch 'master' into vite-3
benmccann Jul 7, 2022
3aace45
merge master
benmccann Jul 11, 2022
c05dd5d
upgrade to 3.0.0-beta.9
benmccann Jul 11, 2022
4f6c759
update default port
benmccann Jul 12, 2022
400193b
merge master
benmccann Jul 12, 2022
aa787e9
3.0.0 final
benmccann Jul 13, 2022
ab34658
3.0.0 final for site
benmccann Jul 13, 2022
7ee5936
vite-plugin-svelte 1.0.1
benmccann Jul 13, 2022
2674271
bump rollup
benmccann Jul 13, 2022
2b51a0f
merge master
benmccann Jul 13, 2022
495fddc
update enforced_config
Rich-Harris Jul 13, 2022
c7a2e8b
add EnforcedConfig type
Rich-Harris Jul 13, 2022
bc6b112
merge master
benmccann Jul 13, 2022
03e7d40
Merge branch 'vite-3' of github.com:sveltejs/kit into vite-3
benmccann Jul 13, 2022
5c009d8
wow i did a terrible job of implementing this before
Rich-Harris Jul 13, 2022
df6d568
Merge branch 'vite-3' of github.com:sveltejs/kit into vite-3
Rich-Harris Jul 13, 2022
06de105
leave windows paths as-is
Rich-Harris Jul 13, 2022
743fbe8
Revert "leave windows paths as-is"
Rich-Harris Jul 13, 2022
8762468
try this
Rich-Harris Jul 13, 2022
c366895
actually if anything i guess it should be this
Rich-Harris Jul 13, 2022
639e2ac
ugh
Rich-Harris Jul 13, 2022
d9ff77b
merge master
Rich-Harris Jul 13, 2022
ddd4690
Merge branch 'master' into vite-3
Rich-Harris Jul 13, 2022
f1cfe98
merge master
benmccann Jul 13, 2022
0f2d34e
Merge branch 'vite-3' of github.com:sveltejs/kit into vite-3
benmccann Jul 13, 2022
a6033c9
fix firefox test
Rich-Harris Jul 13, 2022
3905374
Merge branch 'vite-3' of github.com:sveltejs/kit into vite-3
Rich-Harris Jul 13, 2022
eccfdcb
add some logging that might help diagnose windows failure
Rich-Harris Jul 13, 2022
a9c1de2
i think this might work
Rich-Harris Jul 13, 2022
24738b4
more logging
Rich-Harris Jul 13, 2022
602e023
maybe this
Rich-Harris Jul 13, 2022
2a975ec
try to simplify logging so that stuff is slightly easier to diagnose
Rich-Harris Jul 13, 2022
9e2d017
argh
Rich-Harris Jul 13, 2022
f91a711
Revert "argh"
benmccann Jul 14, 2022
fcf10cf
Revert "try to simplify logging so that stuff is slightly easier to d…
benmccann Jul 14, 2022
9a90c35
[docs] add JSDocs for Vite build process
benmccann Jul 14, 2022
4f8bbd1
format
benmccann Jul 14, 2022
d9a059c
loop over values instead with `Object.values`
ignatiusmb Jul 14, 2022
7496850
missed a closing bracket
ignatiusmb Jul 14, 2022
77f4abe
fix: ensure isolated testing of create-svelte with local packages (#5…
dominikg Jul 14, 2022
c75a4f7
Merge branch 'master' into vite-3
Rich-Harris Jul 14, 2022
902d730
Merge branch 'master' into vite-docs
Rich-Harris Jul 14, 2022
3be0e9a
merge vite-docs
Rich-Harris Jul 14, 2022
3f5eb6a
remove logging
Rich-Harris Jul 14, 2022
293d564
enforce vite 3
Rich-Harris Jul 14, 2022
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
[docs] add JSDocs for Vite build process
  • Loading branch information
benmccann committed Jul 14, 2022
commit 9a90c3549870a65051853e6382772e5c46f9ecf3
3 changes: 3 additions & 0 deletions packages/kit/src/vite/build/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ export function assets_base(config) {
}

/**
* vite.config.js will contain vite-plugin-svelte-kit, which kicks off the server and service
* worker builds in a hook. When running the server and service worker builds we must remove
* the SvelteKit plugin so that we do not kick off additional instances of these builds.
* @param {import('vite').UserConfig} config
*/
export function remove_svelte_kit(config) {
Expand Down
123 changes: 84 additions & 39 deletions packages/kit/src/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ export function sveltekit() {
}

/**
* Returns the SvelteKit Vite plugin. Vite executes Rollup hooks as well as some of its own.
* Background reading is available at:
* - https://vitejs.dev/guide/api-plugin.html
* - https://rollupjs.org/guide/en/#plugin-development
*
* You can get an idea of the lifecycle by looking at the flow charts here:
* - https://rollupjs.org/guide/en/#build-hooks
* - https://rollupjs.org/guide/en/#output-generation-hooks
*
* @return {import('vite').Plugin}
*/
function kit() {
Expand Down Expand Up @@ -99,7 +108,7 @@ function kit() {
*/
let paths;

function create_client_config() {
function vite_client_config() {
/** @type {Record<string, string>} */
const input = {
// Put unchanging assets in immutable directory. We don't set that in the
Expand Down Expand Up @@ -128,9 +137,35 @@ function kit() {
});
}

/**
* @param {import('rollup').OutputAsset[]} assets
* @param {import('rollup').OutputChunk[]} chunks
*/
function client_build_info(assets, chunks) {
/** @type {import('vite').Manifest} */
const vite_manifest = JSON.parse(
fs.readFileSync(`${paths.client_out_dir}/manifest.json`, 'utf-8')
);

const entry_id = posixify(
path.relative(cwd, `${get_runtime_path(svelte_config.kit)}/client/start.js`)
);

return {
assets,
chunks,
entry: find_deps(vite_manifest, entry_id, false),
vite_manifest
};
}

return {
name: 'vite-plugin-svelte-kit',

/**
* Build the SvelteKit-provided Vite config to be merged with the user's vite.config.js file.
* @see https://vitejs.dev/guide/api-plugin.html#config
*/
async config(config, config_env) {
vite_config_env = config_env;
svelte_config = await load_config();
Expand All @@ -149,7 +184,7 @@ function kit() {

manifest_data = sync.all(svelte_config).manifest_data;

const new_config = create_client_config();
const new_config = vite_client_config();

warn_overridden_config(config, new_config);

Expand Down Expand Up @@ -195,11 +230,17 @@ function kit() {
return result;
},

/**
* Stores the final config.
*/
configResolved(config) {
vite_config = config;
},

buildStart() {
/**
* Clears the output directories.
*/
buildStart() {
if (is_build) {
rimraf(paths.build_dir);
mkdirp(paths.build_dir);
Expand All @@ -209,6 +250,11 @@ function kit() {
}
},

/**
* Vite builds a single bundle. We need three bundles: client, server, and service worker.
* The user's package.json scripts will invoke the Vite CLI to execute the client build. We
* then use this hook to kick off builds for the server and service worker.
*/
async writeBundle(_options, bundle) {
log = logger({
verbose: vite_config.logLevel === 'info'
Expand All @@ -219,36 +265,10 @@ function kit() {
JSON.stringify({ version: process.env.VITE_SVELTEKIT_APP_VERSION })
);

/** @type {import('rollup').OutputChunk[]} */
const chunks = [];
/** @type {import('rollup').OutputAsset[]} */
const assets = [];
for (const key of Object.keys(bundle)) {
// collect asset and output chunks
if (bundle[key].type === 'asset') {
assets.push(/** @type {import('rollup').OutputAsset} */ (bundle[key]));
} else {
chunks.push(/** @type {import('rollup').OutputChunk} */ (bundle[key]));
}
}

/** @type {import('vite').Manifest} */
const vite_manifest = JSON.parse(
fs.readFileSync(`${paths.client_out_dir}/manifest.json`, 'utf-8')
);

const entry_id = posixify(
path.relative(cwd, `${get_runtime_path(svelte_config.kit)}/client/start.js`)
);

const client = {
assets,
chunks,
entry: find_deps(vite_manifest, entry_id, false),
vite_manifest
};
const { assets, chunks } = collect_output(bundle);
log.info(`Client build completed. Wrote ${chunks.length} chunks and ${assets.length} assets`);

log.info('Building server');
const options = {
cwd,
config: svelte_config,
Expand All @@ -258,13 +278,9 @@ function kit() {
output_dir: paths.output_dir,
service_worker_entry_file: resolve_entry(svelte_config.kit.files.serviceWorker)
};

log.info('Building server');

const client = client_build_info(assets, chunks);
const server = await build_server(options, client);

process.env.SVELTEKIT_SERVER_BUILD_COMPLETED = 'true';

/** @type {import('types').BuildData} */
build_data = {
app_dir: svelte_config.kit.appDir,
Expand All @@ -283,6 +299,9 @@ function kit() {
})};\n`
);

process.env.SVELTEKIT_SERVER_BUILD_COMPLETED = 'true';
log.info('Prerendering');

const static_files = manifest_data.assets.map((asset) => posixify(asset.file));

const files = new Set([
Expand All @@ -298,8 +317,6 @@ function kit() {
}
});

log.info('Prerendering');

prerendered = await prerender({
config: svelte_config.kit,
entries: manifest_data.routes
Expand All @@ -324,6 +341,9 @@ function kit() {
);
},

/**
* Runs the adapter.
*/
async closeBundle() {
if (!is_build) {
return; // vite calls closeBundle when dev-server restarts, ignore that
Expand All @@ -341,22 +361,47 @@ function kit() {

if (svelte_config.kit.prerender.enabled) {
// this is necessary to close any open db connections, etc.
// TODO: prerender in a subprocess so we can exit in isolation
// TODO: prerender in a subprocess so we can exit in isolation and then remove this
// https://github.com/sveltejs/kit/issues/5306
process.exit(0);
}
},

/**
* Adds the SvelteKit middleware to do SSR in dev mode.
* @see https://vitejs.dev/guide/api-plugin.html#configureserver
*/
async configureServer(vite) {
return await dev(vite, vite_config, svelte_config);
},

/**
* Adds the SvelteKit middleware to do SSR in preview mode.
* @see https://vitejs.dev/guide/api-plugin.html#configurepreviewserver
*/
configurePreviewServer(vite) {
return preview(vite, svelte_config, vite_config.preview.https ? 'https' : 'http');
}
};
}

/** @param {import('rollup').OutputBundle} bundle */
function collect_output(bundle) {
/** @type {import('rollup').OutputChunk[]} */
const chunks = [];
/** @type {import('rollup').OutputAsset[]} */
const assets = [];
for (const key of Object.keys(bundle)) {
// collect asset and output chunks
if (bundle[key].type === 'asset') {
assets.push(/** @type {import('rollup').OutputAsset} */ (bundle[key]));
} else {
chunks.push(/** @type {import('rollup').OutputChunk} */ (bundle[key]));
}
}
return {assets, chunks};
}

/**
* @param {Record<string, any>} config
* @param {Record<string, any>} resolved_config
Expand Down