-
-
Notifications
You must be signed in to change notification settings - Fork 4
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
Use with Vite + React + Tailwind ? #2
Comments
Hi, Thanks for Asking. I got it running with React + Vite + TailwindCSS :Fork this Replit:https://replit.com/@n4j1Br4ch1D/postcss-obfuscator-react-vite-tailwindcss How it's done:1- install postcss-obfuscator: # npm
npm install postcss-obfuscator --save-dev
# yarn
yarn add postcss-obfuscator --dev 2- install tailwindcss and configure it: # Using NPM
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p 3- create @tailwind base;
@tailwind components;
@tailwind utilities; 4- create //postcss.config.cjs
process.env.NODE_ENV = "development" //development //obfuscation //production
const isObfscMode = process.env.NODE_ENV === 'obfuscation';
module.exports = {
plugins: [
require('tailwindcss'),
require('autoprefixer'),
require('postcss-obfuscator')({
/* options */
enable: isObfscMode, // Force Run on Dev Env or when srcPath equals desPath.
srcPath: "src", // Source of your files.
desPath: "out", // Destination for obfuscated html/js/.. files.
extensions: ['.jsx'],
formatJson: true, // Format obfuscation data JSON file.
})
]
} 5- add this npm script's to package.json "tailwindcss": "npx tailwindcss -i ./src/input.css -o ./src/output.css",
"postcss": "postcss src/**/*.css --dir out/css",
"obfuscate": "npm run tailwindcss && npm run postcss" 6- Install postcss-cli: npm i -D postcss postcss-cli 7- now to obfuscate, set the env mode to npm run obfuscate which will generate obfuscated JSON file, with obfuscated files in an output folder. Note: postcss is automatically run with Vite so its preferred to create a customer postcss script to run only when you want to obfuscate ready for production see the readme for more details. and feel free to contact me again.
process.env.NODE_ENV = "development" //development //obfuscation //production
const isObfscMode = process.env.NODE_ENV === 'obfuscation';
//enable: isObfscMode,
so basically you use |
I think thats a different issue for me it worked fine so plz try this: <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React</title>
<script type="module" crossorigin src="assets/index-6e17a1d2.js"></script>
<link rel="stylesheet" href="assets/index-fffba02c.css">
</head>
<body>
<div id="root"></div>
</body>
</html> Also if u have any classes that uses reserved names change them: import React from 'react' that will be turned in to import React from 'j6yay' which will result in an error:
@sort72 plz if the issue was resolved then close it. else contact me back I don't close issues until they are truly solved and the issuers close them themselves. and consider starting the project to keep it going |
Hi, thanks for your response I did the steps you mentioned and I'm almost there, but I have a problem I can't understand why is happening, it looks like a bug I run first yarn obfuscate Then yarn build And it's showing some warnings, because I was using classes with arbitrary values before (Like h-[0.5rem]) and the parser was not able to obfuscate them properly, showing those warnings. Then I removed the arbitrary values and replaced them for built-in tailwind values, then obfuscated it again and all that. But, when I build it one more time, it still shows warnings (Even when I'm not using the arbitrary values anymore). If I search any of the warning classes, none of them exist! See the photo: So I ignored warning and renamed out to src and built it, but it was complainig about a syntax error in the CSS. I fixed it and built again, with a successful built, then I opened my project but none of the styles were there. The css classes in the html tags were named different than in the CSS file (For example, in the html class, the name was 'abcd' and in the css file, that class had the 'xyz' name). Maybe this last issue is related with warnings thing, I removed css-obfuscator, dist and out folders before building again (I also removed node_modules, and restarted the pc, lol), and the warnings were still there. Any hint? Thank you! |
You welcome ok let me check m but 1st I been updating my answers: also i would recommend checking this https://stackoverflow.com/questions/66863200/changing-the-input-and-output-directory-in-vite |
Actually, I tried the thing in vite.config.js like 1 hour ago, but I was not able to get it working. I added export default defineConfig({
plugins: [svgr(), react()],
root: "src",
}); But it said it was not able to find the file |
I opened an issue for |
@sort72 hi check the replit demo I made this changes:
npm install --save-dev cross-env
"tailwindPostcss": "npm run tailwindcss && npm run postcss",
"obfuscate": "cross-env NODE_ENV=obfuscation npm run tailwindPostcss"
//postcss.config.cjs
const isObfscMode = process.env.NODE_ENV === 'obfuscation';
module.exports = {
dir: 'dist/css',
plugins: [
require('tailwindcss'),
require('autoprefixer'),
require('postcss-obfuscator')({
/* options */
enable: isObfscMode, // Force Run on Dev Env or when srcPath equals desPath.
srcPath: "src", // Source of your files.
desPath: "out", // Destination for obfuscated html/js/.. files.
extensions: ['.jsx'],
formatJson: true, // Format obfuscation data JSON file.
callBack: function () { // Callback function to call after obfuscation is done.
process.env.NODE_ENV = "production" // make sure postcss-obfuscator doesnt run
}
})
]
}
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
if (command === 'build' && mode === 'production') { // build and production specific config
return {
base: '',
root: 'out',
server: {
host: '0.0.0.0',
},
plugins: [react()],
}
} else {
return {
server: {
host: '0.0.0.0',
},
plugins: [react()],
}
}
}) |
I made your suggested changes and I first ran yarn obfucaste Then yarn build But I'm facing the following error: yarn run v1.22.5
$ vite build
vite v4.2.0 building for production...
✓ 0 modules transformed.
✓ built in 24ms
Could not resolve entry module "out/index.html".
error during build:
RollupError: Could not resolve entry module "out/index.html".
at error (file:///C:/dev/book/node_modules/rollup/dist/es/shared/node-entry.js:2125:30)
at ModuleLoader.loadEntryModule (file:///C:/dev/book/booking/node_modules/rollup/dist/es/shared/node-entry.js:23798:20)
at async Promise.all (index 0)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. I saw you changed I think that this is the last thing we need to fix to get it working ya I think so a problem with vite(Rollup): plz notice recent change in my previous answer: since cross-env doesn't support && "tailwindPostcss": "npm run tailwindcss && npm run postcss",
"obfuscate": "cross-env NODE_ENV=obfuscation npm run tailwindPostcss" |
@sort72 have you tried this? "build": "vite build out --emptyOutDir", |
I discovered that we don't need to set a From <script type="module" src="/src/index.jsx"></script> To <script type="module" src="/out/index.jsx"></script> How could we handle it with environment vars? |
So I added 2 .env files: .env.production and .env.dev . And I'm able to switch what folder I want to use on a build (src or out)
VITE_OBFUSCATE=true
VITE_SOURCE_PATH='out'
VITE_OBFUSCATE=false
VITE_SOURCE_PATH='src' And in <script type="module" src="/%VITE_SOURCE_PATH%/main.jsx"></script> So on production, I need to run
And it creates everything with However, I'm still facing an issue: The css is not shown properly because the classes in html are obfuscated, but the index.css generated file on /dist contains non obfuscated classes (Html: asdhjba, CSS: border-2, for example) |
@sort72 can i see your postcss config and can u validate if 1st |
Here is my postcss config module.exports = {
plugins: [
require("tailwindcss"),
require("autoprefixer"),
require("postcss-obfuscator")({
/* options */
enable: true, // Force Run on Dev Env or when srcPath equals desPath.
fresh: true,
srcPath: "src", // Source of your files.
desPath: "out", // Destination for obfuscated html/js/.. files.
extensions: [".jsx", ".js"],
classPrefix: "msy-", // Prefix for obfuscated CSS class names.
formatJson: true, // Format obfuscation data JSON file.
callBack: function () {
process.env.NODE_ENV = "production"; // make sure postcss-obfuscator doesnt run
},
}),
],
}; I'm using I added I don't understand why the heck this is including a very old css code in the out/index.css (aka input.css) file, as I mentioned earlier today, I had tailwind arbitrary values and it didn't work (You created a new issue about this), so I changed it to tailwind classes to fix and be able to compile the obfuscated css. BUT, I don't understand why it is still adding old css classes with arbitrary values, even when those arbitrary values DON'T exist anywhere on my code and I also reinstalled node_modules... |
maybe because postcss is automatically run so plz consider my demo its working fine const isObfscMode = process.env.NODE_ENV === 'obfuscation';
// enable: isObfscMode, // Force Run on Dev Env or when srcPath equals desPath. npm script "obfuscate": "cross-env NODE_ENV=obfuscation npm run tailwindPostcss" refer to cross-env doc for linux |
So I fixed that issue but I'm still having problems showing the CSS. My classes on out/output.css are obfuscated and the classes inside dist/assets/main-*.css are obfuscated as well BUT those classes do not match I have a prefix set on my postcss settings (msy-) And, on output, it generates class with prefixes as shown here: .msy-qhcmd {
--tw-bg-opacity: 1;
background-color: rgb(243 244 246 / var(--tw-bg-opacity));
} However, on dist/assets/main-*.css that same class is: .chb5e {
--tw-bg-opacity: 1;
background-color: rgb(243 244 246 / var(--tw-bg-opacity));
} The prefix is not present and also the class name changed totally, so they don't match and the style is not shown. (That's for ALL classes, but I gave you just an example) |
I see can you try deleting the |
I have removed it so many times without success. Also tried with and without fresh option. I’ll continue trying things and will add an answer with my new findings
…________________________________
De: Najib Rachid ***@***.***>
Enviado: Thursday, May 4, 2023 1:51:43 PM
Para: n4j1Br4ch1D/postcss-obfuscator ***@***.***>
Cc: sort72 ***@***.***>; Mention ***@***.***>
Asunto: Re: [n4j1Br4ch1D/postcss-obfuscator] How could I use this within a project using React + Tailwind + Vite? (Issue #2)
I see can you try deleting the css-obsucator folder
—
Reply to this email directly, view it on GitHub<#2 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/ALMDVHBAP6TP5FNUV5RKVKTXEP3D7ANCNFSM6AAAAAAXU3GMRQ>.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
yes plz do, I wish I can re-produce the issue, and by the way, Check Using tailwindcss arbitrary values. I just identified the problem. |
Thanks for taking the time to handle this issue, the solution you mentioned there seems good for all Tailwind arbitrary values, please do so. I'll continue my tests related to css names mismatch tomorrow and let you know |
The new Version released, check the release notes: and to help you with your issue
also try excluding some paths or files maybe focus on one file as a start.
or recreate a fresh demo project for testing: see if the same issue will appear. |
Hi, I'm working on this at the moment but I'm facing some issues with the parser. It seems not to support responsive classes in Tailwind I created a separated project just for testing this (And uploaded my code here so you can test) I just created a new react vite project and added tailwindcss and the obfuscator, and followed the instructions you have given in this topic. I added a div with 2 classes: bg-red-400 md:bg-blue-400 On dev, if you resize the window to something small, the background is red. And, on bigger screens, the background is blue. However, on the obfuscated built, the div is always red (It doesn't recognize md:bg-blue-400) I think the parser is good on the css part, but, the class in the html is malformed. Now it's generated as That's probably a problem only with prefixes. |
i just tested the last version as well in the replit , responsive seems working there's still an issue though with arbitrary values replacement in HTML can u make a replit for your situation plz, I think I get the bug cuz if used with prefixes and suffixes right? |
in the replit i have this: <main class="pt-[50px] m-4 bg-blue-500 md:bg-red-500">
<h1 class="text-xl">Welcome to <span class="text-indigo-500">postcss-obsfucator</span></h1>
<p class="border p-4 m-2 rounded">
this demo is to try postcss-obsfucator with react, tyescript, vite & tailwindcss
</p>
</main> I get this: {
".read-the-docs": ".c-va7vn-c",
".m-2": ".c-ecywa-c",
".m-4": ".c-fwnil-c",
".rounded": ".c-fpb7h-c",
".border": ".c-zup9i-c",
".bg-blue-500": ".c-alvnn-c",
".p-4": ".c-mc2w6-c",
".pt-\\[50px\\]": ".c-vi531-c",
".text-xl": ".c-qu856-c",
".text-indigo-500": ".c-j4e13-c",
".md\\:bg-red-500": ".c-ilin8-c"
} <main class="pt-[50px] c-fwnil-c c-alvnn-c c-ilin8-c">
<h1 class="c-qu856-c">Welcome to <span class="c-j4e13-c">postcss-obsfucator</span></h1>
<p class="c-zup9i-c c-mc2w6-c c-ecywa-c c-fpb7h-c">
this demo is to try postcss-obsfucator with react, tyescript, vite & tailwindcss
</p>
</main> .c-ecywa-c {
margin: 0.5rem;
}
.c-fwnil-c {
margin: 1rem;
}
.c-fpb7h-c {
border-radius: 0.25rem;
}
.c-zup9i-c {
border-width: 1px;
}
.c-alvnn-c {
--tw-bg-opacity: 1;
background-color: rgb(59 130 246 / var(--tw-bg-opacity));
}
.c-mc2w6-c {
padding: 1rem;
}
.c-vi531-c {
padding-top: 50px;
}
.c-qu856-c {
font-size: 1.25rem;
line-height: 1.75rem;
}
.c-j4e13-c {
--tw-text-opacity: 1;
color: rgb(99 102 241 / var(--tw-text-opacity));
}
@media (min-width: 768px) {
.c-ilin8-c {
--tw-bg-opacity: 1;
background-color: rgb(239 68 68 / var(--tw-bg-opacity));
}
} I used prefixes and suffixes. the only issue I see is |
@sort72 ok I reproduced the issue it's related to |
I found another bug. In tailwind, you can use That is not mapped properly in the output.css file: ...
.o58kp {
--tw-shadow: 0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1);
--tw-shadow-colored: 0 1px 3px 0 var(--tw-shadow-color), 0 1px 2px -1px var(--tw-shadow-color);
box-shadow: var(--tw-ring-offset-shadow, 0 0 #0000), var(--tw-ring-shadow, 0 0 #0000), var(--tw-shadow);
}
@media (min-width: 768px) {
.md\:w-1\/2 /* this should be .x3bsu */
{
width: 50%;
}
.od5k0 {
--tw-bg-opacity: 1;
background-color: rgb(96 165 250 / var(--tw-bg-opacity));
}
} css-obfuscator/main.json {
...
".shadow": ".o58kp",
".md\\:w-1\\\\/2": ".x3bsu",
".md\\:bg-blue-400": ".od5k0"
} |
yes, I think it's probably broken by the last version, ill fix it as well. ill open an issue. but is everything beside those issues work fine? if so close this one and follow up with the other issues. |
Yeah, with the blank project I created to test it, it seems to be working perfectly. So we can continue with the new issues you created, I'll close this. Thanks! Little note: I set |
@sort72 glad to hear it open new issues as a feature request. |
Hi, I like this plugin, but I'm not able to install it on my project which uses Tailwind, React, Vite and PostCSS, could you offer some guidance, please?
I saw the instructions in Astro + Tailwind example but it won't work for me
Thanks!
The text was updated successfully, but these errors were encountered: