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
Conversation
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.
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 |
I think this should require a minor bump ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
`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
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>
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).
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).
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: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 ofcss-loader
into a plain CSS string.