-
Notifications
You must be signed in to change notification settings - Fork 74
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
added script to upload vanilla .svelte components. #31
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this! I added some comments in the code. Also, you need to remove your package-lock.json
from the PR.
On a broader note: I thought about going the route of a script, too, but the problem is that other files might need preprocessing, too. If you use TypeScript inside your Svelte files, you will likely also write .ts
files, not .js
, and those need preprocessing, too. What to do about this? I'm not sure. Several options come to my mind:
- Assume that the only file type that needs preprocessing outside of Svelte files are TS files (likely the 80% use case) and do one of these three:
- add code specific to transpiling ts code. But this would mean adding a TS dependency which will bloat the template even for everyone who is not using it.
- add docs on how to tackle this for TS with some code snippets to get going fast.
- add a script which will add the TS dependency and other changes required to get going with TS (similar to the script in the template-repo) (my favorite)
- Add an extension point to the script, maybe some kind of hook users can fill if they need to preprocess other files. But this will likely not work for TS because TS needs the whole picture, not transpilation on a file-per-file basis.
// copy other files | ||
else { | ||
// copy static files | ||
fs.copyFileSync(file, distFile); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just copying other files will not work if they need preprocessing, too, like TS files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The svelte pre-processor can handle the .svelte files, i was thinking of using the typescript plugin for rollup to compile the .ts files after the preprocessing. I can just get rid of the copy file logic to prevent the .ts files from going into the output folder.
as an alternative, i could change the postbuild to a prebuild script so that the preprocessor can spit our compiled .svelte files and then have rollup bundle/minify the remaining .svelte and .ts files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note typescript (and maybe other) compiler can simply be called directly on the source with the output directory specified.
tsc --declaration --outDir dist
It will then compile the files over to the out directory. Done this way, I would say copying the files over is unnecessary We still need to copy though so non-preprocessed files can be imported, like plain js. Perhaps we should only preprocess svelte files in this script, and let any non-svelte files be handled separately afterwards (possibly by their native compilers).
Failing to see this, I wrote one as well. As far as non-svelte files are concerned. As much as I would like better integration, I believe typescript, postcss, and etc. package.json...
"scripts": {
"preprocess": "npm run preprocess:svelte; npm run preprocess:typescript",
"preprocess:svelte": "node script.js",
"preprocess:typescript": "tsc --declaration --outDir wherever/preprocessed/files/go"
}
... and add calls to all preprocessors in package.json via (possibly utilizing npm-run-all). As for improvements, the only major thing I can think of is using an arrays instead of just svelte-preprocess.config.jsconst sveltePreprocess = require("svelte-preprocess")
const {mdsvex} = require("mdsvex")
const generatePreprocess = ({sourceMap}={})=>[
sveltePreprocess({sourceMap: sourceMap ?? false}),
mdsvex(),
]
module.exports={
generatePreprocess: generatePreprocess,
preprocess: generatePreprocess(),
extensions: [".svelte",".svx"],
outDir: "./preprocessed",
srcDir: "./src",
} You can then use them in rollup, webpack, and the script. For the script, I would do script.js (or a more descriptive filename) const {extensions,preprocessors} = require('./svelte-preprocess.config.js')
...
// process .svelte file
// Use endsWith instead of includes so we don't process .svelte.disabled, .svelte.css and other improper extensions.
if (extensions.some(ext=>file.endsWith(ext))) {
// run autopreprocessor
await parseSvelte(sourceFile, distFile);
}
...
svelte.preprocess(source, preprocessors, {
filename: path.basename(destination),
}) In my version, I also DependenciesIMHO, There is no need real need for component-template to ship with svelte-preprocess, typescript, or any other preprocessors added to devDependencies. Actually, might be better if this script lived in its own package and we just had Also include some (ideally short) directions on how to enable compiling a few other common languages. I gave an example for typescript using tsc. If that is method is acceptable, just a short explanation of what it does should suffice. |
I also did something similar for my library. with the extra step of removing the |
That's a good catch. If the preprocessed output still contains the |
Would this also happen if we enable defaults in svelte-preprocess? I prefer to specify lang rather than using defaults as well, but I am curious if the preprocessor gets triggered when a user set their defaults to pug, ts, and sass and then installs pure svelte components. |
I guess it will. Maybe the default settings hurt more overall than they help.. |
I wouldn't say they are the issue. When I enable a preprocessor, I expect it to run on my code that I wrote in my current project. Touching code anywhere else is a taboo which I should need to explicitly enable. The issue is, atm, it seems that taboo is the default behavior, non-optional, and for some people it is even something that got forced down their throats because they wanted to install some component that used a preprocessor. While this script is certainly a necessity that will finally stop adding converts to our cult and give the devs a chance their throats with mouthwash. Perhaps we still need a bit more to change with the ecosystem before it will become possible to achieve a full and proper solution to our problem. This conversation leaves me wondering
For the first question, I know Now, does re-running any of the preprocessors seem likely to cause issues in practice (other than wasted cycles)? I would imagine so, though it depends on what preprocessor and what plugins are enabled by the end dev. For instance:
Typescript might be fine, since (iirc) type checking isn't done by svelte-preprocess, and js is valid ts under normal circumstances. Both of those cases should be possible to test now using either this script, or by enabling defaults in svelte-preprocess and installing any pure-svelte component. I have not tried either of them yet, so let me know if you do. TL;DR |
cc @kaisermann who is the author of The fact that |
Sounds good to me. |
Those |
|
Agreed, include is just a convenient way to exclude everything else in our case. Totally unnecessary, could even be more confusing to have it since improper usage would easily conflict with svelte's |
I added a script to preprocess svelte files with different Template,Script and Style languages and spit out vanilla .svelte files. This is to serve 2 use cases for a component library.
This may help Sapper projects more than anything else, bare bones svelte projects shouldn't have a problem loading modules.