Last active
May 2, 2023 11:21
-
-
Save torshid/9e2b9b1ea55617785f2c1c1175d04e7f to your computer and use it in GitHub Desktop.
https://larawind.com/obfuscate-tailwind-classes-in-laravel Obfuscate Tailwind classes in a Laravel project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// ... | |
public function boot() | |
{ | |
\Illuminate\Support\Facades\Blade::directive('map', function ($inputs) { | |
return "<?php echo map($inputs); ?>"; | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (!function_exists('map')) { | |
function map($inputs): string | |
{ | |
if (!\Illuminate\Support\Facades\App::environment('production')) { | |
return $inputs; // don't process if we're not in production | |
} | |
$classes = explode(' ', $inputs); | |
$output = ''; | |
foreach ($classes as $class) { | |
$class = trim($class); | |
if ($class == '') { | |
continue; | |
} | |
$output .= config('map.' . $class, $class) . ' '; | |
} | |
return trim($output); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mix = require('laravel-mix') | |
const fs = require('fs') | |
// source: https://stackoverflow.com/questions/1349404/generate-random-string-characters-in-javascript | |
function random(length) { | |
var result = []; | |
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | |
var charactersLength = characters.length; | |
for (var i = 0; i < length; i++) { | |
result.push(characters.charAt(Math.floor(Math.random() * | |
charactersLength))); | |
} | |
return result.join(''); | |
} | |
mix.js("resources/js/app.js", "public/js") | |
.sass('resources/scss/app.scss', 'public/css') | |
.options({ | |
postCss: [ | |
require("tailwindcss")('./tailwind.config.js'), | |
require('postcss-rename')({ | |
strategy: !mix.inProduction() | |
? 'none' | |
: function (input) { | |
// you may also make use of the original class name | |
return random(5); | |
}, | |
outputMapCallback: function (map) { | |
let content = | |
'<?php\n\nreturn [\n\n' + | |
Object.entries(map).map(([k, v]) => { | |
return `\t'${k}' => '${v}'`; | |
}).join(', \n') | |
+ '\n\n];' | |
fs.writeFile('config/map.php', content, err => { | |
if (err) { | |
console.error(err) | |
return | |
} | |
}) | |
} | |
}) | |
], | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mix = require('laravel-mix') | |
const fs = require('fs') | |
mix.js("resources/js/app.js", "public/js") | |
.sass('resources/scss/app.scss', 'public/css') | |
.options({ | |
postCss: [ | |
require("tailwindcss")('./tailwind.config.js'), | |
require('postcss-rename')({ | |
strategy: !mix.inProduction() ? 'none' : 'minimal', | |
outputMapCallback: function (map) { | |
let content = | |
'<?php\n\nreturn [\n\n' + | |
Object.entries(map).map(([k, v]) => { | |
return `\t'${k}' => '${v}'`; | |
}).join(', \n') | |
+ '\n\n];' | |
fs.writeFile('config/map.php', content, err => { | |
if (err) { | |
console.error(err) | |
return | |
} | |
}) | |
} | |
}) | |
], | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment