Skip to content
Open
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
4 changes: 2 additions & 2 deletions packages/sv/lib/cli/add/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ export const commonFilePaths = {
packageJson: 'package.json',
svelteConfig: 'svelte.config.js',
svelteConfigTS: 'svelte.config.ts',
jsconfig: 'jsconfig.json',
tsconfig: 'tsconfig.json',
jsConfig: 'jsconfig.json',
tsConfig: 'tsconfig.json',
viteConfig: 'vite.config.js',
viteConfigTS: 'vite.config.ts'
} as const;
Expand Down
19 changes: 12 additions & 7 deletions packages/sv/lib/cli/add/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ export async function createWorkspace({
const resolvedCwd = path.resolve(cwd);

// Will go up and prioritize jsconfig.json as it's first in the array
const tjsconfig = find.any([commonFilePaths.jsconfig, commonFilePaths.tsconfig], { cwd });
// If the file is not ending with jsconfig.json, then we are using typescript
const usesTypescript = !tjsconfig?.endsWith(commonFilePaths.jsconfig);
const jtsConfigPath = find.any([commonFilePaths.jsConfig, commonFilePaths.tsConfig], { cwd });
const typescript = jtsConfigPath?.endsWith(commonFilePaths.tsConfig) ?? false;

// This is not linked with typescript detection
const viteConfigPath = path.join(resolvedCwd, commonFilePaths.viteConfigTS);
const viteConfig = fs.existsSync(viteConfigPath)
? commonFilePaths.viteConfigTS
: commonFilePaths.viteConfig;

// ecosystem doesn't support `svelte.config.ts` https://github.com/sveltejs/cli/issues/816#issuecomment-3568032867
// but the user may have opt in to it
Comment on lines +36 to +37
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// ecosystem doesn't support `svelte.config.ts` https://github.com/sveltejs/cli/issues/816#issuecomment-3568032867
// but the user may have opt in to it
// `sv` uses `svelte.config.js` by default
// but users may have changed to `svelte.config.ts`

const svelteConfigPath = path.join(resolvedCwd, commonFilePaths.svelteConfigTS);
const svelteConfig = fs.existsSync(svelteConfigPath)
? commonFilePaths.svelteConfigTS
Expand Down Expand Up @@ -82,7 +84,7 @@ export async function createWorkspace({
return {
cwd: resolvedCwd,
packageManager: packageManager ?? (await detect({ cwd }))?.name ?? getUserAgent() ?? 'npm',
typescript: usesTypescript,
typescript,
files: {
viteConfig,
svelteConfig,
Expand Down Expand Up @@ -135,7 +137,7 @@ function parseKitOptions(cwd: string) {
const { ast } = parseScript(configSource);

const defaultExport = ast.body.find((s) => s.type === 'ExportDefaultDeclaration');
if (!defaultExport) throw Error('Missing default export in `svelte.config.js`');
if (!defaultExport) throw Error(`Missing default export in \`${commonFilePaths.svelteConfig}\``);

let objectExpression: AstTypes.ObjectExpression | undefined;
if (defaultExport.declaration.type === 'Identifier') {
Expand All @@ -157,14 +159,17 @@ function parseKitOptions(cwd: string) {
}

if (!objectExpression)
throw Error('Unable to find svelte config object expression from `svelte.config.js`');
throw Error(
`Unable to find svelte config object expression from \`${commonFilePaths.svelteConfig}\``
);
} else if (defaultExport.declaration.type === 'ObjectExpression') {
// e.g. `export default { ... };`
objectExpression = defaultExport.declaration;
}

// We'll error out since we can't safely determine the config object
if (!objectExpression) throw new Error('Unexpected svelte config shape from `svelte.config.js`');
if (!objectExpression)
throw new Error(`Unexpected svelte config shape from \`${commonFilePaths.svelteConfig}\``);

const kit = object.property(objectExpression, { name: 'kit', fallback: object.create({}) });
const files = object.property(kit, { name: 'files', fallback: object.create({}) });
Expand Down
3 changes: 2 additions & 1 deletion packages/sv/lib/cli/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,8 @@ export async function createVirtualWorkspace({
files: {
...tentativeWorkspace.files,
viteConfig: type === 'typescript' ? commonFilePaths.viteConfigTS : commonFilePaths.viteConfig,
svelteConfig: commonFilePaths.svelteConfig // currently we always use js files, never typescript files
// ecosystem doesn't support `svelte.config.ts` https://github.com/sveltejs/cli/issues/816#issuecomment-3568032867
svelteConfig: commonFilePaths.svelteConfig
}
};

Expand Down
5 changes: 3 additions & 2 deletions packages/sv/lib/create/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'node:fs';
import path from 'node:path';
import { mkdirp, copy, dist, getSharedFiles } from './utils.ts';
import { commonFilePaths } from '../cli/add/utils.ts';

export type TemplateType = (typeof templateTypes)[number];
export type LanguageType = (typeof languageTypes)[number];
Expand Down Expand Up @@ -68,7 +69,7 @@ function write_template_files(template: string, types: LanguageType, name: strin
function write_common_files(cwd: string, options: Options, name: string) {
const files = getSharedFiles();

const pkg_file = path.join(cwd, 'package.json');
const pkg_file = path.join(cwd, commonFilePaths.packageJson);
const pkg = /** @type {any} */ JSON.parse(fs.readFileSync(pkg_file, 'utf-8'));

sort_files(files).forEach((file) => {
Expand All @@ -77,7 +78,7 @@ function write_common_files(cwd: string, options: Options, name: string) {

if (exclude || !include) return;

if (file.name === 'package.json') {
if (file.name === commonFilePaths.packageJson) {
const new_pkg = JSON.parse(file.contents);
merge(pkg, new_pkg);
} else {
Expand Down
8 changes: 5 additions & 3 deletions packages/sv/lib/create/playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { parseJson, parseScript, parseSvelte } from '../core/tooling/parsers.ts'
import { isVersionUnsupportedBelow } from '../core/index.ts';
import { getSharedFiles } from './utils.ts';
import { walk } from 'zimmerframe';
import { commonFilePaths } from '../cli/add/utils.ts';

export function validatePlaygroundUrl(link: string): boolean {
try {
Expand Down Expand Up @@ -231,7 +232,7 @@ export function setupPlaygroundProject(
fs.writeFileSync(filePath, newContent, 'utf-8');

// add packages as dependencies to package.json if requested
const pkgPath = path.join(cwd, 'package.json');
const pkgPath = path.join(cwd, commonFilePaths.packageJson);
const pkgSource = fs.readFileSync(pkgPath, 'utf-8');
const pkgJson = parseJson(pkgSource);
let updatePackageJson = false;
Expand All @@ -245,15 +246,16 @@ export function setupPlaygroundProject(

let experimentalAsyncNeeded = true;
const addExperimentalAsync = () => {
const svelteConfigPath = path.join(cwd, 'svelte.config.js');
// ecosystem doesn't support `svelte.config.ts` https://github.com/sveltejs/cli/issues/816#issuecomment-3568032867
const svelteConfigPath = path.join(cwd, commonFilePaths.svelteConfig);
const svelteConfig = fs.readFileSync(svelteConfigPath, 'utf-8');
const { ast, generateCode } = parseScript(svelteConfig);
const { value: config } = js.exports.createDefault(ast, { fallback: js.object.create({}) });
js.object.overrideProperties(config, { compilerOptions: { experimental: { async: true } } });
fs.writeFileSync(svelteConfigPath, generateCode(), 'utf-8');
};

// we want to change the svelte version, even if the user decieded
// we want to change the svelte version, even if the user decided
// to not install external dependencies
if (playground.svelteVersion) {
updatePackageJson = true;
Expand Down
4 changes: 2 additions & 2 deletions packages/sv/lib/create/templates/demo/src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import Counter from './Counter.svelte';
import welcome from '$lib/images/svelte-welcome.webp';
import welcomeFallback from '$lib/images/svelte-welcome.png';
import welcome from './svelte-welcome.webp';
import welcomeFallback from './svelte-welcome.png';
</script>

<svelte:head>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<script lang="ts">
import { resolve } from '$app/paths';
import { page } from '$app/state';
import logo from '$lib/images/svelte-logo.svg';
import github from '$lib/images/github.svg';
import logo from './svelte-logo.svg';
import github from './github.svg';
</script>

<header>
Expand Down
Loading