Skip to content
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

build production bug Modified the contents of the original document #155

Closed
hppking opened this issue Dec 6, 2022 · 16 comments · Fixed by #194 or #193
Closed

build production bug Modified the contents of the original document #155

hppking opened this issue Dec 6, 2022 · 16 comments · Fixed by #194 or #193

Comments

@hppking
Copy link

hppking commented Dec 6, 2022

const cssRecordSelector = config.mangledSelector ? this.mangledSelector : this.selector;
-> const cssRecordSelector = config.minimize ? this.mangledSelector : this.selector;

@Machy8
Copy link
Member

Machy8 commented Dec 6, 2022

Hi, @hppking.

Could you please provide a bit more detail on this issue? I don't understand, what you mean.
Like how can I reproduce that bug? Which tool you use?

@hppking
Copy link
Author

hppking commented Dec 7, 2022

@Machy8
solidjs+stylify demo:
cmd yarn build production
image to
image

@hppking
Copy link
Author

hppking commented Dec 7, 2022

Cannot modify source file todo-list.jsx

@Machy8
Copy link
Member

Machy8 commented Dec 7, 2022

This behavior is actually correct. Since some frameworks (Symfony, Laravel, Next) don't have hooks for generating files as NuxtJS does. Because of that, Stylify must access the files before framework compilers, therefore it processes source files directly.

However try to add rewriteSelectorsInFiles: false to your Bundle configuration as follows:

 bundles: [
    {
      outputFile: './src/stylify.css',
      files: ['./src/**/*.jsx'],
      rewriteSelectorsInFiles: false,
    },
  ],

I have tested it in SolidJS example and it seems to work (I have updated the example).

Then, you can run yarn build and the selectors will be rewritten only in build files.

@hppking hppking closed this as completed Dec 7, 2022
@hppking
Copy link
Author

hppking commented Dec 7, 2022

Good

@mzaini30
Copy link

This behavior is actually correct. Since some frameworks (Symfony, Laravel, Next) don't have hooks for generating files as NuxtJS does. Because of that, Stylify must access the files before framework compilers, therefore it processes source files directly.

However try to add rewriteSelectorsInFiles: false to your Bundle configuration as follows:

 bundles: [
    {
      outputFile: './src/stylify.css',
      files: ['./src/**/*.jsx'],
      rewriteSelectorsInFiles: false,
    },
  ],

I have tested it in SolidJS example and it seems to work (I have updated the example).

Then, you can run yarn build and the selectors will be rewritten only in build files.

But not works in Svelte component parent

Dev:

<div class="padding:10px background:pink">
  <p class="color:blue font-style:italic">Cat</p>
</div>

Build:

<div class="padding:10px background:pink">
  <p class="c d">Cat</p>
</div>

@Machy8
Copy link
Member

Machy8 commented Feb 28, 2023

Hi @mzaini30.

Are you using SvelteKit or svelte+vite or any other combination? If it is a Stackblitz playground, could you share a link?

Could you please send the parent and child component template? I will try to reproduce the error and fix it.

@Machy8 Machy8 reopened this Feb 28, 2023
@mzaini30
Copy link

I using it: https://github.com/mzaini30/svelte

@Machy8
Copy link
Member

Machy8 commented Feb 28, 2023

I have forked your example.

Here is your example with a working configuration: https://stackblitz.com/edit/github-z2waev?file=vite.config.js.

The problem is with Svelte compilation under the hood with the combination with Vite converts class="..." to add_attribute(div, "class", "...") that Stylify doesn't match.

The only option here now is to remove the rewriteSelectorsInFiles option from the config. This will rewrite selectors during build (production) pipeline in Vercel/Github but not in local environment (unless you run build locally).

If you need to test the production build process locally without selectors mangling or disabling it entirely, you can set the mangleSelectors:false in the Compiler config. I have added comment for you into the vite.config.js configuration at stackblitz:

const stylifyPlugin = stylifyVite({
	// ...
	bundles: [ /* */ ],
	compiler: {
		mangleSelectors: false
	}
});

image

Please let me know if it works.

P.S.

If you use class:something="{variable === 'foo'}", then you need to add this area to be matched like below. I have added that into the config file too:

const stylifyPlugin = stylifyVite({
	// ...
	compiler: {
		selectorsAreas: ['(?:^|\\s+)class:(\\S+)=["\']']
	}
});

I will add that to the Stylify in the next version, so you this will not be needed anymore.

@mzaini30
Copy link

I usually build it locally because I using compiled Svelte for Android app webview 😅

Okay, I'll use mangleSelectors false

@Machy8
Copy link
Member

Machy8 commented Feb 28, 2023

Hm, this is bad. Sorry for that.

I have tried to configure some parsing before you send me your repo, but Svelte compiles that into a ton of variants of output, so I haven't send any patch.

Will have to look into it. But I need to test it first.

You can possibly check if there is for example .env.local file, that you create in your local cloned repo, in the vite.config.js through fs.existsSync(), which returns true/false and pass it to the compiler.

If the .env.local will be in the gitignore and not pushed into the git repository, it will always automatically setup dev environment. I do it for my repos in PHP frameworks like Symfony/Laravel and it works fine.

Something like:

import fs from 'fs';

const isLocalEnv = fs.existsSync('./.env.local');

const stylifyPlugin = stylifyVite({
	// ...
	bundles: [ /* */ ],
	compiler: {
		mangleSelectors: !isLocalEnv
	}
});

But if you release the package after manual bundling, this will probably not be what you need.

@Machy8 Machy8 pinned this issue Feb 28, 2023
@mzaini30
Copy link

Nice idea. I'll create scripts like pnpm build:online

@Machy8
Copy link
Member

Machy8 commented Feb 28, 2023

Try it. If you find any issues, let me know.

@mzaini30
Copy link

Solved

import { defineConfig } from "vite";
import { svelte } from "@sveltejs/vite-plugin-svelte";
import legacy from "@vitejs/plugin-legacy";
import pages from "vite-plugin-pages-svelte";
import { stylifyVite } from '@stylify/unplugin';

const stylifyPlugin = (mangle) => (stylifyVite({
    bundles: [{
        outputFile: './src/stylify.css',
        files: ['./src/**/*.svelte'],
    }],
    compiler: {
      mangleSelectors: mangle
    }
}));

let plugins = [
  svelte(),
  pages(),
];

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  if (mode == "android") {
    return {
      plugins: [legacy(), ...plugins, stylifyPlugin(false)],
    };
  } else if (mode == "online") {
    return {
      plugins: [...plugins, stylifyPlugin(true)],
    };
  } else {
    return {
      plugins: [...plugins, stylifyPlugin(false)],
    };
  }
});

@Machy8
Copy link
Member

Machy8 commented Feb 28, 2023

Great! I will add this to snippets. Thanks a lot for testing!

@Machy8
Copy link
Member

Machy8 commented May 17, 2023

The mangling is now opt-in.

For further reading.
Mangling info: https://stylifycss.com/en/docs/get-started/mangling-selectors/
Release info: https://stylifycss.com/en/docs/get-started/releases/

@Machy8 Machy8 closed this as completed May 17, 2023
@Machy8 Machy8 unpinned this issue May 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants