Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mini repl for svelte2tsx #744

Merged
merged 18 commits into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from 7 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
13 changes: 13 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@
"outFiles": ["${workspaceRoot}/packkages/svelte-vscode/dist/**/*.js"],
"preLaunchTask": "npm: watch"
},
{
"type": "node",
"request": "launch",
"name": "Run 'svelte2tsx/repl/debug.ts' with debugger",
"runtimeArgs": ["-r", "ts-node/register"],
"args": ["${workspaceFolder}/packages/svelte2tsx/repl/debug.ts"],
"env": {
"TS_NODE_COMPILER_OPTIONS": "{\"esModuleInterop\":true, \"target\": \"es2018\"}",
"TS_NODE_TRANSPILE_ONLY": "true"
},
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "launch",
Expand Down
1 change: 1 addition & 0 deletions packages/svelte2tsx/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ node_modules
/index.mjs
test/typecheck/samples/**/input.svelte.tsx
test/build
repl/output
8 changes: 4 additions & 4 deletions packages/svelte2tsx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
"main": "index.js",
"types": "index.d.ts",
"devDependencies": {
"@rollup/plugin-commonjs": "^15.0.0",
"@rollup/plugin-json": "^4.0.0",
"@rollup/plugin-node-resolve": "^9.0.0",
"@rollup/plugin-typescript": "^6.0.0",
"@types/mocha": "^5.2.7",
"@types/node": "^8.10.53",
"@types/unist": "^2.0.3",
Expand All @@ -25,11 +29,7 @@
"mocha": "^6.2.2",
"periscopic": "^2.0.2",
"rollup": "^2.28.0",
"@rollup/plugin-commonjs": "^15.0.0",
"rollup-plugin-delete": "^2.0.0",
"@rollup/plugin-json": "^4.0.0",
"@rollup/plugin-node-resolve": "^9.0.0",
"@rollup/plugin-typescript": "^6.0.0",
"source-map": "^0.6.1",
"source-map-support": "^0.5.16",
"svelte": "3.28.0",
Expand Down
13 changes: 13 additions & 0 deletions packages/svelte2tsx/repl/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import fs from 'fs';
import svelte2tsx from '../src';
const content = fs.readFileSync(`${__dirname}/index.svelte`, 'utf-8');
svelte2tsx(content);
/**
* To enable the REPL, simply run the "dev" package script.
*
* The "/repl/index.svelte" file will be converted to tsx
dummdidumm marked this conversation as resolved.
Show resolved Hide resolved
* at "/repl/output/" using the modified source code on change.
*
* Alternatively you may run this file with a debugger attached,
* to do so, hit "Ctrl+Shift+D" and select "svelte2tsx" in the dropdown.
*/
Empty file.
31 changes: 28 additions & 3 deletions packages/svelte2tsx/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
import typescript from '@rollup/plugin-typescript';
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import json from '@rollup/plugin-json';
import resolve from '@rollup/plugin-node-resolve';
import typescript from '@rollup/plugin-typescript';
import builtins from 'builtin-modules';
import fs from 'fs';
import path from 'path';

function repl() {
Copy link
Member

Choose a reason for hiding this comment

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

Not quite sure why we need this? Isn't all this handled without Rollup, just through sucrase?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This part uses rollup watch, so just by having the dev script running in the background it automatically updates the repl results on save. i.e. Everything is instant, just hit ctrl+s on any change anywhere ( source or repl input ) and the results get live updates like an actual repl would

It is true that sucrase computes the same thing but you have to run it yourself and checking values in the debugger is a much slower process

Copy link
Contributor Author

@pushkine pushkine Jan 9, 2021

Choose a reason for hiding this comment

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

On a side note that part only outputs tsx right now, I imagine there are other outputs that could be interesting to print

return {
name: 'dev-repl',
buildStart() {
this.addWatchFile('./repl/index.svelte');
},
writeBundle() {
if (!this.meta.watchMode) return;

const repl = `${__dirname}/repl/`;
const output = `${__dirname}/repl/output/`;

delete require.cache[path.resolve(__dirname, 'index.js')];
const svelte2tsx = require('./index.js');

const tsx = svelte2tsx(fs.readFileSync(`${repl}/index.svelte`, 'utf-8'));

if (!fs.existsSync(output)) fs.mkdirSync(output);
fs.writeFileSync(`${repl}/output/code.tsx`, tsx.code);
}
};
}
export default [
{
input: 'src/index.ts',
Expand All @@ -22,7 +46,8 @@ export default [
resolve({ browser: false, preferBuiltins: true }),
commonjs(),
json(),
typescript({ include: ['src/**/*'] })
typescript({ include: ['src/**/*'] }),
repl()
],
watch: {
clearScreen: false
Expand Down
4 changes: 2 additions & 2 deletions packages/svelte2tsx/src/htmlxtojsx/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Node } from 'estree-walker';
import MagicString from 'magic-string';
import svelte from 'svelte/compiler';
import * as svelte from 'svelte/compiler';
dummdidumm marked this conversation as resolved.
Show resolved Hide resolved
import { parseHtmlx } from '../utils/htmlxparser';
import { handleActionDirective } from './nodes/action-directive';
import { handleAnimateDirective } from './nodes/animation-directive';
import { handleAttribute } from './nodes/attribute';
import { handleAwait } from './nodes/await';
import { handleKey } from './nodes/key';
import { handleBinding } from './nodes/binding';
import { handleClassDirective } from './nodes/class-directive';
import { handleComment } from './nodes/comment';
Expand All @@ -16,6 +15,7 @@ import { handleEach } from './nodes/each';
import { handleElement } from './nodes/element';
import { handleEventHandler } from './nodes/event-handler';
import { handleElse, handleIf } from './nodes/if-else';
import { handleKey } from './nodes/key';
import { handleRawHtml } from './nodes/raw-html';
import { handleSvelteTag } from './nodes/svelte-tag';
import { handleTransitionDirective } from './nodes/transition-directive';
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte2tsx/src/utils/htmlxparser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import compiler from 'svelte/compiler';
import { Node } from 'estree-walker';
import * as compiler from 'svelte/compiler';

function parseAttributeValue(value: string): string {
return /^['"]/.test(value) ? value.slice(1, -1) : value;
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte2tsx/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
"@/*": ["src/*"]
},
// "include" is set in rollup.config(.test).js
"exclude": ["node_modules"]
"exclude": ["node_modules", "repl"]
}