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

Running in client side browser - browserify file is huge #99

Open
thebordella opened this issue Jun 12, 2020 · 6 comments
Open

Running in client side browser - browserify file is huge #99

thebordella opened this issue Jun 12, 2020 · 6 comments

Comments

@thebordella
Copy link

Great module, but I need to run this on the client side in the user's browser. Following some advice online I tried using browserify to convert the inline-css module to a js file I can include in the web page.

This actually works fine and inline-css does it job inside the browser! But ... the js file created by browserify of the inline-css module is 3.6MB! That is a big js file to weigh down loading the site especially for users on slower connections.

Is there a better way to run inline-css inside a browser without such a huge compiled bundle? Apologies if I am not asking in the right place...

@nolandg
Copy link

nolandg commented Aug 17, 2020

Did you configure browserify to minify it? and treeshake it? Can you post a public link to the compiled file?

@bernd-wechner
Copy link

Did you have any luck client siding this? I am keen to find a style inliner that runs clientside not server side.

@Stvad
Copy link

Stvad commented Apr 14, 2022

Any luck on finding in-browser solution? My first attempt at rolling my own is something like:

function inlineStyleRecursively(element: HTMLElement) {
	inlineStyle(element)
	for (const child of element.children) {
		inlineStyleRecursively(child as HTMLElement)
	}
}

function inlineStyle(element: HTMLElement) {
	const computedStyle = getComputedStyle(element)
	for (let i = 0; i < computedStyle.length; i++) {
		const name = computedStyle[i]
		element.style.setProperty(name,
			computedStyle.getPropertyValue(name),
			computedStyle.getPropertyPriority(name),
		)
	}
}

but it's surprisingly slow and does not handle things like pseudo-elements (:before/etc) and adaptive styling won't be preserved 🤔

@bernd-wechner
Copy link

Gave up ages ago and wrote my own using pretty much the method you just shared:

https://github.com/bernd-wechner/Copy-with-Style

https://dev.to/thumbone/series/14563

@Stvad
Copy link

Stvad commented Apr 15, 2022

@bernd-wechner nice, thanks for the reference! how do you handle the pseudo-selector issue I mentioned?

@bernd-wechner
Copy link

I don't ;-). I don't generally use those pseudo elements, so it I'd need to test it to see what happens.

getComputedStyle is slow alas and hence I put a lot of effort into speed tests and working out how to keep a responsive UI, a progress bar and more for costly client side operations. Took a lot of learning hence that series of articles walking through it all.

But I haven't consciously looked at pseudo elements at all. It might work, it might need special handling. I'm also not sure what you mean by adaptive styling alas. I'm an ardent learner in all areas it seems ;-).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants