Skip to content

Api for script[src] & style[src] attributes to break up large components & support compiled languages (e.g. Typescript) using vendor tools (no preprocessor necessary) #4026

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

Closed
btakita opened this issue Nov 30, 2019 · 2 comments

Comments

@btakita
Copy link
Contributor

btakita commented Nov 30, 2019

Moved from #3677

This seems to be a discussion in it's own right & I didn't want to distract from the discussion on the svelte-ts thread.


My personal experience. I recently have been working on an Angular project & I have to say that having separate *.ts, *.html, *.scss files is not too bad, especially when components are large.

Perhaps, svelte could offer an api to have the contents of the <script> tag defined with a function that can be defined in a *.ts file. Same with <script context=module>.

This way, one can use tsc -w for the fastest typescript compilation performance while interfacing with Svelte. Such an api could also assist with svelte-ts.

It could look something like:

MyComponent.svelte

<script context=module src="./MyComponent.module"></script>
<script src="./MyComponent"></script>
{#if has_title}
<h1 on:click={onclick}>{title}</h1>
{/if}

MyComponent.ts

export title = ''
let has_title
$: has_title = !!title
function onclick() {
}

MyComponent.module.ts

export async function preload(req, res) {
  // ...
}

The typescript compiler will have a chance to compile MyComponent.ts to MyComponent.js & MyComponent.module.ts to MyComponent.module.js.

The svelte compiler would then copy the contents of MyComponent.js & MyComponent.component.js & place the contents inside the respective <script> tags. The src attribute would keep the semantics of scoping to be within the component as well.

In the case of svelte-ts, the compiler can implicitly create MyComponent.ts from the typescript contents of the <script> tag. This way, the Svelte library has an explicit contract on handling other languages such as typescript, livescript, coffeescript, etc. while using the standard cli tools for these libraries & not having to resort to preprocessors.

This also seems fairly simple to implement from the current svelte implementation.

Love to hear any thoughts...

@btakita btakita changed the title Api for script[src] attribute to break up large components & support compiled languages (e.g. Typescript) using vendor tools (no preprocessor necessary) Api for script[src] attribute to break up large components & support compiled languages (e.g. Typescript) using vendor tools (no preprocessor necessary) Nov 30, 2019
@btakita btakita changed the title Api for script[src] attribute to break up large components & support compiled languages (e.g. Typescript) using vendor tools (no preprocessor necessary) Api for script[src] & style[href] attributes to break up large components & support compiled languages (e.g. Typescript) using vendor tools (no preprocessor necessary) Nov 30, 2019
@btakita btakita changed the title Api for script[src] & style[href] attributes to break up large components & support compiled languages (e.g. Typescript) using vendor tools (no preprocessor necessary) Api for script[src] & style[src] attributes to break up large components & support compiled languages (e.g. Typescript) using vendor tools (no preprocessor necessary) Nov 30, 2019
@Conduitry
Copy link
Member

Is there anything here that can't go in #65?

@btakita
Copy link
Contributor Author

btakita commented Dec 1, 2019

It does look like a preprocessor could handle the src attribute to prepend the contents of that file into the tag. Thank you for pointing out the #65.

@btakita btakita closed this as completed Dec 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants