Svelte (npm run dev) marks all CSS in global scss as unused and purges on macOS - sass

So, I've tested this on two machines. One is a Debian VPS and the other is a local Mac. They're both running the latest softwares. However, running npm dev run on exactly the same project results in very different outcomes – on the Linux VPS, scss from global stylesheets declared with <style lang="scss" global> #use 'path to stylesheet';</style> everything is included nicely and propagates across my app, like I want it to. Do I do exactly the same on the Mac machine however, every css selector is marked as "unused" and is thus purged, resulting in all global css not making it to the app. How is it possible that with all the same files these two very different outcomes come to be, and how could I fix the situation on the Mac machine?
The project was set up with Sveltekit.
edit: to drive this point home: it even marks "html" as an unused css selector.

Related

How can I compile different groups of sass files seperately?

I am doing some wordpress development and I am struggling to setup my development environment is a comfortable way. I am building a parent theme and a child theme. Each theme has its own set of sass files. I want to compile the parent themes files only when I make edits to those parent theme stylesheets. I want to compile the child theme stylesheets only when I make edits to those stylesheets.
I am using live-sass-compiler extension to compile my sass. I am open to using a different method for compiling, maybe something like Gulp.
Right now, I have split my parent and child into two different workspaces. I've added my .vscode/settings.json files into each of the parent and child theme folders since they are both the root folder now.
I don't like this setup because I have to use two vscode windows and also vscode can't see all of the functions Wordpress provides via intellisense.
Do you have any recommendations? Should I setup a gulp task instead to compile my sass?
Like you talked about, Gulp is an excellent idea to control your Sass compilation "flow" from start to finish, even conditionally run one pipeline for file A, and a different one for file B. There are a lot of plugins you can use with PostCSS to future-proof your code, for example.
You can have a look at this Gulp Sass tutorial to get the hang of it, using npm.

How to install node modules but commit only relevant styles

So, I am setting up a new site and my project's folder structure looks like this now.
foo.com/
index.php
assets/
css/
img/
js/
vendor/
I have added vendor/ for js/css libraries that I must install to keep them separate, since I want anyone who installs my project to install those in vendor from package.json - most libraries contain too many files 99% which I don't want to push to production.
Now once the project is finished, I would like to push the code to production with only the necessary js/css files.
This is where the problem comes. For example, if I install bulma css using:
yarn add bulma --modules-folder ./assets/vendor
It will dump all bulma-related files which are almost 70 into /vendor/bulma/ but I will only be needing one or two css files afterwards, since I will compiles the sass file to css as:
sass vendors/bulmna/style.scss assets/css/style.css
So my questions is: I am assuming this is how every developer does it and there are no documentations I can find that suggest how to do it. Is it safe to ignore the /vendor directory? What if I install vue, font-awesome, bootstrap .. how can I only fetch the files I need but not everything in /vendors folder?
Your question is actually quite broad - however, I'll try to list as much as possible.
Lets say you're building a project from scratch and needed to include vuejs, jquery, fontawesome but only need to include a couple of files.
The issue you're hitting here is module dependency with respect to npm modules. (and there are many different tools that you can use to manage versions on your library dependencies as well as ensuring they are included into your project, i.e. package managers).
Ok - now from here, you may say to yourself
but I only need say, one icon from fontawesome in your final build (dist) and I don't want to commit all my modules into source control
Again, this is where you omit node_modules and other dependent libraries from source control (i.e. include node_modules your .gitignore)
To reiterate
You can install the required library,
add node_modules to .gitignore ,
bundle those libraries into a vendor single file to be consumed by your users (can be via browserify/webpack/rollup/gulp/grunt/yarn etc).
generate bundle within npm script
Now you may ask further -
Why would I use any of those tools? - they're distracting me from simply copy/pasting vendor libaries into my source control.
Build tools were created to
streamline the developer pipeline so that you DONT have to copy/paste vendor libaries into a vendor folder.
ensures that all files are bundled to the client automatically
allows you to keep track/restrict library version updates/ when required via package.json
allows you to add other build steps (such as minification, md5hash versioning, compression, code splitting, asset management to name a few).
Now lets break down the original question here:
How to ensure other developers get everything they need when cloning the repository
how do I ensure I can provide only the necessary files to the end user (if I only use specific parts of vendor libaries?)
1. How to ensure developers get what they need
Again, to reiterate above, adding devDependancies and .gitignoring allows you to only add the necessary files to your project.
2. How can I ensure clients get what they need without bloating request files?
This is where build tools such as webpack, browserify, gulp, grunt, rollup, attempt to achieve. This is because with some libraries that exceed in file size of 200kb minified, you may need to separate these files into different client requests (and as such, not demand the user to request one large file, which was symtomatic of browserify projects).
The second technique you will need to be aware of, is with specific libraries, you can use import mdn link where you can require one function/class from a dependant library (which further reduces file size).
Another technique is using less import statements (which can extract less functions/styles similar to above, but this isn't currently supported in SCSS). for SCSS, you're basically left with copy/pasting the necessary styles into your base scss and that'll save you space as well.
EDIT
How to create a bundle from npm install libaries
From the comments you've mentioned above (about not wanting to include a tool into your workflow, there's no point outlining any one particular strategy - you can find answers/tutorials online about how to setup gulp/browserify/webpack for your particular needs).
However, As you are currently using yarn - I'll go into details about that.
Firstly, yarn is a package manager (like npm). All it does with the --modules-folder is install the package into the specified folder, that's all. So we don't really care about that (since it's doing the same thing as npm). (i.e. your vendor folder is the same as node_modules in many respects).
We could use
webpack
gulp
grunt
rollup
browserify
brunch
(All build tools that essentially allow you to bundle all those packages into a single entry point to be packaged to the client).
I won't go into how, because that is a process you can find online, and from the above comments, I can tell you don't particularly care either.
What you are looking for is a zero-config javascript build tool. (Extremely out of the scope of your original question, and i'll only answer that in a separate Q&A).
I'd try Googling on "tree shaking CSS" to see if that gives you something.
Maybe something like: https://github.com/jacobp100/es-css-modules
Rollup plugin may be useful. It works for js, with postcss, the link says it works with css also.
https://code.lengstorf.com/learn-rollup-css
Have a look at Pancake. It has been built specifically for the purpose of moving only those files out of the node_modules folder that you need. I wrote an article about it here: https://medium.com/dailyjs/npm-and-the-front-end-950c79fc22ce
(probably not an answer but a good tip)
PS: I am the author of both, the article and the tool so with clear bias :)

how to load assets in laravel for different environments

I have this
but I would like to eliminate uring the IF statement whenever I want to load such resources.
in the above figure for LOCAL environment I have bootstrap.min.cs and app.css files so that I can debug easily.
in PRODUCTION these two files are minified and combined in one file at core.min.css (using gulp).
is there a better way without using an IF statement but still be able to debug easily using the original files on LOCAL environment?
something like app-local.css (which includes two raw files) and app-production.css (which is one file minified and combined)?
not sure if I'm clear but I hope you get what I mean.
Webpack and Gulp combination is exactly what you are looking for.
It doesnt matter if you are in production or in local you can merge all your CSS and Javascript files as minified into single file. So you don't have to worry about your environment. Also you can use them to watch your changes in real time which is handy for development too.
check this link for more information https://webpack.github.io/

SASS: keep CSS files separate in development and merge them in production

I am using Grunt and SASS. I am currently using the grunt-contrib-compass plugin.
My goal seemed simple, but it is very hard to achieve.
During development I want my CSS to be compiled in separate files (because dev tools, speed of watch etc.)
For production I want all of my CSS to be merged into one.
However using the concat task does not do the job.
When I have #import statements and SASS is compiling every file separately this outputs the same CSS from vendor libs like Bootstrap or Compass plugins over and over again in the different files. In development this is fine. When I concat the CSS files for production though it produces a quite bigger file with repetitive CSS.
How should I configure Grunt and use #import statements so this does not happen?
Not an exact answer but an alternate solution.
For the use of dev tools I would recommend investigating .map files. This will allow you to have a compiled and minified css file but when you open the developer tools you can see the location of styles as they are in the original sass files.
Details on the grunt plugins GitHub page.
A guide on Sass and source maps.
I hope this helps.

Automatically compiling persistent css without compass watch

I have a compass sinatra project based off of this example.
As noted in this commit in a pull request and this pull request comment, sinatra is able to auto generate compiled css files on demand, as per request.
However, this seems like a waste since the server has to recompile even if there was no changes to the css file. Wouldn't it be better if the server watched for changes, recompiled on file changes, but would serve the same static css file on demand? This would work as if we ran "compass watch" and just served the compiled file.
My question then is: is there a good way to have sinatra automatically compile my sass files into a static CSS on change, without having to run watch compass separately?
Note: I also have a express/node/stylus project and it behaves like this, which leads me to believe this is possible and the logical way, and that I am just mis-configuring my sinatra app.
To change the CSS you have to either:
push new files to the application server (with a possible restart)
recompile in the background (e.g. compass watch on the server)
get the server to compile on changes (using something like the example you gave)
Personally, I favour the first. I'm not sure why I'd want the server to compile static assets? It takes up valuable resources, and the CSS changes on my dev machine, so why not compile them on my dev machine? I've not heard good answers to these questions, so I use a Guardfile (or you could use sass watch or compass watch as a background task e.g. sass --watch app/views/stylesheets:app/public/css &) to compile them, and then I check in the .css files and push them to the server.
YMMV.

Resources