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

Allow separated input of HTML, CSS and JS in addition of Single File Components #65

Closed
cj opened this issue Nov 30, 2016 · 17 comments
Closed

Comments

@cj
Copy link

cj commented Nov 30, 2016

I have briefly looked over all the docs, I really like the direction svelte has taken. Quick question, can the compiler handle <script src="hello.js"> so that you can split the javascript into a separate file and not have everything in hello.html?

@proyb6
Copy link

proyb6 commented Nov 30, 2016

If JS is serve in the production, a single file is easier and efficient. Do you mean

page1.html to page1.js - link to index.html
page2.html to page2.js - link to aboutus.html
page3.html to page3.js - link to contactus.html

Would be possible to compile in a different folders or filename which I'll be achieving my blogs with this method.

@Rich-Harris
Copy link
Member

The compiler needs to be able to 'see' the JS and the markup at the same time so that it can do various bits of analysis. So for now, that step would have to happen in a preprocesser that read the <script src='hello.js'>, resolved hello.js, loaded it, and injected it into the template.

It's possible we might want to add that in core at some point – I'll flag this issue with the 'enhancement' label.

@Rich-Harris Rich-Harris added this to the one day milestone Nov 30, 2016
@cj
Copy link
Author

cj commented Nov 30, 2016

@Rich-Harris The same would go for the <style> tag. I guess the best example of this would be vue-loader https://vue-loader.vuejs.org/en/start/spec.html#src-imports. Having the ability to choose the pre-processing language would be a good idea too.

@Rich-Harris Rich-Harris changed the title script src? Allow separated input of HTML, CSS and JS in addition of Single File Components Dec 2, 2016
@rhengles
Copy link

rhengles commented Dec 2, 2016

I want to point out that there are two different ways this can be done, and maybe supported.

One is the original proposed by @cj - using tags with the paths to the external files.

The other is passing directly each file to the compiler, without need of tags. As an example, in my (Vue) project I have a convention of creating three files like this: <component-name>.(html|js|css). So I would load them directly into the compiler, without need of repeating the js and css file paths in all components.

@Conduitry
Copy link
Member

I'm kind of a fan of having the core compiler providing only a method that takes separate HTML, JS, and CSS - and to leave things like looking for script and style and link tags (and accessing external files) to external tools. This is an even lower-level API than the compiler provides now, so this would obviously be a breaking change.

I've used tools before where the lowest-level API that's exposed still makes assumptions about your setup (e.g., that things are coming from physical files on the disk), and it's not fun. I definitely want to avoid that here. I don't want svelte.compile to start doing disk access.

@arxpoetica
Copy link
Member

What about importing just js files in the case where a component is literally just js?

import AudioService from `./services/AudioService.js`

I've already broken out my precompile processes via my own npm script methodology, but occasionally I find myself with only a js file, and some boilerplate HTML file just to load the js script.

https://svelte.technology/repl?version=1.38.0&gist=3f3260fa442911b8f78d13d0a0f12eeb

Catch my drift?

@EsmeraldiBejolli
Copy link

EsmeraldiBejolli commented Mar 17, 2018

@Rich-Harris I'm using path to script tag that point to external file but data are still unresolved and I get undefined instead of resolving placeholder on svelte template .

`

Have you tried Svelte? It's {{adjective}}.

<script type="text/javascript" src="./Test1.js"> </script>`

Thank you in advance

@kaisermann
Copy link
Member

Allowing to define a external src file would help a lot while we don't have IDE extensions for svelte:

image

vs

image

@kaisermann
Copy link
Member

kaisermann commented May 7, 2018

Oh, you can, at least for styles, do this with the preprocess property. Quick example:

image

Edit:

I've made a little lib to help with this

@ghost
Copy link

ghost commented Nov 13, 2019

this works with JS

<script>
  import test from './test.js';
  let name = `${test} 'world'`;
</script>

<h1>Hello {name}!</h1>

@kaisermann
Copy link
Member

kaisermann commented Nov 13, 2019

I think this is safe to close this issue since separating a component style and script is completely covered by preprocessors, which usually implement this behavior. @pngwn @Conduitry

@antony
Copy link
Member

antony commented Apr 9, 2020

@pngwn I think we should close this. Is there a reason you wanted to keep it open?

@pngwn
Copy link
Member

pngwn commented Apr 9, 2020

I don't fully remember, I think I closed it and someone told/asked me to keep it open, which is why it got reopened quickly after closing.

@antony
Copy link
Member

antony commented Apr 9, 2020 via email

@pngwn
Copy link
Member

pngwn commented Apr 9, 2020

I'm not sure Rich is actually in favour of this any more, I feel like I remember this conversation because this has been discussed quite a lot over the past year or so but you'll have to ask him to be certain.

@antony
Copy link
Member

antony commented Apr 9, 2020 via email

@antony antony added the wontfix label Apr 11, 2020
@antony
Copy link
Member

antony commented Apr 11, 2020

This can be done via https://github.com/kaisermann/svelte-preprocess - and as such, isn't planned for core.

@antony antony closed this as completed Apr 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants