Skip to content

Commit 4a5b9ec

Browse files
committed
use local Svelte installation
1 parent 993add6 commit 4a5b9ec

File tree

5 files changed

+51
-13
lines changed

5 files changed

+51
-13
lines changed

packages/migrate/migrations/self-closing-tags/index.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,18 @@ import fs from 'node:fs';
33
import prompts from 'prompts';
44
import glob from 'tiny-glob/sync.js';
55
import { remove_self_closing_tags } from './migrate.js';
6+
import { pathToFileURL } from 'node:url';
7+
import { resolve } from 'import-meta-resolve';
68

79
export async function migrate() {
10+
let compiler;
11+
try {
12+
compiler = await import_from_cwd('svelte/compiler');
13+
} catch (e) {
14+
console.log(colors.bold().red('❌ Could not find a local Svelte installation.'));
15+
return;
16+
}
17+
818
console.log(
919
colors.bold().yellow('\nThis will update .svelte files inside the current directory\n')
1020
);
@@ -26,7 +36,7 @@ export async function migrate() {
2636

2737
for (const file of files) {
2838
try {
29-
const code = await remove_self_closing_tags(fs.readFileSync(file, 'utf-8'));
39+
const code = await remove_self_closing_tags(compiler, fs.readFileSync(file, 'utf-8'));
3040
fs.writeFileSync(file, code);
3141
} catch (e) {
3242
// continue
@@ -36,3 +46,11 @@ export async function migrate() {
3646
console.log(colors.bold().green('✔ Your project has been updated'));
3747
console.log(' If using Prettier, please upgrade to the latest prettier-plugin-svelte version');
3848
}
49+
50+
/** @param {string} name */
51+
async function import_from_cwd(name) {
52+
const cwd = pathToFileURL(process.cwd()).href;
53+
const url = await resolve(name, cwd + '/x.js');
54+
55+
return import(url);
56+
}

packages/migrate/migrations/self-closing-tags/migrate.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import MagicString from 'magic-string';
2-
import { parse, preprocess, walk } from 'svelte/compiler';
2+
import { walk } from 'zimmerframe';
33

44
const VoidElements = [
55
'area',
@@ -109,15 +109,20 @@ const SVGElements = [
109109
'vkern'
110110
];
111111

112-
/** @param {string} source */
113-
export async function remove_self_closing_tags(source) {
112+
/**
113+
* @param {{ preprocess: any, parse: any }} svelte_compiler
114+
* @param {string} source
115+
*/
116+
export async function remove_self_closing_tags({ preprocess, parse }, source) {
114117
const preprocessed = await preprocess(source, {
118+
/** @param {{ content: string }} input */
115119
script: ({ content }) => ({
116120
code: content
117121
.split('\n')
118122
.map((line) => ' '.repeat(line.length))
119123
.join('\n')
120124
}),
125+
/** @param {{ content: string }} input */
121126
style: ({ content }) => ({
122127
code: content
123128
.split('\n')
@@ -132,16 +137,16 @@ export async function remove_self_closing_tags(source) {
132137
let is_foreign = false;
133138
let is_custom_element = false;
134139

135-
walk(/** @type {any} */ (ast.html), {
136-
/** @param {Record<string, any>} node */
137-
enter(node) {
140+
walk(ast.html, null, {
141+
_(node, { next, stop }) {
138142
if (node.type === 'Options') {
139143
const namespace = node.attributes.find(
140144
/** @param {any} a */
141145
(a) => a.type === 'Attribute' && a.name === 'namespace'
142146
);
143147
if (namespace?.value[0].data === 'foreign') {
144148
is_foreign = true;
149+
stop();
145150
return;
146151
}
147152

@@ -172,6 +177,8 @@ export async function remove_self_closing_tags(source) {
172177
}
173178
});
174179
}
180+
181+
next();
175182
}
176183
});
177184

packages/migrate/migrations/self-closing-tags/migrate.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { assert, test } from 'vitest';
2+
import * as compiler from 'svelte/compiler';
23
import { remove_self_closing_tags } from './migrate.js';
34

45
/** @type {Record<string, string>} */
@@ -25,6 +26,6 @@ const tests = {
2526
for (const input in tests) {
2627
test(input, async () => {
2728
const output = tests[input];
28-
assert.equal(await remove_self_closing_tags(input), output);
29+
assert.equal(await remove_self_closing_tags(compiler, input), output);
2930
});
3031
}

packages/migrate/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,22 @@
2424
"!migrations/**/samples.md"
2525
],
2626
"dependencies": {
27+
"import-meta-resolve": "^4.0.0",
2728
"kleur": "^4.1.5",
2829
"magic-string": "^0.30.5",
2930
"prompts": "^2.4.2",
3031
"semver": "^7.5.4",
31-
"svelte": "^4.0.0",
3232
"tiny-glob": "^0.2.9",
3333
"ts-morph": "^22.0.0",
34-
"typescript": "^5.3.3"
34+
"typescript": "^5.3.3",
35+
"zimmerframe": "^1.1.2"
3536
},
3637
"devDependencies": {
3738
"@types/node": "^18.19.3",
3839
"@types/prompts": "^2.4.9",
3940
"@types/semver": "^7.5.6",
4041
"prettier": "^3.1.1",
42+
"svelte": "^4.2.10",
4143
"vitest": "^1.5.0"
4244
},
4345
"scripts": {

pnpm-lock.yaml

Lines changed: 13 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)