Special characters and PurgeCSS not working as expected - sass

Using PurgeCSS along with Laravel Mix on a non Laravel project.
Everything works well but I want to use some special non CSS characters in my responsive utility classes but PurgeCSS is stripping them even if present in the markup.
My example class would be:
<div class="u<sm:hidden"></div>
In the css this is written as:
.u\<sm\:hidden {}
PurgeCSS has no issue with the colon symbol as the following class acts fine:
.u-sm\:hidden {}
Any way I can make this work as expected?
Thanks

So I was facing the same issue and I did some digging and found the solution here
https://lancecore.com/building-our-sites/.
The way I solved it was to use a custom extractor for Purgecss and in that extractor write a regex expression that correctly matches the way responsive classes are designed in tailwindcss.
Now, here is my postcss.config.js file where all this magic happened:
const purgecss = require('#fullhuman/postcss-purgecss')
class TailwindExtractor {
static extract(content) {
return content.match(/[A-z0-9-:\/]+/g)
}
}
module.exports = {
plugins: [
require('tailwindcss')('./tailwind.js'),
require('autoprefixer'),
purgecss({
content: [
'./pages/**/*.vue',
'./layouts/**/*.vue',
'./components/**/*.vue'
],
extractors: [{
extractor: TailwindExtractor,
extensions: ['vue']
}
],
whitelist: ['html', 'body']
})
]
}

Related

Configure SASS Load Path in Nuxt 3

I have my SCSS partials in my Nuxt 3 project's assets/css directory (e.g. assets/css/_cards.scss). I can import them in my components using the full path (#use '~/assets/css/cards';), but I'm having trouble getting the load path working so that I can import like #use 'cards';
From what I've seen, the Nuxt config should look like this to enable that, but this and similar variations are not working for me.
export default defineNuxtConfig({
vite: {
css: {
preprocessorOptions: {
scss: {
loadPaths: ['#/assets/css'],
},
},
},
},
});
This approach is not working for me either. However, my use case is that I wanted some global styles imported, as opposed to every component on its own.
What worked for me was to use css property directly inside defineNuxtConfig object.
export default defineNuxtConfig({
css: ["#/assets/css/_variables.scss"]
});
The correct key to use is includePaths which is documented here. I tried this key before, but the reason it did not work was that I used #/assets/css for the path. The # alias does not work in this option, so I needed to use ./assets/css for the path. Here is the corrected config:
export default defineNuxtConfig({
vite: {
css: {
preprocessorOptions: {
scss: {
includePaths: ['./assets/css'],
},
},
},
},
});

Laravel mix - class from variable isnt builded

my class from variable is not working, im sure its mix problem.
<div class="rounded bg-gradient-to-r {{$replay->playerOneTeam()}}">
Team: {{ $replay->playerOneTeam()}}
</div>
It seems like purgeCSS or something is removing class "from-red-400". When I set it manually and do npm run dev it works, but when it's used from variable, it's not loading. The class is in code when I inspect but the code runs like it doesn't exist.
Check your tailwind.config.js file and look for something like this:
module.exports = {
mode: 'jit',
purge: [
'./vendor/laravel/framework/src/Illuminate/Pagination/resources/views/*.blade.php',
'./vendor/laravel/jetstream/**/*.blade.php',
'./storage/framework/views/*.php',
'./resources/views/**/*.blade.php',
'./resources/js/**/*.vue',
],
// ...
}
In this example, JIT mode is enabled and purge is where you can specify files where to look for used Tailwind CSS classes. From here, you have three options:
[Not recommended] Disable JIT mode.
Follow the guidelines here to make sure the compiler sees your class name in those files you specified, i.e. to write purgeable HTML code in those files.
Divide purge into content and safelist so the compiler doesn't accidentally remove your classes specified there (which will be classes that do not appear explicitly in purgeable HTML code).
Using the example above, the third option would look something like this:
module.exports = {
mode: 'jit',
purge: {
content: [
'./vendor/laravel/framework/src/Illuminate/Pagination/resources/views/*.blade.php',
'./vendor/laravel/jetstream/**/*.blade.php',
'./storage/framework/views/*.php',
'./resources/views/**/*.blade.php',
'./resources/js/**/*.vue',
],
safelist: [
'from-red-400', // this is the class you wanted
// ... some other classes you may need
'bg-blue-500', // example class
'text-center', // example class
'hover:opacity-100', // example class
]
},
// ...
}
in 2023, if someone has the same problem, this was the solution that I found working:
Add this in tailwind.config.js
safelist: [
'bg-blue-100',
{
pattern: /bg-(gray|red|orange|amber|yellow|green|lime|blue|sky|teal|indigo|violet|purple|pink)-(50|100|200|300|400)/,
},
{
pattern: /text-(gray|red|orange|amber|yellow|green|lime|blue|sky|teal|indigo|violet|purple|pink)-(500|600|700|800)/,
},
],

Sapper - imported scss styles disappear

I have a strange problem which may be related toe tree-shaking. I build a sapper app and in my rollup config, I use the svelte-preprocess package and configure it like this:
const preprocessOptions = {
scss: {
data: `#import '${join(process.cwd(), "src/styles/main.scss")}';`,
includePaths: ["node_modules", "src"],
},
postcss: {
plugins: [
require("autoprefixer"),
require("cssnano")({
preset: "default",
}),
],
minimize: !isDevelopment,
sourceMap: isDevelopment,
},
};
Via the scss plugin I copy the global stylesheet main.scss. All of the styles out of it seem to get applied, except for the styles of the body. Somehow these get ignored, because when I check the body styles in the dev console, it is empty. Can't you access the body in Sapper or is there maybe a special way to do this?
The problem is that with Sapper the body is part of the template.html file. That means there's no component with a body element. Svelte will remove styles that are not used in your component unless they are explicitly marked as global. The global preprocessor can help with this. I think in your case I would just get the bundler to create a separate css file for the template.html and include it explicitly like the global.css:
...
%sapper.base%
<link rel='stylesheet' href='global.css'>
...

ckeditor 5 Disabling content filtering

I've notice when extracting the data from the editor it filters some classes and styles.
I want to use the exact same styling as the editor uses.
So, i have 2 problems i need to solve.
How can i prevent the filtering of classes and styles from happening.
How can i extract the CSS to a separate file?
I know when using previous ckeditor versions you could have used the following to prevent it filtering:
config.allowedContent = true;
You can use the General HTML support plugin in CKEditor 5. More info in the docs
This is what I'm using to enable some features as per my needs. You can customize to your implementation.
ClassicEditor.create(richEditorElem, {
htmlSupport: {
allow: [
{
name: /^(div|ul|li|ol|a|button|p|h[1-6])$/,
classes: true,
styles: true
}
]
}
}).then( editor => {
}).catch( error => {
console.error( error );
});

how to create custom validators in Angular 2 ES5?

I'm trying to create a custom validator for Angular 2 using ES5. I can't find a sample online, and the cheatsheet of the documentation doesn't help and it's not documented for ES5 at all. Also, I can't transpile completely from given examples of TypeScript to ES5. Let's say the validator is a simple code validator that more than a simple Regular Expression match, should check for CRC too. Thus it can't be accomplished with built-in pattern validator of Angular 2.
Here's my code that I've tried:
var securityCode = ng.core.Directive({
selector: '[securityCode][ngModel]',
providers: [
{
provide: ng.forms.NG_VALIDATORS,
useExisting: securityCode,
multi: true
}
]
}).Class({
constructor: function() {
console.log('securityCode');
},
validate: function(value){
console.log('validate...');
},
validator: function(value){
console.log('validator...');
}
});
and here's how I want to use it:
<input mdInput type="text" requried securityCode />
I'm stuck now, I can't go further as this doesn't work because I get Cannot read property 'validate' of undefined exception, and I have no idea what to do next.

Resources