-
Notifications
You must be signed in to change notification settings - Fork 297
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
Add Angular sample #847
Open
sebtoombs
wants to merge
4
commits into
vanilla-extract-css:master
Choose a base branch
from
sebtoombs:feature/example-angular
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add Angular sample #847
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# See http://help.github.com/ignore-files/ for more about ignoring files. | ||
|
||
# Compiled output | ||
/dist | ||
/tmp | ||
/out-tsc | ||
/bazel-out | ||
|
||
# Node | ||
/node_modules | ||
npm-debug.log | ||
yarn-error.log | ||
|
||
# IDEs and editors | ||
.idea/ | ||
.project | ||
.classpath | ||
.c9/ | ||
*.launch | ||
.settings/ | ||
*.sublime-workspace | ||
|
||
# Visual Studio Code | ||
.vscode/* | ||
!.vscode/settings.json | ||
!.vscode/tasks.json | ||
!.vscode/launch.json | ||
!.vscode/extensions.json | ||
.history/* | ||
|
||
# Miscellaneous | ||
/.angular/cache | ||
.sass-cache/ | ||
/connect.lock | ||
/coverage | ||
/libpeerconnection.log | ||
testem.log | ||
/typings | ||
|
||
# System files | ||
.DS_Store | ||
Thumbs.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
node_modules | ||
dist | ||
.angular | ||
.vscode | ||
README.md | ||
Comment on lines
+1
to
+5
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, probably only |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# Angular | ||
|
||
This example shows how to use vanilla-extract with Angular. | ||
|
||
This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 13.3.9. | ||
|
||
**Note** this integration uses the Angular runtime to attach vanilla-extract class names to components. This works, and is probably fine for 99% of use cases. There is **probably** a more "Angular" way to do this where we could hook into the `styleUrls` feature of Angular components. | ||
|
||
## Setup | ||
|
||
Getting vanilla-extract working with Angular requires an extra step or two. | ||
|
||
### 1. Pre-requisites | ||
|
||
Vanilla-extract needs to integrate with your build step. If you're working As a lucky Angular user, the only option here is to extend your webpack config. | ||
|
||
Follow the steps in the [Angular Builders repo](https://github.com/just-jeb/angular-builders/tree/master/packages/custom-webpack) to add a custom webpack config to your Angular app. | ||
|
||
### 2. Webpack config | ||
|
||
To make the necessary changes to the webpack config, some existing rules need to be modified. To do this, you have to use a config function in the custom webpack config file. | ||
|
||
```javascript | ||
// webpack.config.js | ||
|
||
// e.g. | ||
|
||
module.exports = (config) => { | ||
// etc. | ||
return config; | ||
} | ||
``` | ||
|
||
#### **1. Modify the Angular webpack loader & css-loader** | ||
|
||
The Angular webpack loader will try and load all `*.ts` files. To avoid this, the current approach is to modify the loader config and exclude `.css.ts` files. | ||
|
||
The existing css-loader (from the Angular webpack config) will try and handle `vanilla.css` files too - causing a conflict with the MiniCssExtract loader for vanilla-extract. | ||
|
||
```javascript | ||
config.module.rules = config.module?.rules?.map((rule) => { | ||
// Exclude .css.ts files from angular loader | ||
if (rule?.loader?.endsWith('@ngtools/webpack/src/ivy/index.js')) { | ||
(rule.exclude ||= []).push(/\.css\.ts$/); | ||
} | ||
// Exclude .vanilla.css files from angular's css-loader implementation | ||
if ('.css'.match(rule.test)) { | ||
(rule.exclude ||= []).push(/\.vanilla\.css$/); | ||
} | ||
return rule; | ||
}) | ||
``` | ||
|
||
#### **2. (optional) Add a typescript loader** | ||
|
||
Because we excluded the `css.ts` files from the Angular webpack loader, if your setup doesn't include a typescript loader (a default Angular app doesn't seem to), then it'll be necessary to add a loader to handle the `css.ts` files. | ||
|
||
Install the necessary dependencies; | ||
|
||
```bash | ||
npm i -D babel-loader @babel/core @babel/preset-env @babel/preset-typescript | ||
``` | ||
|
||
```javascript | ||
config.module?.rules.push({ | ||
test: /\.css\.ts$/, | ||
exclude: /node_modules/, | ||
use: { | ||
loader: 'babel-loader', | ||
options: { | ||
presets: [ | ||
'@babel/preset-typescript', | ||
['@babel/preset-env', { targets: 'defaults' }], | ||
], | ||
}, | ||
}, | ||
}); | ||
``` | ||
|
||
#### **3. Add vanilla-extract** | ||
|
||
Finally, you can add vanilla-extract to the config, as per the docs. | ||
|
||
Follow the instructions in the docs to [install vanilla-extract](https://vanilla-extract.style/documentation/getting-started/) and the [vanilla-extract webpack plugin](https://vanilla-extract.style/documentation/integrations/webpack/). | ||
|
||
```javascript | ||
config.plugins.push(new VanillaExtractPlugin()); | ||
``` | ||
|
||
```javascript | ||
config.module?.rules.push({ | ||
test: /\.vanilla\.css$/i, | ||
use: [ | ||
MiniCssExtractPlugin.loader, | ||
{ | ||
loader: require.resolve('css-loader'), | ||
options: { | ||
url: false, | ||
}, | ||
}, | ||
], | ||
}); | ||
``` | ||
|
||
## A note on the webpack implementation | ||
|
||
The webpack implementation mentioned here *works*, but it may be possible to instead provide an "emitter" to the Angular webpack plugin/loader to tell it how to handle these files. This would remove the need to exclude these files from the loader in the fairly invasive way we have. It would presumably also mean that adding the babel-loader is not necessary. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
{ | ||
"$schema": "./node_modules/@angular/cli/lib/config/schema.json", | ||
"version": 1, | ||
"newProjectRoot": "projects", | ||
"projects": { | ||
"angular": { | ||
"projectType": "application", | ||
"schematics": { | ||
"@schematics/angular:application": { | ||
"strict": true | ||
} | ||
}, | ||
"root": "", | ||
"sourceRoot": "src", | ||
"prefix": "app", | ||
"architect": { | ||
"build": { | ||
"builder": "@angular-builders/custom-webpack:browser", | ||
"options": { | ||
"outputPath": "dist/angular", | ||
"index": "src/index.html", | ||
"main": "src/main.ts", | ||
"polyfills": "src/polyfills.ts", | ||
"tsConfig": "tsconfig.app.json", | ||
"assets": ["src/favicon.ico", "src/assets"], | ||
"styles": ["src/styles.css"], | ||
"scripts": [], | ||
"customWebpackConfig": { | ||
"path": "./webpack.config.js" | ||
} | ||
}, | ||
"configurations": { | ||
"production": { | ||
"budgets": [ | ||
{ | ||
"type": "initial", | ||
"maximumWarning": "500kb", | ||
"maximumError": "1mb" | ||
}, | ||
{ | ||
"type": "anyComponentStyle", | ||
"maximumWarning": "2kb", | ||
"maximumError": "4kb" | ||
} | ||
], | ||
"outputHashing": "all" | ||
}, | ||
"development": { | ||
"buildOptimizer": false, | ||
"optimization": false, | ||
"vendorChunk": true, | ||
"extractLicenses": false, | ||
"sourceMap": true, | ||
"namedChunks": true | ||
} | ||
}, | ||
"defaultConfiguration": "production" | ||
}, | ||
"serve": { | ||
"builder": "@angular-builders/custom-webpack:dev-server", | ||
"options": { | ||
"browserTarget": "angular:build" | ||
}, | ||
"configurations": { | ||
"production": { | ||
"browserTarget": "angular:build:production" | ||
}, | ||
"development": { | ||
"browserTarget": "angular:build:development" | ||
} | ||
}, | ||
"defaultConfiguration": "development" | ||
} | ||
} | ||
} | ||
}, | ||
"defaultProject": "angular" | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This could probably be trimmed down to just
/dist
and/.angular/cache
. The rest of the stuff isn't really relevant and/or is handled by the repo-root gitignore.