Skip to content

Commit

Permalink
use local Svelte installation
Browse files Browse the repository at this point in the history
  • Loading branch information
dummdidumm committed Apr 16, 2024
1 parent 993add6 commit 4a5b9ec
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 13 deletions.
20 changes: 19 additions & 1 deletion packages/migrate/migrations/self-closing-tags/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,18 @@ import fs from 'node:fs';
import prompts from 'prompts';
import glob from 'tiny-glob/sync.js';
import { remove_self_closing_tags } from './migrate.js';
import { pathToFileURL } from 'node:url';
import { resolve } from 'import-meta-resolve';

export async function migrate() {
let compiler;
try {
compiler = await import_from_cwd('svelte/compiler');
} catch (e) {
console.log(colors.bold().red('❌ Could not find a local Svelte installation.'));
return;
}

console.log(
colors.bold().yellow('\nThis will update .svelte files inside the current directory\n')
);
Expand All @@ -26,7 +36,7 @@ export async function migrate() {

for (const file of files) {
try {
const code = await remove_self_closing_tags(fs.readFileSync(file, 'utf-8'));
const code = await remove_self_closing_tags(compiler, fs.readFileSync(file, 'utf-8'));
fs.writeFileSync(file, code);
} catch (e) {
// continue
Expand All @@ -36,3 +46,11 @@ export async function migrate() {
console.log(colors.bold().green('✔ Your project has been updated'));
console.log(' If using Prettier, please upgrade to the latest prettier-plugin-svelte version');
}

/** @param {string} name */
async function import_from_cwd(name) {
const cwd = pathToFileURL(process.cwd()).href;
const url = await resolve(name, cwd + '/x.js');

return import(url);
}
19 changes: 13 additions & 6 deletions packages/migrate/migrations/self-closing-tags/migrate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import MagicString from 'magic-string';
import { parse, preprocess, walk } from 'svelte/compiler';
import { walk } from 'zimmerframe';

const VoidElements = [
'area',
Expand Down Expand Up @@ -109,15 +109,20 @@ const SVGElements = [
'vkern'
];

/** @param {string} source */
export async function remove_self_closing_tags(source) {
/**
* @param {{ preprocess: any, parse: any }} svelte_compiler
* @param {string} source
*/
export async function remove_self_closing_tags({ preprocess, parse }, source) {
const preprocessed = await preprocess(source, {
/** @param {{ content: string }} input */
script: ({ content }) => ({
code: content
.split('\n')
.map((line) => ' '.repeat(line.length))
.join('\n')
}),
/** @param {{ content: string }} input */
style: ({ content }) => ({
code: content
.split('\n')
Expand All @@ -132,16 +137,16 @@ export async function remove_self_closing_tags(source) {
let is_foreign = false;
let is_custom_element = false;

walk(/** @type {any} */ (ast.html), {
/** @param {Record<string, any>} node */
enter(node) {
walk(ast.html, null, {
_(node, { next, stop }) {
if (node.type === 'Options') {
const namespace = node.attributes.find(
/** @param {any} a */
(a) => a.type === 'Attribute' && a.name === 'namespace'
);
if (namespace?.value[0].data === 'foreign') {
is_foreign = true;
stop();
return;
}

Expand Down Expand Up @@ -172,6 +177,8 @@ export async function remove_self_closing_tags(source) {
}
});
}

next();
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assert, test } from 'vitest';
import * as compiler from 'svelte/compiler';
import { remove_self_closing_tags } from './migrate.js';

/** @type {Record<string, string>} */
Expand All @@ -25,6 +26,6 @@ const tests = {
for (const input in tests) {
test(input, async () => {
const output = tests[input];
assert.equal(await remove_self_closing_tags(input), output);
assert.equal(await remove_self_closing_tags(compiler, input), output);
});
}
6 changes: 4 additions & 2 deletions packages/migrate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,22 @@
"!migrations/**/samples.md"
],
"dependencies": {
"import-meta-resolve": "^4.0.0",
"kleur": "^4.1.5",
"magic-string": "^0.30.5",
"prompts": "^2.4.2",
"semver": "^7.5.4",
"svelte": "^4.0.0",
"tiny-glob": "^0.2.9",
"ts-morph": "^22.0.0",
"typescript": "^5.3.3"
"typescript": "^5.3.3",
"zimmerframe": "^1.1.2"
},
"devDependencies": {
"@types/node": "^18.19.3",
"@types/prompts": "^2.4.9",
"@types/semver": "^7.5.6",
"prettier": "^3.1.1",
"svelte": "^4.2.10",
"vitest": "^1.5.0"
},
"scripts": {
Expand Down
16 changes: 13 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4a5b9ec

Please sign in to comment.