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 3 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
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
10 changes: 6 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,13 +29,10 @@
"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",
"sucrase": "^3.17.0",
Copy link
Member

@dummdidumm dummdidumm Jan 9, 2021

Choose a reason for hiding this comment

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

Could we use ts-node instead? We already have that in this repo and it might serve the same use case and we avoid another dependency.

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.

I agree and I tried many things, but ts-node refuses to work due to a side issue in tsconfig
Sucrase just works, it's also much faster afaik

"svelte": "3.28.0",
"tiny-glob": "^0.2.6",
"tslib": "^1.10.0",
Expand All @@ -45,6 +46,7 @@
"build": "rollup -c",
"prepublishOnly": "npm run build",
"dev": "rollup -c -w",
"debug": "node -r sucrase/register/ts repl/debug.ts",
"test": "mocha --opts mocha.opts",
"pretest": "rollup -c rollup.config.test.js"
},
Expand Down
14 changes: 14 additions & 0 deletions packages/svelte2tsx/repl/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import fs from 'fs';
import svelte2tsx from '../src';
const content = fs.readFileSync(`${__dirname}/index.svelte`, 'utf-8');
svelte2tsx(content);

/**
* Run the "dev" package script to get started
*
* Inputs at "repl/index.svelte" use the modified source code
* and are compiled to "repl/output/" on save.
*
* Alternatively, attach a debugger to the "debug" package script
* and place breakpoints directly in the ts source files.
*/
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
8 changes: 4 additions & 4 deletions packages/svelte2tsx/src/svelte2tsx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import { EventHandler } from './nodes/event-handler';
import { ExportedNames } from './nodes/ExportedNames';
import { createClassGetters, createRenderFunctionGetterStr } from './nodes/exportgetters';
import {
handleScopeAndResolveForSlot,
handleScopeAndResolveLetVarForSlot
handleScopeAndResolveForSlot,
handleScopeAndResolveLetVarForSlot
} from './nodes/handleScopeAndResolveForSlot';
import { Scripts } from './nodes/Scripts';
import { SlotHandler } from './nodes/slot';
import { Stores } from './nodes/Stores';
import TemplateScope from './nodes/TemplateScope';
import {
InstanceScriptProcessResult,
processInstanceScriptContent
InstanceScriptProcessResult,
processInstanceScriptContent
} from './processInstanceScriptContent';
import { processModuleScriptTag } from './processModuleScriptTag';
import { ScopeStack } from './utils/Scope';
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"]
}
69 changes: 68 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,11 @@ ansi-styles@^4.1.0:
"@types/color-name" "^1.1.1"
color-convert "^2.0.1"

any-promise@^1.0.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/any-promise/-/any-promise-1.3.0.tgz#abc6afeedcea52e809cdc0376aed3ce39635d17f"
integrity sha1-q8av7tzqUugJzcA3au0845Y10X8=

anymatch@~3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/anymatch/-/anymatch-3.1.1.tgz#c55ecf02185e2469259399310c173ce31233b142"
Expand Down Expand Up @@ -678,6 +683,11 @@ colorette@^1.1.0:
resolved "https://registry.yarnpkg.com/colorette/-/colorette-1.2.1.tgz#4d0b921325c14faf92633086a536db6e89564b1b"
integrity sha512-puCDz0CzydiSYOrnXpz/PKd69zRrribezjtE9yd4zvytoRc8+RY/KJPvtPFKZS3E3wP6neGyMe0vOTlHO5L3Pw==

commander@^4.0.0:
version "4.1.1"
resolved "https://registry.yarnpkg.com/commander/-/commander-4.1.1.tgz#9fd602bd936294e9e9ef46a3f4d6964044b18068"
integrity sha512-NOKm8xhkzAjzFx8B2v5OAHT+u5pRQc2UCa2Vq9jYL/31o2wi9mxBA7LIFs3sV5VSC49z6pEhfbMULvShKj26WA==

commondir@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/commondir/-/commondir-1.0.1.tgz#ddd800da0c66127393cca5950ea968a3aaf1253b"
Expand Down Expand Up @@ -1270,7 +1280,7 @@ glob@7.1.3:
once "^1.3.0"
path-is-absolute "^1.0.0"

glob@^7.1.3, glob@^7.1.6:
glob@7.1.6, glob@^7.1.3, glob@^7.1.6:
version "7.1.6"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.6.tgz#141f33b81a7c2492e125594307480c46679278a6"
integrity sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==
Expand Down Expand Up @@ -1837,6 +1847,15 @@ ms@^2.1.1:
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==

mz@^2.7.0:
version "2.7.0"
resolved "https://registry.yarnpkg.com/mz/-/mz-2.7.0.tgz#95008057a56cafadc2bc63dde7f9ff6955948e32"
integrity sha512-z81GNO7nnYMEhrGh9LeymoE4+Yr0Wn5McHIZMK5cfQCl+NDX08sCZgUc9/6MHni9IWuFLm1Z3HTCXu2z9fN62Q==
dependencies:
any-promise "^1.0.0"
object-assign "^4.0.1"
thenify-all "^1.0.0"

natural-compare@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
Expand Down Expand Up @@ -1877,6 +1896,11 @@ node-environment-flags@1.0.6:
object.getownpropertydescriptors "^2.0.3"
semver "^5.7.0"

node-modules-regexp@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/node-modules-regexp/-/node-modules-regexp-1.0.0.tgz#8d9dbe28964a4ac5712e9131642107c71e90ec40"
integrity sha1-jZ2+KJZKSsVxLpExZCEHxx6Q7EA=

normalize-package-data@^2.3.2:
version "2.5.0"
resolved "https://registry.yarnpkg.com/normalize-package-data/-/normalize-package-data-2.5.0.tgz#e66db1838b200c1dfc233225d12cb36520e234a8"
Expand All @@ -1892,6 +1916,11 @@ normalize-path@^3.0.0, normalize-path@~3.0.0:
resolved "https://registry.yarnpkg.com/normalize-path/-/normalize-path-3.0.0.tgz#0dcd69ff23a1c9b11fd0978316644a0388216a65"
integrity sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==

object-assign@^4.0.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
integrity sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=

object-inspect@^1.7.0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/object-inspect/-/object-inspect-1.7.0.tgz#f4f6bd181ad77f006b5ece60bd0b6f398ff74a67"
Expand Down Expand Up @@ -2108,6 +2137,13 @@ pify@^2.0.0:
resolved "https://registry.yarnpkg.com/pify/-/pify-2.3.0.tgz#ed141a6ac043a849ea588498e7dca8b15330e90c"
integrity sha1-7RQaasBDqEnqWISY59yosVMw6Qw=

pirates@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/pirates/-/pirates-4.0.1.tgz#643a92caf894566f91b2b986d2c66950a8e2fb87"
integrity sha512-WuNqLTbMI3tmfef2TKxlQmAiLHKtFhlsCZnPIpuv2Ow0RDVO8lfy1Opf4NUzlMXLjPl+Men7AuVdX6TA+s+uGA==
dependencies:
node-modules-regexp "^1.0.0"

pkg-dir@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/pkg-dir/-/pkg-dir-2.0.0.tgz#f6d5d1109e19d63edf428e0bd57e12777615334b"
Expand Down Expand Up @@ -2487,6 +2523,18 @@ strip-json-comments@^3.1.0:
resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-3.1.1.tgz#31f1281b3832630434831c310c01cccda8cbe006"
integrity sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig==

sucrase@^3.17.0:
version "3.17.0"
resolved "https://registry.yarnpkg.com/sucrase/-/sucrase-3.17.0.tgz#d9fe5d7e359d884cdb31130358fbdfc18bfb4c24"
integrity sha512-wtiqaokYRjFSSrv8fQu7pThKTIZSLwiffW+PHQG52hlI8eJO47v1tXbKt6fYb8Z1kCyuCkNH9etpTUebb7g+pA==
dependencies:
commander "^4.0.0"
glob "7.1.6"
lines-and-columns "^1.1.6"
mz "^2.7.0"
pirates "^4.0.1"
ts-interface-checker "^0.1.9"

supports-color@6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-6.0.0.tgz#76cfe742cf1f41bb9b1c29ad03068c05b4c0e40a"
Expand Down Expand Up @@ -2553,6 +2601,20 @@ text-table@^0.2.0:
resolved "https://registry.yarnpkg.com/text-table/-/text-table-0.2.0.tgz#7f5ee823ae805207c00af2df4a84ec3fcfa570b4"
integrity sha1-f17oI66AUgfACvLfSoTsP8+lcLQ=

thenify-all@^1.0.0:
version "1.6.0"
resolved "https://registry.yarnpkg.com/thenify-all/-/thenify-all-1.6.0.tgz#1a1918d402d8fc3f98fbf234db0bcc8cc10e9726"
integrity sha1-GhkY1ALY/D+Y+/I02wvMjMEOlyY=
dependencies:
thenify ">= 3.1.0 < 4"

"thenify@>= 3.1.0 < 4":
version "3.3.1"
resolved "https://registry.yarnpkg.com/thenify/-/thenify-3.3.1.tgz#8932e686a4066038a016dd9e2ca46add9838a95f"
integrity sha512-RVZSIV5IG10Hk3enotrhvz0T9em6cyHBLkH/YAZuKqd8hRkKhSfCGIcP2KUY0EPxndzANBmNllzWPwak+bheSw==
dependencies:
any-promise "^1.0.0"

tiny-glob@^0.2.6:
version "0.2.6"
resolved "https://registry.yarnpkg.com/tiny-glob/-/tiny-glob-0.2.6.tgz#9e056e169d9788fe8a734dfa1ff02e9b92ed7eda"
Expand All @@ -2568,6 +2630,11 @@ to-regex-range@^5.0.1:
dependencies:
is-number "^7.0.0"

ts-interface-checker@^0.1.9:
version "0.1.13"
resolved "https://registry.yarnpkg.com/ts-interface-checker/-/ts-interface-checker-0.1.13.tgz#784fd3d679722bc103b1b4b8030bcddb5db2a699"
integrity sha512-Y/arvbn+rrz3JCKl9C4kVNfTfSm2/mEp5FSz5EsZSANGPSlQrpRI5M4PKF+mJnE52jOO90PnPSc3Ur3bTQw0gA==

ts-node@^8.6.2:
version "8.6.2"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-8.6.2.tgz#7419a01391a818fbafa6f826a33c1a13e9464e35"
Expand Down