Skip to content

Injecting CSS imported from JavaScript in a .vue file as critical CSS #8

@Justineo

Description

@Justineo

Do you want to request a feature or report a bug?

Feature request (maybe?)

What is the current behavior?

CSS directly imported from JS won't be injected as critical CSS on server side.

What is the expected behavior?

All CSS depended by current component should be injected as critical CSS.

If this is a feature request, what is motivation or use case for changing the behavior?

We have a component library which decoupled with its themes. We put style files into separate NPM packages and made a Babel plugin to rewrite component import statements to use user specified theme. eg.

<script>
import { Button } from 'veui'
// ...
</script>

The script above will be transpiled into the following statements when we configured to use veui-theme-x:

import { Button } from 'veui'
import 'veui-theme-x/components/button.less'

As a result, it seemed that styles directly imported from JS won't be treated as critical CSS and injected into server-rendered HTML. I tried to modify the following code but it doesn't seem to be working.

// on the server: attach to Vue SSR context
if (isVue) {
// inside *.vue file: expose a function so it can be called in
// component's lifecycle hooks
return shared.concat([
'// add CSS to SSR context',
'var add = require(' + addStylesServerPath + ')',
'module.exports.__inject__ = function (context) {',
' add(' + id + ', content, ' + isProduction + ', context)',
'};'
]).join('\n')
} else {
// normal import
return shared.concat([
'require(' + addStylesServerPath + ')(' + id + ', content, ' + isProduction + ')'
]).join('\n')
}

So can vue-style-loader automatically inject this kind of styles into HTML as it does for embedded styles?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions