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

[Feature request]: Enable loading css resources from view model #132

Closed
bigopon opened this issue Dec 15, 2017 · 8 comments
Closed

[Feature request]: Enable loading css resources from view model #132

bigopon opened this issue Dec 15, 2017 · 8 comments

Comments

@bigopon
Copy link
Member

bigopon commented Dec 15, 2017

At the moment, doing the following is not working

import {viewResources} from 'aurelia-framework'

@viewResources(PLATFORM.moduleName('./app.css'))
export class App {

}

This is for aurelia/templating#581

@jods4
Copy link
Contributor

jods4 commented Dec 15, 2017

First thing to check when it comes to loading CSS with Aurelia: are your loaders properly configured?

If answer is yes, it would be nice to provide a few details on the error you get or what is actually happening.

Generally speaking, there is no special considerations for this in Webpack plugin, so apart from your configuration there's a good chance that problems stem from the runtime.

@Alexander-Taran
Copy link

Hmm @bigopon can you clarify the difference with this approach and import './xxx.css'

@bigopon
Copy link
Member Author

bigopon commented Mar 22, 2018

For following comparison:

In a custom element view, we can require css

<template>
  <require from='some.css'></require>
</template>

Now if we do the same thing in custom attribute, it would be consistent, the way we load css resources.

There is no issue with import 'some.css', but it would make quite a puzzle for maintenance there, as why require here and there and import there and where ?

@Alexander-Taran
Copy link

@bigopon so to summarize it's kinda syntactic sugar. Right? no difference in produced result. I.e. css is not scoped shadowDommed.. or anything?

@bigopon
Copy link
Member Author

bigopon commented Mar 22, 2018

kind of no. but we can always do some more work to scope it , if need be.

@Alexander-Taran
Copy link

well newcomer advocate in me says it won't be used by many.
It adds consistency in thinking about implementation details..
So you could achieve the same semantic meaning with decorator as with require tag
but nobody will know or find out.
99.9% will end up with import 'some.css' anyway just because there are 100% more examples like this not specific to Aurelia

@doktordirk
Copy link

doktordirk commented Jun 30, 2018

well, actually, i just checked if that would be possible. so, there would be some demand

edit1:
as well neither

$view= `<template>
    <require from='./xy.css'></require>
...

nor

@inlineView(`<template>
    <require from='./xy.css'></require>
...

works 😒

edit2:
ah, something like that i'd like to 'scoped' styles https://github.com/vuejs/vue-loader

@jods4
Copy link
Contributor

jods4 commented Jul 2, 2018

I am closing this as there is no "feature" to add here.
I don't want to add magic and complexity inside the plugin, there's enough already.

Basically, this is about configuring your CSS loaders properly.
It's explained in details here: https://github.com/aurelia/webpack-plugin/wiki/CSS-doesn't-load

Note that if you use the suggested config:

  rules: [
    {
      test: /\.css$/i,
      loader: 'css-loader',
      issuer: /\.html?$/i
    },
    {
      test: /\.css$/i,
      loader: ['style-loader', 'css-loader'],
      issuer: /\.[tj]s$/i
    }
  ]

Then it won't work because when the issuer is .js then style-loader is applied.
This is meant to make this code work:

import "style.css";

export class VM {} 

But if you use

@viewResources(PLATFORM.moduleName("style.css")) 
export class VM { }

Then Aurelia is loading your CSS, even though it is references in a .js, and it expects plain CSS text, not a style-loader module.

Webpack provides many tools to control which loaders are applied in different situations. You need to play with that so that CSS loaders are applied in a way that matches how you use them.

For example, if you never import CSS directly and always go through Aurelia (either with <require> in view or @viewResources in js) you can just go with:

  rules: [
    {
      test: /\.css$/i,
      loader: 'css-loader',
    },
  ]

This simple config will work for the code by OP.

If you have CSS imports in your node_modules (rather uncommon), you may want to apply different rules based on path.

@jods4 jods4 closed this as completed Jul 2, 2018
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