Skip to content

Commit 7c11aa9

Browse files
authored
(chore) mini repl for svelte2tsx (#744)
1 parent 588a93e commit 7c11aa9

File tree

8 files changed

+61
-9
lines changed

8 files changed

+61
-9
lines changed

.vscode/launch.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@
1212
"outFiles": ["${workspaceRoot}/packages/svelte-vscode/dist/**/*.js"],
1313
"preLaunchTask": "npm: watch"
1414
},
15+
{
16+
"type": "node",
17+
"request": "launch",
18+
"name": "Run 'svelte2tsx/repl/debug.ts' with debugger",
19+
"runtimeArgs": ["-r", "ts-node/register"],
20+
"args": ["${workspaceFolder}/packages/svelte2tsx/repl/debug.ts"],
21+
"env": {
22+
"TS_NODE_COMPILER_OPTIONS": "{\"esModuleInterop\":true, \"target\": \"es2018\"}",
23+
"TS_NODE_TRANSPILE_ONLY": "true"
24+
},
25+
"console": "integratedTerminal",
26+
"internalConsoleOptions": "neverOpen"
27+
},
1528
{
1629
"type": "node",
1730
"request": "launch",

packages/svelte2tsx/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ node_modules
55
/index.mjs
66
test/typecheck/samples/**/input.svelte.tsx
77
test/build
8+
repl/output

packages/svelte2tsx/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
"main": "index.js",
1818
"types": "index.d.ts",
1919
"devDependencies": {
20+
"@rollup/plugin-commonjs": "^15.0.0",
21+
"@rollup/plugin-json": "^4.0.0",
22+
"@rollup/plugin-node-resolve": "^9.0.0",
23+
"@rollup/plugin-typescript": "^6.0.0",
2024
"@types/mocha": "^5.2.7",
2125
"@types/node": "^8.10.53",
2226
"@types/unist": "^2.0.3",
@@ -25,11 +29,7 @@
2529
"mocha": "^6.2.2",
2630
"periscopic": "^2.0.2",
2731
"rollup": "^2.28.0",
28-
"@rollup/plugin-commonjs": "^15.0.0",
2932
"rollup-plugin-delete": "^2.0.0",
30-
"@rollup/plugin-json": "^4.0.0",
31-
"@rollup/plugin-node-resolve": "^9.0.0",
32-
"@rollup/plugin-typescript": "^6.0.0",
3333
"source-map": "^0.6.1",
3434
"source-map-support": "^0.5.16",
3535
"svelte": "~3.35.0",

packages/svelte2tsx/repl/debug.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import fs from 'fs';
2+
import svelte2tsx from '../src';
3+
const content = fs.readFileSync(`${__dirname}/index.svelte`, 'utf-8');
4+
svelte2tsx(content);
5+
/**
6+
* To enable the REPL, simply run the "dev" package script.
7+
*
8+
* The "/repl/index.svelte" file will be converted to tsx
9+
* at "/repl/output/" using the modified source code on change.
10+
*
11+
* Alternatively you may run this file with a debugger attached,
12+
* to do so, hit "Ctrl+Shift+D" and select "svelte2tsx" in the dropdown.
13+
*/

packages/svelte2tsx/repl/index.svelte

Whitespace-only changes.

packages/svelte2tsx/rollup.config.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
1-
import typescript from '@rollup/plugin-typescript';
21
import commonjs from '@rollup/plugin-commonjs';
3-
import resolve from '@rollup/plugin-node-resolve';
42
import json from '@rollup/plugin-json';
3+
import resolve from '@rollup/plugin-node-resolve';
4+
import typescript from '@rollup/plugin-typescript';
55
import builtins from 'builtin-modules';
6+
import fs from 'fs';
7+
import path from 'path';
8+
9+
function repl() {
10+
return {
11+
name: 'dev-repl',
12+
buildStart() {
13+
this.addWatchFile('./repl/index.svelte');
14+
},
15+
writeBundle() {
16+
if (!this.meta.watchMode) return;
17+
18+
const repl = `${__dirname}/repl/`;
19+
const output = `${__dirname}/repl/output/`;
20+
21+
delete require.cache[path.resolve(__dirname, 'index.js')];
22+
const svelte2tsx = require('./index.js');
23+
24+
const tsx = svelte2tsx(fs.readFileSync(`${repl}/index.svelte`, 'utf-8'));
625

26+
if (!fs.existsSync(output)) fs.mkdirSync(output);
27+
fs.writeFileSync(`${repl}/output/code.tsx`, tsx.code);
28+
}
29+
};
30+
}
731
export default [
832
{
933
input: 'src/index.ts',
@@ -22,7 +46,8 @@ export default [
2246
resolve({ browser: false, preferBuiltins: true }),
2347
commonjs(),
2448
json(),
25-
typescript({ include: ['src/**/*'] })
49+
typescript({ include: ['src/**/*'] }),
50+
repl()
2651
],
2752
watch: {
2853
clearScreen: false

packages/svelte2tsx/src/htmlxtojsx/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export function convertHtmlxToJsx(
5555

5656
let ifScope = new IfScope(templateScopeManager);
5757

58-
(walk as any)(ast, {
58+
walk(ast, {
5959
enter: (node: Node, parent: Node, prop: string, index: number) => {
6060
try {
6161
switch (node.type) {

packages/svelte2tsx/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@
2323
"@/*": ["src/*"]
2424
},
2525
// "include" is set in rollup.config(.test).js
26-
"exclude": ["node_modules"]
26+
"exclude": ["node_modules", "repl"]
2727
}

0 commit comments

Comments
 (0)