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

Passing the Rollup output options through to Vite #1572

Merged
merged 13 commits into from
Jun 14, 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
2 changes: 1 addition & 1 deletion packages/kit/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { existsSync } from 'fs';
import sade from 'sade';
import colors from 'kleur';
import * as ports from 'port-authority';
import { load_config } from './core/load_config/index.js';
import { load_config } from './core/config/index.js';
import { networkInterfaces, release } from 'os';

async function get_config() {
Expand Down
40 changes: 19 additions & 21 deletions packages/kit/src/core/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'path';
import { rimraf } from '../filesystem/index.js';
import create_manifest_data from '../../core/create_manifest_data/index.js';
import { copy_assets, get_no_external, posixify, resolve_entry } from '../utils.js';
import { deep_merge, print_config_conflicts } from '../config/index.js';
import { create_app } from '../../core/create_app/index.js';
import vite from 'vite';
import { svelte } from '@sveltejs/vite-plugin-svelte';
Expand Down Expand Up @@ -134,19 +135,17 @@ async function build_client({
/** @type {any} */
const user_config = config.kit.vite();

await vite.build({
...user_config,
/** @type {[any, string[]]} */
const [merged_config, conflicts] = deep_merge(user_config, {
configFile: false,
root: cwd,
base,
build: {
...user_config.build,
cssCodeSplit: true,
manifest: true,
outDir: client_out_dir,
polyfillDynamicImport: false,
rollupOptions: {
...(user_config.build && user_config.build.rollupOptions),
input,
output: {
entryFileNames: '[name]-[hash].js',
Expand All @@ -157,22 +156,23 @@ async function build_client({
}
},
resolve: {
...user_config.resolve,
alias: {
...(user_config.resolve && user_config.resolve.alias),
$app: path.resolve(`${build_dir}/runtime/app`),
$lib: config.kit.files.lib
}
},
plugins: [
...(user_config.plugins || []),
svelte({
extensions: config.extensions,
emitCss: !config.kit.amp
})
]
});

print_config_conflicts(conflicts, 'kit.vite.', 'build_client');

await vite.build(merged_config);

/** @type {ClientManifest} */
const client_manifest = JSON.parse(fs.readFileSync(client_manifest_file, 'utf-8'));
fs.renameSync(client_manifest_file, `${output_dir}/manifest.json`); // inspectable but not shipped
Expand Down Expand Up @@ -395,19 +395,17 @@ async function build_server(
/** @type {any} */
const user_config = config.kit.vite();

await vite.build({
...user_config,
/** @type {[any, string[]]} */
const [merged_config, conflicts] = deep_merge(user_config, {
configFile: false,
root: cwd,
base,
build: {
target: 'es2018',
...user_config.build,
ssr: true,
outDir: `${output_dir}/server`,
polyfillDynamicImport: false,
rollupOptions: {
...(user_config.build && user_config.build.rollupOptions),
input: {
app: app_file
},
Expand All @@ -422,15 +420,12 @@ async function build_server(
}
},
resolve: {
...user_config.resolve,
alias: {
...(user_config.resolve && user_config.resolve.alias),
$app: path.resolve(`${build_dir}/runtime/app`),
$lib: config.kit.files.lib
}
},
plugins: [
...(user_config.plugins || []),
svelte({
extensions: config.extensions
})
Expand All @@ -440,7 +435,6 @@ async function build_server(
// so we need to ignore the fact that it's missing
// @ts-ignore
ssr: {
...user_config.ssr,
// note to self: this _might_ need to be ['svelte', '@sveltejs/kit', ...get_no_external()]
// but I'm honestly not sure. roll with this for now and see if it's ok
noExternal: get_no_external(cwd, user_config.ssr && user_config.ssr.noExternal)
Expand All @@ -449,6 +443,10 @@ async function build_server(
entries: []
}
});

print_config_conflicts(conflicts, 'kit.vite.', 'build_server');

await vite.build(merged_config);
}

/**
Expand Down Expand Up @@ -504,20 +502,18 @@ async function build_service_worker(
/** @type {any} */
const user_config = config.kit.vite();

await vite.build({
...user_config,
/** @type {[any, string[]]} */
const [merged_config, conflicts] = deep_merge(user_config, {
configFile: false,
root: cwd,
base,
build: {
...user_config.build,
lib: {
entry: service_worker_entry_file,
name: 'app',
formats: ['es']
},
rollupOptions: {
...(user_config.build && user_config.build.rollupOptions),
output: {
entryFileNames: 'service-worker.js'
}
Expand All @@ -526,16 +522,18 @@ async function build_service_worker(
emptyOutDir: false
},
resolve: {
...user_config.resolve,
alias: {
...(user_config.resolve && user_config.resolve.alias),
'$service-worker': path.resolve(`${build_dir}/runtime/service-worker`)
}
},
optimizeDeps: {
entries: []
}
});

print_config_conflicts(conflicts, 'kit.vite.', 'build_service_worker');

await vite.build(merged_config);
}

/** @param {string[]} array */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import options from './options.js';
import * as url from 'url';
import path from 'path';
import fs from 'fs';
import { logger } from '../utils.js';

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

Expand Down Expand Up @@ -145,3 +146,76 @@ export function validate_config(config) {

return validated;
}

/**
* Merges b into a, recursively, mutating a.
* @param {Record<string, any>} a
* @param {Record<string, any>} b
* @param {string[]} conflicts array to accumulate conflicts in
* @param {string[]} path array of property names representing the current
* location in the tree
*/
function merge_into(a, b, conflicts = [], path = []) {
/**
* @param {any} x
*/
const is_object = (x) => typeof x === 'object' && !Array.isArray(x);

for (const prop in b) {
if (is_object(b[prop])) {
if (!is_object(a[prop])) {
if (a[prop] !== undefined) {
conflicts.push([...path, prop].join('.'));
}
a[prop] = {};
}
merge_into(a[prop], b[prop], conflicts, [...path, prop]);
} else if (Array.isArray(b[prop])) {
if (!Array.isArray(a[prop])) {
if (a[prop] !== undefined) {
conflicts.push([...path, prop].join('.'));
}
a[prop] = [];
}
a[prop].push(...b[prop]);
} else {
if (a[prop] !== undefined) {
conflicts.push([...path, prop].join('.'));
}
a[prop] = b[prop];
}
}
}

/**
* Takes zero or more objects and returns a new object that has all the values
* deeply merged together. None of the original objects will be mutated at any
* level, and the returned object will have no references to the original
* objects at any depth. If there's a conflict the last one wins, except for
* arrays which will be combined.
* @param {...Object} objects
* @returns {[Record<string, any>, string[]]} a 2-tuple with the merged object,
* and a list of merge conflicts if there were any, in dotted notation
*/
export function deep_merge(...objects) {
const result = {};
/** @type {string[]} */
const conflicts = [];
objects.forEach((o) => merge_into(result, o, conflicts));
return [result, conflicts];
}

/**
* @param {string[]} conflicts - array of conflicts in dotted notation
* @param {string=} pathPrefix - prepended in front of the path
* @param {string=} scope - used to prefix the whole error message
*/
export function print_config_conflicts(conflicts, pathPrefix = '', scope) {
const prefix = scope ? scope + ': ' : '';
const log = logger({ verbose: false });
conflicts.forEach((conflict) => {
log.error(
`${prefix}The value for ${pathPrefix}${conflict} specified in svelte.config.js has been ignored. This option is controlled by SvelteKit.`
);
});
}
Loading