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

Missing style after packaging #44

Open
597038837 opened this issue Feb 10, 2023 · 11 comments
Open

Missing style after packaging #44

597038837 opened this issue Feb 10, 2023 · 11 comments

Comments

@597038837
Copy link

image

@597038837
Copy link
Author

The left side is the dev environment effect, and the right side is the deployment effect after packaging

@joshgoebel
Copy link
Member

Not immediately clear this is a problem with the plugin vs your setup/packaging? Can you confirm if this is a JS or CSS issue at least?

@xiaomaiyun
Copy link

xiaomaiyun commented Feb 20, 2023

I have temporarily solved this problem. The reason is that webpack did not pack the unused code when packing, so quotes it here.

import 'highlight.js/styles/atom-one-dark.css'
import hljsCommon from 'highlight.js/lib/common'
import hljsVuePlugin from '@highlightjs/vue-plugin'
. . . 
const app = createApp(App)
. . .
// 注意:解决Vue使用highlight.js build打包发布后样式消失问题,原因是webpack在打包的时候没有把未被使用的代码打包进去,因此,在此处引用一下,看似无意义实则有用
hljsCommon.highlightAuto('<h1>Highlight.js has been registered successfully!</h1>').value
app.use(hljsVuePlugin)
. . .

#38 (comment)

@joshgoebel
Copy link
Member

The reason is that webpack did not pack the unused code when packing, so quotes it here.

HLJS is not unused, it's imported then used... or are you referring just to the CSS?

@xiaomaiyun
Copy link

xiaomaiyun commented Feb 20, 2023

The reason is that webpack did not pack the unused code when packing, so quotes it here.

HLJS is not unused, it's imported then used... or are you referring just to the CSS?

意思就是,仅仅使用“import hljsCommon from 'highlight.js/lib/common'” 把highlight.js/lib/common导入到项目的话,在打包的时候webpack 会认为该依赖没有被使用会被优化掉,不会把该依赖打包到项目中,这样部署到服务器就找不到该依赖,所以就在代码中使用

hljsCommon.highlightAuto('<h1>Highlight.js has been registered successfully!</h1>').value

用一下它,这样打包的时候就不会被优化,就会把该依赖一起打包

@joshgoebel
Copy link
Member

I can only read English.

@xiaomaiyun
Copy link

I can only read English.

That means, if you just use "import hljsCommon from 'highlight.js/lib/common'" to import highlight.js/lib/common into the project, webpack will think that the dependency is not used and will be optimized when packaging. The dependency will not be packaged into the project, so that the dependency cannot be found when deployed to the server, so it is used in the code

hljsCommon.highlightAuto('<h1>Highlight.js has been registered successfully!</h1>').value

Use it, so that it will not be optimized when packaging, and the dependencies will be packaged together

@joshgoebel
Copy link
Member

Would this just be a docfix then to say that in some cases you may need to use the module to make sure it isn't tree-stripped?

@ash-financial-com
Copy link

I have the same problem using it with Vue3 + Vite.

Locally (npm run dev) everything seems in order. The distribution build (npm run build) however does not render the language key/class.

After changing:
import 'highlight.js/lib/common' to import hljsCommon from 'highlight.js/lib/common'

and adding the line:
hljsCommon.highlightAuto('<h1>Highlight.js has been registered successfully!</h1>').value

to my main.js it works.

i am not really sure if this would be just a docfix since the current workaround is quite "hacky" ... but it does work (thanks @xiaomaiyun ).

@joshgoebel
Copy link
Member

I wonder if we could just add a few more values to the sideEffects key in package.json?

@hybridwebdev
Copy link

hybridwebdev commented Dec 8, 2023

image

I had the same issue, where my styling was also disappearing in my bundled files.
If you take a look at the screen shot, the right side shows the bundled code block output, and the left side shows the same code block in "hosting" mode. As you can see, the output is entirely different for some reason.

I don't know if my issue is related, but xiaomaiyun's solution solved it for me. When I apply his solution, my bundled output ends up the same as when running in hosting mode.

The issue appears to be, that without his solution, something is causing the output code block to be different. The end result is that styling isn't applied, as the wrapper elements highlight.js applies are missing.

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

5 participants