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

Simpler CSS import for TS views and CSS @import support #8843

Merged
merged 1 commit into from Aug 12, 2020

Conversation

Haprog
Copy link
Contributor

@Haprog Haprog commented Aug 12, 2020

This modifies the default webpack loader configuration for all *.css files. Instead of the previous raw-loader only, this will use a loader chain of ['lit-css-loader', 'extract-loader', 'css-loader'].

lit-css-loader gives us a nice DX of importing styles to TS views like this:

import styles from './list-view.css';

@customElement('list-view')
export class ListView extends LitElement {
  static styles = [Lumo, styles];

css-loader gives the additional benefit of resolving CSS @import "file.css"; syntax in CSS files. This is a new features that works both for TS views and also when using @CssImport() annotation in Java.

extract-loader is needed in the middle to convert the JS output of css-loader into a plain CSS string.

This modifies the default webpack loader configuration for all *.css files. Instead of the previous `raw-loader` only, this will use a loader chain of `['lit-css-loader', 'extract-loader', 'css-loader']`.

`lit-css-loader` gives us a nice DX of importing styles to TS views like this:
```ts
import styles from './list-view.css';

@CustomElement('list-view')
export class ListView extends LitElement {
  static styles = [Lumo, styles];
```

`css-loader` gives the additional benefit of resolving CSS `@import "file.css";` syntax in CSS files. This is a new features that works both for TS views and also when using `@CssImport()` annotation in Java.

`extract-loader` is needed in the middle to convert the JS output of `css-loader` into a plain CSS string.
@Haprog Haprog added the hilla Issues related to Hilla label Aug 12, 2020
@Haprog
Copy link
Contributor Author

Haprog commented Aug 12, 2020

@Haprog
Copy link
Contributor Author

Haprog commented Aug 12, 2020

I think this can be considered backwards compatible since if you were previously using imported styles in JS/TS in a string context that should still work since the CSSResult object (returned by lit-css-loader) has a toString() method that returns the plain CSS string. And it seems to work even if you still wrap the result in a unsafeCSS() call (though this change makes unsafeCSS() wrapping unnecessary so the view code looks nicer).

@Haprog
Copy link
Contributor Author

Haprog commented Aug 12, 2020

I think this should require a minor bump (+0.1.0) since it's practically a new feature.

Copy link
Contributor

@platosha platosha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@platosha platosha merged commit 04b1692 into master Aug 12, 2020
@platosha platosha deleted the haprog/css-loaders branch August 12, 2020 08:28
Haprog added a commit to vaadin-learning-center/crm-tutorial-typescript that referenced this pull request Aug 12, 2020
`lit-css-loader` is now used by default (in Flow) so we can remove customizations from webpack config.  Uses default contents for webpack.config.js now.

See vaadin/flow#8843
Haprog added a commit to vaadin-learning-center/crm-tutorial-typescript that referenced this pull request Aug 13, 2020
Fixes #8.

* feat: use the Lumo theme

* feat: move component styles into separate `.css` files

This allows importing LitElement `css` literals from `.css` files and using them directly.

For example, styles for the `login-view` component are imported from `login-view.css`:
```ts
import styles from "./login-view.css";

@CustomElement('login-view')
export class LoginView extends LitElement {
  static styles = styles;
}
```

instead of including them inline in the component code:

```ts
@CustomElement('login-view')
export class LoginView extends LitElement {
  static styles = css`
    [styles here]
  `;
}
```

NOTE: it requires a custom Webpack loader to avoid wrapping the imported CSS string into LitElement's `unsafeCSS` in the user code. Loader makes this wrapping automatic and invisible. This loader is part of the default configuration since Flow 4.1 (vaadin/flow#8843).

* chore: rearrange frontend source files

 - elevate the `src/views` folder up to `views` and rename it to `components`
 - create a separate folder for each component (to keep `.css` file next to the `.ts` file in the same folder)
 - gather should-not-be-needed and should-be-in-the-framework code inside a `utils` folder

This brings the project structure closer to what the starter wizard generates.

* refactor: use consistent code style for imports in TS

* update README with info about styling

Co-authored-by: Viktor Lukashov <viktor@vaadin.com>
vlukashov pushed a commit to vaadin/skeleton-starter-flow-spring that referenced this pull request Dec 9, 2020
When a `types.d.ts` file is missing, vaadin-maven-plugin automatically generates an up-to-date default version of it on the first build (when running `mvn`).

Keeping a pre-generated file in this repo means it needs to be updated with every Vaadin version that changes its contents. This one extra maintenance task was forgotten in the V18 release, which lead to issues using the [feature of importing CSS directly as LitElement styles](vaadin/flow#8843) in projects cloned from this repo (i.e. all start.vaadin.com projects).
Artur- pushed a commit to vaadin/skeleton-starter-flow-spring that referenced this pull request Dec 9, 2020
When a `types.d.ts` file is missing, vaadin-maven-plugin automatically generates an up-to-date default version of it on the first build (when running `mvn`).

Keeping a pre-generated file in this repo means it needs to be updated with every Vaadin version that changes its contents. This one extra maintenance task was forgotten in the V18 release, which lead to issues using the [feature of importing CSS directly as LitElement styles](vaadin/flow#8843) in projects cloned from this repo (i.e. all start.vaadin.com projects).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hilla Issues related to Hilla +0.0.1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants