Skip to content

Support for CSS modules #38

Closed
Closed
@LinusBorg

Description

@LinusBorg

As the README states, this lib currently doesn't process the <style> parts.

That's not a big issue when we unit test components that use the normal styles API, by which I mean they use harcoded class names in the template:

<div class="staticClass" v-bind:class="{ 'dnyamicClass' : someFlag }">

However, for CSS modules, this means that this.$styles is undefined in unit tests and rendering will fail.

I think we have two options:

A: Mock $styles

We could simply add this prop to a component if we find a style part with the modulesattribute, and use a Proxy to return the class that was required:

// pseudocode
  this.$style = new Proxy({}, {
    get(_, property) {
      return property
    }
  })

Proxies are available in node since http://node.green/#ES2015-built-ins-Proxy, so that's quite good. We are dropping support for node 4.* in vue-cli etc. as well, so that should be enough.

The only downside I see it that we can't catch missing classes: If I mistype a class name in my component, I will still get back that mistyped classname from the proxy. If we properly evaluated the styles, we would get undefined and could catch this bug better, I think.

B: Evaluating styles and building the $styles object.

We could add the usual popular preprocessors as peer deps, write a small wrapper for each to compile the styles, and then use something like string-extract-class-names to get all classnames from the resulting CSS.

From that, we could build a "real" $styles object and inject it into the component.

@eddyerburgh - what approach would you prefer?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions