-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
namespaced kit options #236
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3034ba4
kitOptions namespace
Rich-Harris 35e7491
kitOptions -> kit
Rich-Harris 234247b
Merge branch 'master' into options
Rich-Harris 9868dba
add validation, rename kit.paths -> kit.files
Rich-Harris 495c7fd
add changeset
Rich-Harris 83ba6e1
fix adapt option
Rich-Harris 748dd2c
lint
Rich-Harris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'create-svelte': patch | ||
'@sveltejs/kit': patch | ||
--- | ||
|
||
Move options behind kit namespace, change paths -> kit.files |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
module.exports = { | ||
// By default, `npm run build` will create a standard Node app. | ||
// You can create optimized builds for different platforms by | ||
// specifying a different adapter | ||
adapter: '@sveltejs/adapter-netlify' | ||
}; | ||
kit: { | ||
// By default, `npm run build` will create a standard Node app. | ||
// You can create optimized builds for different platforms by | ||
// specifying a different adapter | ||
adapter: '@sveltejs/adapter-netlify' | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
module.exports = { | ||
// By default, `npm run build` will create a standard Node app. | ||
// You can create optimized builds for different platforms by | ||
// specifying a different adapter | ||
adapter: '@sveltejs/adapter-node' | ||
kit: { | ||
// By default, `npm run build` will create a standard Node app. | ||
// You can create optimized builds for different platforms by | ||
// specifying a different adapter | ||
adapter: '@sveltejs/adapter-node' | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
module.exports = { | ||
// By default, `npm run build` will create a standard Node app. | ||
// You can create optimized builds for different platforms by | ||
// specifying a different adapter | ||
adapter: '@sveltejs/adapter-node' | ||
kit: { | ||
// By default, `npm run build` will create a standard Node app. | ||
// You can create optimized builds for different platforms by | ||
// specifying a different adapter | ||
adapter: '@sveltejs/adapter-node' | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
module.exports = { | ||
// By default, `npm run build` will create a standard Node app. | ||
// You can create optimized builds for different platforms by | ||
// specifying a different adapter | ||
adapter: '@sveltejs/adapter-node' | ||
kit: { | ||
// By default, `npm run build` will create a standard Node app. | ||
// You can create optimized builds for different platforms by | ||
// specifying a different adapter | ||
adapter: '@sveltejs/adapter-node' | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
module.exports = { | ||
// By default, `npm run build` will create a standard Node app. | ||
// You can create optimized builds for different platforms by | ||
// specifying a different adapter | ||
adapter: '@sveltejs/adapter-node' | ||
kit: { | ||
// By default, `npm run build` will create a standard Node app. | ||
// You can create optimized builds for different platforms by | ||
// specifying a different adapter | ||
adapter: '@sveltejs/adapter-node' | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,62 @@ | ||
import relative from 'require-relative'; | ||
import { bold, yellow } from 'kleur/colors'; | ||
import options from './options'; | ||
|
||
const default_config = { | ||
target: null, | ||
startGlobal: null, // used for testing | ||
paths: { | ||
static: 'static', | ||
routes: 'src/routes', | ||
setup: 'src/setup', | ||
template: 'src/app.html' | ||
function warn(msg) { | ||
console.log(bold(yellow(msg))); | ||
} | ||
|
||
function validate(definition, option, keypath) { | ||
for (const key in option) { | ||
if (!(key in definition)) { | ||
throw new Error(`Unexpected option ${keypath}.${key}`); | ||
} | ||
} | ||
}; | ||
|
||
const merged = {}; | ||
|
||
for (const key in definition) { | ||
const expected = definition[key]; | ||
const actual = option[key]; | ||
|
||
const child_keypath = `${keypath}.${key}`; | ||
const has_children = expected.default && typeof expected.default === 'object' && !Array.isArray(expected.default); | ||
|
||
if (key in option) { | ||
if (has_children) { | ||
if (actual && (typeof actual !== 'object' || Array.isArray(actual))) { | ||
throw new Error(`${keypath}.${key} should be an object`); | ||
} | ||
|
||
merged[key] = validate(expected.default, actual, child_keypath); | ||
} else { | ||
merged[key] = expected.validate(actual, child_keypath); | ||
} | ||
} else { | ||
merged[key] = has_children | ||
? validate(expected.default, {}, child_keypath) | ||
: expected.default; | ||
} | ||
} | ||
|
||
return merged; | ||
} | ||
|
||
const expected = new Set(['compilerOptions', 'kit', 'preprocess']); | ||
|
||
export function load_config({ cwd = process.cwd() } = {}) { | ||
const config = relative('./svelte.config.js', cwd); | ||
return validate_config(config); | ||
} | ||
|
||
return { | ||
...default_config, | ||
...config, | ||
paths: { | ||
...default_config.paths, | ||
...config.paths | ||
export function validate_config(config) { | ||
for (const key in config) { | ||
if (!expected.has(key)) { | ||
warn(`Unexpected option ${key}${key in options ? ` (did you mean kit.${key}?)` : ''}`); | ||
} | ||
}; | ||
} | ||
|
||
const { kit = {} } = config; | ||
|
||
return validate(options, kit, 'kit'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { test } from 'uvu'; | ||
import * as assert from 'uvu/assert'; | ||
import { validate_config } from './index'; | ||
|
||
test('fills in defaults', () => { | ||
const validated = validate_config({}); | ||
|
||
assert.equal(validated, { | ||
adapter: [null], | ||
target: null, | ||
startGlobal: null, | ||
files: { | ||
assets: 'static', | ||
routes: 'src/routes', | ||
setup: 'src/setup', | ||
template: 'src/app.html' | ||
} | ||
}); | ||
}); | ||
|
||
test('errors on invalid values', () => { | ||
assert.throws(() => { | ||
validate_config({ | ||
kit: { | ||
target: 42 | ||
} | ||
}); | ||
}, /^kit\.target should be a string, if specified$/); | ||
}); | ||
|
||
test('errors on invalid nested values', () => { | ||
assert.throws(() => { | ||
validate_config({ | ||
kit: { | ||
files: { | ||
potato: 'blah' | ||
} | ||
} | ||
}); | ||
}, /^Unexpected option kit\.files\.potato$/); | ||
}); | ||
|
||
test('fills in partial blanks', () => { | ||
const validated = validate_config({ | ||
kit: { | ||
files: { | ||
assets: 'public' | ||
} | ||
} | ||
}); | ||
|
||
assert.equal(validated, { | ||
adapter: [null], | ||
target: null, | ||
startGlobal: null, | ||
files: { | ||
assets: 'public', | ||
routes: 'src/routes', | ||
setup: 'src/setup', | ||
template: 'src/app.html' | ||
} | ||
}); | ||
}); | ||
|
||
test.run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
export default { | ||
adapter: { | ||
default: [null], | ||
validate: option => { | ||
if (!option) return [null]; | ||
|
||
if (!Array.isArray(option)) { | ||
option = [option]; | ||
} | ||
|
||
assert_is_string(option[0], 'kit.adapter'); | ||
|
||
return option; | ||
} | ||
}, | ||
|
||
// TODO check that the selector is present in the provided template | ||
target: expect_string(null), | ||
|
||
// used for testing | ||
startGlobal: expect_string(null), | ||
|
||
files: { | ||
default: { | ||
// TODO check these files exist when the config is loaded? | ||
assets: expect_string('static'), | ||
routes: expect_string('src/routes'), | ||
setup: expect_string('src/setup'), | ||
template: expect_string('src/app.html') | ||
} | ||
} | ||
}; | ||
|
||
function expect_string(string) { | ||
return { | ||
default: string, | ||
validate: (option, keypath) => { | ||
assert_is_string(option, keypath); | ||
return option; | ||
} | ||
}; | ||
} | ||
|
||
function assert_is_string(option, keypath) { | ||
if (typeof option !== 'string') { | ||
throw new Error(`${keypath} should be a string, if specified`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think the 'did you mean' here is going to be generally helpful to future users? Singling this out seems to be maybe a bit too focused on people who used Kit before this change, which is practically nobody.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's really just there to reduce the number of discord DMs/mentions i/we get in the near term. agree that it doesn't serve much long term purpose
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I somehow had forgotten that there are people out there using it who don't have access to this repo. In light of that, I'd say that yes this warning was a good idea.