Skip to content

Commit cbd60cc

Browse files
feat: upgrade to prettier 3 and prettier-plugin-svelte 3 (#10410)
This will give users the new versions via create-svelte. The rest of the change is internal closes #10751 closes #10716 --------- Co-authored-by: Tee Ming <[email protected]>
1 parent 2903eb9 commit cbd60cc

File tree

118 files changed

+278
-288
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+278
-288
lines changed

.changeset/ten-bats-sneeze.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'create-svelte': minor
3+
---
4+
5+
feat: upgrade to prettier 3 and prettier-plugin-svelte 3

.prettierrc

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"singleQuote": true,
44
"trailingComma": "none",
55
"printWidth": 100,
6+
"plugins": ["prettier-plugin-svelte"],
67
"overrides": [
78
{
89
"files": ["*.svelte"],

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"postinstall": "pnpm -r generate:types"
2121
},
2222
"devDependencies": {
23-
"@changesets/cli": "^2.26.0",
23+
"@changesets/cli": "^2.26.2",
2424
"@rollup/plugin-commonjs": "^25.0.0",
2525
"@rollup/plugin-json": "^6.0.0",
2626
"@rollup/plugin-node-resolve": "^15.0.1",
@@ -32,7 +32,7 @@
3232
"eslint-plugin-svelte": "^2.31.0",
3333
"eslint-plugin-unicorn": "^49.0.0",
3434
"playwright": "1.30.0",
35-
"prettier": "^2.8.0",
35+
"prettier": "^3.1.0",
3636
"rollup": "^3.29.4",
3737
"svelte": "^4.2.2",
3838
"typescript": "^4.9.4"

packages/adapter-auto/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"adapters.js"
2626
],
2727
"scripts": {
28-
"lint": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore",
28+
"lint": "prettier --check .",
2929
"format": "pnpm lint --write",
3030
"check": "tsc"
3131
},

packages/adapter-cloudflare-workers/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"index.d.ts"
2626
],
2727
"scripts": {
28-
"lint": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore",
28+
"lint": "prettier --check .",
2929
"format": "pnpm lint --write",
3030
"check": "tsc"
3131
},

packages/adapter-cloudflare/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
],
2727
"scripts": {
2828
"build": "esbuild src/worker.js --bundle --outfile=files/worker.js --external:SERVER --external:MANIFEST --format=esm",
29-
"lint": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore",
29+
"lint": "prettier --check .",
3030
"format": "pnpm lint --write",
3131
"check": "tsc --skipLibCheck",
3232
"prepublishOnly": "pnpm build"

packages/adapter-netlify/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"build": "node -e \"fs.rmSync('files', { force: true, recursive: true })\" && rollup -c && cp src/edge.js files/edge.js",
2929
"test": "vitest run",
3030
"check": "tsc",
31-
"lint": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore",
31+
"lint": "prettier --check .",
3232
"format": "pnpm lint --write",
3333
"prepublishOnly": "pnpm build"
3434
},

packages/adapter-node/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"build": "node -e \"fs.rmSync('files', { force: true, recursive: true })\" && rollup -c",
2929
"test": "echo \"tests temporarily disabled\" # c8 vitest run",
3030
"check": "tsc",
31-
"lint": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore",
31+
"lint": "prettier --check .",
3232
"format": "pnpm lint --write",
3333
"prepublishOnly": "pnpm build"
3434
},

packages/adapter-static/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"platforms.js"
2525
],
2626
"scripts": {
27-
"lint": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore",
27+
"lint": "prettier --check .",
2828
"check": "tsc",
2929
"format": "pnpm lint --write",
3030
"test": "pnpm -r --workspace-concurrency 1 --filter=\"./test/**\" test"

packages/adapter-static/test/apps/prerendered/src/app.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!DOCTYPE html>
1+
<!doctype html>
22
<html lang="en">
33
<head>
44
<meta charset="utf-8" />

packages/adapter-static/test/apps/spa/src/app.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!DOCTYPE html>
1+
<!doctype html>
22
<html lang="en">
33
<head>
44
<meta charset="utf-8" />

packages/adapter-static/test/apps/spa/src/routes/+layout.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
<a href="/about">about</a>
44
</nav>
55

6-
<slot></slot>
6+
<slot />
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<h1>This page was not prerendered</h1>
1+
<h1>This page was not prerendered</h1>
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<h1>This page was prerendered</h1>
1+
<h1>This page was prerendered</h1>

packages/adapter-vercel/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"index.d.ts"
2626
],
2727
"scripts": {
28-
"lint": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore",
28+
"lint": "prettier --check .",
2929
"format": "pnpm lint --write",
3030
"check": "tsc",
3131
"test": "vitest run"

packages/amp/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"index.d.ts"
2424
],
2525
"scripts": {
26-
"lint": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore",
26+
"lint": "prettier --check .",
2727
"format": "pnpm lint --write"
2828
},
2929
"peerDependencies": {

packages/create-svelte/.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
/templates/*/.vercel_build_output
1212
/templates/*/.netlify
1313
/templates/*/dist
14-
/templates/*/package
14+
/templates/*/package

packages/create-svelte/package.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818
"devDependencies": {
1919
"@playwright/test": "1.30.0",
2020
"@types/gitignore-parser": "^0.0.2",
21-
"@types/prettier": "^2.7.1",
2221
"gitignore-parser": "^0.0.2",
23-
"prettier": "^2.8.0",
24-
"prettier-plugin-svelte": "^2.10.1",
22+
"prettier": "^3.1.0",
23+
"prettier-plugin-svelte": "^3.0.0",
2524
"sucrase": "^3.29.0",
2625
"svelte": "^4.2.2",
2726
"tiny-glob": "^0.2.9",
@@ -31,7 +30,7 @@
3130
"build": "node scripts/build-templates",
3231
"test": "pnpm build && vitest run",
3332
"check": "tsc",
34-
"lint": "prettier --check . --config ../../.prettierrc --ignore-path ../../.gitignore --ignore-path .gitignore --plugin prettier-plugin-svelte --plugin-search-dir=.",
33+
"lint": "prettier --check . --config ../../.prettierrc --ignore-path ../../.gitignore --ignore-path .gitignore",
3534
"format": "pnpm lint --write",
3635
"prepublishOnly": "pnpm build",
3736
"postpublish": "echo \"Updating template repo\" && bash ./scripts/update-template-repo.sh"

packages/create-svelte/scripts/build-templates.js

+37-15
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import glob from 'tiny-glob/sync.js';
77
import { mkdirp, rimraf } from '../utils.js';
88

99
/** @param {string} content */
10-
function convert_typescript(content) {
10+
async function convert_typescript(content) {
1111
let { code } = transform(content, {
1212
transforms: ['typescript'],
1313
disableESTransforms: true
@@ -19,7 +19,7 @@ function convert_typescript(content) {
1919
// Prettier strips 'unnecessary' parens from .ts files, we need to hack them back in
2020
code = code.replace(/(\/\*\* @type.+? \*\/) (.+?) \/\*\*\*\//g, '$1($2)');
2121

22-
return prettier.format(code, {
22+
return await prettier.format(code, {
2323
parser: 'babel',
2424
useTabs: true,
2525
singleQuote: true,
@@ -78,7 +78,8 @@ async function generate_templates(shared) {
7878
null: []
7979
};
8080

81-
glob('**/*', { cwd, filesOnly: true, dot: true }).forEach((name) => {
81+
const files = glob('**/*', { cwd, filesOnly: true, dot: true });
82+
for (const name of files) {
8283
// the package.template.json thing is a bit annoying — basically we want
8384
// to be able to develop and deploy the app from here, but have a different
8485
// package.json in newly created projects (based on package.template.json)
@@ -87,14 +88,14 @@ async function generate_templates(shared) {
8788
// TODO package-specific versions
8889
contents = contents.replace(/workspace:\*/g, 'next');
8990
fs.writeFileSync(`${dir}/package.json`, contents);
90-
return;
91+
continue;
9192
}
9293

9394
// ignore files that are written conditionally
94-
if (shared.has(name)) return;
95+
if (shared.has(name)) continue;
9596

9697
// ignore contents of .gitignore or .ignore
97-
if (!gitignore.accepts(name) || !ignore.accepts(name) || name === '.ignore') return;
98+
if (!gitignore.accepts(name) || !ignore.accepts(name) || name === '.ignore') continue;
9899

99100
if (/\.(ts|svelte)$/.test(name)) {
100101
const contents = fs.readFileSync(path.join(cwd, name), 'utf8');
@@ -103,7 +104,7 @@ async function generate_templates(shared) {
103104
if (name.endsWith('app.d.ts')) types.checkjs.push({ name, contents });
104105
types.typescript.push({ name, contents });
105106
} else if (name.endsWith('.ts')) {
106-
const js = convert_typescript(contents);
107+
const js = await convert_typescript(contents);
107108

108109
types.typescript.push({
109110
name,
@@ -125,9 +126,14 @@ async function generate_templates(shared) {
125126
// possible (e.g. preserving double line breaks). Sucrase is the best
126127
// tool for the job because it just removes the types; Prettier then
127128
// tidies up the end result
128-
const js_contents = contents.replace(
129+
const js_contents = await replace_async(
130+
contents,
129131
/<script([^>]+)>([\s\S]+?)<\/script>/g,
130-
(m, attrs, typescript) => {
132+
async (
133+
/** @type {any} */ m,
134+
/** @type {string} */ attrs,
135+
/** @type {string} */ typescript
136+
) => {
131137
// Sucrase assumes 'unused' imports (which _are_ used, but only
132138
// in the markup) are type imports, and strips them. This step
133139
// prevents it from drawing that conclusion
@@ -149,14 +155,15 @@ async function generate_templates(shared) {
149155
disableESTransforms: true
150156
}).code.slice(0, -suffix.length);
151157

152-
const contents = prettier
153-
.format(transformed, {
158+
const contents = (
159+
await prettier.format(transformed, {
154160
parser: 'babel',
155161
useTabs: true,
156162
singleQuote: true,
157163
trailingComma: 'none',
158164
printWidth: 100
159165
})
166+
)
160167
.trim()
161168
.replace(/^(.)/gm, '\t$1');
162169

@@ -184,7 +191,7 @@ async function generate_templates(shared) {
184191
mkdirp(path.dirname(dest));
185192
fs.copyFileSync(path.join(cwd, name), dest);
186193
}
187-
});
194+
}
188195

189196
fs.copyFileSync(meta_file, `${dir}/meta.json`);
190197
fs.writeFileSync(
@@ -196,6 +203,20 @@ async function generate_templates(shared) {
196203
}
197204
}
198205

206+
/**
207+
* @param {string} string
208+
* @param {RegExp} regexp
209+
* @param {{ (m: any, attrs: string, typescript: string): Promise<string>; (arg0: any): any; }} replacer
210+
*/
211+
async function replace_async(string, regexp, replacer) {
212+
const replacements = await Promise.all(
213+
// @ts-ignore
214+
Array.from(string.matchAll(regexp), (match) => replacer(...match))
215+
);
216+
let i = 0;
217+
return string.replace(regexp, () => replacements[i++]);
218+
}
219+
199220
async function generate_shared() {
200221
const cwd = path.resolve('shared');
201222

@@ -205,7 +226,8 @@ async function generate_shared() {
205226
/** @type {Array<{ name: string, include: string[], exclude: string[], contents: string }>} */
206227
const files = [];
207228

208-
glob('**/*', { cwd, filesOnly: true, dot: true }).forEach((file) => {
229+
const globbed = glob('**/*', { cwd, filesOnly: true, dot: true });
230+
for (const file of globbed) {
209231
const contents = fs.readFileSync(path.join(cwd, file), 'utf8');
210232

211233
/** @type {string[]} */
@@ -232,7 +254,7 @@ async function generate_shared() {
232254
if (name.endsWith('.ts') && !include.includes('typescript')) {
233255
// file includes types in TypeScript and JSDoc —
234256
// create .js file, with and without JSDoc
235-
const js = convert_typescript(contents);
257+
const js = await convert_typescript(contents);
236258
const js_name = name.replace(/\.ts$/, '.js');
237259

238260
// typescript
@@ -265,7 +287,7 @@ async function generate_shared() {
265287
shared.add(name);
266288
files.push({ name, include, exclude, contents });
267289
}
268-
});
290+
}
269291

270292
files.sort((a, b) => a.include.length + a.exclude.length - (b.include.length + b.exclude.length));
271293

packages/create-svelte/shared/+eslint+prettier/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"eslint-config-prettier": "^8.5.0"
44
},
55
"scripts": {
6-
"lint": "prettier --plugin-search-dir . --check . && eslint .",
7-
"format": "prettier --plugin-search-dir . --write ."
6+
"lint": "prettier --check . && eslint .",
7+
"format": "prettier --write ."
88
}
99
}

packages/create-svelte/shared/+prettier/.prettierrc

-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@
44
"trailingComma": "none",
55
"printWidth": 100,
66
"plugins": ["prettier-plugin-svelte"],
7-
"pluginSearchDirs": ["."],
87
"overrides": [{ "files": "*.svelte", "options": { "parser": "svelte" } }]
98
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"devDependencies": {
3-
"prettier": "^2.8.0",
4-
"prettier-plugin-svelte": "^2.10.1"
3+
"prettier": "^3.0.0",
4+
"prettier-plugin-svelte": "^3.0.0"
55
}
66
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"scripts": {
3-
"lint": "prettier --plugin-search-dir . --check .",
4-
"format": "prettier --plugin-search-dir . --write ."
3+
"lint": "prettier --check .",
4+
"format": "prettier --write ."
55
}
66
}

packages/create-svelte/templates/default/src/app.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!DOCTYPE html>
1+
<!doctype html>
22
<html lang="en">
33
<head>
44
<meta charset="utf-8" />

packages/create-svelte/templates/skeleton/src/app.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!DOCTYPE html>
1+
<!doctype html>
22
<html lang="en">
33
<head>
44
<meta charset="utf-8" />

packages/create-svelte/templates/skeletonlib/src/app.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!DOCTYPE html>
1+
<!doctype html>
22
<html lang="en">
33
<head>
44
<meta charset="utf-8" />

packages/enhanced-img/.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test/Output.svelte

packages/enhanced-img/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
"homepage": "https://kit.svelte.dev",
1212
"type": "module",
1313
"scripts": {
14-
"lint": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore",
14+
"lint": "prettier --check .",
1515
"check": "tsc",
16-
"format": "prettier --write . --config ../../.prettierrc --ignore-path .gitignore",
16+
"format": "prettier --write .",
1717
"test": "vitest"
1818
},
1919
"files": [

0 commit comments

Comments
 (0)